Update mojo sdk to rev c02a28868825edfa57ab77947b8cb15e741c5598
[chromium-blink-merge.git] / components / update_client / crx_downloader.h
blob7d7720f7765f93a7915c1ca86b92ae2ed7eec466
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_UPDATE_CLIENT_CRX_DOWNLOADER_H_
6 #define COMPONENTS_UPDATE_CLIENT_CRX_DOWNLOADER_H_
8 #include <stdint.h>
9 #include <vector>
11 #include "base/callback.h"
12 #include "base/files/file_path.h"
13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/threading/thread_checker.h"
17 #include "url/gurl.h"
19 namespace base {
20 class SequencedTaskRunner;
21 class SingleThreadTaskRunner;
24 namespace net {
25 class URLRequestContextGetter;
28 namespace update_client {
30 // Defines a download interface for downloading components, with retrying on
31 // fallback urls in case of errors. This class implements a chain of
32 // responsibility design pattern. It can give successors in the chain a chance
33 // to handle a download request, until one of them succeeds, or there are no
34 // more urls or successors to try. A callback is always called at the end of
35 // the download, one time only.
36 // When multiple urls and downloaders exists, first all the urls are tried, in
37 // the order they are provided in the StartDownload function argument. After
38 // that, the download request is routed to the next downloader in the chain.
39 // The members of this class expect to be called from the main thread only.
40 class CrxDownloader {
41 public:
42 struct DownloadMetrics {
43 enum Downloader { kNone = 0, kUrlFetcher, kBits };
45 DownloadMetrics();
47 GURL url;
49 Downloader downloader;
51 int error;
53 int64_t downloaded_bytes; // -1 means that the byte count is unknown.
54 int64_t total_bytes;
56 uint64_t download_time_ms;
59 // Contains the progress or the outcome of the download.
60 struct Result {
61 Result();
63 // Download error: 0 indicates success.
64 int error;
66 // Path of the downloaded file if the download was successful.
67 base::FilePath response;
69 // Number of bytes actually downloaded, not including the bytes downloaded
70 // as a result of falling back on urls.
71 int64_t downloaded_bytes;
73 // Number of bytes expected to be downloaded.
74 int64_t total_bytes;
77 // The callback fires only once, regardless of how many urls are tried, and
78 // how many successors in the chain of downloaders have handled the
79 // download. The callback interface can be extended if needed to provide
80 // more visibility into how the download has been handled, including
81 // specific error codes and download metrics.
82 using DownloadCallback = base::Callback<void(const Result& result)>;
84 // The callback may fire 0 or many times during a download. Since this
85 // class implements a chain of responsibility, the callback can fire for
86 // different urls and different downloaders. The number of actual downloaded
87 // bytes is not guaranteed to monotonically increment over time.
88 using ProgressCallback = base::Callback<void(const Result& result)>;
90 using Factory = scoped_ptr<CrxDownloader>(*)(
91 bool,
92 net::URLRequestContextGetter*,
93 const scoped_refptr<base::SequencedTaskRunner>&,
94 const scoped_refptr<base::SingleThreadTaskRunner>&);
96 // Factory method to create an instance of this class and build the
97 // chain of responsibility. |is_background_download| specifies that a
98 // background downloader be used, if the platform supports it.
99 // |url_fetcher_task_runner| should be an IO capable task runner able to
100 // support UrlFetcherDownloader. |background_task_runner| should be an
101 // IO capable thread able to support BackgroundDownloader.
102 static scoped_ptr<CrxDownloader> Create(
103 bool is_background_download,
104 net::URLRequestContextGetter* context_getter,
105 const scoped_refptr<base::SequencedTaskRunner>& url_fetcher_task_runner,
106 const scoped_refptr<base::SingleThreadTaskRunner>&
107 background_task_runner);
108 virtual ~CrxDownloader();
110 void set_progress_callback(const ProgressCallback& progress_callback);
112 // Starts the download. One instance of the class handles one download only.
113 // One instance of CrxDownloader can only be started once, otherwise the
114 // behavior is undefined. The callback gets invoked if the download can't
115 // be started.
116 void StartDownloadFromUrl(const GURL& url,
117 const DownloadCallback& download_callback);
118 void StartDownload(const std::vector<GURL>& urls,
119 const DownloadCallback& download_callback);
121 const std::vector<DownloadMetrics> download_metrics() const;
123 protected:
124 explicit CrxDownloader(scoped_ptr<CrxDownloader> successor);
126 // Handles the fallback in the case of multiple urls and routing of the
127 // download to the following successor in the chain. Derived classes must call
128 // this function after each attempt at downloading the urls provided
129 // in the StartDownload function.
130 // In case of errors, |is_handled| indicates that a server side error has
131 // occured for the current url and the url should not be retried down
132 // the chain to avoid DDOS of the server. This url will be removed from the
133 // list of url and never tried again.
134 void OnDownloadComplete(bool is_handled,
135 const Result& result,
136 const DownloadMetrics& download_metrics);
138 // Calls the callback when progress is made.
139 void OnDownloadProgress(const Result& result);
141 // Returns the url which is currently being downloaded from.
142 GURL url() const;
144 private:
145 virtual void DoStartDownload(const GURL& url) = 0;
147 base::ThreadChecker thread_checker_;
149 std::vector<GURL> urls_;
150 scoped_ptr<CrxDownloader> successor_;
151 DownloadCallback download_callback_;
152 ProgressCallback progress_callback_;
154 std::vector<GURL>::iterator current_url_;
156 std::vector<DownloadMetrics> download_metrics_;
158 DISALLOW_COPY_AND_ASSIGN(CrxDownloader);
161 } // namespace update_client
163 #endif // COMPONENTS_UPDATE_CLIENT_CRX_DOWNLOADER_H_