| 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 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/sequenced_task_runner.h" | 15 #include "base/threading/thread_checker.h" |
| 16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 17 | 17 |
| 18 namespace base { |
| 19 class SequencedTaskRunner; |
| 20 class SingleThreadTaskRunner; |
| 21 } |
| 22 |
| 18 namespace net { | 23 namespace net { |
| 19 class URLRequestContextGetter; | 24 class URLRequestContextGetter; |
| 20 } | 25 } |
| 21 | 26 |
| 22 namespace component_updater { | 27 namespace component_updater { |
| 23 | 28 |
| 24 // Defines a download interface for downloading components, with retrying on | 29 // Defines a download interface for downloading components, with retrying on |
| 25 // fallback urls in case of errors. This class implements a chain of | 30 // fallback urls in case of errors. This class implements a chain of |
| 26 // responsibility design pattern. It can give successors in the chain a chance | 31 // responsibility design pattern. It can give successors in the chain a chance |
| 27 // to handle a download request, until one of them succeeds, or there are no | 32 // to handle a download request, until one of them succeeds, or there are no |
| 28 // more urls or successors to try. A callback is always called at the end of | 33 // more urls or successors to try. A callback is always called at the end of |
| 29 // the download, one time only. | 34 // the download, one time only. |
| 30 // When multiple urls and downloaders exists, first all the urls are tried, in | 35 // When multiple urls and downloaders exists, first all the urls are tried, in |
| 31 // the order they are provided in the StartDownload function argument. After | 36 // the order they are provided in the StartDownload function argument. After |
| 32 // that, the download request is routed to the next downloader in the chain. | 37 // that, the download request is routed to the next downloader in the chain. |
| 33 // The members of this class expect to be called from the UI thread only. | 38 // The members of this class expect to be called from the main thread only. |
| 34 class CrxDownloader { | 39 class CrxDownloader { |
| 35 public: | 40 public: |
| 36 struct DownloadMetrics { | 41 struct DownloadMetrics { |
| 37 enum Downloader { kNone = 0, kUrlFetcher, kBits }; | 42 enum Downloader { kNone = 0, kUrlFetcher, kBits }; |
| 38 | 43 |
| 39 DownloadMetrics(); | 44 DownloadMetrics(); |
| 40 | 45 |
| 41 GURL url; | 46 GURL url; |
| 42 | 47 |
| 43 Downloader downloader; | 48 Downloader downloader; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 | 82 |
| 78 // The callback may fire 0 or many times during a download. Since this | 83 // The callback may fire 0 or many times during a download. Since this |
| 79 // class implements a chain of responsibility, the callback can fire for | 84 // class implements a chain of responsibility, the callback can fire for |
| 80 // different urls and different downloaders. The number of actual downloaded | 85 // different urls and different downloaders. The number of actual downloaded |
| 81 // bytes is not guaranteed to monotonically increment over time. | 86 // bytes is not guaranteed to monotonically increment over time. |
| 82 typedef base::Callback<void(const Result& result)> ProgressCallback; | 87 typedef base::Callback<void(const Result& result)> ProgressCallback; |
| 83 | 88 |
| 84 // Factory method to create an instance of this class and build the | 89 // Factory method to create an instance of this class and build the |
| 85 // chain of responsibility. |is_background_download| specifies that a | 90 // chain of responsibility. |is_background_download| specifies that a |
| 86 // background downloader be used, if the platform supports it. | 91 // background downloader be used, if the platform supports it. |
| 92 // |url_fetcher_task_runner| should be an IO capable task runner able to |
| 93 // support UrlFetcherDownloader. |background_task_runner| should be an |
| 94 // IO capable thread able to support BackgroundDownloader. |
| 87 static CrxDownloader* Create( | 95 static CrxDownloader* Create( |
| 88 bool is_background_download, | 96 bool is_background_download, |
| 89 net::URLRequestContextGetter* context_getter, | 97 net::URLRequestContextGetter* context_getter, |
| 90 scoped_refptr<base::SequencedTaskRunner> task_runner); | 98 scoped_refptr<base::SequencedTaskRunner> url_fetcher_task_runner, |
| 99 scoped_refptr<base::SingleThreadTaskRunner> background_task_runner); |
| 91 virtual ~CrxDownloader(); | 100 virtual ~CrxDownloader(); |
| 92 | 101 |
| 93 void set_progress_callback(const ProgressCallback& progress_callback); | 102 void set_progress_callback(const ProgressCallback& progress_callback); |
| 94 | 103 |
| 95 // Starts the download. One instance of the class handles one download only. | 104 // Starts the download. One instance of the class handles one download only. |
| 96 // One instance of CrxDownloader can only be started once, otherwise the | 105 // One instance of CrxDownloader can only be started once, otherwise the |
| 97 // behavior is undefined. The callback gets invoked if the download can't | 106 // behavior is undefined. The callback gets invoked if the download can't |
| 98 // be started. | 107 // be started. |
| 99 void StartDownloadFromUrl(const GURL& url, | 108 void StartDownloadFromUrl(const GURL& url, |
| 100 const DownloadCallback& download_callback); | 109 const DownloadCallback& download_callback); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 120 | 129 |
| 121 // Calls the callback when progress is made. | 130 // Calls the callback when progress is made. |
| 122 void OnDownloadProgress(const Result& result); | 131 void OnDownloadProgress(const Result& result); |
| 123 | 132 |
| 124 // Returns the url which is currently being downloaded from. | 133 // Returns the url which is currently being downloaded from. |
| 125 GURL url() const; | 134 GURL url() const; |
| 126 | 135 |
| 127 private: | 136 private: |
| 128 virtual void DoStartDownload(const GURL& url) = 0; | 137 virtual void DoStartDownload(const GURL& url) = 0; |
| 129 | 138 |
| 139 base::ThreadChecker thread_checker_; |
| 140 |
| 130 std::vector<GURL> urls_; | 141 std::vector<GURL> urls_; |
| 131 scoped_ptr<CrxDownloader> successor_; | 142 scoped_ptr<CrxDownloader> successor_; |
| 132 DownloadCallback download_callback_; | 143 DownloadCallback download_callback_; |
| 133 ProgressCallback progress_callback_; | 144 ProgressCallback progress_callback_; |
| 134 | 145 |
| 135 std::vector<GURL>::iterator current_url_; | 146 std::vector<GURL>::iterator current_url_; |
| 136 | 147 |
| 137 std::vector<DownloadMetrics> download_metrics_; | 148 std::vector<DownloadMetrics> download_metrics_; |
| 138 | 149 |
| 139 DISALLOW_COPY_AND_ASSIGN(CrxDownloader); | 150 DISALLOW_COPY_AND_ASSIGN(CrxDownloader); |
| 140 }; | 151 }; |
| 141 | 152 |
| 142 } // namespace component_updater | 153 } // namespace component_updater |
| 143 | 154 |
| 144 #endif // CHROME_BROWSER_COMPONENT_UPDATER_CRX_DOWNLOADER_H_ | 155 #endif // CHROME_BROWSER_COMPONENT_UPDATER_CRX_DOWNLOADER_H_ |
| OLD | NEW |