ctkPlugin_p.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #ifndef CTKPLUGINPRIVATE_P_H
  16. #define CTKPLUGINPRIVATE_P_H
  17. #include "ctkPlugin.h"
  18. #include "ctkPluginException.h"
  19. #include "ctkRequirePlugin_p.h"
  20. #include <QHash>
  21. #include <QPluginLoader>
  22. #include <QDateTime>
  23. #include <QFileInfo>
  24. #include <QMutex>
  25. #include <QWaitCondition>
  26. class ctkPluginActivator;
  27. class ctkPluginArchive;
  28. class ctkPluginFrameworkContext;
  29. /**
  30. * \ingroup PluginFramework
  31. */
  32. class ctkPluginPrivate {
  33. protected:
  34. const QWeakPointer<ctkPlugin> q_ptr;
  35. class LockObject
  36. {
  37. public:
  38. LockObject() {};
  39. void lock();
  40. bool tryLock();
  41. bool tryLock(int timeout);
  42. void unlock();
  43. bool wait(unsigned long time = ULONG_MAX);
  44. void wakeAll();
  45. void wakeOne();
  46. private:
  47. Q_DISABLE_COPY(LockObject)
  48. QMutex m_Lock;
  49. QWaitCondition m_Condition;
  50. };
  51. public:
  52. inline QWeakPointer<ctkPlugin> q_func() { return q_ptr; }
  53. inline QWeakPointer<const ctkPlugin> q_func() const { return q_ptr; }
  54. friend class ctkPlugin;
  55. struct Locker
  56. {
  57. Locker(LockObject* obj) : m_Obj(obj) { m_Obj->lock(); }
  58. ~Locker() { m_Obj->unlock(); }
  59. private:
  60. LockObject* m_Obj;
  61. };
  62. /**
  63. * Construct a new plugin based on a ctkPluginArchive.
  64. *
  65. * @param fw ctkPluginFrameworkContext for this plugin.
  66. * @param ba ctkPlugin archive representing the shared library and cached data
  67. * @param checkContext AccessConrolContext to do permission checks against.
  68. * @exception ctkInvalidArgumentException Faulty manifest for bundle
  69. */
  70. ctkPluginPrivate(QWeakPointer<ctkPlugin> qq, ctkPluginFrameworkContext* fw,
  71. QSharedPointer<ctkPluginArchive> pa /*, Object checkContext*/);
  72. /**
  73. * Construct a new empty ctkPlugin.
  74. *
  75. * Only called for the system plugin
  76. *
  77. * @param fw Framework for this plugin.
  78. */
  79. ctkPluginPrivate(QWeakPointer<ctkPlugin> qq,
  80. ctkPluginFrameworkContext* fw,
  81. long id,
  82. const QString& loc,
  83. const QString& sym,
  84. const ctkVersion& ver);
  85. virtual ~ctkPluginPrivate();
  86. /**
  87. * Get updated plugin state. That means check if an installed
  88. * plugin has been resolved.
  89. *
  90. * @return ctkPlugin state
  91. */
  92. ctkPlugin::State getUpdatedState();
  93. /**
  94. * Get root for persistent storage area for this plugin.
  95. *
  96. * @return A QDir object representing the data root.
  97. */
  98. QFileInfo getDataRoot();
  99. /**
  100. * Set state to INSTALLED. We assume that the plug-in is resolved
  101. * when entering this method.
  102. */
  103. void setStateInstalled(bool sendEvent);
  104. /**
  105. * Purge any old files associated with this plug-in.
  106. */
  107. void purge();
  108. /**
  109. * Save the autostart setting to the persistent plugin storage.
  110. *
  111. * @param setting The autostart options to save.
  112. */
  113. void setAutostartSetting(const ctkPlugin::StartOptions& setting);
  114. void ignoreAutostartSetting();
  115. void modified();
  116. /**
  117. * Performs the actual activation.
  118. */
  119. void finalizeActivation();
  120. const ctkRuntimeException* stop0();
  121. /**
  122. * Stop code that is executed in the pluginThread without holding the
  123. * operationLock.
  124. */
  125. const ctkRuntimeException* stop1();
  126. /**
  127. *
  128. */
  129. void update0(const QUrl &updateLocation, bool wasActive);
  130. /**
  131. *
  132. */
  133. int getStartLevel();
  134. /**
  135. * Wait for an ongoing operation to finish.
  136. *
  137. * @param lock QMutex used for locking.
  138. * @param src Caller to include in exception message.
  139. * @param longWait True, if we should wait extra long before aborting.
  140. * @throws ctkPluginException if the ongoing (de-)activation does not finish
  141. * within reasonable time.
  142. */
  143. void waitOnOperation(LockObject* lock, const QString& src, bool longWait);
  144. /**
  145. *
  146. */
  147. QStringList findResourceEntries(const QString& path,
  148. const QString& pattern, bool recurse) const;
  149. /**
  150. * Union of flags allowing plugin class access
  151. */
  152. static const ctkPlugin::States RESOLVED_FLAGS;
  153. ctkPluginFrameworkContext * const fwCtx;
  154. /**
  155. * ctkPlugin identifier
  156. */
  157. const long id;
  158. /**
  159. * ctkPlugin location identifier
  160. */
  161. const QString location;
  162. /**
  163. * ctkPlugin symbolic name
  164. */
  165. QString symbolicName;
  166. /**
  167. * ctkPlugin version
  168. */
  169. ctkVersion version;
  170. /**
  171. * State of the plugin
  172. */
  173. ctkPlugin::State state;
  174. /**
  175. * ctkPlugin archive
  176. */
  177. QSharedPointer<ctkPluginArchive> archive;
  178. /**
  179. * Directory for plugin data
  180. */
  181. QFileInfo pluginDir;
  182. /**
  183. * ctkPluginContext for the plugin
  184. */
  185. QScopedPointer<ctkPluginContext> pluginContext;
  186. /**
  187. * ctkPluginActivator for the plugin
  188. */
  189. ctkPluginActivator* pluginActivator;
  190. /**
  191. * The Qt plugin loader for the plugin
  192. */
  193. QPluginLoader pluginLoader;
  194. /**
  195. * Time when the plugin was last modified
  196. */
  197. QDateTime lastModified;
  198. /**
  199. * Stores the default locale entries when uninstalled
  200. */
  201. QHash<QString, QString> cachedHeaders;
  202. /**
  203. * Stores the raw manifest headers
  204. */
  205. QHash<QString, QString> cachedRawHeaders;
  206. /**
  207. * Type of operation in progress. Blocks bundle calls during activator and
  208. * listener calls
  209. */
  210. QAtomicInt operation;
  211. static const int IDLE = 0;
  212. static const int ACTIVATING = 1;
  213. static const int DEACTIVATING = 2;
  214. static const int RESOLVING = 3;
  215. static const int UNINSTALLING = 4;
  216. static const int UNRESOLVING = 5;
  217. static const int UPDATING = 6;
  218. LockObject operationLock;
  219. /** Saved exception of resolve failure. */
  220. ctkPluginException* resolveFailException;
  221. /**
  222. * True when this plugin has its activation policy
  223. * set to "eager"
  224. */
  225. bool eagerActivation;
  226. /** List of ctkRequirePlugin entries. */
  227. QList<ctkRequirePlugin*> require;
  228. private:
  229. /** Rember if plugin was started */
  230. bool wasStarted;
  231. /**
  232. * Check manifest and cache certain manifest headers as variables.
  233. */
  234. void checkManifestHeaders();
  235. // This could potentially be run in its own thread,
  236. // parallelizing plugin activations
  237. ctkPluginException* start0();
  238. void startDependencies();
  239. /**
  240. * Remove a plugins registered listeners, registered services and
  241. * used services.
  242. *
  243. */
  244. void removePluginResources();
  245. ctkPlugin::State getUpdatedState_unlocked();
  246. };
  247. #endif // CTKPLUGINPRIVATE_P_H