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.

statusxmldownloader.h 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 _STATUSXMLDOWNLOADER_H_INCLUDED
  11. #define _STATUSXMLDOWNLOADER_H_INCLUDED
  12. #ifdef AUTO_UPDATE_SUPPORT
  13. #include "modules/windowcommander/OpTransferManager.h"
  14. #include "modules/xmlutils/xmlfragment.h"
  15. #include "adjunct/autoupdate/updatableresource.h"
  16. class AutoUpdater;
  17. class StatusXMLDownloader;
  18. class StatusXMLDownloaderListener;
  19. class StatusXMLDownloaderHolder
  20. {
  21. public:
  22. static OP_STATUS AddDownloader(StatusXMLDownloader* downloader);
  23. static OP_STATUS RemoveDownloader(StatusXMLDownloader* downloader);
  24. static void ReplaceRedirectedFileURLs(const OpString& url, const OpString redirected_url);
  25. private:
  26. static OpVector<StatusXMLDownloader> downloader_list;
  27. };
  28. /**
  29. * Class that encapsulates the download and parsing of the status xml
  30. * document. The class is only supposed to be used by the AutoUpdater
  31. * class, and you need an AutoUpdater instance to instantiate and
  32. * run the download and parsing.
  33. *
  34. * How to use:
  35. * (1) From the AutoUpdater, instantiate using itself as
  36. * argument to the constructor.
  37. * (2) Call StartDownload() using the download url with parameters
  38. * as parameter
  39. * (3) Release control and wait for callbacks to either
  40. * StatusXMLDownloaded or StatusXMLDownloadFailed.
  41. * (4) If success, fetch the downloaded information by calling the
  42. * GetDownloadFileArray() and GetChangeSettingArray() functions.
  43. * (5) If error, fetch the error code through the GetErrorCode function.
  44. *
  45. * @todo The xml should be signed in some way, probably with a private key...
  46. */
  47. class StatusXMLDownloader : public OpTransferListener
  48. {
  49. public:
  50. /**
  51. * Enum of states, can be used to tell what went wrong if download or parsing
  52. * fails
  53. */
  54. enum DownloadStatus
  55. {
  56. READY,
  57. INPROGRESS,
  58. NO_TRANSFERITEM,
  59. NO_URL,
  60. LOAD_FAILED,
  61. DOWNLOAD_FAILED,
  62. DOWNLOAD_ABORTED,
  63. PARSE_ERROR,
  64. WRONG_XML,
  65. OUT_OF_MEMORY,
  66. SUCCESS
  67. };
  68. /**
  69. * The StatusXMLDownloader requires a check type given when calling Init().
  70. * The value is used to determine the storage location for the
  71. */
  72. enum CheckType
  73. {
  74. CheckTypeInvalid,
  75. CheckTypeUpdate,
  76. CheckTypeOther
  77. };
  78. private:
  79. OpFileFolder m_response_file_folder;
  80. OpString m_response_file_name;
  81. CheckType m_check_type;
  82. /**
  83. * Pointer to the AutoUpdater that initated this download to use for
  84. * calling back when the download is finished.
  85. */
  86. StatusXMLDownloaderListener* m_listener;
  87. /**
  88. * A reference to the TransferItem that represents the transfer
  89. * initiated by this class.
  90. */
  91. OpTransferItem* m_transferItem;
  92. /**
  93. * List of resources for updating for the status xml.
  94. */
  95. OpVector<UpdatableResource> m_resource_list;
  96. /**
  97. * Iterator for going through list of resources that are requested
  98. * for update.
  99. */
  100. UINT m_resource_iterator;
  101. /**
  102. * Status of the xml download and parsing.
  103. */
  104. DownloadStatus m_status;
  105. /**
  106. * Download url string from the xml
  107. */
  108. OpString m_download_url;
  109. /**
  110. * Parse resource element (setting or file)
  111. */
  112. OP_STATUS ParseResourceElement(const OpStringC& resource_type, XMLFragment& fragment);
  113. /**
  114. * Parse resources element
  115. */
  116. OP_STATUS ParseResourcesElement(XMLFragment& fragment);
  117. /**
  118. * Internal function used in parsing the xml fragment. Enters the node to
  119. * fetch the text then closes it again. Note: this call modifies frag, it
  120. * passes the relevant node, so subsequent calls to GetSubnodeText() or
  121. * XMLFragment::EnterNode() with the same node name will enter the NEXT node.
  122. * Also note: If the requested node doesen't exist, the function returns NULL
  123. *
  124. * @param frag The fragment to work with. Will be modified by the call.
  125. * @param node_name The name of the node to enter.
  126. * @return pointer to uni_char buffer holding the text in the requested node.
  127. * NULL if requested node does not exist.
  128. */
  129. OP_STATUS GetSubnodeText(XMLFragment* frag, const uni_char* node_name, OpString &text);
  130. virtual void OnProgress(OpTransferItem* transferItem, TransferStatus status); ///< OpTransferListener implementation
  131. virtual void OnReset(OpTransferItem* transferItem) {} ///< OpTransferListener implementation
  132. virtual void OnRedirect(OpTransferItem* transferItem, URL* redirect_from, URL* redirect_to) {} ///< OpTransferListener implementation
  133. OP_STATUS GenerateResponseFilename();
  134. OP_STATUS DeleteResponseFile();
  135. public:
  136. /**
  137. * Create the downloader for the xml status document. Supply
  138. * a pointer to the AutoUpdater that will receive the callback
  139. * when the download is done.
  140. */
  141. StatusXMLDownloader();
  142. ~StatusXMLDownloader();
  143. OP_STATUS Init(CheckType check_type, StatusXMLDownloaderListener* listener);
  144. /**
  145. * Call this to start downloading from the location pointed to by
  146. * xml_url.
  147. *
  148. * @param xml_url Full url to download the status xml including
  149. * parameters.
  150. *
  151. */
  152. OP_STATUS StartDownload(const OpString &xml_url);
  153. /**
  154. * Call this function to start posting the xml to the autoupdate server
  155. *
  156. * @param post_address Url to post xml to
  157. * @param xml String containing the xml to post
  158. *
  159. */
  160. OP_STATUS StartXMLRequest(const OpString& post_address, const OpString8 &xml);
  161. /**
  162. * Function that parses the downloaded document and stores the information
  163. * from the xml in the download_file_array and change_setting_array.
  164. *
  165. * @return TRUE if the xml was successfully parsed, FALSE if not.
  166. */
  167. DownloadStatus ParseDownloadedXML();
  168. /**
  169. * Call this function to stop the request
  170. *
  171. */
  172. OP_STATUS StopRequest();
  173. /**
  174. * Get first resource
  175. */
  176. UpdatableResource* GetFirstResource() { m_resource_iterator = 0; return GetNextResource(); }
  177. /**
  178. * Call this to traverse the list of resources requested for update found
  179. * in the xml. Each call returns a new element until the list has been
  180. * traversed, then it returns NULL, and is reset.
  181. */
  182. UpdatableResource* GetNextResource();
  183. /**
  184. * Remove resource
  185. */
  186. OP_STATUS RemoveResource(UpdatableResource* res);
  187. UINT32 GetResourceCount();
  188. /**
  189. * Get the download page url string (www.opera.com/download)
  190. */
  191. OP_STATUS GetDownloadURL(OpString& download_url) { return download_url.Set(m_download_url); }
  192. /**
  193. * Replace redirected url in xml file.
  194. */
  195. OP_STATUS ReplaceRedirectedFileURL(const OpString& url, const OpString& redirected_url);
  196. };
  197. class StatusXMLDownloaderListener
  198. {
  199. public:
  200. virtual ~StatusXMLDownloaderListener() {};
  201. /**
  202. * Callback that signals that the status XML document has been downloaded
  203. * into the supplied url.
  204. */
  205. virtual void StatusXMLDownloaded(StatusXMLDownloader* downloader) = 0;
  206. /**
  207. * Callback used from the StatusXMLDownloader that signals that the
  208. * status XML document couldn't be downloaded.
  209. */
  210. virtual void StatusXMLDownloadFailed(StatusXMLDownloader* downloader, StatusXMLDownloader::DownloadStatus status) = 0;
  211. };
  212. #endif // AUTO_UPDATE_SUPPORT
  213. #endif // _STATUSXMLDOWNLOADER_H_INCLUDED