ctkPluginPrivate_p.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 std::invalid_argument Faulty manifest for bundle
  69. */
  70. ctkPluginPrivate(QWeakPointer<ctkPlugin> qq, ctkPluginFrameworkContext* fw,
  71. 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. * Wait for an ongoing operation to finish.
  128. *
  129. * @param lock QMutex used for locking.
  130. * @param src Caller to include in exception message.
  131. * @param longWait True, if we should wait extra long before aborting.
  132. * @throws ctkPluginException if the ongoing (de-)activation does not finish
  133. * within reasonable time.
  134. */
  135. void waitOnOperation(LockObject* lock, const QString& src, bool longWait);
  136. /**
  137. *
  138. */
  139. QStringList findResourceEntries(const QString& path,
  140. const QString& pattern, bool recurse) const;
  141. /**
  142. * Union of flags allowing plugin class access
  143. */
  144. static const ctkPlugin::States RESOLVED_FLAGS;
  145. ctkPluginFrameworkContext * const fwCtx;
  146. /**
  147. * ctkPlugin identifier
  148. */
  149. const long id;
  150. /**
  151. * ctkPlugin location identifier
  152. */
  153. const QString location;
  154. /**
  155. * ctkPlugin symbolic name
  156. */
  157. QString symbolicName;
  158. /**
  159. * ctkPlugin version
  160. */
  161. ctkVersion version;
  162. /**
  163. * State of the plugin
  164. */
  165. ctkPlugin::State state;
  166. /**
  167. * ctkPlugin archive
  168. */
  169. ctkPluginArchive* archive;
  170. /**
  171. * Directory for plugin data
  172. */
  173. QFileInfo pluginDir;
  174. /**
  175. * ctkPluginContext for the plugin
  176. */
  177. QScopedPointer<ctkPluginContext> pluginContext;
  178. /**
  179. * ctkPluginActivator for the plugin
  180. */
  181. ctkPluginActivator* pluginActivator;
  182. /**
  183. * The Qt plugin loader for the plugin
  184. */
  185. QPluginLoader pluginLoader;
  186. /**
  187. * Time when the plugin was last modified
  188. */
  189. QDateTime lastModified;
  190. /**
  191. * Stores the default locale entries when uninstalled
  192. */
  193. QHash<QString, QString> cachedHeaders;
  194. /**
  195. * Stores the raw manifest headers
  196. */
  197. QHash<QString, QString> cachedRawHeaders;
  198. /**
  199. * Type of operation in progress. Blocks bundle calls during activator and
  200. * listener calls
  201. */
  202. QAtomicInt operation;
  203. static const int IDLE = 0;
  204. static const int ACTIVATING = 1;
  205. static const int DEACTIVATING = 2;
  206. static const int RESOLVING = 3;
  207. static const int UNINSTALLING = 4;
  208. static const int UNRESOLVING = 5;
  209. static const int UPDATING = 6;
  210. LockObject operationLock;
  211. /** Saved exception of resolve failure. */
  212. ctkPluginException* resolveFailException;
  213. /**
  214. * True when this plugin has its activation policy
  215. * set to "eager"
  216. */
  217. bool eagerActivation;
  218. /** List of ctkRequirePlugin entries. */
  219. QList<ctkRequirePlugin*> require;
  220. private:
  221. /** Rember if plugin was started */
  222. bool wasStarted;
  223. /**
  224. * Check manifest and cache certain manifest headers as variables.
  225. */
  226. void checkManifestHeaders();
  227. // This could potentially be run in its own thread,
  228. // parallelizing plugin activations
  229. ctkPluginException* start0();
  230. void startDependencies();
  231. /**
  232. * Remove a plugins registered listeners, registered services and
  233. * used services.
  234. *
  235. */
  236. void removePluginResources();
  237. };
  238. #endif // CTKPLUGINPRIVATE_P_H