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.

updatableresource.h 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 Marius Blomli, Michal Zajaczkowski
  9. */
  10. #ifndef _UPDATEABLERESOURCE_H_INCLUDED_
  11. #define _UPDATEABLERESOURCE_H_INCLUDED_
  12. #ifdef AUTO_UPDATE_SUPPORT
  13. #include "modules/xmlutils/xmlfragment.h"
  14. class UpdatableResource;
  15. /**
  16. * Implementations of the UpdatableResource class need to be able to store properties of the represented file, i.e. a mime-type for a plugin.
  17. * The properties are stored dynamically in order to not to hardcode parsing of the response XML and passing the information to hardcoded string
  18. * properties of the objects.
  19. *
  20. * Each of the URAttr enum items represents one XML element that may be found in the response XML, i.e.
  21. * Each value is held as a string value. In case you belive a specific attribute is parseable as int, you can try to get it using the int version of the GetAttrValue()
  22. * method that will try to parse the value for you.
  23. *
  24. * <plugin>
  25. * <mime-type>application/x-test-mime-type</mime-type>
  26. * <has-installer>0</has-installer>
  27. * </plugin>
  28. *
  29. * The value found for the <mime-type> element is stored and accesed using the URA_MIME_TYPE value.
  30. * In order to make the parsing possible, the values of this enum need to be coupled with the string values found in the UpdatableResourceFactory::URAttrName[]
  31. * array.
  32. * All that is important here is:
  33. * 1. You need to add new XML element descriptions to the URAttr enum if you add a new resource type(s) described by new XML elements;
  34. * 2. You need to add new XML element names to the UpdatableResourceFactory::URAttrName[] array if you add a new resource type(s) described by new XML elements;
  35. * 3. The items in the URAttr enum *MUST* have the same order that the strings in the UpdatableResourceFactory::URAttrName[] have;
  36. * 4. The last item in the URAttr enum *MUST* be URA_LAST.
  37. */
  38. enum URAttr
  39. {
  40. URA_TYPE = 0,
  41. URA_NAME,
  42. URA_FOLDER,
  43. URA_URL,
  44. URA_URLALT,
  45. URA_VERSION,
  46. URA_BUILDNUM,
  47. URA_ID,
  48. URA_SIZE,
  49. URA_LANG,
  50. URA_TIMESTAMP,
  51. URA_CHECKSUM,
  52. URA_INFOURL,
  53. URA_SHOWDIALOG,
  54. URA_VENDOR_NAME,
  55. URA_INSTALL_PARAMS,
  56. URA_EULA_URL,
  57. URA_PLUGIN_NAME,
  58. URA_MIME_TYPE,
  59. URA_INSTALLS_SILENTLY,
  60. URA_HAS_INSTALLER,
  61. URA_SECTION,
  62. URA_KEY,
  63. URA_DATA,
  64. URA_FILE,
  65. /***CAREFUL*WHEN*ADDING*VALUES*HERE*SEE*THE*UpdatableResourceFactory::URAttrName[]*ARRAY***/
  66. URA_LAST
  67. };
  68. /**
  69. * Class that represents an UpdatableResource property coupled with the value for that property as found in the autoupdate XML response.
  70. *
  71. * Example:
  72. * <mime-type>application/x-mime-type</mime-type> => URANameValuePair::m_attr_name = URA_MIME_TYPE, URANameValuePair::m_attr_value = "application/x-mime-type"
  73. */
  74. class URNameValuePair
  75. {
  76. friend class UpdatableResource;
  77. friend class UpdatableResourceFactory;
  78. private:
  79. /**
  80. * Return the attribute that this pair represents, i.e. URA_MIME_TYPE.
  81. *
  82. * returns - A value of URAttr type representing the XML element name that this pair represents.
  83. */
  84. URAttr GetAttrName() { return m_attr_name; }
  85. /**
  86. * Set the attribute that this pair is to represent, i.e. URA_MIME_TYPE.
  87. *
  88. * @param attr_name - The URAttr value that this pair should represent.
  89. */
  90. void SetAttrName(URAttr attr_name) { m_attr_name = attr_name; }
  91. /**
  92. * Get the value that this pair represents, i.e. "application/x-mime-type".
  93. *
  94. * @param value - A reference to an OpString object that will hold the parameter value.
  95. *
  96. * @returns - OpStatus::OK if everything went OK, ERR otherwise.
  97. */
  98. OP_STATUS GetAttrValue(OpString& value) { return value.Set(m_attr_value); }
  99. /**
  100. * Set the value that this pair is to represent, i.e. "application/x-mime-type".
  101. *
  102. * @param value - The value that should be set for this pair.
  103. *
  104. * @returns - OpStatus::OK if the value was set with success, ERR otherwise.
  105. */
  106. OP_STATUS SetValue(const OpStringC& value) { return m_attr_value.Set(value); }
  107. /**
  108. * Checks if this pair represents an attribute of the given type.
  109. *
  110. * @param attr - The type that we want to check this pair against.
  111. *
  112. * @returns - TRUE if this pair has the given type, FALSE otherwise.
  113. */
  114. BOOL IsAttr(URAttr attr) { return m_attr_name == attr; }
  115. /**
  116. * The type of this pair, i.e. the XML element name that this pair represents
  117. */
  118. URAttr m_attr_name;
  119. /**
  120. * The value of this pair, i.e. the XML element value that this pair represents.
  121. * Note that all XML element values are stored as strings and reparsed for an int value if needed at the time of
  122. * retrieving the value using the UpdatableResource::GetAttrValue() method.
  123. */
  124. OpString m_attr_value;
  125. };
  126. /**
  127. * This class creates implementations of the UpdatableResource class with the proper XML name-value pairs as found in the autoupdate response XML.
  128. * This class is fed with an XMLFragment and returns the parsed object or NULL if an error occurred.
  129. */
  130. class UpdatableResourceFactory
  131. {
  132. friend class UpdatableResource;
  133. public:
  134. /**
  135. * Takes an XMLFragment representing an UpdatableFile (<file>...</file>) or an UpdatableSetting (<setting>...</setting>) and returns a pointer to
  136. * a newly created UpdatableResource* object. Will do all the work needed to parse thorugh the XML, recognize the XML element names and values and
  137. * store them with the newly created object.
  138. * Also, will choose the correct implementation of the UpdatableResource abstract basing on the <type> XML element found.
  139. *
  140. * @param resource_type - "file" or "setting" in order to be able to parse the XMLFragment correctly
  141. * @param fragment - the XML fragment at the start of the resource description, i.e. right after the <file> or <setting> element
  142. * @param resource - an UpdatableResource** pointer that will be changed to point to the newly created resource. Will be set to NULL if no resource
  143. * could be created with this call.
  144. *
  145. * @returns - OpStatus::OK if an object implementing the UpdatableResource abstract could be created successfuly with the given XML, ERR otherwise.
  146. * You receive the ownership of the resulting resource in case this function succeeds, and you get a NULL otherwise.
  147. */
  148. static OP_STATUS GetResourceFromXML(const OpStringC& resource_type, XMLFragment& fragment, UpdatableResource** resource);
  149. protected:
  150. /**
  151. * The maximal attribute count per resource. XML sanity safety mechanism, can be raised reasonably if needed.
  152. */
  153. static const unsigned URA_MAX_PROPERTY_COUNT = 20;
  154. /**
  155. * The array representing the XML element names as found in the request XML. See documentation in updatableresource.cpp.
  156. */
  157. static const uni_char* URAttrName[];
  158. /**
  159. * Tries to convert the XML element name passed as the string parameter to a value of URAttr type.
  160. * Relies on the correct order of the elements in the URAttr enum and URAttrName[]!
  161. *
  162. * @param name - The string representing the XML element name, i.e. "mime-type".
  163. *
  164. * @returns - An item from the URAttr enum representing the given string (i.e. URA_MIME_TYPE), or URA_LAST in case the string could not be
  165. * recognized.
  166. */
  167. static URAttr ConvertNameToAttr(const OpStringC& name);
  168. /**
  169. * Creates (i.e. OP_NEW()) a new resource of the correct type basing on the type string passed as the parameter.
  170. * May return NULL if the resource type string is not recognized or on OOM.
  171. *
  172. * @param type - A string representing the resource type as found in the XML, i.e. <type>plugin</type>, additionally the "setting" type is recognized
  173. *
  174. * @returns - A pointer to the newly created object or NULL if something went wrong.
  175. */
  176. static UpdatableResource* GetEmptyResourceByType(const OpStringC& type);
  177. };
  178. /**
  179. * This is a base class for a representation of a resource (file or pref)
  180. * that is to be kept up to date by the auto update system.
  181. *
  182. * There are two primary subclasses, UpdatableFile and UpdatableSetting,
  183. * for holding information files and settings that should be kept up to date.
  184. * Instances of these classes are registered with the AutoUpdater, which
  185. * stores them and checks them against whatever the status xml says when it's
  186. * downloaded. If the status xml has notifications of updates that match
  187. * updates that have been registered, they should be downloaded.
  188. *
  189. * UpdatableFile and Updatable Setting are both intended to be subclassed if
  190. * they need the CheckResource function to do something special, like
  191. * integrity or signature checks.
  192. */
  193. class UpdatableResource
  194. {
  195. friend class UpdatableResourceFactory;
  196. public:
  197. enum ResourceClass
  198. {
  199. File,
  200. Setting
  201. };
  202. /**
  203. * Updatable resource type. This is what each implementation of this abstract class should return with the GetType() call.
  204. */
  205. enum UpdatableResourceType
  206. {
  207. RTEmpty = 0x0000,
  208. RTPackage = 0x0001,
  209. RTPatch = 0x0002,
  210. RTSpoofFile = 0x0004,
  211. RTBrowserJSFile = 0x0008,
  212. RTSetting = 0x0010,
  213. RTDictionary = 0x0040,
  214. RTPlugin = 0x0080,
  215. RTHardwareBlocklist = 0x0100,
  216. RTHandlersIgnore = 0x0200,
  217. RTAll = 0xFFFFFFFF
  218. };
  219. UpdatableResource();
  220. virtual ~UpdatableResource() {}
  221. /**
  222. * Gets the type of resource
  223. *
  224. * @return returns the type of resource based on the UpdatableResourceType
  225. */
  226. virtual UpdatableResourceType GetType() = 0;
  227. /**
  228. * Gets the class of resource
  229. *
  230. * @return returns the type of resource based on the ResourceClass enum
  231. */
  232. virtual ResourceClass GetResourceClass() = 0;
  233. /**
  234. * Function that checks whether a resource is ok to install. Override this
  235. * to introduce signature/sanity checking of downloaded resources.
  236. *
  237. * @return TRUE if the resource checks out and can be used, FALSE otherwise.
  238. */
  239. virtual BOOL CheckResource() = 0;
  240. /**
  241. * Function that initiates the replacement of the old resource with the
  242. * updated one.
  243. */
  244. virtual OP_STATUS UpdateResource() = 0;
  245. /**
  246. * Function to clean up after updating.
  247. */
  248. virtual OP_STATUS Cleanup() = 0;
  249. /**
  250. * Get name of resource
  251. */
  252. virtual const uni_char* GetResourceName() = 0;
  253. /**
  254. * @return TRUE if resource requires an additional unpacking step to be executed
  255. */
  256. virtual BOOL UpdateRequiresUnpacking() = 0;
  257. /**
  258. * @return TRUE if resource implies a restart (of the Opera binary) for the update to complete
  259. */
  260. virtual BOOL UpdateRequiresRestart() = 0;
  261. /**
  262. * This defines the resource key that is needed to resolve the resource type as an addition.
  263. * You only need to implement this if you want to use the particular resource type as an addition since
  264. * additions need to have an unique key.
  265. * The key for plugins is the mime-type, the key for dictionaries is the language ID.
  266. *
  267. * @returns URAttr name of the attribute that should serve as the key, i.e. URA_MIME_TYPE for plugins.
  268. */
  269. virtual URAttr GetResourceKey() { return URA_LAST; };
  270. /**
  271. * Retrieve the value of the given attribute as string.
  272. *
  273. * @param attr - URAttr name of the requested attribute
  274. * @param value - the string value of the requested attribute, if found
  275. *
  276. * @returns OpStatus::OK if the attribute with the given name could be found and the value could be set OK, ERR otherwise
  277. */
  278. OP_STATUS GetAttrValue(URAttr attr, OpString& value);
  279. /**
  280. * Retrieve the value of the given attribute as int.
  281. * Note that it might not be possible to parse the attribute as int and an OpStatus::ERR will be returned.
  282. *
  283. * @param attr - URAttr name of the requested attribute
  284. * @param value - the int value of the attribute if it's possible to parse the value as int
  285. *
  286. * @returns OpStatus::OK if the attribute was found and could be parsed as int, ERR otherwise.
  287. */
  288. OP_STATUS GetAttrValue(URAttr attr, int& value);
  289. /**
  290. * Retrieve the value of the given attribute as bool.
  291. * Note that it might not be possible to parse the attribute as bool and an OpStatus::ERR will be returned.
  292. * The parser looks for the following strings: "1", "0", "true", "false", "yes", "no". If the attribute value
  293. * doesn't match any of the above, ERR is returned and the value of the bool parameter is not determined.
  294. *
  295. * @param attr - URAttr name of the requested attribute
  296. * @param value - the bool value of the attribute if it's possible to parse the value as int
  297. *
  298. * @returns OpStatus::OK if the attribute was found and could be parsed as int, ERR otherwise.
  299. */
  300. OP_STATUS GetAttrValue(URAttr attr, bool& value);
  301. protected:
  302. /**
  303. * Helper method. Searches the given pairs vector for an attribute with the attr name, returns the value of the attribute with the value parameter.
  304. *
  305. * @param pairs - The input pairs vector that should be searched
  306. * @param attr - The URAttr name of the attribute searched for
  307. * @param value - The value of the found attribute, if any
  308. *
  309. * @returns OpStatus::OK if the attribute could be found and the value was set with success, ERR otherwise.
  310. */
  311. static OP_STATUS GetValueByAttr(OpAutoVector<URNameValuePair>& pairs, URAttr attr, OpString& value);
  312. /**
  313. * Implement this method to verify the attribute set after the response XML has been parsed.
  314. * This method should return FALSE in case that the attribute list and/or the attribute values are not acceptable for the resource that implements
  315. * this method. A resource that failes the attribute verification won't be returned as a valid resource by the StatusXMLDownloader after parsing the
  316. * response XML.
  317. *
  318. * returns TRUE if the resource is valid, FALSE otherwise.
  319. */
  320. virtual BOOL VerifyAttributes() = 0;
  321. /**
  322. * Add a name/value pair for this resource. Note that there is a limit of attribute count per resource, see URA_MAX_PROPERTY_COUNT.
  323. *
  324. * @param pair The name/value pair that should be added to this resource. Ownership of the URNameValuePair object is NOT passed to the UpdatableResource class.
  325. *
  326. * @returns OpStatus::OK if everything went OK, ERR otherwise.
  327. */
  328. OP_STATUS AddNameValuePair(URNameValuePair* pair);
  329. /**
  330. * Checks if this resource has an attribute with the given name that has any content, i.e. the string representing the value of the attribute is not empty.
  331. *
  332. * @returns TRUE if the above condition is fulfilled, FALSE otherwise.
  333. */
  334. BOOL HasAttrWithContent(URAttr attr);
  335. /**
  336. * Holds the name/value pairs.
  337. */
  338. OpAutoVector<URNameValuePair> m_pairs;
  339. };
  340. #endif // AUTO_UPDATE_SUPPORT
  341. #endif // _UPDATEABLERESOURCE_H_INCLUDED_