OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_COMPONENT_UPDATER_CRX_DOWNLOADER_H_ | 5 #ifndef CHROME_BROWSER_COMPONENT_UPDATER_CRX_DOWNLOADER_H_ |
6 #define CHROME_BROWSER_COMPONENT_UPDATER_CRX_DOWNLOADER_H_ | 6 #define CHROME_BROWSER_COMPONENT_UPDATER_CRX_DOWNLOADER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 // fallback urls in case of errors. This class implements a chain of | 24 // fallback urls in case of errors. This class implements a chain of |
25 // responsibility design pattern. It can give successors in the chain a chance | 25 // responsibility design pattern. It can give successors in the chain a chance |
26 // to handle a download request, until one of them succeeds, or there are no | 26 // to handle a download request, until one of them succeeds, or there are no |
27 // more urls or successors to try. A callback is always called at the end of | 27 // more urls or successors to try. A callback is always called at the end of |
28 // the download, one time only. | 28 // the download, one time only. |
29 // When multiple urls and downloaders exists, first all the urls are tried, in | 29 // When multiple urls and downloaders exists, first all the urls are tried, in |
30 // the order they are provided in the StartDownload function argument. After | 30 // the order they are provided in the StartDownload function argument. After |
31 // that, the download request is routed to the next downloader in the chain. | 31 // that, the download request is routed to the next downloader in the chain. |
32 // The members of this class expect to be called from the UI thread only. | 32 // The members of this class expect to be called from the UI thread only. |
33 class CrxDownloader { | 33 class CrxDownloader { |
34 // |error| contains the download error: 0 indicates success. |response| | 34 public: |
35 // contains the file path of the downloaded file, if the download was | 35 // Contains the outcome of the download. |
36 // successful. | 36 struct Result { |
| 37 Result() : error(0), is_background_download(0) {} |
| 38 |
| 39 // Download error: 0 indicates success. |
| 40 int error; |
| 41 |
| 42 // True if the download has been completed using the background downloader. |
| 43 int is_background_download; |
| 44 |
| 45 // Path of the downloaded file if the download was successful. |
| 46 base::FilePath response; |
| 47 }; |
| 48 |
37 // The callback fires only once, regardless of how many urls are tried, and | 49 // The callback fires only once, regardless of how many urls are tried, and |
38 // how many successors in the chain of downloaders have handled the | 50 // how many successors in the chain of downloaders have handled the |
39 // download. The callback interface can be extended if needed to provide | 51 // download. The callback interface can be extended if needed to provide |
40 // more visibility into how the download has been handled, including | 52 // more visibility into how the download has been handled, including |
41 // specific error codes and download metrics. | 53 // specific error codes and download metrics. |
42 typedef base::Callback<void(int error, | 54 typedef base::Callback<void (const Result& result)> DownloadCallback; |
43 const base::FilePath& response)> DownloadCallback; | |
44 | 55 |
45 public: | |
46 // Factory method to create an instance of this class and build the | 56 // Factory method to create an instance of this class and build the |
47 // chain of responsibility. | 57 // chain of responsibility. |is_background_download| specifies that a |
| 58 // background downloader be used, if the platform supports it. |
48 static CrxDownloader* Create( | 59 static CrxDownloader* Create( |
| 60 bool is_background_download, |
49 net::URLRequestContextGetter* context_getter, | 61 net::URLRequestContextGetter* context_getter, |
50 scoped_refptr<base::SequencedTaskRunner> task_runner, | 62 scoped_refptr<base::SequencedTaskRunner> task_runner, |
51 const DownloadCallback& download_callback); | 63 const DownloadCallback& download_callback); |
52 virtual ~CrxDownloader(); | 64 virtual ~CrxDownloader(); |
53 | 65 |
54 // Starts the download. One instance of the class handles one download only. | 66 // Starts the download. One instance of the class handles one download only. |
55 void StartDownloadFromUrl(const GURL& url); | 67 // Returns true if success or false in case of errors. One instance of |
56 void StartDownload(const std::vector<GURL>& urls); | 68 // CrxDownloader can only be started once. Calling the StartDownload functions |
57 | 69 // more than once results in an error. |
| 70 bool StartDownloadFromUrl(const GURL& url); |
| 71 bool StartDownload(const std::vector<GURL>& urls); |
58 | 72 |
59 protected: | 73 protected: |
60 CrxDownloader(); | 74 CrxDownloader(scoped_ptr<CrxDownloader> successor, |
| 75 const DownloadCallback& download_callback); |
61 | 76 |
62 // Derived class must call this function after each attempt at downloading | 77 // Handles the fallback in the case of multiple urls and routing of the |
63 // from the urls provided in the StartDownload function. | 78 // download to the following successor in the chain. Derived classes must call |
64 void OnDownloadComplete(bool is_handled, | 79 // this function after each attempt at downloading the urls provided |
65 int error, | 80 // in the StartDownload function. |
66 const base::FilePath& response); | 81 // In case of errors, |is_handled| indicates that a server side error has |
| 82 // occured and the corresponding url should not be retried down the chain to |
| 83 // avoid DDOS of the server. |
| 84 void OnDownloadComplete(bool is_handled, const Result& result); |
67 | 85 |
68 private: | 86 private: |
69 virtual void DoStartDownload(const GURL& url) = 0; | 87 virtual void DoStartDownload(const GURL& url) = 0; |
70 | 88 |
71 std::vector<GURL> urls_; | 89 std::vector<GURL> urls_; |
72 scoped_ptr<CrxDownloader> successor_; | 90 scoped_ptr<CrxDownloader> successor_; |
73 DownloadCallback download_callback_; | 91 DownloadCallback download_callback_; |
74 | 92 |
75 size_t current_url_; | 93 std::vector<GURL>::iterator current_url_; |
| 94 |
| 95 bool is_active_; |
76 | 96 |
77 DISALLOW_COPY_AND_ASSIGN(CrxDownloader); | 97 DISALLOW_COPY_AND_ASSIGN(CrxDownloader); |
78 }; | 98 }; |
79 | 99 |
80 } // namespace component_updater | 100 } // namespace component_updater |
81 | 101 |
82 #endif // CHROME_BROWSER_COMPONENT_UPDATER_CRX_DOWNLOADER_H_ | 102 #endif // CHROME_BROWSER_COMPONENT_UPDATER_CRX_DOWNLOADER_H_ |
83 | 103 |
OLD | NEW |