Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: chrome/browser/component_updater/crx_downloader.h

Issue 385013002: Componentize component_updater: Replace content::BrowserThread usage with task runners (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 typedef base::Callback<void(const Result& result)> DownloadCallback; 81 typedef base::Callback<void(const Result& result)> DownloadCallback;
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.
blundell 2014/07/15 08:45:20 The new params should be documented.
tommycli 2014/07/15 18:58:25 Done.
87 static CrxDownloader* Create( 92 static CrxDownloader* Create(
88 bool is_background_download, 93 bool is_background_download,
89 net::URLRequestContextGetter* context_getter, 94 net::URLRequestContextGetter* context_getter,
90 scoped_refptr<base::SequencedTaskRunner> task_runner); 95 scoped_refptr<base::SequencedTaskRunner> sequenced_task_runner,
blundell 2014/07/15 08:45:20 It would be good if these params could be given na
tommycli 2014/07/15 18:58:25 Done.
96 scoped_refptr<base::SingleThreadTaskRunner> single_thread_task_runner);
91 virtual ~CrxDownloader(); 97 virtual ~CrxDownloader();
92 98
93 void set_progress_callback(const ProgressCallback& progress_callback); 99 void set_progress_callback(const ProgressCallback& progress_callback);
94 100
95 // Starts the download. One instance of the class handles one download only. 101 // Starts the download. One instance of the class handles one download only.
96 // One instance of CrxDownloader can only be started once, otherwise the 102 // One instance of CrxDownloader can only be started once, otherwise the
97 // behavior is undefined. The callback gets invoked if the download can't 103 // behavior is undefined. The callback gets invoked if the download can't
98 // be started. 104 // be started.
99 void StartDownloadFromUrl(const GURL& url, 105 void StartDownloadFromUrl(const GURL& url,
100 const DownloadCallback& download_callback); 106 const DownloadCallback& download_callback);
(...skipping 19 matching lines...) Expand all
120 126
121 // Calls the callback when progress is made. 127 // Calls the callback when progress is made.
122 void OnDownloadProgress(const Result& result); 128 void OnDownloadProgress(const Result& result);
123 129
124 // Returns the url which is currently being downloaded from. 130 // Returns the url which is currently being downloaded from.
125 GURL url() const; 131 GURL url() const;
126 132
127 private: 133 private:
128 virtual void DoStartDownload(const GURL& url) = 0; 134 virtual void DoStartDownload(const GURL& url) = 0;
129 135
136 base::ThreadChecker thread_checker_;
137
130 std::vector<GURL> urls_; 138 std::vector<GURL> urls_;
131 scoped_ptr<CrxDownloader> successor_; 139 scoped_ptr<CrxDownloader> successor_;
132 DownloadCallback download_callback_; 140 DownloadCallback download_callback_;
133 ProgressCallback progress_callback_; 141 ProgressCallback progress_callback_;
134 142
135 std::vector<GURL>::iterator current_url_; 143 std::vector<GURL>::iterator current_url_;
136 144
137 std::vector<DownloadMetrics> download_metrics_; 145 std::vector<DownloadMetrics> download_metrics_;
138 146
139 DISALLOW_COPY_AND_ASSIGN(CrxDownloader); 147 DISALLOW_COPY_AND_ASSIGN(CrxDownloader);
140 }; 148 };
141 149
142 } // namespace component_updater 150 } // namespace component_updater
143 151
144 #endif // CHROME_BROWSER_COMPONENT_UPDATER_CRX_DOWNLOADER_H_ 152 #endif // CHROME_BROWSER_COMPONENT_UPDATER_CRX_DOWNLOADER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698