blob: c93bc642484efd03f6d3730fdd9cafdc863add97 [file] [log] [blame]
[email protected]93e8e2c2014-01-04 12:29:231// Copyright 2012 The Chromium Authors. All rights reserved.
[email protected]e8f96ff2011-08-03 05:07:332// 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]ccb4feef2013-02-14 06:16:478#include <set>
[email protected]e8f96ff2011-08-03 05:07:339#include <vector>
10
11#include "base/at_exit.h"
[email protected]44da56e2011-11-21 19:59:1412#include "base/bind.h"
[email protected]e3e696d32013-06-21 20:41:3613#include "base/compiler_specific.h"
[email protected]e8f96ff2011-08-03 05:07:3314#include "base/file_util.h"
[email protected]57999812013-02-24 05:40:5215#include "base/files/file_path.h"
[email protected]e8f96ff2011-08-03 05:07:3316#include "base/logging.h"
[email protected]7226b33c2011-08-18 08:44:2217#include "base/memory/scoped_ptr.h"
[email protected]00a77fa2013-11-02 04:18:4618#include "base/memory/weak_ptr.h"
[email protected]d3268fe2014-04-25 02:14:2319#include "base/observer_list.h"
[email protected]f5d27e32014-01-31 06:48:5320#include "base/sequenced_task_runner.h"
[email protected]e8f96ff2011-08-03 05:07:3321#include "base/stl_util.h"
[email protected]8f5f2ea2013-10-31 09:39:1022#include "base/threading/sequenced_worker_pool.h"
[email protected]41a17c52013-06-28 00:27:5323#include "base/timer/timer.h"
[email protected]e8f96ff2011-08-03 05:07:3324#include "chrome/browser/browser_process.h"
25#include "chrome/browser/component_updater/component_unpacker.h"
[email protected]7b0529242013-07-20 05:45:4626#include "chrome/browser/component_updater/component_updater_ping_manager.h"
[email protected]2cddef42013-11-22 08:23:2227#include "chrome/browser/component_updater/component_updater_utils.h"
[email protected]afa378f22013-12-02 03:37:5428#include "chrome/browser/component_updater/crx_downloader.h"
[email protected]7b0529242013-07-20 05:45:4629#include "chrome/browser/component_updater/crx_update_item.h"
[email protected]93e8e2c2014-01-04 12:29:2330#include "chrome/browser/component_updater/update_checker.h"
[email protected]6268d3a2013-11-27 01:28:0931#include "chrome/browser/component_updater/update_response.h"
[email protected]e8f96ff2011-08-03 05:07:3332#include "chrome/common/chrome_version_info.h"
[email protected]b7b63872013-01-03 02:41:1933#include "content/public/browser/browser_thread.h"
[email protected]00a77fa2013-11-02 04:18:4634#include "content/public/browser/resource_controller.h"
35#include "content/public/browser/resource_throttle.h"
[email protected]761fa4702013-07-02 15:25:1536#include "url/gurl.h"
[email protected]e8f96ff2011-08-03 05:07:3337
[email protected]631bb742011-11-02 11:29:3938using content::BrowserThread;
39
[email protected]055981f2014-01-17 20:22:3240namespace component_updater {
[email protected]3a0092d2013-12-18 03:04:3541
[email protected]44da56e2011-11-21 19:59:1442// The component updater is designed to live until process shutdown, so
43// base::Bind() calls are not refcounted.
44
[email protected]e8f96ff2011-08-03 05:07:3345namespace {
[email protected]e3e696d32013-06-21 20:41:3646
[email protected]2cddef42013-11-22 08:23:2247// Returns true if the |proposed| version is newer than |current| version.
[email protected]c5e4a2222014-01-03 16:06:1348bool IsVersionNewer(const Version& current, const std::string& proposed) {
49 Version proposed_ver(proposed);
[email protected]2cddef42013-11-22 08:23:2250 return proposed_ver.IsValid() && current.CompareTo(proposed_ver) < 0;
[email protected]e8f96ff2011-08-03 05:07:3351}
52
[email protected]e3e696d32013-06-21 20:41:3653// Returns true if a differential update is available, it has not failed yet,
54// and the configuration allows it.
55bool CanTryDiffUpdate(const CrxUpdateItem* update_item,
56 const ComponentUpdateService::Configurator& config) {
[email protected]055981f2014-01-17 20:22:3257 return HasDiffUpdate(update_item) &&
[email protected]e3e696d32013-06-21 20:41:3658 !update_item->diff_update_failed &&
59 config.DeltasEnabled();
60}
61
[email protected]3a0092d2013-12-18 03:04:3562void AppendDownloadMetrics(
63 const std::vector<CrxDownloader::DownloadMetrics>& source,
64 std::vector<CrxDownloader::DownloadMetrics>* destination) {
65 destination->insert(destination->end(), source.begin(), source.end());
66}
67
[email protected]7b0529242013-07-20 05:45:4668} // namespace
69
70CrxUpdateItem::CrxUpdateItem()
71 : status(kNew),
[email protected]61aca4cd2013-10-26 10:50:5972 on_demand(false),
[email protected]7b0529242013-07-20 05:45:4673 diff_update_failed(false),
74 error_category(0),
75 error_code(0),
76 extra_code1(0),
77 diff_error_category(0),
78 diff_error_code(0),
79 diff_extra_code1(0) {
80}
81
82CrxUpdateItem::~CrxUpdateItem() {
83}
[email protected]86550a42013-06-21 15:20:4984
[email protected]dc06f0b2013-01-23 20:03:1685CrxComponent::CrxComponent()
[email protected]85e61d52013-08-01 22:23:4286 : installer(NULL),
[email protected]cfd13e52014-02-05 09:35:0787 allow_background_download(true) {
[email protected]dc06f0b2013-01-23 20:03:1688}
89
90CrxComponent::~CrxComponent() {
91}
[email protected]e8f96ff2011-08-03 05:07:3392
[email protected]2e919ddd2013-08-21 05:05:1793CrxComponentInfo::CrxComponentInfo() {
94}
95
96CrxComponentInfo::~CrxComponentInfo() {
97}
98
[email protected]00a77fa2013-11-02 04:18:4699///////////////////////////////////////////////////////////////////////////////
100// In charge of blocking url requests until the |crx_id| component has been
101// updated. This class is touched solely from the IO thread. The UI thread
102// can post tasks to it via weak pointers. By default the request is blocked
103// unless the CrxUpdateService calls Unblock().
104// The lifetime is controlled by Chrome's resource loader so the component
105// updater cannot touch objects from this class except via weak pointers.
106class CUResourceThrottle
107 : public content::ResourceThrottle,
108 public base::SupportsWeakPtr<CUResourceThrottle> {
109 public:
110 explicit CUResourceThrottle(const net::URLRequest* request);
111 virtual ~CUResourceThrottle();
[email protected]2cddef42013-11-22 08:23:22112
[email protected]00a77fa2013-11-02 04:18:46113 // Overriden from ResourceThrottle.
114 virtual void WillStartRequest(bool* defer) OVERRIDE;
115 virtual void WillRedirectRequest(const GURL& new_url, bool* defer) OVERRIDE;
[email protected]f8fe5cf2013-12-04 20:11:53116 virtual const char* GetNameForLogging() const OVERRIDE;
[email protected]00a77fa2013-11-02 04:18:46117
118 // Component updater calls this function via PostTask to unblock the request.
119 void Unblock();
120
121 typedef std::vector<base::WeakPtr<CUResourceThrottle> > WeakPtrVector;
122
123 private:
[email protected]2cddef42013-11-22 08:23:22124 enum State {
125 NEW,
126 BLOCKED,
127 UNBLOCKED
128 };
[email protected]00a77fa2013-11-02 04:18:46129
[email protected]2cddef42013-11-22 08:23:22130 State state_;
[email protected]00a77fa2013-11-02 04:18:46131};
132
133void UnblockResourceThrottle(base::WeakPtr<CUResourceThrottle> rt) {
134 BrowserThread::PostTask(
135 BrowserThread::IO,
136 FROM_HERE,
137 base::Bind(&CUResourceThrottle::Unblock, rt));
138}
139
140void UnblockandReapAllThrottles(CUResourceThrottle::WeakPtrVector* throttles) {
141 CUResourceThrottle::WeakPtrVector::iterator it;
142 for (it = throttles->begin(); it != throttles->end(); ++it)
143 UnblockResourceThrottle(*it);
144 throttles->clear();
145}
146
[email protected]e8f96ff2011-08-03 05:07:33147//////////////////////////////////////////////////////////////////////////////
148// The one and only implementation of the ComponentUpdateService interface. In
149// charge of running the show. The main method is ProcessPendingItems() which
[email protected]50b01392012-09-01 03:33:13150// is called periodically to do the upgrades/installs or the update checks.
[email protected]e8f96ff2011-08-03 05:07:33151// An important consideration here is to be as "low impact" as we can to the
152// rest of the browser, so even if we have many components registered and
[email protected]f1050432012-02-15 01:35:46153// eligible for update, we only do one thing at a time with pauses in between
[email protected]e8f96ff2011-08-03 05:07:33154// the tasks. Also when we do network requests there is only one |url_fetcher_|
[email protected]e3e696d32013-06-21 20:41:36155// in flight at a time.
[email protected]e8f96ff2011-08-03 05:07:33156// There are no locks in this code, the main structure |work_items_| is mutated
[email protected]74be2642014-02-07 09:40:37157// only from the UI thread. The unpack and installation is done in a blocking
158// pool thread. The network requests are done in the IO thread or in the file
[email protected]e8f96ff2011-08-03 05:07:33159// thread.
160class CrxUpdateService : public ComponentUpdateService {
161 public:
162 explicit CrxUpdateService(ComponentUpdateService::Configurator* config);
[email protected]e8f96ff2011-08-03 05:07:33163 virtual ~CrxUpdateService();
164
165 // Overrides for ComponentUpdateService.
[email protected]d3268fe2014-04-25 02:14:23166 virtual void AddObserver(Observer* observer) OVERRIDE;
167 virtual void RemoveObserver(Observer* observer) OVERRIDE;
[email protected]e8f96ff2011-08-03 05:07:33168 virtual Status Start() OVERRIDE;
169 virtual Status Stop() OVERRIDE;
170 virtual Status RegisterComponent(const CrxComponent& component) OVERRIDE;
[email protected]61aca4cd2013-10-26 10:50:59171 virtual Status OnDemandUpdate(const std::string& component_id) OVERRIDE;
[email protected]2e919ddd2013-08-21 05:05:17172 virtual void GetComponents(
173 std::vector<CrxComponentInfo>* components) OVERRIDE;
[email protected]00a77fa2013-11-02 04:18:46174 virtual content::ResourceThrottle* GetOnDemandResourceThrottle(
175 net::URLRequest* request, const std::string& crx_id) OVERRIDE;
[email protected]e8f96ff2011-08-03 05:07:33176
[email protected]28ea9ac2014-05-03 22:07:18177 // Context for a crx download url request.
178 struct CRXContext {
179 ComponentInstaller* installer;
180 std::vector<uint8> pk_hash;
181 std::string id;
182 std::string fingerprint;
183 CRXContext() : installer(NULL) {}
184 };
[email protected]e8f96ff2011-08-03 05:07:33185
[email protected]e8f96ff2011-08-03 05:07:33186 private:
[email protected]7b0529242013-07-20 05:45:46187 enum ErrorCategory {
188 kErrorNone = 0,
189 kNetworkError,
190 kUnpackError,
191 kInstallError,
192 };
193
[email protected]32a6c8382013-08-20 00:29:20194 enum StepDelayInterval {
195 kStepDelayShort = 0,
196 kStepDelayMedium,
197 kStepDelayLong,
198 };
199
[email protected]055981f2014-01-17 20:22:32200 void UpdateCheckComplete(int error,
201 const std::string& error_message,
202 const UpdateResponse::Results& results);
203 void OnUpdateCheckSucceeded(const UpdateResponse::Results& results);
[email protected]93e8e2c2014-01-04 12:29:23204 void OnUpdateCheckFailed(int error, const std::string& error_message);
[email protected]e8f96ff2011-08-03 05:07:33205
[email protected]8a5ebd432014-05-02 00:21:22206 void DownloadProgress(const std::string& component_id,
207 const CrxDownloader::Result& download_result);
208
209 void DownloadComplete(scoped_ptr<CRXContext> crx_context,
210 const CrxDownloader::Result& download_result);
[email protected]afa378f22013-12-02 03:37:54211
[email protected]00a77fa2013-11-02 04:18:46212 Status OnDemandUpdateInternal(CrxUpdateItem* item);
213
[email protected]e8f96ff2011-08-03 05:07:33214 void ProcessPendingItems();
215
[email protected]93e8e2c2014-01-04 12:29:23216 // Find a component that is ready to update.
217 CrxUpdateItem* FindReadyComponent() const;
218
219 // Prepares the components for an update check and initiates the request.
220 // Returns true if an update check request has been made. Returns false if
221 // no update check was needed or an error occured.
222 bool CheckForUpdates();
[email protected]61aca4cd2013-10-26 10:50:59223
224 void UpdateComponent(CrxUpdateItem* workitem);
225
[email protected]32a6c8382013-08-20 00:29:20226 void ScheduleNextRun(StepDelayInterval step_delay);
[email protected]e8f96ff2011-08-03 05:07:33227
[email protected]6268d3a2013-11-27 01:28:09228 void ParseResponse(const std::string& xml);
[email protected]e8f96ff2011-08-03 05:07:33229
[email protected]afa378f22013-12-02 03:37:54230 void Install(scoped_ptr<CRXContext> context, const base::FilePath& crx_path);
[email protected]e8f96ff2011-08-03 05:07:33231
[email protected]f5d27e32014-01-31 06:48:53232 void EndUnpacking(const std::string& component_id,
233 const base::FilePath& crx_path,
234 ComponentUnpacker::Error error,
235 int extended_error);
236
[email protected]07f93af12011-08-17 20:57:22237 void DoneInstalling(const std::string& component_id,
[email protected]e3e696d32013-06-21 20:41:36238 ComponentUnpacker::Error error,
239 int extended_error);
[email protected]e8f96ff2011-08-03 05:07:33240
[email protected]61aca4cd2013-10-26 10:50:59241 void ChangeItemState(CrxUpdateItem* item, CrxUpdateItem::Status to);
242
[email protected]e8f96ff2011-08-03 05:07:33243 size_t ChangeItemStatus(CrxUpdateItem::Status from,
244 CrxUpdateItem::Status to);
245
246 CrxUpdateItem* FindUpdateItemById(const std::string& id);
247
[email protected]d3268fe2014-04-25 02:14:23248 void NotifyObservers(Observer::Events event, const std::string& id);
[email protected]85e61d52013-08-01 22:23:42249
[email protected]61aca4cd2013-10-26 10:50:59250 bool HasOnDemandItems() const;
251
[email protected]00a77fa2013-11-02 04:18:46252 void OnNewResourceThrottle(base::WeakPtr<CUResourceThrottle> rt,
253 const std::string& crx_id);
254
[email protected]e3e696d32013-06-21 20:41:36255 scoped_ptr<ComponentUpdateService::Configurator> config_;
256
[email protected]055981f2014-01-17 20:22:32257 scoped_ptr<UpdateChecker> update_checker_;
[email protected]e8f96ff2011-08-03 05:07:33258
[email protected]055981f2014-01-17 20:22:32259 scoped_ptr<PingManager> ping_manager_;
[email protected]7b0529242013-07-20 05:45:46260
[email protected]94a481b2014-03-28 19:41:55261 scoped_refptr<ComponentUnpacker> unpacker_;
[email protected]f5d27e32014-01-31 06:48:53262
[email protected]055981f2014-01-17 20:22:32263 scoped_ptr<CrxDownloader> crx_downloader_;
[email protected]afa378f22013-12-02 03:37:54264
[email protected]86550a42013-06-21 15:20:49265 // A collection of every work item.
[email protected]e3e696d32013-06-21 20:41:36266 typedef std::vector<CrxUpdateItem*> UpdateItems;
[email protected]e8f96ff2011-08-03 05:07:33267 UpdateItems work_items_;
268
269 base::OneShotTimer<CrxUpdateService> timer_;
270
[email protected]8f5f2ea2013-10-31 09:39:10271 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
272
[email protected]c5e4a2222014-01-03 16:06:13273 const Version chrome_version_;
[email protected]e8f96ff2011-08-03 05:07:33274
275 bool running_;
276
[email protected]d3268fe2014-04-25 02:14:23277 ObserverList<Observer> observer_list_;
278
[email protected]e8f96ff2011-08-03 05:07:33279 DISALLOW_COPY_AND_ASSIGN(CrxUpdateService);
280};
281
[email protected]e8f96ff2011-08-03 05:07:33282//////////////////////////////////////////////////////////////////////////////
283
[email protected]e3e696d32013-06-21 20:41:36284CrxUpdateService::CrxUpdateService(ComponentUpdateService::Configurator* config)
[email protected]e8f96ff2011-08-03 05:07:33285 : config_(config),
[email protected]055981f2014-01-17 20:22:32286 ping_manager_(new PingManager(config->PingUrl(),
287 config->RequestContext())),
[email protected]8f5f2ea2013-10-31 09:39:10288 blocking_task_runner_(BrowserThread::GetBlockingPool()->
289 GetSequencedTaskRunnerWithShutdownBehavior(
290 BrowserThread::GetBlockingPool()->GetSequenceToken(),
291 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)),
[email protected]e8f96ff2011-08-03 05:07:33292 chrome_version_(chrome::VersionInfo().Version()),
293 running_(false) {
[email protected]1ea21ad2013-09-02 04:20:39294}
[email protected]e8f96ff2011-08-03 05:07:33295
296CrxUpdateService::~CrxUpdateService() {
297 // Because we are a singleton, at this point only the UI thread should be
298 // alive, this simplifies the management of the work that could be in
299 // flight in other threads.
300 Stop();
301 STLDeleteElements(&work_items_);
[email protected]1ea21ad2013-09-02 04:20:39302}
[email protected]e8f96ff2011-08-03 05:07:33303
[email protected]d3268fe2014-04-25 02:14:23304void CrxUpdateService::AddObserver(Observer* observer) {
305 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
306 observer_list_.AddObserver(observer);
307}
308
309void CrxUpdateService::RemoveObserver(Observer* observer) {
310 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
311 observer_list_.RemoveObserver(observer);
312}
313
[email protected]e8f96ff2011-08-03 05:07:33314ComponentUpdateService::Status CrxUpdateService::Start() {
315 // Note that RegisterComponent will call Start() when the first
316 // component is registered, so it can be called twice. This way
317 // we avoid scheduling the timer if there is no work to do.
[email protected]fb53e652014-04-30 11:27:19318 VLOG(1) << "CrxUpdateService starting up";
[email protected]e8f96ff2011-08-03 05:07:33319 running_ = true;
320 if (work_items_.empty())
321 return kOk;
322
[email protected]d3268fe2014-04-25 02:14:23323 NotifyObservers(Observer::COMPONENT_UPDATER_STARTED, "");
[email protected]85e61d52013-08-01 22:23:42324
[email protected]fb53e652014-04-30 11:27:19325 VLOG(1) << "First update attempt will take place in "
326 << config_->InitialDelay() << " seconds";
[email protected]d323a172011-09-02 18:23:02327 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(config_->InitialDelay()),
[email protected]e8f96ff2011-08-03 05:07:33328 this, &CrxUpdateService::ProcessPendingItems);
329 return kOk;
330}
331
332// Stop the main check + update loop. In flight operations will be
333// completed.
334ComponentUpdateService::Status CrxUpdateService::Stop() {
[email protected]fb53e652014-04-30 11:27:19335 VLOG(1) << "CrxUpdateService stopping";
[email protected]e8f96ff2011-08-03 05:07:33336 running_ = false;
337 timer_.Stop();
338 return kOk;
339}
340
[email protected]61aca4cd2013-10-26 10:50:59341bool CrxUpdateService::HasOnDemandItems() const {
342 class Helper {
343 public:
344 static bool IsOnDemand(CrxUpdateItem* item) {
345 return item->on_demand;
346 }
347 };
348 return std::find_if(work_items_.begin(),
349 work_items_.end(),
350 Helper::IsOnDemand) != work_items_.end();
351}
352
[email protected]ccb4feef2013-02-14 06:16:47353// This function sets the timer which will call ProcessPendingItems() or
[email protected]61aca4cd2013-10-26 10:50:59354// ProcessRequestedItem() if there is an on_demand item. There
[email protected]32a6c8382013-08-20 00:29:20355// are three kinds of waits:
356// - a short delay, when there is immediate work to be done.
357// - a medium delay, when there are updates to be applied within the current
358// update cycle, or there are components that are still unchecked.
359// - a long delay when a full check/update cycle has completed for all
360// components.
361void CrxUpdateService::ScheduleNextRun(StepDelayInterval step_delay) {
[email protected]e8f96ff2011-08-03 05:07:33362 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23363 DCHECK(!update_checker_);
[email protected]e8f96ff2011-08-03 05:07:33364 CHECK(!timer_.IsRunning());
365 // It could be the case that Stop() had been called while a url request
366 // or unpacking was in flight, if so we arrive here but |running_| is
367 // false. In that case do not loop again.
368 if (!running_)
369 return;
370
[email protected]ccb4feef2013-02-14 06:16:47371 // Keep the delay short if in the middle of an update (step_delay),
372 // or there are new requested_work_items_ that have not been processed yet.
[email protected]32a6c8382013-08-20 00:29:20373 int64 delay_seconds = 0;
[email protected]61aca4cd2013-10-26 10:50:59374 if (!HasOnDemandItems()) {
[email protected]32a6c8382013-08-20 00:29:20375 switch (step_delay) {
376 case kStepDelayShort:
377 delay_seconds = config_->StepDelay();
378 break;
379 case kStepDelayMedium:
380 delay_seconds = config_->StepDelayMedium();
381 break;
382 case kStepDelayLong:
383 delay_seconds = config_->NextCheckDelay();
384 break;
385 }
386 } else {
387 delay_seconds = config_->StepDelay();
388 }
[email protected]cf442612011-08-09 20:20:12389
[email protected]32a6c8382013-08-20 00:29:20390 if (step_delay != kStepDelayShort) {
[email protected]d3268fe2014-04-25 02:14:23391 NotifyObservers(Observer::COMPONENT_UPDATER_SLEEPING, "");
[email protected]85e61d52013-08-01 22:23:42392
[email protected]e8f96ff2011-08-03 05:07:33393 // Zero is only used for unit tests.
[email protected]32a6c8382013-08-20 00:29:20394 if (0 == delay_seconds)
[email protected]e8f96ff2011-08-03 05:07:33395 return;
396 }
397
[email protected]fb53e652014-04-30 11:27:19398 VLOG(1) << "Scheduling next run to occur in " << delay_seconds << " seconds";
[email protected]32a6c8382013-08-20 00:29:20399 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(delay_seconds),
[email protected]e8f96ff2011-08-03 05:07:33400 this, &CrxUpdateService::ProcessPendingItems);
401}
402
403// Given a extension-like component id, find the associated component.
404CrxUpdateItem* CrxUpdateService::FindUpdateItemById(const std::string& id) {
405 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
406 CrxUpdateItem::FindById finder(id);
407 UpdateItems::iterator it = std::find_if(work_items_.begin(),
408 work_items_.end(),
409 finder);
[email protected]93e8e2c2014-01-04 12:29:23410 return it != work_items_.end() ? *it : NULL;
[email protected]e8f96ff2011-08-03 05:07:33411}
412
[email protected]61aca4cd2013-10-26 10:50:59413// Changes a component's status, clearing on_demand and firing notifications as
414// necessary. By convention, this is the only function that can change a
415// CrxUpdateItem's |status|.
416// TODO(waffles): Do we want to add DCHECKS for valid state transitions here?
417void CrxUpdateService::ChangeItemState(CrxUpdateItem* item,
418 CrxUpdateItem::Status to) {
419 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
420 if (to == CrxUpdateItem::kNoUpdate ||
421 to == CrxUpdateItem::kUpdated ||
422 to == CrxUpdateItem::kUpToDate) {
423 item->on_demand = false;
424 }
425
426 item->status = to;
427
[email protected]d3268fe2014-04-25 02:14:23428 switch (to) {
429 case CrxUpdateItem::kCanUpdate:
430 NotifyObservers(Observer::COMPONENT_UPDATE_FOUND, item->id);
431 break;
432 case CrxUpdateItem::kUpdatingDiff:
433 case CrxUpdateItem::kUpdating:
434 NotifyObservers(Observer::COMPONENT_UPDATE_READY, item->id);
435 break;
436 case CrxUpdateItem::kUpdated:
437 NotifyObservers(Observer::COMPONENT_UPDATED, item->id);
438 break;
439 case CrxUpdateItem::kUpToDate:
440 case CrxUpdateItem::kNoUpdate:
441 NotifyObservers(Observer::COMPONENT_NOT_UPDATED, item->id);
442 break;
443 case CrxUpdateItem::kNew:
444 case CrxUpdateItem::kChecking:
445 case CrxUpdateItem::kDownloading:
446 case CrxUpdateItem::kDownloadingDiff:
447 case CrxUpdateItem::kLastStatus:
448 // No notification for these states.
449 break;
[email protected]61aca4cd2013-10-26 10:50:59450 }
[email protected]00a77fa2013-11-02 04:18:46451
452 // Free possible pending network requests.
453 if ((to == CrxUpdateItem::kUpdated) ||
454 (to == CrxUpdateItem::kUpToDate) ||
455 (to == CrxUpdateItem::kNoUpdate)) {
456 UnblockandReapAllThrottles(&item->throttles);
457 }
[email protected]61aca4cd2013-10-26 10:50:59458}
459
[email protected]e8f96ff2011-08-03 05:07:33460// Changes all the components in |work_items_| that have |from| status to
[email protected]e3e696d32013-06-21 20:41:36461// |to| status and returns how many have been changed.
[email protected]e8f96ff2011-08-03 05:07:33462size_t CrxUpdateService::ChangeItemStatus(CrxUpdateItem::Status from,
463 CrxUpdateItem::Status to) {
464 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
465 size_t count = 0;
466 for (UpdateItems::iterator it = work_items_.begin();
467 it != work_items_.end(); ++it) {
468 CrxUpdateItem* item = *it;
[email protected]93e8e2c2014-01-04 12:29:23469 if (item->status == from) {
470 ChangeItemState(item, to);
471 ++count;
472 }
[email protected]e8f96ff2011-08-03 05:07:33473 }
474 return count;
475}
476
477// Adds a component to be checked for upgrades. If the component exists it
478// it will be replaced and the return code is kReplaced.
[email protected]e8f96ff2011-08-03 05:07:33479ComponentUpdateService::Status CrxUpdateService::RegisterComponent(
480 const CrxComponent& component) {
481 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
482 if (component.pk_hash.empty() ||
483 !component.version.IsValid() ||
484 !component.installer)
485 return kError;
486
[email protected]055981f2014-01-17 20:22:32487 std::string id(GetCrxComponentID(component));
[email protected]93e8e2c2014-01-04 12:29:23488 CrxUpdateItem* uit = FindUpdateItemById(id);
[email protected]e8f96ff2011-08-03 05:07:33489 if (uit) {
490 uit->component = component;
491 return kReplaced;
492 }
493
494 uit = new CrxUpdateItem;
495 uit->id.swap(id);
496 uit->component = component;
[email protected]e3e696d32013-06-21 20:41:36497
[email protected]e8f96ff2011-08-03 05:07:33498 work_items_.push_back(uit);
499 // If this is the first component registered we call Start to
500 // schedule the first timer.
501 if (running_ && (work_items_.size() == 1))
502 Start();
503
504 return kOk;
505}
506
[email protected]ccb4feef2013-02-14 06:16:47507// Start the process of checking for an update, for a particular component
508// that was previously registered.
[email protected]2e919ddd2013-08-21 05:05:17509// |component_id| is a value returned from GetCrxComponentID().
[email protected]61aca4cd2013-10-26 10:50:59510ComponentUpdateService::Status CrxUpdateService::OnDemandUpdate(
[email protected]2e919ddd2013-08-21 05:05:17511 const std::string& component_id) {
[email protected]00a77fa2013-11-02 04:18:46512 return OnDemandUpdateInternal(FindUpdateItemById(component_id));
513}
514
515ComponentUpdateService::Status CrxUpdateService::OnDemandUpdateInternal(
516 CrxUpdateItem* uit) {
[email protected]ccb4feef2013-02-14 06:16:47517 if (!uit)
518 return kError;
[email protected]2cddef42013-11-22 08:23:22519
[email protected]ccb4feef2013-02-14 06:16:47520 // Check if the request is too soon.
521 base::TimeDelta delta = base::Time::Now() - uit->last_check;
[email protected]e3e696d32013-06-21 20:41:36522 if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay()))
[email protected]ccb4feef2013-02-14 06:16:47523 return kError;
[email protected]ccb4feef2013-02-14 06:16:47524
525 switch (uit->status) {
526 // If the item is already in the process of being updated, there is
527 // no point in this call, so return kInProgress.
528 case CrxUpdateItem::kChecking:
529 case CrxUpdateItem::kCanUpdate:
[email protected]e3e696d32013-06-21 20:41:36530 case CrxUpdateItem::kDownloadingDiff:
[email protected]ccb4feef2013-02-14 06:16:47531 case CrxUpdateItem::kDownloading:
[email protected]e3e696d32013-06-21 20:41:36532 case CrxUpdateItem::kUpdatingDiff:
[email protected]ccb4feef2013-02-14 06:16:47533 case CrxUpdateItem::kUpdating:
534 return kInProgress;
535 // Otherwise the item was already checked a while back (or it is new),
536 // set its status to kNew to give it a slightly higher priority.
537 case CrxUpdateItem::kNew:
538 case CrxUpdateItem::kUpdated:
539 case CrxUpdateItem::kUpToDate:
540 case CrxUpdateItem::kNoUpdate:
[email protected]61aca4cd2013-10-26 10:50:59541 ChangeItemState(uit, CrxUpdateItem::kNew);
542 uit->on_demand = true;
[email protected]ccb4feef2013-02-14 06:16:47543 break;
544 case CrxUpdateItem::kLastStatus:
545 NOTREACHED() << uit->status;
546 }
547
548 // In case the current delay is long, set the timer to a shorter value
549 // to get the ball rolling.
550 if (timer_.IsRunning()) {
551 timer_.Stop();
552 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(config_->StepDelay()),
553 this, &CrxUpdateService::ProcessPendingItems);
554 }
555
556 return kOk;
557}
558
[email protected]2e919ddd2013-08-21 05:05:17559void CrxUpdateService::GetComponents(
560 std::vector<CrxComponentInfo>* components) {
561 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
562 for (UpdateItems::const_iterator it = work_items_.begin();
563 it != work_items_.end(); ++it) {
564 const CrxUpdateItem* item = *it;
565 CrxComponentInfo info;
[email protected]055981f2014-01-17 20:22:32566 info.id = GetCrxComponentID(item->component);
[email protected]2e919ddd2013-08-21 05:05:17567 info.version = item->component.version.GetString();
568 info.name = item->component.name;
569 components->push_back(info);
570 }
571}
572
[email protected]93e8e2c2014-01-04 12:29:23573// This is the main loop of the component updater. It updates one component
574// at a time if updates are available. Otherwise, it does an update check or
575// takes a long sleep until the loop runs again.
[email protected]e8f96ff2011-08-03 05:07:33576void CrxUpdateService::ProcessPendingItems() {
577 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23578
[email protected]61aca4cd2013-10-26 10:50:59579 CrxUpdateItem* ready_upgrade = FindReadyComponent();
580 if (ready_upgrade) {
581 UpdateComponent(ready_upgrade);
[email protected]e8f96ff2011-08-03 05:07:33582 return;
583 }
[email protected]93e8e2c2014-01-04 12:29:23584
585 if (!CheckForUpdates())
586 ScheduleNextRun(kStepDelayLong);
[email protected]61aca4cd2013-10-26 10:50:59587}
588
[email protected]93e8e2c2014-01-04 12:29:23589CrxUpdateItem* CrxUpdateService::FindReadyComponent() const {
[email protected]c4b4cfa2014-03-10 18:55:00590 class Helper {
591 public:
592 static bool IsReadyOnDemand(CrxUpdateItem* item) {
593 return item->on_demand && IsReady(item);
[email protected]61aca4cd2013-10-26 10:50:59594 }
[email protected]c4b4cfa2014-03-10 18:55:00595 static bool IsReady(CrxUpdateItem* item) {
596 return item->status == CrxUpdateItem::kCanUpdate;
597 }
598 };
[email protected]61aca4cd2013-10-26 10:50:59599
[email protected]c4b4cfa2014-03-10 18:55:00600 std::vector<CrxUpdateItem*>::const_iterator it = std::find_if(
601 work_items_.begin(), work_items_.end(), Helper::IsReadyOnDemand);
602 if (it != work_items_.end())
603 return *it;
604 it = std::find_if(work_items_.begin(), work_items_.end(), Helper::IsReady);
605 if (it != work_items_.end())
606 return *it;
[email protected]61aca4cd2013-10-26 10:50:59607 return NULL;
608}
609
[email protected]93e8e2c2014-01-04 12:29:23610// Prepares the components for an update check and initiates the request.
[email protected]21a9c9a2014-02-19 19:37:11611// On demand components are always included in the update check request.
612// Otherwise, only include components that have not been checked recently.
[email protected]93e8e2c2014-01-04 12:29:23613bool CrxUpdateService::CheckForUpdates() {
[email protected]21a9c9a2014-02-19 19:37:11614 const base::TimeDelta minimum_recheck_wait_time =
615 base::TimeDelta::FromSeconds(config_->MinimumReCheckWait());
616 const base::Time now(base::Time::Now());
617
[email protected]93e8e2c2014-01-04 12:29:23618 std::vector<CrxUpdateItem*> items_to_check;
619 for (size_t i = 0; i != work_items_.size(); ++i) {
620 CrxUpdateItem* item = work_items_[i];
621 DCHECK(item->status == CrxUpdateItem::kNew ||
622 item->status == CrxUpdateItem::kNoUpdate ||
623 item->status == CrxUpdateItem::kUpToDate ||
624 item->status == CrxUpdateItem::kUpdated);
625
[email protected]21a9c9a2014-02-19 19:37:11626 const base::TimeDelta time_since_last_checked(now - item->last_check);
627
628 if (!item->on_demand &&
629 time_since_last_checked < minimum_recheck_wait_time) {
[email protected]fb53e652014-04-30 11:27:19630 VLOG(1) << "Skipping check for component update: id=" << item->id
631 << ", time_since_last_checked="
632 << time_since_last_checked.InSeconds()
633 << " seconds: too soon to check for an update";
[email protected]21a9c9a2014-02-19 19:37:11634 continue;
635 }
636
[email protected]fb53e652014-04-30 11:27:19637 VLOG(1) << "Scheduling update check for component id=" << item->id
638 << ", time_since_last_checked="
639 << time_since_last_checked.InSeconds()
640 << " seconds";
641
[email protected]93e8e2c2014-01-04 12:29:23642 ChangeItemState(item, CrxUpdateItem::kChecking);
643
[email protected]21a9c9a2014-02-19 19:37:11644 item->last_check = now;
[email protected]93e8e2c2014-01-04 12:29:23645 item->crx_urls.clear();
646 item->crx_diffurls.clear();
647 item->previous_version = item->component.version;
648 item->next_version = Version();
649 item->previous_fp = item->component.fingerprint;
650 item->next_fp.clear();
651 item->diff_update_failed = false;
652 item->error_category = 0;
653 item->error_code = 0;
654 item->extra_code1 = 0;
655 item->diff_error_category = 0;
656 item->diff_error_code = 0;
657 item->diff_extra_code1 = 0;
658 item->download_metrics.clear();
659
660 items_to_check.push_back(item);
661 }
662
663 if (items_to_check.empty())
664 return false;
665
[email protected]055981f2014-01-17 20:22:32666 update_checker_ = UpdateChecker::Create(
[email protected]93e8e2c2014-01-04 12:29:23667 config_->UpdateUrl(),
668 config_->RequestContext(),
669 base::Bind(&CrxUpdateService::UpdateCheckComplete,
670 base::Unretained(this))).Pass();
671 return update_checker_->CheckForUpdates(items_to_check,
672 config_->ExtraRequestParams());
673}
674
[email protected]61aca4cd2013-10-26 10:50:59675void CrxUpdateService::UpdateComponent(CrxUpdateItem* workitem) {
[email protected]afa378f22013-12-02 03:37:54676 scoped_ptr<CRXContext> crx_context(new CRXContext);
677 crx_context->pk_hash = workitem->component.pk_hash;
678 crx_context->id = workitem->id;
679 crx_context->installer = workitem->component.installer;
680 crx_context->fingerprint = workitem->next_fp;
[email protected]da37c1d2013-12-19 01:04:38681 const std::vector<GURL>* urls = NULL;
[email protected]cfd13e52014-02-05 09:35:07682 bool allow_background_download = false;
[email protected]61aca4cd2013-10-26 10:50:59683 if (CanTryDiffUpdate(workitem, *config_)) {
[email protected]da37c1d2013-12-19 01:04:38684 urls = &workitem->crx_diffurls;
[email protected]61aca4cd2013-10-26 10:50:59685 ChangeItemState(workitem, CrxUpdateItem::kDownloadingDiff);
686 } else {
[email protected]cfd13e52014-02-05 09:35:07687 // Background downloads are enabled only for selected components and
688 // only for full downloads (see issue 340448).
689 allow_background_download = workitem->component.allow_background_download;
[email protected]da37c1d2013-12-19 01:04:38690 urls = &workitem->crx_urls;
[email protected]61aca4cd2013-10-26 10:50:59691 ChangeItemState(workitem, CrxUpdateItem::kDownloading);
692 }
[email protected]3cb2a4f2013-12-07 21:54:34693
694 // On demand component updates are always downloaded in foreground.
[email protected]cfd13e52014-02-05 09:35:07695 const bool is_background_download =
696 !workitem->on_demand && allow_background_download &&
697 config_->UseBackgroundDownloader();
[email protected]3cb2a4f2013-12-07 21:54:34698
[email protected]1b6587dc52014-04-26 00:38:55699 crx_downloader_.reset(CrxDownloader::Create(is_background_download,
700 config_->RequestContext(),
701 blocking_task_runner_));
[email protected]8a5ebd432014-05-02 00:21:22702 crx_downloader_->set_progress_callback(
703 base::Bind(&CrxUpdateService::DownloadProgress,
704 base::Unretained(this),
705 crx_context->id));
[email protected]1b6587dc52014-04-26 00:38:55706 crx_downloader_->StartDownload(*urls,
707 base::Bind(&CrxUpdateService::DownloadComplete,
708 base::Unretained(this),
709 base::Passed(&crx_context)));
[email protected]61aca4cd2013-10-26 10:50:59710}
711
[email protected]93e8e2c2014-01-04 12:29:23712void CrxUpdateService::UpdateCheckComplete(
713 int error,
714 const std::string& error_message,
[email protected]055981f2014-01-17 20:22:32715 const UpdateResponse::Results& results) {
[email protected]e8f96ff2011-08-03 05:07:33716 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23717 update_checker_.reset();
718 if (!error)
719 OnUpdateCheckSucceeded(results);
[email protected]652f4272013-11-13 09:23:41720 else
[email protected]93e8e2c2014-01-04 12:29:23721 OnUpdateCheckFailed(error, error_message);
[email protected]e8f96ff2011-08-03 05:07:33722}
723
[email protected]93e8e2c2014-01-04 12:29:23724// Handles a valid Omaha update check response by matching the results with
725// the registered components which were checked for updates.
726// If updates are found, prepare the components for the actual version upgrade.
727// One of these components will be drafted for the upgrade next time
728// ProcessPendingItems is called.
729void CrxUpdateService::OnUpdateCheckSucceeded(
[email protected]055981f2014-01-17 20:22:32730 const UpdateResponse::Results& results) {
[email protected]2cddef42013-11-22 08:23:22731 size_t num_updates_pending = 0;
[email protected]e8f96ff2011-08-03 05:07:33732 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]fb53e652014-04-30 11:27:19733 VLOG(1) << "Update check succeeded.";
[email protected]055981f2014-01-17 20:22:32734 std::vector<UpdateResponse::Result>::const_iterator it;
[email protected]e8f96ff2011-08-03 05:07:33735 for (it = results.list.begin(); it != results.list.end(); ++it) {
736 CrxUpdateItem* crx = FindUpdateItemById(it->extension_id);
737 if (!crx)
738 continue;
739
[email protected]2cddef42013-11-22 08:23:22740 if (crx->status != CrxUpdateItem::kChecking) {
741 NOTREACHED();
[email protected]e8f96ff2011-08-03 05:07:33742 continue; // Not updating this component now.
[email protected]2cddef42013-11-22 08:23:22743 }
[email protected]e8f96ff2011-08-03 05:07:33744
[email protected]2cddef42013-11-22 08:23:22745 if (it->manifest.version.empty()) {
[email protected]cf442612011-08-09 20:20:12746 // No version means no update available.
[email protected]61aca4cd2013-10-26 10:50:59747 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]fb53e652014-04-30 11:27:19748 VLOG(1) << "No update available for component: " << crx->id;
[email protected]cf442612011-08-09 20:20:12749 continue;
[email protected]e8f96ff2011-08-03 05:07:33750 }
[email protected]2cddef42013-11-22 08:23:22751
752 if (!IsVersionNewer(crx->component.version, it->manifest.version)) {
753 // The component is up to date.
[email protected]61aca4cd2013-10-26 10:50:59754 ChangeItemState(crx, CrxUpdateItem::kUpToDate);
[email protected]fb53e652014-04-30 11:27:19755 VLOG(1) << "Component already up-to-date: " << crx->id;
[email protected]cf442612011-08-09 20:20:12756 continue;
[email protected]e8f96ff2011-08-03 05:07:33757 }
[email protected]2cddef42013-11-22 08:23:22758
759 if (!it->manifest.browser_min_version.empty()) {
760 if (IsVersionNewer(chrome_version_, it->manifest.browser_min_version)) {
761 // The component is not compatible with this Chrome version.
[email protected]fb53e652014-04-30 11:27:19762 VLOG(1) << "Ignoring incompatible component: " << crx->id;
[email protected]61aca4cd2013-10-26 10:50:59763 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]cf442612011-08-09 20:20:12764 continue;
765 }
[email protected]e8f96ff2011-08-03 05:07:33766 }
[email protected]2cddef42013-11-22 08:23:22767
768 if (it->manifest.packages.size() != 1) {
769 // Assume one and only one package per component.
[email protected]fb53e652014-04-30 11:27:19770 VLOG(1) << "Ignoring multiple packages for component: " << crx->id;
[email protected]2cddef42013-11-22 08:23:22771 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
772 continue;
773 }
774
775 // Parse the members of the result and queue an upgrade for this component.
[email protected]c5e4a2222014-01-03 16:06:13776 crx->next_version = Version(it->manifest.version);
[email protected]2cddef42013-11-22 08:23:22777
[email protected]fb53e652014-04-30 11:27:19778 VLOG(1) << "Update found for component: " << crx->id;
779
[email protected]055981f2014-01-17 20:22:32780 typedef UpdateResponse::Result::Manifest::Package Package;
[email protected]2cddef42013-11-22 08:23:22781 const Package& package(it->manifest.packages[0]);
782 crx->next_fp = package.fingerprint;
783
[email protected]da37c1d2013-12-19 01:04:38784 // Resolve the urls by combining the base urls with the package names.
785 for (size_t i = 0; i != it->crx_urls.size(); ++i) {
786 const GURL url(it->crx_urls[i].Resolve(package.name));
787 if (url.is_valid())
788 crx->crx_urls.push_back(url);
789 }
790 for (size_t i = 0; i != it->crx_diffurls.size(); ++i) {
791 const GURL url(it->crx_diffurls[i].Resolve(package.namediff));
792 if (url.is_valid())
793 crx->crx_diffurls.push_back(url);
794 }
[email protected]2cddef42013-11-22 08:23:22795
[email protected]61aca4cd2013-10-26 10:50:59796 ChangeItemState(crx, CrxUpdateItem::kCanUpdate);
[email protected]2cddef42013-11-22 08:23:22797 ++num_updates_pending;
[email protected]e8f96ff2011-08-03 05:07:33798 }
[email protected]cf442612011-08-09 20:20:12799
[email protected]2cddef42013-11-22 08:23:22800 // All components that are not included in the update response are
801 // considered up to date.
[email protected]cf442612011-08-09 20:20:12802 ChangeItemStatus(CrxUpdateItem::kChecking, CrxUpdateItem::kUpToDate);
803
[email protected]32a6c8382013-08-20 00:29:20804 // If there are updates pending we do a short wait, otherwise we take
805 // a longer delay until we check the components again.
[email protected]21a9c9a2014-02-19 19:37:11806 ScheduleNextRun(num_updates_pending > 0 ? kStepDelayShort : kStepDelayLong);
[email protected]e8f96ff2011-08-03 05:07:33807}
808
[email protected]93e8e2c2014-01-04 12:29:23809void CrxUpdateService::OnUpdateCheckFailed(int error,
810 const std::string& error_message) {
[email protected]e8f96ff2011-08-03 05:07:33811 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23812 DCHECK(error);
[email protected]e8f96ff2011-08-03 05:07:33813 size_t count = ChangeItemStatus(CrxUpdateItem::kChecking,
814 CrxUpdateItem::kNoUpdate);
815 DCHECK_GT(count, 0ul);
[email protected]fb53e652014-04-30 11:27:19816 VLOG(1) << "Update check failed.";
[email protected]32a6c8382013-08-20 00:29:20817 ScheduleNextRun(kStepDelayLong);
[email protected]e8f96ff2011-08-03 05:07:33818}
819
[email protected]8a5ebd432014-05-02 00:21:22820// Called when progress is being made downloading a CRX. The progress may
821// not monotonically increase due to how the CRX downloader switches between
822// different downloaders and fallback urls.
823void CrxUpdateService::DownloadProgress(
824 const std::string& component_id,
825 const CrxDownloader::Result& download_result) {
826 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
827 NotifyObservers(Observer::COMPONENT_UPDATE_DOWNLOADING, component_id);
828}
829
[email protected]e8f96ff2011-08-03 05:07:33830// Called when the CRX package has been downloaded to a temporary location.
831// Here we fire the notifications and schedule the component-specific installer
832// to be called in the file thread.
[email protected]3cb2a4f2013-12-07 21:54:34833void CrxUpdateService::DownloadComplete(
834 scoped_ptr<CRXContext> crx_context,
[email protected]3a0092d2013-12-18 03:04:35835 const CrxDownloader::Result& download_result) {
[email protected]e8f96ff2011-08-03 05:07:33836 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]64f39fa12013-09-03 21:49:37837
[email protected]64f39fa12013-09-03 21:49:37838 CrxUpdateItem* crx = FindUpdateItemById(crx_context->id);
[email protected]e3e696d32013-06-21 20:41:36839 DCHECK(crx->status == CrxUpdateItem::kDownloadingDiff ||
840 crx->status == CrxUpdateItem::kDownloading);
841
[email protected]3a0092d2013-12-18 03:04:35842 AppendDownloadMetrics(crx_downloader_->download_metrics(),
843 &crx->download_metrics);
844
[email protected]8a5ebd432014-05-02 00:21:22845 crx_downloader_.reset();
846
[email protected]3cb2a4f2013-12-07 21:54:34847 if (download_result.error) {
[email protected]e3e696d32013-06-21 20:41:36848 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
[email protected]7b0529242013-07-20 05:45:46849 crx->diff_error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34850 crx->diff_error_code = download_result.error;
[email protected]e630ba62013-06-22 15:22:34851 crx->diff_update_failed = true;
[email protected]e3e696d32013-06-21 20:41:36852 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
853 CrxUpdateItem::kCanUpdate);
854 DCHECK_EQ(count, 1ul);
[email protected]7b0529242013-07-20 05:45:46855
[email protected]32a6c8382013-08-20 00:29:20856 ScheduleNextRun(kStepDelayShort);
[email protected]e3e696d32013-06-21 20:41:36857 return;
858 }
[email protected]7b0529242013-07-20 05:45:46859 crx->error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34860 crx->error_code = download_result.error;
[email protected]e8f96ff2011-08-03 05:07:33861 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloading,
862 CrxUpdateItem::kNoUpdate);
863 DCHECK_EQ(count, 1ul);
[email protected]e3e696d32013-06-21 20:41:36864
[email protected]7b0529242013-07-20 05:45:46865 // At this point, since both the differential and the full downloads failed,
866 // the update for this component has finished with an error.
867 ping_manager_->OnUpdateComplete(crx);
868
[email protected]32a6c8382013-08-20 00:29:20869 // Move on to the next update, if there is one available.
870 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33871 } else {
[email protected]e3e696d32013-06-21 20:41:36872 size_t count = 0;
873 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
874 count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
875 CrxUpdateItem::kUpdatingDiff);
876 } else {
877 count = ChangeItemStatus(CrxUpdateItem::kDownloading,
878 CrxUpdateItem::kUpdating);
879 }
[email protected]e8f96ff2011-08-03 05:07:33880 DCHECK_EQ(count, 1ul);
[email protected]cf442612011-08-09 20:20:12881
[email protected]44da56e2011-11-21 19:59:14882 // Why unretained? See comment at top of file.
[email protected]8f5f2ea2013-10-31 09:39:10883 blocking_task_runner_->PostDelayedTask(
[email protected]73251e72012-03-04 02:10:33884 FROM_HERE,
[email protected]44da56e2011-11-21 19:59:14885 base::Bind(&CrxUpdateService::Install,
886 base::Unretained(this),
[email protected]afa378f22013-12-02 03:37:54887 base::Passed(&crx_context),
[email protected]3cb2a4f2013-12-07 21:54:34888 download_result.response),
[email protected]73251e72012-03-04 02:10:33889 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]e8f96ff2011-08-03 05:07:33890 }
[email protected]e8f96ff2011-08-03 05:07:33891}
892
893// Install consists of digital signature verification, unpacking and then
894// calling the component specific installer. All that is handled by the
[email protected]f5d27e32014-01-31 06:48:53895// |unpacker_|. If there is an error this function is in charge of deleting
[email protected]e8f96ff2011-08-03 05:07:33896// the files created.
[email protected]afa378f22013-12-02 03:37:54897void CrxUpdateService::Install(scoped_ptr<CRXContext> context,
[email protected]650b2d52013-02-10 03:41:45898 const base::FilePath& crx_path) {
[email protected]8f5f2ea2013-10-31 09:39:10899 // This function owns the file at |crx_path| and the |context| object.
[email protected]94a481b2014-03-28 19:41:55900 unpacker_ = new ComponentUnpacker(context->pk_hash,
901 crx_path,
902 context->fingerprint,
903 context->installer,
904 config_->InProcess(),
905 blocking_task_runner_);
[email protected]f5d27e32014-01-31 06:48:53906 unpacker_->Unpack(base::Bind(&CrxUpdateService::EndUnpacking,
907 base::Unretained(this),
908 context->id,
909 crx_path));
910}
911
912void CrxUpdateService::EndUnpacking(const std::string& component_id,
913 const base::FilePath& crx_path,
914 ComponentUnpacker::Error error,
915 int extended_error) {
[email protected]055981f2014-01-17 20:22:32916 if (!DeleteFileAndEmptyParentDirectory(crx_path))
[email protected]e8f96ff2011-08-03 05:07:33917 NOTREACHED() << crx_path.value();
[email protected]73251e72012-03-04 02:10:33918 BrowserThread::PostDelayedTask(
919 BrowserThread::UI,
920 FROM_HERE,
[email protected]44da56e2011-11-21 19:59:14921 base::Bind(&CrxUpdateService::DoneInstalling, base::Unretained(this),
[email protected]f5d27e32014-01-31 06:48:53922 component_id, error, extended_error),
[email protected]73251e72012-03-04 02:10:33923 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]94a481b2014-03-28 19:41:55924 // Reset the unpacker last, otherwise we free our own arguments.
925 unpacker_ = NULL;
[email protected]e8f96ff2011-08-03 05:07:33926}
927
928// Installation has been completed. Adjust the component status and
[email protected]7b0529242013-07-20 05:45:46929// schedule the next check. Schedule a short delay before trying the full
930// update when the differential update failed.
[email protected]07f93af12011-08-17 20:57:22931void CrxUpdateService::DoneInstalling(const std::string& component_id,
[email protected]e3e696d32013-06-21 20:41:36932 ComponentUnpacker::Error error,
933 int extra_code) {
[email protected]e8f96ff2011-08-03 05:07:33934 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]07f93af12011-08-17 20:57:22935
[email protected]7b0529242013-07-20 05:45:46936 ErrorCategory error_category = kErrorNone;
937 switch (error) {
938 case ComponentUnpacker::kNone:
939 break;
940 case ComponentUnpacker::kInstallerError:
941 error_category = kInstallError;
942 break;
943 default:
944 error_category = kUnpackError;
945 break;
946 }
947
948 const bool is_success = error == ComponentUnpacker::kNone;
949
[email protected]07f93af12011-08-17 20:57:22950 CrxUpdateItem* item = FindUpdateItemById(component_id);
[email protected]7b0529242013-07-20 05:45:46951 if (item->status == CrxUpdateItem::kUpdatingDiff && !is_success) {
952 item->diff_error_category = error_category;
953 item->diff_error_code = error;
954 item->diff_extra_code1 = extra_code;
[email protected]1ea21ad2013-09-02 04:20:39955 item->diff_update_failed = true;
956 size_t count = ChangeItemStatus(CrxUpdateItem::kUpdatingDiff,
957 CrxUpdateItem::kCanUpdate);
958 DCHECK_EQ(count, 1ul);
959 ScheduleNextRun(kStepDelayShort);
960 return;
[email protected]e3e696d32013-06-21 20:41:36961 }
[email protected]e3e696d32013-06-21 20:41:36962
[email protected]7b0529242013-07-20 05:45:46963 if (is_success) {
[email protected]61aca4cd2013-10-26 10:50:59964 ChangeItemState(item, CrxUpdateItem::kUpdated);
[email protected]07f93af12011-08-17 20:57:22965 item->component.version = item->next_version;
[email protected]e3e696d32013-06-21 20:41:36966 item->component.fingerprint = item->next_fp;
[email protected]7b0529242013-07-20 05:45:46967 } else {
[email protected]61aca4cd2013-10-26 10:50:59968 ChangeItemState(item, CrxUpdateItem::kNoUpdate);
[email protected]7b0529242013-07-20 05:45:46969 item->error_category = error_category;
970 item->error_code = error;
971 item->extra_code1 = extra_code;
[email protected]e3e696d32013-06-21 20:41:36972 }
[email protected]07f93af12011-08-17 20:57:22973
[email protected]7b0529242013-07-20 05:45:46974 ping_manager_->OnUpdateComplete(item);
[email protected]360b8bb2011-09-01 21:48:06975
[email protected]32a6c8382013-08-20 00:29:20976 // Move on to the next update, if there is one available.
977 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33978}
979
[email protected]d3268fe2014-04-25 02:14:23980void CrxUpdateService::NotifyObservers(Observer::Events event,
981 const std::string& id) {
982 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
983 FOR_EACH_OBSERVER(Observer, observer_list_, OnEvent(event, id));
[email protected]85e61d52013-08-01 22:23:42984}
985
[email protected]00a77fa2013-11-02 04:18:46986content::ResourceThrottle* CrxUpdateService::GetOnDemandResourceThrottle(
987 net::URLRequest* request, const std::string& crx_id) {
988 // We give the raw pointer to the caller, who will delete it at will
989 // and we keep for ourselves a weak pointer to it so we can post tasks
990 // from the UI thread without having to track lifetime directly.
991 CUResourceThrottle* rt = new CUResourceThrottle(request);
992 BrowserThread::PostTask(
993 BrowserThread::UI,
994 FROM_HERE,
995 base::Bind(&CrxUpdateService::OnNewResourceThrottle,
996 base::Unretained(this),
997 rt->AsWeakPtr(),
998 crx_id));
999 return rt;
1000}
1001
1002void CrxUpdateService::OnNewResourceThrottle(
1003 base::WeakPtr<CUResourceThrottle> rt, const std::string& crx_id) {
1004 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1005 // Check if we can on-demand update, else unblock the request anyway.
1006 CrxUpdateItem* item = FindUpdateItemById(crx_id);
1007 Status status = OnDemandUpdateInternal(item);
1008 if (status == kOk || status == kInProgress) {
1009 item->throttles.push_back(rt);
1010 return;
1011 }
1012 UnblockResourceThrottle(rt);
1013}
1014
1015///////////////////////////////////////////////////////////////////////////////
1016
1017CUResourceThrottle::CUResourceThrottle(const net::URLRequest* request)
1018 : state_(NEW) {
1019 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1020}
1021
1022CUResourceThrottle::~CUResourceThrottle() {
1023 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1024}
1025
1026void CUResourceThrottle::WillStartRequest(bool* defer) {
1027 if (state_ != UNBLOCKED) {
1028 state_ = BLOCKED;
1029 *defer = true;
1030 } else {
1031 *defer = false;
1032 }
1033}
1034
1035void CUResourceThrottle::WillRedirectRequest(const GURL& new_url, bool* defer) {
1036 WillStartRequest(defer);
1037}
1038
[email protected]f8fe5cf2013-12-04 20:11:531039const char* CUResourceThrottle::GetNameForLogging() const {
1040 return "ComponentUpdateResourceThrottle";
1041}
1042
[email protected]00a77fa2013-11-02 04:18:461043void CUResourceThrottle::Unblock() {
1044 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1045 if (state_ == BLOCKED)
1046 controller()->Resume();
1047 state_ = UNBLOCKED;
1048}
1049
[email protected]e8f96ff2011-08-03 05:07:331050// The component update factory. Using the component updater as a singleton
1051// is the job of the browser process.
1052ComponentUpdateService* ComponentUpdateServiceFactory(
1053 ComponentUpdateService::Configurator* config) {
1054 DCHECK(config);
1055 return new CrxUpdateService(config);
1056}
[email protected]2cddef42013-11-22 08:23:221057
[email protected]055981f2014-01-17 20:22:321058} // namespace component_updater