Opera 12.15 Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

additionchecker.h 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* -*- Mode: c++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
  2. *
  3. * Copyright (C) 1995-2011 Opera Software AS. All rights reserved.
  4. *
  5. * This file is part of the Opera web browser. It may not be distributed
  6. * under any circumstances.
  7. *
  8. * @author Michal Zajaczkowski
  9. */
  10. #ifdef AUTO_UPDATE_SUPPORT
  11. #ifndef AUTOUPDATE_ADDITION_CHECKER_H
  12. #define AUTOUPDATE_ADDITION_CHECKER_H
  13. #include "modules/util/adt/oplisteners.h"
  14. #include "modules/hardcore/timer/optimer.h"
  15. #include "adjunct/autoupdate/autoupdatexml.h"
  16. #include "adjunct/autoupdate/autoupdateserverurl.h"
  17. #include "adjunct/autoupdate/statusxmldownloader.h"
  18. class AutoUpdateXML;
  19. class AutoUpdateManager;
  20. class AutoUpdateServerURL;
  21. class AdditionCheckerListener;
  22. /**
  23. * AdditionCheckerItem represents a single addition item that is being resolved by the addition checker.
  24. * Each item has a type (i.e. UpdatableResource::ResourceTypePlugin), a key (i.e. the mimetype of the plugin) and a
  25. * StatusXMLDownloader* value.
  26. * If the item is not waiting to be resolved by any StatusXMLDownloader, then it has the downloader value set to NULL.
  27. * Otherwise the downloader value points to the StatusXMLDownloader object that should send back information about this
  28. * particular item.
  29. */
  30. class AdditionCheckerItem
  31. {
  32. public:
  33. AdditionCheckerItem();
  34. ~AdditionCheckerItem();
  35. /**
  36. * Sets the value of the key of the addition item (i.e. the plugin mime-type)
  37. *
  38. * @param key - the new value of the key
  39. *
  40. * @returns - OpStatus:OK if setting the key value succeeds, ERR otherwise
  41. */
  42. OP_STATUS SetKey(const OpStringC& key);
  43. /**
  44. * Sets the type of the addition item (i.e. UpdatableResource::ResourceTypePlugin)
  45. *
  46. * @param type - the new type to set
  47. *
  48. * @returns nothing
  49. */
  50. void SetType(UpdatableResource::UpdatableResourceType type);
  51. /**
  52. * Assings a StatusXMLDownloader to the addition item, designating that item is pending an autoupdate response from the
  53. * particular downloader. If this value is NULL, then the item is pending to be resolved.
  54. *
  55. * @param downloader - the new StatusXMLDownloader* value
  56. */
  57. void SetXMLDownloader(const StatusXMLDownloader* downloader);
  58. /**
  59. * Get the key of the item (i.e. the mime-type of a plugin)
  60. *
  61. * @param key - a reference to an OpString that will get the key value in case of success
  62. *
  63. * @returns - OpStatus::OK in case the value was set OK, ERR otherwise
  64. */
  65. OP_STATUS GetKey(OpString& key);
  66. /**
  67. * Get the type of the item, i.e. UpdatableResource::ResourceTypePlugin
  68. *
  69. * @returns - the type of the item
  70. */
  71. UpdatableResource::UpdatableResourceType GetType();
  72. /**
  73. * Get the StatusXMLDownloader* value assinged to the item, see SetXMLDownloader()
  74. *
  75. * @returns - the StatusXMLDownloader* value
  76. */
  77. const StatusXMLDownloader* GetXMLDownloader();
  78. /**
  79. * Check if this item is of the given type
  80. *
  81. * @param type - the type that we want to verify against the type of this item
  82. *
  83. * @returns - TRUE in case the type of this item matches the given type, FALSE otherwise
  84. */
  85. bool IsType(UpdatableResource::UpdatableResourceType type);
  86. /**
  87. * Check if this item has the given key value
  88. *
  89. * @param key - the key value that we want to check against the key of this item
  90. *
  91. * @returns TRUE in case the keys match, FALSE otherwise
  92. */
  93. bool IsKey(const OpStringC& key);
  94. /**
  95. * Check if the value of the StatusXMLDownloader pointer assigned to this item matches the given value
  96. *
  97. * @param downloader - the StatusXMLDownloader pointer value that is to be checked against the value stored with this item
  98. *
  99. * @returns - TRUE in case the pointers match, FALSE otherwise
  100. */
  101. bool IsXMLDownloader(const StatusXMLDownloader* downloader);
  102. /**
  103. * Increment the resolve retry counter for this item. This method does not check if the pointer exceeded the maximum retry count.
  104. */
  105. void IncRetryCounter() { m_retry_counter++; }
  106. /**
  107. * Get the retry counter value to compare against the maximum retry count in the calling code.
  108. *
  109. * @returns - the current retry count for the given item
  110. */
  111. unsigned int GetRetryCounterValue() { return m_retry_counter; }
  112. private:
  113. /**
  114. * The value of the key for this item. The key is an unique identifier among all items of the same type, i.e. a mime-type for a plugin.
  115. */
  116. OpString m_key;
  117. /**
  118. * The value of the StatusXMLDownloader pointer assigned to this item. This is NULL in case this item is pending resolving and it points
  119. * to a downloader that should resolve this item in case the item has been requested to resolve.
  120. */
  121. const StatusXMLDownloader* m_status_xml_downloader;
  122. /**
  123. * Type of the addition represented by this item, i.e. UpdatableResource::ResourceTypePlugin.
  124. */
  125. UpdatableResource::UpdatableResourceType m_type;
  126. /**
  127. * The resolve retry counter that counts how many times a request about the given item has been sent to the autoupdate server.
  128. * Used to have a resolve retry mechanism that will give up after RESOLVE_MAX_RETRY_COUNT_PER_ITEM attempts.
  129. */
  130. unsigned int m_retry_counter;
  131. };
  132. /**
  133. * The autoupdate server currently serves two quite different purposes:
  134. * 1. Keeping the browser up-to-date with periodic automatic checks and manual checks;
  135. * 2. Making it possible to discover and download "additions" via the autoupdate server.
  136. *
  137. * An addition is any additional installable resource that might be needed during the browser run, currently
  138. * the only supported addition types are plugins and dictionaries, both installable on user demand. If the user
  139. * visits a page that requires a given plugin, the plugin availablity is checked against the autoupdate server with
  140. * the additions mechanisms.
  141. *
  142. * If you want to add a new type of resource that you want to resolve via the autoupdate server, you need to:
  143. * 1. Add a new resource type (see updatableresource.h, updatablefile.h);
  144. * 2. Add the new resource to the KNOWN_TYPES[] array;
  145. * 3. Agree with the core services team that the autoupdate server will support the new resource, agree a new update level for the new resource type! See
  146. * AutoUpdateXML::AutoUpdateLevel.
  147. * 4. Make sure the UpdatableResourceFactory is able to recognize all the XML elements used to describe the new resource
  148. * type;
  149. * 5. Choose a key for the new resource. This is the value that is supposed to be unique across all resources of the same type,
  150. * i.e. the mimetype for a plugin or the language for a dictionary, make sure you implement the key by implementing the
  151. * UpdatableResource::GetResourceKey() method.
  152. * 6. Make sure AutoUpdateXML can recognize the new resource type, choose a new update check level and generate a proper XML for the new item type.
  153. * 7. You should get asserts if you forget about any of the above;
  154. *
  155. * In order to resolve a resource:
  156. * 1. Make your class inherit from the AdditionCheckerListener class, implement its methods;
  157. * 2. Register yourself as a listener with something like g_autoupdate_manager->AddListener(this);
  158. * 3. Call g_autoupdate_manager->CheckForAddition() to check the availability of the addition and wait for a callback via the
  159. * AdditionCheckerListener interface;
  160. * 4. Carry on.
  161. *
  162. * The addition checker will try to repeat failed requests to the autoupdate server on its own, so you can consider that when it
  163. * fails to resolve the addition, it fails for good.
  164. * The addition checker may make a couple of HTTP requests to the autoupdate server in parallel in order to speed up things.
  165. * The addition checker will send a cummulative requst about all additions of a given type that are not resolved at the moment
  166. * of seding the request.
  167. *
  168. * The AdditionChecker class is meant to be used by the AutoUpdateManager only. The AutoUpdateManager has all the interfaces
  169. * required to use the addition resolving functionality.
  170. *
  171. */
  172. class AdditionChecker:
  173. public OpTimerListener,
  174. public StatusXMLDownloaderListener
  175. {
  176. friend class AutoUpdateManager;
  177. public:
  178. AdditionChecker();
  179. virtual ~AdditionChecker();
  180. protected:
  181. /**
  182. * The list of types that the AdditionChecker is be able to resolve.
  183. */
  184. static UpdatableResource::UpdatableResourceType KNOWN_TYPES[];
  185. /**
  186. * The resolve timer timeout period for the KickNow kick type.
  187. */
  188. static const unsigned int RESOLVE_TIMER_KICK_NOW_PERIOD_SEC;
  189. /**
  190. * The resolve timer timeout period for the KickDelayed kick type.
  191. */
  192. static const unsigned int RESOLVE_TIMER_KICK_DELAYED_PERDIOD_SEC;
  193. /**
  194. * The maximum retry count per a queued addition item, if the addition is not resolved after this many tries, it's
  195. * broadcasted as failed.
  196. */
  197. static const unsigned int RESOLVE_MAX_RETRY_COUNT_PER_ITEM;
  198. enum KickType
  199. {
  200. /**
  201. * Start the resolve timer using the RESOLVE_TIMER_KICK_DELAYED_PERDIOD_SEC period
  202. */
  203. KickDelayed,
  204. /**
  205. * Start the resolve timer using the RESOLVE_TIMER_KICK_NOW_PERIOD_SEC period
  206. */
  207. KickNow
  208. };
  209. /**
  210. * Initializes the object, needs to be called once before any operations are made on the AdditionChecker.
  211. *
  212. * @returns - OpStatus::OK if initialization went OK, ERR otherwise. The object can't be used until Init() returns OpStatus::OK.
  213. */
  214. OP_STATUS Init();
  215. /**
  216. * OpTimerListener implementation.
  217. */
  218. virtual void OnTimeOut(OpTimer* timer);
  219. /**
  220. * Handler for resolve timer. Will send out autoupdate XML requests for items awaiting resolving.
  221. *
  222. * @returns - OpStatus::OK on success, ERR otherwise.
  223. */
  224. OP_STATUS OnResolveTimeOut();
  225. /**
  226. * Implementation of the StatusXMLDownloaderListener interface.
  227. */
  228. virtual void StatusXMLDownloaded(StatusXMLDownloader* downloader);
  229. virtual void StatusXMLDownloadFailed(StatusXMLDownloader* downloader, StatusXMLDownloader::DownloadStatus status);
  230. /**
  231. * Register a listener with the addition resolver. You don't want to do that with your classes, use g_autoupdate_manager->AddListener().
  232. *
  233. * @param listener - a pointer to a class inheriting from the AdditionCheckerListener interface.
  234. *
  235. * @returns - OpStatus::OK if adding the listener succeeded, OpStatus::ERR otherwise.
  236. */
  237. OP_STATUS AddListener(AdditionCheckerListener* listener);
  238. /**
  239. * Unregister a listener with the addition resolver. You don't want to do that with your classes, use g_autoupdate_manager->RemoveListener().
  240. *
  241. * @param listener - a pointer to a class inheriting from the AdditionCheckerListener interface.
  242. *
  243. * @returns - OpStatus::OK if removing the listener succeeded, OpStatus::ERR otherwise.
  244. */
  245. OP_STATUS RemoveListener(AdditionCheckerListener* listener);
  246. /**
  247. * Try to resolve an addition of the given type and the given key against the autoupdate server. You don't want to use this, use
  248. * g_autoupdate_manager->CheckForAddition() instead.
  249. * Before you call this method, be sure to register as a listener to AdditionChecker.
  250. * After a call to this method, wait for a callback via the AdditionCheckerListener interface.
  251. * Note that the actual XML request will be made later on, after the resolve timer fires.
  252. *
  253. * @param type - the type of the addition to be resolved.
  254. * @param key - the key of the addition to be resolved.
  255. *
  256. * @returns - OpStatus::OK if queing the addition check was OK, ERR otherwise.
  257. */
  258. OP_STATUS CheckForAddition(UpdatableResource::UpdatableResourceType type, const OpStringC& key);
  259. /**
  260. * Starts the resolve timer with the timeout period determined by the parameter given.
  261. *
  262. * @param type - determines the timeout period, see KickType definition.
  263. */
  264. void KickResolveTimer(KickType type);
  265. /**
  266. * Notifies all the registered listeners about an addition being resolved or failed to resolve.
  267. */
  268. void NotifyAdditionResolved(UpdatableResource::UpdatableResourceType type, const OpStringC& key, UpdatableResource* resource);
  269. void NotifyAdditionResolveFailed(UpdatableResource::UpdatableResourceType type, const OpStringC& key);
  270. /**
  271. * Checks if a resource with the given type can be resolved as an addition.
  272. */
  273. bool IsKnownType(UpdatableResource::UpdatableResourceType type);
  274. /**
  275. * Get the total number of the types recognized as additions. Returns the number of items in the KNOWN_TYPES[] array.
  276. */
  277. UINT32 GetKnownTypesCount();
  278. /**
  279. * Searches the m_items vector for an item with the given type and key.
  280. * The m_items vector should never contain more than one item with the same type AND key, i.e. we should never have queued
  281. * more than one plugin with the same mime-type.
  282. * This method doesn't check that condition, it relies on CheckForAddition() making the proper checks.
  283. *
  284. * @param type - addition type
  285. * @param key - addition key
  286. *
  287. * @returns - a pointer to the addition item found, if one, NULL otherwise.
  288. */
  289. AdditionCheckerItem* GetItem(UpdatableResource::UpdatableResourceType type, const OpStringC& key);
  290. /**
  291. * Searches the m_items vector for all items that have the given StatusXMLDownloader assigned to them.
  292. *
  293. * @param items - a reference to a vector that will contain the items found.
  294. * @param downloader - a pointer to the StatusXMLDownloader that we're looking for, may be NULL.
  295. *
  296. * @returns - OpStatus::OK if everything went fine, ERR otherwise.
  297. */
  298. OP_STATUS GetItemsForDownloader(OpVector<AdditionCheckerItem>& items, StatusXMLDownloader* downloader);
  299. /**
  300. * Searches the m_items vector for all items that have the given StatusXMLDownloader assigned to them AND that are of the given type.
  301. *
  302. * @param items - a reference to a vector that will contain the items found.
  303. * @param downloader - a pointer to the StatusXMLDownloader that we're looking for, may be NULL.
  304. * @param type - the type of the items that we care about.
  305. *
  306. * @returns - OpStatus::OK if everything went fine, ERR otherwise.
  307. */
  308. OP_STATUS GetItemsForDownloaderAndType(OpVector<AdditionCheckerItem>& items, StatusXMLDownloader* downloader, UpdatableResource::UpdatableResourceType type);
  309. /**
  310. * Hold a list of StatusXMLDownloader objects that were created when making the XML requests.
  311. * A downloader is destroyed and removed after a callback is returned from it via the StatusXMLDownloaderListener interface.
  312. * Additionally, if any downloaders still exist while the AdditionChecker is destroyed, they are stopped and destroyed as well.
  313. */
  314. OpVector<StatusXMLDownloader> m_xml_downloaders;
  315. /**
  316. * The resolve timer.
  317. */
  318. OpTimer* m_resolve_timer;
  319. /**
  320. * The list of registered listeners.
  321. */
  322. OpListeners<AdditionCheckerListener> m_listeners;
  323. /**
  324. * The list of addition checker items that are awaiting resolving.
  325. */
  326. OpVector<AdditionCheckerItem> m_items;
  327. AutoUpdateServerURL* m_autoupdate_server_url;
  328. AutoUpdateXML* m_autoupdate_xml;
  329. };
  330. #endif // AUTOUPDATE_ADDITION_CHECKER_H
  331. #endif // AUTO_UPDATE_SUPPORT