[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors. All rights reserved. |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/component_updater/component_updater_service.h" |
| 6 | |
| 7 | #include <algorithm> |
[email protected] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 8 | #include <set> |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
| 11 | #include "base/at_exit.h" |
[email protected] | 44da56e | 2011-11-21 19:59:14 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 14 | #include "base/file_util.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 15 | #include "base/files/file_path.h" |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 16 | #include "base/logging.h" |
[email protected] | 7226b33c | 2011-08-18 08:44:22 | [diff] [blame] | 17 | #include "base/memory/scoped_ptr.h" |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 18 | #include "base/memory/weak_ptr.h" |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 19 | #include "base/stl_util.h" |
[email protected] | 8f5f2ea | 2013-10-31 09:39:10 | [diff] [blame] | 20 | #include "base/threading/sequenced_worker_pool.h" |
[email protected] | 41a17c5 | 2013-06-28 00:27:53 | [diff] [blame] | 21 | #include "base/timer/timer.h" |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 22 | #include "chrome/browser/browser_process.h" |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 23 | #include "chrome/browser/component_updater/component_patcher.h" |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 24 | #include "chrome/browser/component_updater/component_unpacker.h" |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 25 | #include "chrome/browser/component_updater/component_updater_ping_manager.h" |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 26 | #include "chrome/browser/component_updater/component_updater_utils.h" |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 27 | #include "chrome/browser/component_updater/crx_downloader.h" |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 28 | #include "chrome/browser/component_updater/crx_update_item.h" |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 29 | #include "chrome/browser/component_updater/update_checker.h" |
[email protected] | 6268d3a | 2013-11-27 01:28:09 | [diff] [blame] | 30 | #include "chrome/browser/component_updater/update_response.h" |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 31 | #include "chrome/common/chrome_version_info.h" |
[email protected] | b7b6387 | 2013-01-03 02:41:19 | [diff] [blame] | 32 | #include "content/public/browser/browser_thread.h" |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 33 | #include "content/public/browser/resource_controller.h" |
| 34 | #include "content/public/browser/resource_throttle.h" |
[email protected] | 761fa470 | 2013-07-02 15:25:15 | [diff] [blame] | 35 | #include "url/gurl.h" |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 36 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 37 | using content::BrowserThread; |
| 38 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame^] | 39 | namespace component_updater { |
[email protected] | 3a0092d | 2013-12-18 03:04:35 | [diff] [blame] | 40 | |
[email protected] | 44da56e | 2011-11-21 19:59:14 | [diff] [blame] | 41 | // The component updater is designed to live until process shutdown, so |
| 42 | // base::Bind() calls are not refcounted. |
| 43 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 44 | namespace { |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 45 | |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 46 | // Returns true if the |proposed| version is newer than |current| version. |
[email protected] | c5e4a222 | 2014-01-03 16:06:13 | [diff] [blame] | 47 | bool IsVersionNewer(const Version& current, const std::string& proposed) { |
| 48 | Version proposed_ver(proposed); |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 49 | return proposed_ver.IsValid() && current.CompareTo(proposed_ver) < 0; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 50 | } |
| 51 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 52 | // Returns true if a differential update is available, it has not failed yet, |
| 53 | // and the configuration allows it. |
| 54 | bool CanTryDiffUpdate(const CrxUpdateItem* update_item, |
| 55 | const ComponentUpdateService::Configurator& config) { |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame^] | 56 | return HasDiffUpdate(update_item) && |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 57 | !update_item->diff_update_failed && |
| 58 | config.DeltasEnabled(); |
| 59 | } |
| 60 | |
[email protected] | 3a0092d | 2013-12-18 03:04:35 | [diff] [blame] | 61 | void AppendDownloadMetrics( |
| 62 | const std::vector<CrxDownloader::DownloadMetrics>& source, |
| 63 | std::vector<CrxDownloader::DownloadMetrics>* destination) { |
| 64 | destination->insert(destination->end(), source.begin(), source.end()); |
| 65 | } |
| 66 | |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 67 | } // namespace |
| 68 | |
| 69 | CrxUpdateItem::CrxUpdateItem() |
| 70 | : status(kNew), |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 71 | on_demand(false), |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 72 | diff_update_failed(false), |
| 73 | error_category(0), |
| 74 | error_code(0), |
| 75 | extra_code1(0), |
| 76 | diff_error_category(0), |
| 77 | diff_error_code(0), |
| 78 | diff_extra_code1(0) { |
| 79 | } |
| 80 | |
| 81 | CrxUpdateItem::~CrxUpdateItem() { |
| 82 | } |
[email protected] | 86550a4 | 2013-06-21 15:20:49 | [diff] [blame] | 83 | |
[email protected] | dc06f0b | 2013-01-23 20:03:16 | [diff] [blame] | 84 | CrxComponent::CrxComponent() |
[email protected] | 85e61d5 | 2013-08-01 22:23:42 | [diff] [blame] | 85 | : installer(NULL), |
| 86 | observer(NULL) { |
[email protected] | dc06f0b | 2013-01-23 20:03:16 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | CrxComponent::~CrxComponent() { |
| 90 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 91 | |
[email protected] | 2e919ddd | 2013-08-21 05:05:17 | [diff] [blame] | 92 | CrxComponentInfo::CrxComponentInfo() { |
| 93 | } |
| 94 | |
| 95 | CrxComponentInfo::~CrxComponentInfo() { |
| 96 | } |
| 97 | |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 98 | /////////////////////////////////////////////////////////////////////////////// |
| 99 | // In charge of blocking url requests until the |crx_id| component has been |
| 100 | // updated. This class is touched solely from the IO thread. The UI thread |
| 101 | // can post tasks to it via weak pointers. By default the request is blocked |
| 102 | // unless the CrxUpdateService calls Unblock(). |
| 103 | // The lifetime is controlled by Chrome's resource loader so the component |
| 104 | // updater cannot touch objects from this class except via weak pointers. |
| 105 | class CUResourceThrottle |
| 106 | : public content::ResourceThrottle, |
| 107 | public base::SupportsWeakPtr<CUResourceThrottle> { |
| 108 | public: |
| 109 | explicit CUResourceThrottle(const net::URLRequest* request); |
| 110 | virtual ~CUResourceThrottle(); |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 111 | |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 112 | // Overriden from ResourceThrottle. |
| 113 | virtual void WillStartRequest(bool* defer) OVERRIDE; |
| 114 | virtual void WillRedirectRequest(const GURL& new_url, bool* defer) OVERRIDE; |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 115 | virtual const char* GetNameForLogging() const OVERRIDE; |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 116 | |
| 117 | // Component updater calls this function via PostTask to unblock the request. |
| 118 | void Unblock(); |
| 119 | |
| 120 | typedef std::vector<base::WeakPtr<CUResourceThrottle> > WeakPtrVector; |
| 121 | |
| 122 | private: |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 123 | enum State { |
| 124 | NEW, |
| 125 | BLOCKED, |
| 126 | UNBLOCKED |
| 127 | }; |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 128 | |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 129 | State state_; |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | void UnblockResourceThrottle(base::WeakPtr<CUResourceThrottle> rt) { |
| 133 | BrowserThread::PostTask( |
| 134 | BrowserThread::IO, |
| 135 | FROM_HERE, |
| 136 | base::Bind(&CUResourceThrottle::Unblock, rt)); |
| 137 | } |
| 138 | |
| 139 | void UnblockandReapAllThrottles(CUResourceThrottle::WeakPtrVector* throttles) { |
| 140 | CUResourceThrottle::WeakPtrVector::iterator it; |
| 141 | for (it = throttles->begin(); it != throttles->end(); ++it) |
| 142 | UnblockResourceThrottle(*it); |
| 143 | throttles->clear(); |
| 144 | } |
| 145 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 146 | ////////////////////////////////////////////////////////////////////////////// |
| 147 | // The one and only implementation of the ComponentUpdateService interface. In |
| 148 | // charge of running the show. The main method is ProcessPendingItems() which |
[email protected] | 50b0139 | 2012-09-01 03:33:13 | [diff] [blame] | 149 | // is called periodically to do the upgrades/installs or the update checks. |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 150 | // An important consideration here is to be as "low impact" as we can to the |
| 151 | // rest of the browser, so even if we have many components registered and |
[email protected] | f105043 | 2012-02-15 01:35:46 | [diff] [blame] | 152 | // eligible for update, we only do one thing at a time with pauses in between |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 153 | // the tasks. Also when we do network requests there is only one |url_fetcher_| |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 154 | // in flight at a time. |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 155 | // There are no locks in this code, the main structure |work_items_| is mutated |
| 156 | // only from the UI thread. The unpack and installation is done in the file |
| 157 | // thread and the network requests are done in the IO thread and in the file |
| 158 | // thread. |
| 159 | class CrxUpdateService : public ComponentUpdateService { |
| 160 | public: |
| 161 | explicit CrxUpdateService(ComponentUpdateService::Configurator* config); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 162 | virtual ~CrxUpdateService(); |
| 163 | |
| 164 | // Overrides for ComponentUpdateService. |
| 165 | virtual Status Start() OVERRIDE; |
| 166 | virtual Status Stop() OVERRIDE; |
| 167 | virtual Status RegisterComponent(const CrxComponent& component) OVERRIDE; |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 168 | virtual Status OnDemandUpdate(const std::string& component_id) OVERRIDE; |
[email protected] | 2e919ddd | 2013-08-21 05:05:17 | [diff] [blame] | 169 | virtual void GetComponents( |
| 170 | std::vector<CrxComponentInfo>* components) OVERRIDE; |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 171 | virtual content::ResourceThrottle* GetOnDemandResourceThrottle( |
| 172 | net::URLRequest* request, const std::string& crx_id) OVERRIDE; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 173 | |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 174 | // Context for a crx download url request. |
| 175 | struct CRXContext { |
| 176 | ComponentInstaller* installer; |
| 177 | std::vector<uint8> pk_hash; |
| 178 | std::string id; |
| 179 | std::string fingerprint; |
| 180 | CRXContext() : installer(NULL) {} |
| 181 | }; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 182 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 183 | private: |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 184 | enum ErrorCategory { |
| 185 | kErrorNone = 0, |
| 186 | kNetworkError, |
| 187 | kUnpackError, |
| 188 | kInstallError, |
| 189 | }; |
| 190 | |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 191 | enum StepDelayInterval { |
| 192 | kStepDelayShort = 0, |
| 193 | kStepDelayMedium, |
| 194 | kStepDelayLong, |
| 195 | }; |
| 196 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame^] | 197 | void UpdateCheckComplete(int error, |
| 198 | const std::string& error_message, |
| 199 | const UpdateResponse::Results& results); |
| 200 | void OnUpdateCheckSucceeded(const UpdateResponse::Results& results); |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 201 | void OnUpdateCheckFailed(int error, const std::string& error_message); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 202 | |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 203 | void DownloadComplete( |
| 204 | scoped_ptr<CRXContext> crx_context, |
[email protected] | 3a0092d | 2013-12-18 03:04:35 | [diff] [blame] | 205 | const CrxDownloader::Result& download_result); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 206 | |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 207 | Status OnDemandUpdateInternal(CrxUpdateItem* item); |
| 208 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 209 | void ProcessPendingItems(); |
| 210 | |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 211 | // Find a component that is ready to update. |
| 212 | CrxUpdateItem* FindReadyComponent() const; |
| 213 | |
| 214 | // Prepares the components for an update check and initiates the request. |
| 215 | // Returns true if an update check request has been made. Returns false if |
| 216 | // no update check was needed or an error occured. |
| 217 | bool CheckForUpdates(); |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 218 | |
| 219 | void UpdateComponent(CrxUpdateItem* workitem); |
| 220 | |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 221 | void ScheduleNextRun(StepDelayInterval step_delay); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 222 | |
[email protected] | 6268d3a | 2013-11-27 01:28:09 | [diff] [blame] | 223 | void ParseResponse(const std::string& xml); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 224 | |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 225 | void Install(scoped_ptr<CRXContext> context, const base::FilePath& crx_path); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 226 | |
[email protected] | 07f93af1 | 2011-08-17 20:57:22 | [diff] [blame] | 227 | void DoneInstalling(const std::string& component_id, |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 228 | ComponentUnpacker::Error error, |
| 229 | int extended_error); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 230 | |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 231 | void ChangeItemState(CrxUpdateItem* item, CrxUpdateItem::Status to); |
| 232 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 233 | size_t ChangeItemStatus(CrxUpdateItem::Status from, |
| 234 | CrxUpdateItem::Status to); |
| 235 | |
| 236 | CrxUpdateItem* FindUpdateItemById(const std::string& id); |
| 237 | |
[email protected] | 85e61d5 | 2013-08-01 22:23:42 | [diff] [blame] | 238 | void NotifyComponentObservers(ComponentObserver::Events event, |
| 239 | int extra) const; |
| 240 | |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 241 | bool HasOnDemandItems() const; |
| 242 | |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 243 | void OnNewResourceThrottle(base::WeakPtr<CUResourceThrottle> rt, |
| 244 | const std::string& crx_id); |
| 245 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 246 | scoped_ptr<ComponentUpdateService::Configurator> config_; |
| 247 | |
| 248 | scoped_ptr<ComponentPatcher> component_patcher_; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 249 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame^] | 250 | scoped_ptr<UpdateChecker> update_checker_; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 251 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame^] | 252 | scoped_ptr<PingManager> ping_manager_; |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 253 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame^] | 254 | scoped_ptr<CrxDownloader> crx_downloader_; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 255 | |
[email protected] | 86550a4 | 2013-06-21 15:20:49 | [diff] [blame] | 256 | // A collection of every work item. |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 257 | typedef std::vector<CrxUpdateItem*> UpdateItems; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 258 | UpdateItems work_items_; |
| 259 | |
| 260 | base::OneShotTimer<CrxUpdateService> timer_; |
| 261 | |
[email protected] | 8f5f2ea | 2013-10-31 09:39:10 | [diff] [blame] | 262 | scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
| 263 | |
[email protected] | c5e4a222 | 2014-01-03 16:06:13 | [diff] [blame] | 264 | const Version chrome_version_; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 265 | |
| 266 | bool running_; |
| 267 | |
| 268 | DISALLOW_COPY_AND_ASSIGN(CrxUpdateService); |
| 269 | }; |
| 270 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 271 | ////////////////////////////////////////////////////////////////////////////// |
| 272 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 273 | CrxUpdateService::CrxUpdateService(ComponentUpdateService::Configurator* config) |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 274 | : config_(config), |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 275 | component_patcher_(config->CreateComponentPatcher()), |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame^] | 276 | ping_manager_(new PingManager(config->PingUrl(), |
| 277 | config->RequestContext())), |
[email protected] | 8f5f2ea | 2013-10-31 09:39:10 | [diff] [blame] | 278 | blocking_task_runner_(BrowserThread::GetBlockingPool()-> |
| 279 | GetSequencedTaskRunnerWithShutdownBehavior( |
| 280 | BrowserThread::GetBlockingPool()->GetSequenceToken(), |
| 281 | base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 282 | chrome_version_(chrome::VersionInfo().Version()), |
| 283 | running_(false) { |
[email protected] | 1ea21ad | 2013-09-02 04:20:39 | [diff] [blame] | 284 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 285 | |
| 286 | CrxUpdateService::~CrxUpdateService() { |
| 287 | // Because we are a singleton, at this point only the UI thread should be |
| 288 | // alive, this simplifies the management of the work that could be in |
| 289 | // flight in other threads. |
| 290 | Stop(); |
| 291 | STLDeleteElements(&work_items_); |
[email protected] | 1ea21ad | 2013-09-02 04:20:39 | [diff] [blame] | 292 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 293 | |
| 294 | ComponentUpdateService::Status CrxUpdateService::Start() { |
| 295 | // Note that RegisterComponent will call Start() when the first |
| 296 | // component is registered, so it can be called twice. This way |
| 297 | // we avoid scheduling the timer if there is no work to do. |
| 298 | running_ = true; |
| 299 | if (work_items_.empty()) |
| 300 | return kOk; |
| 301 | |
[email protected] | 85e61d5 | 2013-08-01 22:23:42 | [diff] [blame] | 302 | NotifyComponentObservers(ComponentObserver::COMPONENT_UPDATER_STARTED, 0); |
| 303 | |
[email protected] | d323a17 | 2011-09-02 18:23:02 | [diff] [blame] | 304 | timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(config_->InitialDelay()), |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 305 | this, &CrxUpdateService::ProcessPendingItems); |
| 306 | return kOk; |
| 307 | } |
| 308 | |
| 309 | // Stop the main check + update loop. In flight operations will be |
| 310 | // completed. |
| 311 | ComponentUpdateService::Status CrxUpdateService::Stop() { |
| 312 | running_ = false; |
| 313 | timer_.Stop(); |
| 314 | return kOk; |
| 315 | } |
| 316 | |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 317 | bool CrxUpdateService::HasOnDemandItems() const { |
| 318 | class Helper { |
| 319 | public: |
| 320 | static bool IsOnDemand(CrxUpdateItem* item) { |
| 321 | return item->on_demand; |
| 322 | } |
| 323 | }; |
| 324 | return std::find_if(work_items_.begin(), |
| 325 | work_items_.end(), |
| 326 | Helper::IsOnDemand) != work_items_.end(); |
| 327 | } |
| 328 | |
[email protected] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 329 | // This function sets the timer which will call ProcessPendingItems() or |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 330 | // ProcessRequestedItem() if there is an on_demand item. There |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 331 | // are three kinds of waits: |
| 332 | // - a short delay, when there is immediate work to be done. |
| 333 | // - a medium delay, when there are updates to be applied within the current |
| 334 | // update cycle, or there are components that are still unchecked. |
| 335 | // - a long delay when a full check/update cycle has completed for all |
| 336 | // components. |
| 337 | void CrxUpdateService::ScheduleNextRun(StepDelayInterval step_delay) { |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 338 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 339 | DCHECK(!update_checker_); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 340 | CHECK(!timer_.IsRunning()); |
| 341 | // It could be the case that Stop() had been called while a url request |
| 342 | // or unpacking was in flight, if so we arrive here but |running_| is |
| 343 | // false. In that case do not loop again. |
| 344 | if (!running_) |
| 345 | return; |
| 346 | |
[email protected] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 347 | // Keep the delay short if in the middle of an update (step_delay), |
| 348 | // or there are new requested_work_items_ that have not been processed yet. |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 349 | int64 delay_seconds = 0; |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 350 | if (!HasOnDemandItems()) { |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 351 | switch (step_delay) { |
| 352 | case kStepDelayShort: |
| 353 | delay_seconds = config_->StepDelay(); |
| 354 | break; |
| 355 | case kStepDelayMedium: |
| 356 | delay_seconds = config_->StepDelayMedium(); |
| 357 | break; |
| 358 | case kStepDelayLong: |
| 359 | delay_seconds = config_->NextCheckDelay(); |
| 360 | break; |
| 361 | } |
| 362 | } else { |
| 363 | delay_seconds = config_->StepDelay(); |
| 364 | } |
[email protected] | cf44261 | 2011-08-09 20:20:12 | [diff] [blame] | 365 | |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 366 | if (step_delay != kStepDelayShort) { |
[email protected] | 85e61d5 | 2013-08-01 22:23:42 | [diff] [blame] | 367 | NotifyComponentObservers(ComponentObserver::COMPONENT_UPDATER_SLEEPING, 0); |
| 368 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 369 | // Zero is only used for unit tests. |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 370 | if (0 == delay_seconds) |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 371 | return; |
| 372 | } |
| 373 | |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 374 | timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(delay_seconds), |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 375 | this, &CrxUpdateService::ProcessPendingItems); |
| 376 | } |
| 377 | |
| 378 | // Given a extension-like component id, find the associated component. |
| 379 | CrxUpdateItem* CrxUpdateService::FindUpdateItemById(const std::string& id) { |
| 380 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 381 | CrxUpdateItem::FindById finder(id); |
| 382 | UpdateItems::iterator it = std::find_if(work_items_.begin(), |
| 383 | work_items_.end(), |
| 384 | finder); |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 385 | return it != work_items_.end() ? *it : NULL; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 386 | } |
| 387 | |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 388 | // Changes a component's status, clearing on_demand and firing notifications as |
| 389 | // necessary. By convention, this is the only function that can change a |
| 390 | // CrxUpdateItem's |status|. |
| 391 | // TODO(waffles): Do we want to add DCHECKS for valid state transitions here? |
| 392 | void CrxUpdateService::ChangeItemState(CrxUpdateItem* item, |
| 393 | CrxUpdateItem::Status to) { |
| 394 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 395 | if (to == CrxUpdateItem::kNoUpdate || |
| 396 | to == CrxUpdateItem::kUpdated || |
| 397 | to == CrxUpdateItem::kUpToDate) { |
| 398 | item->on_demand = false; |
| 399 | } |
| 400 | |
| 401 | item->status = to; |
| 402 | |
| 403 | ComponentObserver* observer = item->component.observer; |
| 404 | if (observer) { |
| 405 | switch (to) { |
| 406 | case CrxUpdateItem::kCanUpdate: |
| 407 | observer->OnEvent(ComponentObserver::COMPONENT_UPDATE_FOUND, 0); |
| 408 | break; |
| 409 | case CrxUpdateItem::kUpdatingDiff: |
| 410 | case CrxUpdateItem::kUpdating: |
| 411 | observer->OnEvent(ComponentObserver::COMPONENT_UPDATE_READY, 0); |
| 412 | break; |
| 413 | case CrxUpdateItem::kUpdated: |
| 414 | observer->OnEvent(ComponentObserver::COMPONENT_UPDATED, 0); |
| 415 | break; |
| 416 | case CrxUpdateItem::kUpToDate: |
| 417 | case CrxUpdateItem::kNoUpdate: |
| 418 | observer->OnEvent(ComponentObserver::COMPONENT_NOT_UPDATED, 0); |
| 419 | break; |
| 420 | case CrxUpdateItem::kNew: |
| 421 | case CrxUpdateItem::kChecking: |
| 422 | case CrxUpdateItem::kDownloading: |
| 423 | case CrxUpdateItem::kDownloadingDiff: |
| 424 | case CrxUpdateItem::kLastStatus: |
| 425 | // No notification for these states. |
| 426 | break; |
| 427 | } |
| 428 | } |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 429 | |
| 430 | // Free possible pending network requests. |
| 431 | if ((to == CrxUpdateItem::kUpdated) || |
| 432 | (to == CrxUpdateItem::kUpToDate) || |
| 433 | (to == CrxUpdateItem::kNoUpdate)) { |
| 434 | UnblockandReapAllThrottles(&item->throttles); |
| 435 | } |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 436 | } |
| 437 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 438 | // Changes all the components in |work_items_| that have |from| status to |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 439 | // |to| status and returns how many have been changed. |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 440 | size_t CrxUpdateService::ChangeItemStatus(CrxUpdateItem::Status from, |
| 441 | CrxUpdateItem::Status to) { |
| 442 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 443 | size_t count = 0; |
| 444 | for (UpdateItems::iterator it = work_items_.begin(); |
| 445 | it != work_items_.end(); ++it) { |
| 446 | CrxUpdateItem* item = *it; |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 447 | if (item->status == from) { |
| 448 | ChangeItemState(item, to); |
| 449 | ++count; |
| 450 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 451 | } |
| 452 | return count; |
| 453 | } |
| 454 | |
| 455 | // Adds a component to be checked for upgrades. If the component exists it |
| 456 | // it will be replaced and the return code is kReplaced. |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 457 | ComponentUpdateService::Status CrxUpdateService::RegisterComponent( |
| 458 | const CrxComponent& component) { |
| 459 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 460 | if (component.pk_hash.empty() || |
| 461 | !component.version.IsValid() || |
| 462 | !component.installer) |
| 463 | return kError; |
| 464 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame^] | 465 | std::string id(GetCrxComponentID(component)); |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 466 | CrxUpdateItem* uit = FindUpdateItemById(id); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 467 | if (uit) { |
| 468 | uit->component = component; |
| 469 | return kReplaced; |
| 470 | } |
| 471 | |
| 472 | uit = new CrxUpdateItem; |
| 473 | uit->id.swap(id); |
| 474 | uit->component = component; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 475 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 476 | work_items_.push_back(uit); |
| 477 | // If this is the first component registered we call Start to |
| 478 | // schedule the first timer. |
| 479 | if (running_ && (work_items_.size() == 1)) |
| 480 | Start(); |
| 481 | |
| 482 | return kOk; |
| 483 | } |
| 484 | |
[email protected] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 485 | // Start the process of checking for an update, for a particular component |
| 486 | // that was previously registered. |
[email protected] | 2e919ddd | 2013-08-21 05:05:17 | [diff] [blame] | 487 | // |component_id| is a value returned from GetCrxComponentID(). |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 488 | ComponentUpdateService::Status CrxUpdateService::OnDemandUpdate( |
[email protected] | 2e919ddd | 2013-08-21 05:05:17 | [diff] [blame] | 489 | const std::string& component_id) { |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 490 | return OnDemandUpdateInternal(FindUpdateItemById(component_id)); |
| 491 | } |
| 492 | |
| 493 | ComponentUpdateService::Status CrxUpdateService::OnDemandUpdateInternal( |
| 494 | CrxUpdateItem* uit) { |
[email protected] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 495 | if (!uit) |
| 496 | return kError; |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 497 | |
[email protected] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 498 | // Check if the request is too soon. |
| 499 | base::TimeDelta delta = base::Time::Now() - uit->last_check; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 500 | if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay())) |
[email protected] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 501 | return kError; |
[email protected] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 502 | |
| 503 | switch (uit->status) { |
| 504 | // If the item is already in the process of being updated, there is |
| 505 | // no point in this call, so return kInProgress. |
| 506 | case CrxUpdateItem::kChecking: |
| 507 | case CrxUpdateItem::kCanUpdate: |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 508 | case CrxUpdateItem::kDownloadingDiff: |
[email protected] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 509 | case CrxUpdateItem::kDownloading: |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 510 | case CrxUpdateItem::kUpdatingDiff: |
[email protected] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 511 | case CrxUpdateItem::kUpdating: |
| 512 | return kInProgress; |
| 513 | // Otherwise the item was already checked a while back (or it is new), |
| 514 | // set its status to kNew to give it a slightly higher priority. |
| 515 | case CrxUpdateItem::kNew: |
| 516 | case CrxUpdateItem::kUpdated: |
| 517 | case CrxUpdateItem::kUpToDate: |
| 518 | case CrxUpdateItem::kNoUpdate: |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 519 | ChangeItemState(uit, CrxUpdateItem::kNew); |
| 520 | uit->on_demand = true; |
[email protected] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 521 | break; |
| 522 | case CrxUpdateItem::kLastStatus: |
| 523 | NOTREACHED() << uit->status; |
| 524 | } |
| 525 | |
| 526 | // In case the current delay is long, set the timer to a shorter value |
| 527 | // to get the ball rolling. |
| 528 | if (timer_.IsRunning()) { |
| 529 | timer_.Stop(); |
| 530 | timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(config_->StepDelay()), |
| 531 | this, &CrxUpdateService::ProcessPendingItems); |
| 532 | } |
| 533 | |
| 534 | return kOk; |
| 535 | } |
| 536 | |
[email protected] | 2e919ddd | 2013-08-21 05:05:17 | [diff] [blame] | 537 | void CrxUpdateService::GetComponents( |
| 538 | std::vector<CrxComponentInfo>* components) { |
| 539 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 540 | for (UpdateItems::const_iterator it = work_items_.begin(); |
| 541 | it != work_items_.end(); ++it) { |
| 542 | const CrxUpdateItem* item = *it; |
| 543 | CrxComponentInfo info; |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame^] | 544 | info.id = GetCrxComponentID(item->component); |
[email protected] | 2e919ddd | 2013-08-21 05:05:17 | [diff] [blame] | 545 | info.version = item->component.version.GetString(); |
| 546 | info.name = item->component.name; |
| 547 | components->push_back(info); |
| 548 | } |
| 549 | } |
| 550 | |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 551 | // This is the main loop of the component updater. It updates one component |
| 552 | // at a time if updates are available. Otherwise, it does an update check or |
| 553 | // takes a long sleep until the loop runs again. |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 554 | void CrxUpdateService::ProcessPendingItems() { |
| 555 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 556 | |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 557 | CrxUpdateItem* ready_upgrade = FindReadyComponent(); |
| 558 | if (ready_upgrade) { |
| 559 | UpdateComponent(ready_upgrade); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 560 | return; |
| 561 | } |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 562 | |
| 563 | if (!CheckForUpdates()) |
| 564 | ScheduleNextRun(kStepDelayLong); |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 565 | } |
| 566 | |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 567 | CrxUpdateItem* CrxUpdateService::FindReadyComponent() const { |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 568 | class Helper { |
| 569 | public: |
| 570 | static bool IsReadyOnDemand(CrxUpdateItem* item) { |
| 571 | return item->on_demand && IsReady(item); |
| 572 | } |
| 573 | static bool IsReady(CrxUpdateItem* item) { |
| 574 | return item->status == CrxUpdateItem::kCanUpdate; |
| 575 | } |
| 576 | }; |
| 577 | |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 578 | std::vector<CrxUpdateItem*>::const_iterator it = std::find_if( |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 579 | work_items_.begin(), work_items_.end(), Helper::IsReadyOnDemand); |
| 580 | if (it != work_items_.end()) |
| 581 | return *it; |
| 582 | it = std::find_if(work_items_.begin(), work_items_.end(), Helper::IsReady); |
| 583 | if (it != work_items_.end()) |
| 584 | return *it; |
| 585 | return NULL; |
| 586 | } |
| 587 | |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 588 | // Prepares the components for an update check and initiates the request. |
| 589 | bool CrxUpdateService::CheckForUpdates() { |
| 590 | // All components are selected for the update check, regardless of when they |
| 591 | // were last checked. More selective algorithms could be implemented in the |
| 592 | // future. |
| 593 | std::vector<CrxUpdateItem*> items_to_check; |
| 594 | for (size_t i = 0; i != work_items_.size(); ++i) { |
| 595 | CrxUpdateItem* item = work_items_[i]; |
| 596 | DCHECK(item->status == CrxUpdateItem::kNew || |
| 597 | item->status == CrxUpdateItem::kNoUpdate || |
| 598 | item->status == CrxUpdateItem::kUpToDate || |
| 599 | item->status == CrxUpdateItem::kUpdated); |
| 600 | |
| 601 | ChangeItemState(item, CrxUpdateItem::kChecking); |
| 602 | |
| 603 | item->last_check = base::Time::Now(); |
| 604 | item->crx_urls.clear(); |
| 605 | item->crx_diffurls.clear(); |
| 606 | item->previous_version = item->component.version; |
| 607 | item->next_version = Version(); |
| 608 | item->previous_fp = item->component.fingerprint; |
| 609 | item->next_fp.clear(); |
| 610 | item->diff_update_failed = false; |
| 611 | item->error_category = 0; |
| 612 | item->error_code = 0; |
| 613 | item->extra_code1 = 0; |
| 614 | item->diff_error_category = 0; |
| 615 | item->diff_error_code = 0; |
| 616 | item->diff_extra_code1 = 0; |
| 617 | item->download_metrics.clear(); |
| 618 | |
| 619 | items_to_check.push_back(item); |
| 620 | } |
| 621 | |
| 622 | if (items_to_check.empty()) |
| 623 | return false; |
| 624 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame^] | 625 | update_checker_ = UpdateChecker::Create( |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 626 | config_->UpdateUrl(), |
| 627 | config_->RequestContext(), |
| 628 | base::Bind(&CrxUpdateService::UpdateCheckComplete, |
| 629 | base::Unretained(this))).Pass(); |
| 630 | return update_checker_->CheckForUpdates(items_to_check, |
| 631 | config_->ExtraRequestParams()); |
| 632 | } |
| 633 | |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 634 | void CrxUpdateService::UpdateComponent(CrxUpdateItem* workitem) { |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 635 | scoped_ptr<CRXContext> crx_context(new CRXContext); |
| 636 | crx_context->pk_hash = workitem->component.pk_hash; |
| 637 | crx_context->id = workitem->id; |
| 638 | crx_context->installer = workitem->component.installer; |
| 639 | crx_context->fingerprint = workitem->next_fp; |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 640 | const std::vector<GURL>* urls = NULL; |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 641 | if (CanTryDiffUpdate(workitem, *config_)) { |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 642 | urls = &workitem->crx_diffurls; |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 643 | ChangeItemState(workitem, CrxUpdateItem::kDownloadingDiff); |
| 644 | } else { |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 645 | urls = &workitem->crx_urls; |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 646 | ChangeItemState(workitem, CrxUpdateItem::kDownloading); |
| 647 | } |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 648 | |
| 649 | // On demand component updates are always downloaded in foreground. |
| 650 | const bool is_background_download = !workitem->on_demand && |
| 651 | config_->UseBackgroundDownloader(); |
| 652 | |
[email protected] | 3a0092d | 2013-12-18 03:04:35 | [diff] [blame] | 653 | crx_downloader_.reset(CrxDownloader::Create( |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 654 | is_background_download, |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 655 | config_->RequestContext(), |
| 656 | blocking_task_runner_, |
| 657 | base::Bind(&CrxUpdateService::DownloadComplete, |
| 658 | base::Unretained(this), |
| 659 | base::Passed(&crx_context)))); |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 660 | crx_downloader_->StartDownload(*urls); |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 661 | } |
| 662 | |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 663 | void CrxUpdateService::UpdateCheckComplete( |
| 664 | int error, |
| 665 | const std::string& error_message, |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame^] | 666 | const UpdateResponse::Results& results) { |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 667 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 668 | update_checker_.reset(); |
| 669 | if (!error) |
| 670 | OnUpdateCheckSucceeded(results); |
[email protected] | 652f427 | 2013-11-13 09:23:41 | [diff] [blame] | 671 | else |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 672 | OnUpdateCheckFailed(error, error_message); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 673 | } |
| 674 | |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 675 | // Handles a valid Omaha update check response by matching the results with |
| 676 | // the registered components which were checked for updates. |
| 677 | // If updates are found, prepare the components for the actual version upgrade. |
| 678 | // One of these components will be drafted for the upgrade next time |
| 679 | // ProcessPendingItems is called. |
| 680 | void CrxUpdateService::OnUpdateCheckSucceeded( |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame^] | 681 | const UpdateResponse::Results& results) { |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 682 | size_t num_updates_pending = 0; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 683 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame^] | 684 | std::vector<UpdateResponse::Result>::const_iterator it; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 685 | for (it = results.list.begin(); it != results.list.end(); ++it) { |
| 686 | CrxUpdateItem* crx = FindUpdateItemById(it->extension_id); |
| 687 | if (!crx) |
| 688 | continue; |
| 689 | |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 690 | if (crx->status != CrxUpdateItem::kChecking) { |
| 691 | NOTREACHED(); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 692 | continue; // Not updating this component now. |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 693 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 694 | |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 695 | if (it->manifest.version.empty()) { |
[email protected] | cf44261 | 2011-08-09 20:20:12 | [diff] [blame] | 696 | // No version means no update available. |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 697 | ChangeItemState(crx, CrxUpdateItem::kNoUpdate); |
[email protected] | cf44261 | 2011-08-09 20:20:12 | [diff] [blame] | 698 | continue; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 699 | } |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 700 | |
| 701 | if (!IsVersionNewer(crx->component.version, it->manifest.version)) { |
| 702 | // The component is up to date. |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 703 | ChangeItemState(crx, CrxUpdateItem::kUpToDate); |
[email protected] | cf44261 | 2011-08-09 20:20:12 | [diff] [blame] | 704 | continue; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 705 | } |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 706 | |
| 707 | if (!it->manifest.browser_min_version.empty()) { |
| 708 | if (IsVersionNewer(chrome_version_, it->manifest.browser_min_version)) { |
| 709 | // The component is not compatible with this Chrome version. |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 710 | ChangeItemState(crx, CrxUpdateItem::kNoUpdate); |
[email protected] | cf44261 | 2011-08-09 20:20:12 | [diff] [blame] | 711 | continue; |
| 712 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 713 | } |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 714 | |
| 715 | if (it->manifest.packages.size() != 1) { |
| 716 | // Assume one and only one package per component. |
| 717 | ChangeItemState(crx, CrxUpdateItem::kNoUpdate); |
| 718 | continue; |
| 719 | } |
| 720 | |
| 721 | // Parse the members of the result and queue an upgrade for this component. |
[email protected] | c5e4a222 | 2014-01-03 16:06:13 | [diff] [blame] | 722 | crx->next_version = Version(it->manifest.version); |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 723 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame^] | 724 | typedef UpdateResponse::Result::Manifest::Package Package; |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 725 | const Package& package(it->manifest.packages[0]); |
| 726 | crx->next_fp = package.fingerprint; |
| 727 | |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 728 | // Resolve the urls by combining the base urls with the package names. |
| 729 | for (size_t i = 0; i != it->crx_urls.size(); ++i) { |
| 730 | const GURL url(it->crx_urls[i].Resolve(package.name)); |
| 731 | if (url.is_valid()) |
| 732 | crx->crx_urls.push_back(url); |
| 733 | } |
| 734 | for (size_t i = 0; i != it->crx_diffurls.size(); ++i) { |
| 735 | const GURL url(it->crx_diffurls[i].Resolve(package.namediff)); |
| 736 | if (url.is_valid()) |
| 737 | crx->crx_diffurls.push_back(url); |
| 738 | } |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 739 | |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 740 | ChangeItemState(crx, CrxUpdateItem::kCanUpdate); |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 741 | ++num_updates_pending; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 742 | } |
[email protected] | cf44261 | 2011-08-09 20:20:12 | [diff] [blame] | 743 | |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 744 | // All components that are not included in the update response are |
| 745 | // considered up to date. |
[email protected] | cf44261 | 2011-08-09 20:20:12 | [diff] [blame] | 746 | ChangeItemStatus(CrxUpdateItem::kChecking, CrxUpdateItem::kUpToDate); |
| 747 | |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 748 | // If there are updates pending we do a short wait, otherwise we take |
| 749 | // a longer delay until we check the components again. |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 750 | ScheduleNextRun(num_updates_pending > 0 ? kStepDelayShort : kStepDelayMedium); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 751 | } |
| 752 | |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 753 | // TODO: record UMA stats. |
| 754 | void CrxUpdateService::OnUpdateCheckFailed(int error, |
| 755 | const std::string& error_message) { |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 756 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 757 | DCHECK(error); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 758 | size_t count = ChangeItemStatus(CrxUpdateItem::kChecking, |
| 759 | CrxUpdateItem::kNoUpdate); |
| 760 | DCHECK_GT(count, 0ul); |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 761 | ScheduleNextRun(kStepDelayLong); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | // Called when the CRX package has been downloaded to a temporary location. |
| 765 | // Here we fire the notifications and schedule the component-specific installer |
| 766 | // to be called in the file thread. |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 767 | void CrxUpdateService::DownloadComplete( |
| 768 | scoped_ptr<CRXContext> crx_context, |
[email protected] | 3a0092d | 2013-12-18 03:04:35 | [diff] [blame] | 769 | const CrxDownloader::Result& download_result) { |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 770 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 64f39fa1 | 2013-09-03 21:49:37 | [diff] [blame] | 771 | |
[email protected] | 64f39fa1 | 2013-09-03 21:49:37 | [diff] [blame] | 772 | CrxUpdateItem* crx = FindUpdateItemById(crx_context->id); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 773 | DCHECK(crx->status == CrxUpdateItem::kDownloadingDiff || |
| 774 | crx->status == CrxUpdateItem::kDownloading); |
| 775 | |
[email protected] | 3a0092d | 2013-12-18 03:04:35 | [diff] [blame] | 776 | AppendDownloadMetrics(crx_downloader_->download_metrics(), |
| 777 | &crx->download_metrics); |
| 778 | |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 779 | if (download_result.error) { |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 780 | if (crx->status == CrxUpdateItem::kDownloadingDiff) { |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 781 | crx->diff_error_category = kNetworkError; |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 782 | crx->diff_error_code = download_result.error; |
[email protected] | e630ba6 | 2013-06-22 15:22:34 | [diff] [blame] | 783 | crx->diff_update_failed = true; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 784 | size_t count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff, |
| 785 | CrxUpdateItem::kCanUpdate); |
| 786 | DCHECK_EQ(count, 1ul); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 787 | crx_downloader_.reset(); |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 788 | |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 789 | ScheduleNextRun(kStepDelayShort); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 790 | return; |
| 791 | } |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 792 | crx->error_category = kNetworkError; |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 793 | crx->error_code = download_result.error; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 794 | size_t count = ChangeItemStatus(CrxUpdateItem::kDownloading, |
| 795 | CrxUpdateItem::kNoUpdate); |
| 796 | DCHECK_EQ(count, 1ul); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 797 | crx_downloader_.reset(); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 798 | |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 799 | // At this point, since both the differential and the full downloads failed, |
| 800 | // the update for this component has finished with an error. |
| 801 | ping_manager_->OnUpdateComplete(crx); |
| 802 | |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 803 | // Move on to the next update, if there is one available. |
| 804 | ScheduleNextRun(kStepDelayMedium); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 805 | } else { |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 806 | size_t count = 0; |
| 807 | if (crx->status == CrxUpdateItem::kDownloadingDiff) { |
| 808 | count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff, |
| 809 | CrxUpdateItem::kUpdatingDiff); |
| 810 | } else { |
| 811 | count = ChangeItemStatus(CrxUpdateItem::kDownloading, |
| 812 | CrxUpdateItem::kUpdating); |
| 813 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 814 | DCHECK_EQ(count, 1ul); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 815 | crx_downloader_.reset(); |
[email protected] | cf44261 | 2011-08-09 20:20:12 | [diff] [blame] | 816 | |
[email protected] | 44da56e | 2011-11-21 19:59:14 | [diff] [blame] | 817 | // Why unretained? See comment at top of file. |
[email protected] | 8f5f2ea | 2013-10-31 09:39:10 | [diff] [blame] | 818 | blocking_task_runner_->PostDelayedTask( |
[email protected] | 73251e7 | 2012-03-04 02:10:33 | [diff] [blame] | 819 | FROM_HERE, |
[email protected] | 44da56e | 2011-11-21 19:59:14 | [diff] [blame] | 820 | base::Bind(&CrxUpdateService::Install, |
| 821 | base::Unretained(this), |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 822 | base::Passed(&crx_context), |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 823 | download_result.response), |
[email protected] | 73251e7 | 2012-03-04 02:10:33 | [diff] [blame] | 824 | base::TimeDelta::FromMilliseconds(config_->StepDelay())); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 825 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | // Install consists of digital signature verification, unpacking and then |
| 829 | // calling the component specific installer. All that is handled by the |
| 830 | // |unpacker|. If there is an error this function is in charge of deleting |
| 831 | // the files created. |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 832 | void CrxUpdateService::Install(scoped_ptr<CRXContext> context, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 833 | const base::FilePath& crx_path) { |
[email protected] | 8f5f2ea | 2013-10-31 09:39:10 | [diff] [blame] | 834 | // This function owns the file at |crx_path| and the |context| object. |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 835 | ComponentUnpacker unpacker(context->pk_hash, |
| 836 | crx_path, |
| 837 | context->fingerprint, |
| 838 | component_patcher_.get(), |
| 839 | context->installer); |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame^] | 840 | if (!DeleteFileAndEmptyParentDirectory(crx_path)) |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 841 | NOTREACHED() << crx_path.value(); |
[email protected] | 2e2c529 | 2013-12-17 03:48:40 | [diff] [blame] | 842 | |
[email protected] | 44da56e | 2011-11-21 19:59:14 | [diff] [blame] | 843 | // Why unretained? See comment at top of file. |
[email protected] | 73251e7 | 2012-03-04 02:10:33 | [diff] [blame] | 844 | BrowserThread::PostDelayedTask( |
| 845 | BrowserThread::UI, |
| 846 | FROM_HERE, |
[email protected] | 44da56e | 2011-11-21 19:59:14 | [diff] [blame] | 847 | base::Bind(&CrxUpdateService::DoneInstalling, base::Unretained(this), |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 848 | context->id, unpacker.error(), unpacker.extended_error()), |
[email protected] | 73251e7 | 2012-03-04 02:10:33 | [diff] [blame] | 849 | base::TimeDelta::FromMilliseconds(config_->StepDelay())); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | // Installation has been completed. Adjust the component status and |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 853 | // schedule the next check. Schedule a short delay before trying the full |
| 854 | // update when the differential update failed. |
[email protected] | 07f93af1 | 2011-08-17 20:57:22 | [diff] [blame] | 855 | void CrxUpdateService::DoneInstalling(const std::string& component_id, |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 856 | ComponentUnpacker::Error error, |
| 857 | int extra_code) { |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 858 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 07f93af1 | 2011-08-17 20:57:22 | [diff] [blame] | 859 | |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 860 | ErrorCategory error_category = kErrorNone; |
| 861 | switch (error) { |
| 862 | case ComponentUnpacker::kNone: |
| 863 | break; |
| 864 | case ComponentUnpacker::kInstallerError: |
| 865 | error_category = kInstallError; |
| 866 | break; |
| 867 | default: |
| 868 | error_category = kUnpackError; |
| 869 | break; |
| 870 | } |
| 871 | |
| 872 | const bool is_success = error == ComponentUnpacker::kNone; |
| 873 | |
[email protected] | 07f93af1 | 2011-08-17 20:57:22 | [diff] [blame] | 874 | CrxUpdateItem* item = FindUpdateItemById(component_id); |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 875 | if (item->status == CrxUpdateItem::kUpdatingDiff && !is_success) { |
| 876 | item->diff_error_category = error_category; |
| 877 | item->diff_error_code = error; |
| 878 | item->diff_extra_code1 = extra_code; |
[email protected] | 1ea21ad | 2013-09-02 04:20:39 | [diff] [blame] | 879 | item->diff_update_failed = true; |
| 880 | size_t count = ChangeItemStatus(CrxUpdateItem::kUpdatingDiff, |
| 881 | CrxUpdateItem::kCanUpdate); |
| 882 | DCHECK_EQ(count, 1ul); |
| 883 | ScheduleNextRun(kStepDelayShort); |
| 884 | return; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 885 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 886 | |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 887 | if (is_success) { |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 888 | ChangeItemState(item, CrxUpdateItem::kUpdated); |
[email protected] | 07f93af1 | 2011-08-17 20:57:22 | [diff] [blame] | 889 | item->component.version = item->next_version; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 890 | item->component.fingerprint = item->next_fp; |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 891 | } else { |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 892 | ChangeItemState(item, CrxUpdateItem::kNoUpdate); |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 893 | item->error_category = error_category; |
| 894 | item->error_code = error; |
| 895 | item->extra_code1 = extra_code; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 896 | } |
[email protected] | 07f93af1 | 2011-08-17 20:57:22 | [diff] [blame] | 897 | |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 898 | ping_manager_->OnUpdateComplete(item); |
[email protected] | 360b8bb | 2011-09-01 21:48:06 | [diff] [blame] | 899 | |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 900 | // Move on to the next update, if there is one available. |
| 901 | ScheduleNextRun(kStepDelayMedium); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 902 | } |
| 903 | |
[email protected] | 85e61d5 | 2013-08-01 22:23:42 | [diff] [blame] | 904 | void CrxUpdateService::NotifyComponentObservers( |
| 905 | ComponentObserver::Events event, int extra) const { |
| 906 | for (UpdateItems::const_iterator it = work_items_.begin(); |
| 907 | it != work_items_.end(); ++it) { |
| 908 | ComponentObserver* observer = (*it)->component.observer; |
| 909 | if (observer) |
| 910 | observer->OnEvent(event, 0); |
| 911 | } |
| 912 | } |
| 913 | |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 914 | content::ResourceThrottle* CrxUpdateService::GetOnDemandResourceThrottle( |
| 915 | net::URLRequest* request, const std::string& crx_id) { |
| 916 | // We give the raw pointer to the caller, who will delete it at will |
| 917 | // and we keep for ourselves a weak pointer to it so we can post tasks |
| 918 | // from the UI thread without having to track lifetime directly. |
| 919 | CUResourceThrottle* rt = new CUResourceThrottle(request); |
| 920 | BrowserThread::PostTask( |
| 921 | BrowserThread::UI, |
| 922 | FROM_HERE, |
| 923 | base::Bind(&CrxUpdateService::OnNewResourceThrottle, |
| 924 | base::Unretained(this), |
| 925 | rt->AsWeakPtr(), |
| 926 | crx_id)); |
| 927 | return rt; |
| 928 | } |
| 929 | |
| 930 | void CrxUpdateService::OnNewResourceThrottle( |
| 931 | base::WeakPtr<CUResourceThrottle> rt, const std::string& crx_id) { |
| 932 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 933 | // Check if we can on-demand update, else unblock the request anyway. |
| 934 | CrxUpdateItem* item = FindUpdateItemById(crx_id); |
| 935 | Status status = OnDemandUpdateInternal(item); |
| 936 | if (status == kOk || status == kInProgress) { |
| 937 | item->throttles.push_back(rt); |
| 938 | return; |
| 939 | } |
| 940 | UnblockResourceThrottle(rt); |
| 941 | } |
| 942 | |
| 943 | /////////////////////////////////////////////////////////////////////////////// |
| 944 | |
| 945 | CUResourceThrottle::CUResourceThrottle(const net::URLRequest* request) |
| 946 | : state_(NEW) { |
| 947 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 948 | } |
| 949 | |
| 950 | CUResourceThrottle::~CUResourceThrottle() { |
| 951 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 952 | } |
| 953 | |
| 954 | void CUResourceThrottle::WillStartRequest(bool* defer) { |
| 955 | if (state_ != UNBLOCKED) { |
| 956 | state_ = BLOCKED; |
| 957 | *defer = true; |
| 958 | } else { |
| 959 | *defer = false; |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | void CUResourceThrottle::WillRedirectRequest(const GURL& new_url, bool* defer) { |
| 964 | WillStartRequest(defer); |
| 965 | } |
| 966 | |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 967 | const char* CUResourceThrottle::GetNameForLogging() const { |
| 968 | return "ComponentUpdateResourceThrottle"; |
| 969 | } |
| 970 | |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 971 | void CUResourceThrottle::Unblock() { |
| 972 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 973 | if (state_ == BLOCKED) |
| 974 | controller()->Resume(); |
| 975 | state_ = UNBLOCKED; |
| 976 | } |
| 977 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 978 | // The component update factory. Using the component updater as a singleton |
| 979 | // is the job of the browser process. |
| 980 | ComponentUpdateService* ComponentUpdateServiceFactory( |
| 981 | ComponentUpdateService::Configurator* config) { |
| 982 | DCHECK(config); |
| 983 | return new CrxUpdateService(config); |
| 984 | } |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 985 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame^] | 986 | } // namespace component_updater |
| 987 | |