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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_COMPONENT_UPDATER_CRX_DOWNLOADER_H_
6 #define CHROME_BROWSER_COMPONENT_UPDATER_CRX_DOWNLOADER_H_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/files/file_path.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/sequenced_task_runner.h"
19 class URLRequestContextGetter
;
22 namespace component_updater
{
24 // Defines a download interface for downloading components, with retrying on
25 // 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
27 // 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
29 // the download, one time only.
30 // 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
32 // 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.
36 struct DownloadMetrics
{
47 Downloader downloader
;
51 int64 downloaded_bytes
; // -1 means that the byte count is unknown.
54 uint64 download_time_ms
;
57 // Contains the progress or the outcome of the download.
61 // Download error: 0 indicates success.
64 // Path of the downloaded file if the download was successful.
65 base::FilePath response
;
67 // Number of bytes actually downloaded, not including the bytes downloaded
68 // as a result of falling back on urls.
69 int64 downloaded_bytes
;
71 // Number of bytes expected to be downloaded.
75 // The callback fires only once, regardless of how many urls are tried, and
76 // how many successors in the chain of downloaders have handled the
77 // download. The callback interface can be extended if needed to provide
78 // more visibility into how the download has been handled, including
79 // specific error codes and download metrics.
80 typedef base::Callback
<void (const Result
& result
)> DownloadCallback
;
82 // The callback may fire 0 or many times during a download. Since this
83 // class implements a chain of responsibility, the callback can fire for
84 // different urls and different downloaders. The number of actual downloaded
85 // bytes is not guaranteed to monotonically increment over time.
86 typedef base::Callback
<void(const Result
& result
)> ProgressCallback
;
88 // Factory method to create an instance of this class and build the
89 // chain of responsibility. |is_background_download| specifies that a
90 // background downloader be used, if the platform supports it.
91 static CrxDownloader
* Create(
92 bool is_background_download
,
93 net::URLRequestContextGetter
* context_getter
,
94 scoped_refptr
<base::SequencedTaskRunner
> task_runner
);
95 virtual ~CrxDownloader();
97 void set_progress_callback(const ProgressCallback
& progress_callback
);
99 // Starts the download. One instance of the class handles one download only.
100 // One instance of CrxDownloader can only be started once, otherwise the
101 // behavior is undefined. The callback gets invoked if the download can't
103 void StartDownloadFromUrl(const GURL
& url
,
104 const DownloadCallback
& download_callback
);
105 void StartDownload(const std::vector
<GURL
>& urls
,
106 const DownloadCallback
& download_callback
);
108 const std::vector
<DownloadMetrics
> download_metrics() const;
111 explicit CrxDownloader(scoped_ptr
<CrxDownloader
> successor
);
113 // Handles the fallback in the case of multiple urls and routing of the
114 // download to the following successor in the chain. Derived classes must call
115 // this function after each attempt at downloading the urls provided
116 // in the StartDownload function.
117 // In case of errors, |is_handled| indicates that a server side error has
118 // occured for the current url and the url should not be retried down
119 // the chain to avoid DDOS of the server. This url will be removed from the
120 // list of url and never tried again.
121 void OnDownloadComplete(bool is_handled
,
122 const Result
& result
,
123 const DownloadMetrics
& download_metrics
);
125 // Calls the callback when progress is made.
126 void OnDownloadProgress(const Result
& result
);
128 // Returns the url which is currently being downloaded from.
132 virtual void DoStartDownload(const GURL
& url
) = 0;
134 std::vector
<GURL
> urls_
;
135 scoped_ptr
<CrxDownloader
> successor_
;
136 DownloadCallback download_callback_
;
137 ProgressCallback progress_callback_
;
139 std::vector
<GURL
>::iterator current_url_
;
141 std::vector
<DownloadMetrics
> download_metrics_
;
143 DISALLOW_COPY_AND_ASSIGN(CrxDownloader
);
146 } // namespace component_updater
148 #endif // CHROME_BROWSER_COMPONENT_UPDATER_CRX_DOWNLOADER_H_