blob: de0b90a5f5572061a3bf6c8fa52241ec5a3cb30d [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]e8f96ff2011-08-03 05:07:3319#include "base/stl_util.h"
[email protected]8f5f2ea2013-10-31 09:39:1020#include "base/threading/sequenced_worker_pool.h"
[email protected]41a17c52013-06-28 00:27:5321#include "base/timer/timer.h"
[email protected]e8f96ff2011-08-03 05:07:3322#include "chrome/browser/browser_process.h"
[email protected]e3e696d32013-06-21 20:41:3623#include "chrome/browser/component_updater/component_patcher.h"
[email protected]e8f96ff2011-08-03 05:07:3324#include "chrome/browser/component_updater/component_unpacker.h"
[email protected]7b0529242013-07-20 05:45:4625#include "chrome/browser/component_updater/component_updater_ping_manager.h"
[email protected]2cddef42013-11-22 08:23:2226#include "chrome/browser/component_updater/component_updater_utils.h"
[email protected]afa378f22013-12-02 03:37:5427#include "chrome/browser/component_updater/crx_downloader.h"
[email protected]7b0529242013-07-20 05:45:4628#include "chrome/browser/component_updater/crx_update_item.h"
[email protected]93e8e2c2014-01-04 12:29:2329#include "chrome/browser/component_updater/update_checker.h"
[email protected]6268d3a2013-11-27 01:28:0930#include "chrome/browser/component_updater/update_response.h"
[email protected]e8f96ff2011-08-03 05:07:3331#include "chrome/common/chrome_version_info.h"
[email protected]b7b63872013-01-03 02:41:1932#include "content/public/browser/browser_thread.h"
[email protected]00a77fa2013-11-02 04:18:4633#include "content/public/browser/resource_controller.h"
34#include "content/public/browser/resource_throttle.h"
[email protected]761fa4702013-07-02 15:25:1535#include "url/gurl.h"
[email protected]e8f96ff2011-08-03 05:07:3336
[email protected]631bb742011-11-02 11:29:3937using content::BrowserThread;
38
[email protected]055981f2014-01-17 20:22:3239namespace component_updater {
[email protected]3a0092d2013-12-18 03:04:3540
[email protected]44da56e2011-11-21 19:59:1441// The component updater is designed to live until process shutdown, so
42// base::Bind() calls are not refcounted.
43
[email protected]e8f96ff2011-08-03 05:07:3344namespace {
[email protected]e3e696d32013-06-21 20:41:3645
[email protected]2cddef42013-11-22 08:23:2246// Returns true if the |proposed| version is newer than |current| version.
[email protected]c5e4a2222014-01-03 16:06:1347bool IsVersionNewer(const Version& current, const std::string& proposed) {
48 Version proposed_ver(proposed);
[email protected]2cddef42013-11-22 08:23:2249 return proposed_ver.IsValid() && current.CompareTo(proposed_ver) < 0;
[email protected]e8f96ff2011-08-03 05:07:3350}
51
[email protected]e3e696d32013-06-21 20:41:3652// Returns true if a differential update is available, it has not failed yet,
53// and the configuration allows it.
54bool CanTryDiffUpdate(const CrxUpdateItem* update_item,
55 const ComponentUpdateService::Configurator& config) {
[email protected]055981f2014-01-17 20:22:3256 return HasDiffUpdate(update_item) &&
[email protected]e3e696d32013-06-21 20:41:3657 !update_item->diff_update_failed &&
58 config.DeltasEnabled();
59}
60
[email protected]3a0092d2013-12-18 03:04:3561void 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]7b0529242013-07-20 05:45:4667} // namespace
68
69CrxUpdateItem::CrxUpdateItem()
70 : status(kNew),
[email protected]61aca4cd2013-10-26 10:50:5971 on_demand(false),
[email protected]7b0529242013-07-20 05:45:4672 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
81CrxUpdateItem::~CrxUpdateItem() {
82}
[email protected]86550a42013-06-21 15:20:4983
[email protected]dc06f0b2013-01-23 20:03:1684CrxComponent::CrxComponent()
[email protected]85e61d52013-08-01 22:23:4285 : installer(NULL),
86 observer(NULL) {
[email protected]dc06f0b2013-01-23 20:03:1687}
88
89CrxComponent::~CrxComponent() {
90}
[email protected]e8f96ff2011-08-03 05:07:3391
[email protected]2e919ddd2013-08-21 05:05:1792CrxComponentInfo::CrxComponentInfo() {
93}
94
95CrxComponentInfo::~CrxComponentInfo() {
96}
97
[email protected]00a77fa2013-11-02 04:18:4698///////////////////////////////////////////////////////////////////////////////
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.
105class CUResourceThrottle
106 : public content::ResourceThrottle,
107 public base::SupportsWeakPtr<CUResourceThrottle> {
108 public:
109 explicit CUResourceThrottle(const net::URLRequest* request);
110 virtual ~CUResourceThrottle();
[email protected]2cddef42013-11-22 08:23:22111
[email protected]00a77fa2013-11-02 04:18:46112 // Overriden from ResourceThrottle.
113 virtual void WillStartRequest(bool* defer) OVERRIDE;
114 virtual void WillRedirectRequest(const GURL& new_url, bool* defer) OVERRIDE;
[email protected]f8fe5cf2013-12-04 20:11:53115 virtual const char* GetNameForLogging() const OVERRIDE;
[email protected]00a77fa2013-11-02 04:18:46116
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]2cddef42013-11-22 08:23:22123 enum State {
124 NEW,
125 BLOCKED,
126 UNBLOCKED
127 };
[email protected]00a77fa2013-11-02 04:18:46128
[email protected]2cddef42013-11-22 08:23:22129 State state_;
[email protected]00a77fa2013-11-02 04:18:46130};
131
132void UnblockResourceThrottle(base::WeakPtr<CUResourceThrottle> rt) {
133 BrowserThread::PostTask(
134 BrowserThread::IO,
135 FROM_HERE,
136 base::Bind(&CUResourceThrottle::Unblock, rt));
137}
138
139void 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]e8f96ff2011-08-03 05:07:33146//////////////////////////////////////////////////////////////////////////////
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]50b01392012-09-01 03:33:13149// is called periodically to do the upgrades/installs or the update checks.
[email protected]e8f96ff2011-08-03 05:07:33150// 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]f1050432012-02-15 01:35:46152// eligible for update, we only do one thing at a time with pauses in between
[email protected]e8f96ff2011-08-03 05:07:33153// the tasks. Also when we do network requests there is only one |url_fetcher_|
[email protected]e3e696d32013-06-21 20:41:36154// in flight at a time.
[email protected]e8f96ff2011-08-03 05:07:33155// 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.
159class CrxUpdateService : public ComponentUpdateService {
160 public:
161 explicit CrxUpdateService(ComponentUpdateService::Configurator* config);
[email protected]e8f96ff2011-08-03 05:07:33162 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]61aca4cd2013-10-26 10:50:59168 virtual Status OnDemandUpdate(const std::string& component_id) OVERRIDE;
[email protected]2e919ddd2013-08-21 05:05:17169 virtual void GetComponents(
170 std::vector<CrxComponentInfo>* components) OVERRIDE;
[email protected]00a77fa2013-11-02 04:18:46171 virtual content::ResourceThrottle* GetOnDemandResourceThrottle(
172 net::URLRequest* request, const std::string& crx_id) OVERRIDE;
[email protected]e8f96ff2011-08-03 05:07:33173
[email protected]93e8e2c2014-01-04 12:29:23174 // 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]e8f96ff2011-08-03 05:07:33182
[email protected]e8f96ff2011-08-03 05:07:33183 private:
[email protected]7b0529242013-07-20 05:45:46184 enum ErrorCategory {
185 kErrorNone = 0,
186 kNetworkError,
187 kUnpackError,
188 kInstallError,
189 };
190
[email protected]32a6c8382013-08-20 00:29:20191 enum StepDelayInterval {
192 kStepDelayShort = 0,
193 kStepDelayMedium,
194 kStepDelayLong,
195 };
196
[email protected]055981f2014-01-17 20:22:32197 void UpdateCheckComplete(int error,
198 const std::string& error_message,
199 const UpdateResponse::Results& results);
200 void OnUpdateCheckSucceeded(const UpdateResponse::Results& results);
[email protected]93e8e2c2014-01-04 12:29:23201 void OnUpdateCheckFailed(int error, const std::string& error_message);
[email protected]e8f96ff2011-08-03 05:07:33202
[email protected]3cb2a4f2013-12-07 21:54:34203 void DownloadComplete(
204 scoped_ptr<CRXContext> crx_context,
[email protected]3a0092d2013-12-18 03:04:35205 const CrxDownloader::Result& download_result);
[email protected]afa378f22013-12-02 03:37:54206
[email protected]00a77fa2013-11-02 04:18:46207 Status OnDemandUpdateInternal(CrxUpdateItem* item);
208
[email protected]e8f96ff2011-08-03 05:07:33209 void ProcessPendingItems();
210
[email protected]93e8e2c2014-01-04 12:29:23211 // 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]61aca4cd2013-10-26 10:50:59218
219 void UpdateComponent(CrxUpdateItem* workitem);
220
[email protected]32a6c8382013-08-20 00:29:20221 void ScheduleNextRun(StepDelayInterval step_delay);
[email protected]e8f96ff2011-08-03 05:07:33222
[email protected]6268d3a2013-11-27 01:28:09223 void ParseResponse(const std::string& xml);
[email protected]e8f96ff2011-08-03 05:07:33224
[email protected]afa378f22013-12-02 03:37:54225 void Install(scoped_ptr<CRXContext> context, const base::FilePath& crx_path);
[email protected]e8f96ff2011-08-03 05:07:33226
[email protected]07f93af12011-08-17 20:57:22227 void DoneInstalling(const std::string& component_id,
[email protected]e3e696d32013-06-21 20:41:36228 ComponentUnpacker::Error error,
229 int extended_error);
[email protected]e8f96ff2011-08-03 05:07:33230
[email protected]61aca4cd2013-10-26 10:50:59231 void ChangeItemState(CrxUpdateItem* item, CrxUpdateItem::Status to);
232
[email protected]e8f96ff2011-08-03 05:07:33233 size_t ChangeItemStatus(CrxUpdateItem::Status from,
234 CrxUpdateItem::Status to);
235
236 CrxUpdateItem* FindUpdateItemById(const std::string& id);
237
[email protected]85e61d52013-08-01 22:23:42238 void NotifyComponentObservers(ComponentObserver::Events event,
239 int extra) const;
240
[email protected]61aca4cd2013-10-26 10:50:59241 bool HasOnDemandItems() const;
242
[email protected]00a77fa2013-11-02 04:18:46243 void OnNewResourceThrottle(base::WeakPtr<CUResourceThrottle> rt,
244 const std::string& crx_id);
245
[email protected]e3e696d32013-06-21 20:41:36246 scoped_ptr<ComponentUpdateService::Configurator> config_;
247
248 scoped_ptr<ComponentPatcher> component_patcher_;
[email protected]e8f96ff2011-08-03 05:07:33249
[email protected]055981f2014-01-17 20:22:32250 scoped_ptr<UpdateChecker> update_checker_;
[email protected]e8f96ff2011-08-03 05:07:33251
[email protected]055981f2014-01-17 20:22:32252 scoped_ptr<PingManager> ping_manager_;
[email protected]7b0529242013-07-20 05:45:46253
[email protected]055981f2014-01-17 20:22:32254 scoped_ptr<CrxDownloader> crx_downloader_;
[email protected]afa378f22013-12-02 03:37:54255
[email protected]86550a42013-06-21 15:20:49256 // A collection of every work item.
[email protected]e3e696d32013-06-21 20:41:36257 typedef std::vector<CrxUpdateItem*> UpdateItems;
[email protected]e8f96ff2011-08-03 05:07:33258 UpdateItems work_items_;
259
260 base::OneShotTimer<CrxUpdateService> timer_;
261
[email protected]8f5f2ea2013-10-31 09:39:10262 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
263
[email protected]c5e4a2222014-01-03 16:06:13264 const Version chrome_version_;
[email protected]e8f96ff2011-08-03 05:07:33265
266 bool running_;
267
268 DISALLOW_COPY_AND_ASSIGN(CrxUpdateService);
269};
270
[email protected]e8f96ff2011-08-03 05:07:33271//////////////////////////////////////////////////////////////////////////////
272
[email protected]e3e696d32013-06-21 20:41:36273CrxUpdateService::CrxUpdateService(ComponentUpdateService::Configurator* config)
[email protected]e8f96ff2011-08-03 05:07:33274 : config_(config),
[email protected]e3e696d32013-06-21 20:41:36275 component_patcher_(config->CreateComponentPatcher()),
[email protected]055981f2014-01-17 20:22:32276 ping_manager_(new PingManager(config->PingUrl(),
277 config->RequestContext())),
[email protected]8f5f2ea2013-10-31 09:39:10278 blocking_task_runner_(BrowserThread::GetBlockingPool()->
279 GetSequencedTaskRunnerWithShutdownBehavior(
280 BrowserThread::GetBlockingPool()->GetSequenceToken(),
281 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)),
[email protected]e8f96ff2011-08-03 05:07:33282 chrome_version_(chrome::VersionInfo().Version()),
283 running_(false) {
[email protected]1ea21ad2013-09-02 04:20:39284}
[email protected]e8f96ff2011-08-03 05:07:33285
286CrxUpdateService::~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]1ea21ad2013-09-02 04:20:39292}
[email protected]e8f96ff2011-08-03 05:07:33293
294ComponentUpdateService::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]85e61d52013-08-01 22:23:42302 NotifyComponentObservers(ComponentObserver::COMPONENT_UPDATER_STARTED, 0);
303
[email protected]d323a172011-09-02 18:23:02304 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(config_->InitialDelay()),
[email protected]e8f96ff2011-08-03 05:07:33305 this, &CrxUpdateService::ProcessPendingItems);
306 return kOk;
307}
308
309// Stop the main check + update loop. In flight operations will be
310// completed.
311ComponentUpdateService::Status CrxUpdateService::Stop() {
312 running_ = false;
313 timer_.Stop();
314 return kOk;
315}
316
[email protected]61aca4cd2013-10-26 10:50:59317bool 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]ccb4feef2013-02-14 06:16:47329// This function sets the timer which will call ProcessPendingItems() or
[email protected]61aca4cd2013-10-26 10:50:59330// ProcessRequestedItem() if there is an on_demand item. There
[email protected]32a6c8382013-08-20 00:29:20331// 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.
337void CrxUpdateService::ScheduleNextRun(StepDelayInterval step_delay) {
[email protected]e8f96ff2011-08-03 05:07:33338 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23339 DCHECK(!update_checker_);
[email protected]e8f96ff2011-08-03 05:07:33340 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]ccb4feef2013-02-14 06:16:47347 // 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]32a6c8382013-08-20 00:29:20349 int64 delay_seconds = 0;
[email protected]61aca4cd2013-10-26 10:50:59350 if (!HasOnDemandItems()) {
[email protected]32a6c8382013-08-20 00:29:20351 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]cf442612011-08-09 20:20:12365
[email protected]32a6c8382013-08-20 00:29:20366 if (step_delay != kStepDelayShort) {
[email protected]85e61d52013-08-01 22:23:42367 NotifyComponentObservers(ComponentObserver::COMPONENT_UPDATER_SLEEPING, 0);
368
[email protected]e8f96ff2011-08-03 05:07:33369 // Zero is only used for unit tests.
[email protected]32a6c8382013-08-20 00:29:20370 if (0 == delay_seconds)
[email protected]e8f96ff2011-08-03 05:07:33371 return;
372 }
373
[email protected]32a6c8382013-08-20 00:29:20374 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(delay_seconds),
[email protected]e8f96ff2011-08-03 05:07:33375 this, &CrxUpdateService::ProcessPendingItems);
376}
377
378// Given a extension-like component id, find the associated component.
379CrxUpdateItem* 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]93e8e2c2014-01-04 12:29:23385 return it != work_items_.end() ? *it : NULL;
[email protected]e8f96ff2011-08-03 05:07:33386}
387
[email protected]61aca4cd2013-10-26 10:50:59388// 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?
392void 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]00a77fa2013-11-02 04:18:46429
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]61aca4cd2013-10-26 10:50:59436}
437
[email protected]e8f96ff2011-08-03 05:07:33438// Changes all the components in |work_items_| that have |from| status to
[email protected]e3e696d32013-06-21 20:41:36439// |to| status and returns how many have been changed.
[email protected]e8f96ff2011-08-03 05:07:33440size_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]93e8e2c2014-01-04 12:29:23447 if (item->status == from) {
448 ChangeItemState(item, to);
449 ++count;
450 }
[email protected]e8f96ff2011-08-03 05:07:33451 }
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]e8f96ff2011-08-03 05:07:33457ComponentUpdateService::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]055981f2014-01-17 20:22:32465 std::string id(GetCrxComponentID(component));
[email protected]93e8e2c2014-01-04 12:29:23466 CrxUpdateItem* uit = FindUpdateItemById(id);
[email protected]e8f96ff2011-08-03 05:07:33467 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]e3e696d32013-06-21 20:41:36475
[email protected]e8f96ff2011-08-03 05:07:33476 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]ccb4feef2013-02-14 06:16:47485// Start the process of checking for an update, for a particular component
486// that was previously registered.
[email protected]2e919ddd2013-08-21 05:05:17487// |component_id| is a value returned from GetCrxComponentID().
[email protected]61aca4cd2013-10-26 10:50:59488ComponentUpdateService::Status CrxUpdateService::OnDemandUpdate(
[email protected]2e919ddd2013-08-21 05:05:17489 const std::string& component_id) {
[email protected]00a77fa2013-11-02 04:18:46490 return OnDemandUpdateInternal(FindUpdateItemById(component_id));
491}
492
493ComponentUpdateService::Status CrxUpdateService::OnDemandUpdateInternal(
494 CrxUpdateItem* uit) {
[email protected]ccb4feef2013-02-14 06:16:47495 if (!uit)
496 return kError;
[email protected]2cddef42013-11-22 08:23:22497
[email protected]ccb4feef2013-02-14 06:16:47498 // Check if the request is too soon.
499 base::TimeDelta delta = base::Time::Now() - uit->last_check;
[email protected]e3e696d32013-06-21 20:41:36500 if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay()))
[email protected]ccb4feef2013-02-14 06:16:47501 return kError;
[email protected]ccb4feef2013-02-14 06:16:47502
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]e3e696d32013-06-21 20:41:36508 case CrxUpdateItem::kDownloadingDiff:
[email protected]ccb4feef2013-02-14 06:16:47509 case CrxUpdateItem::kDownloading:
[email protected]e3e696d32013-06-21 20:41:36510 case CrxUpdateItem::kUpdatingDiff:
[email protected]ccb4feef2013-02-14 06:16:47511 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]61aca4cd2013-10-26 10:50:59519 ChangeItemState(uit, CrxUpdateItem::kNew);
520 uit->on_demand = true;
[email protected]ccb4feef2013-02-14 06:16:47521 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]2e919ddd2013-08-21 05:05:17537void 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]055981f2014-01-17 20:22:32544 info.id = GetCrxComponentID(item->component);
[email protected]2e919ddd2013-08-21 05:05:17545 info.version = item->component.version.GetString();
546 info.name = item->component.name;
547 components->push_back(info);
548 }
549}
550
[email protected]93e8e2c2014-01-04 12:29:23551// 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]e8f96ff2011-08-03 05:07:33554void CrxUpdateService::ProcessPendingItems() {
555 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23556
[email protected]61aca4cd2013-10-26 10:50:59557 CrxUpdateItem* ready_upgrade = FindReadyComponent();
558 if (ready_upgrade) {
559 UpdateComponent(ready_upgrade);
[email protected]e8f96ff2011-08-03 05:07:33560 return;
561 }
[email protected]93e8e2c2014-01-04 12:29:23562
563 if (!CheckForUpdates())
564 ScheduleNextRun(kStepDelayLong);
[email protected]61aca4cd2013-10-26 10:50:59565}
566
[email protected]93e8e2c2014-01-04 12:29:23567CrxUpdateItem* CrxUpdateService::FindReadyComponent() const {
[email protected]61aca4cd2013-10-26 10:50:59568 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]93e8e2c2014-01-04 12:29:23578 std::vector<CrxUpdateItem*>::const_iterator it = std::find_if(
[email protected]61aca4cd2013-10-26 10:50:59579 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]93e8e2c2014-01-04 12:29:23588// Prepares the components for an update check and initiates the request.
589bool 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]055981f2014-01-17 20:22:32625 update_checker_ = UpdateChecker::Create(
[email protected]93e8e2c2014-01-04 12:29:23626 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]61aca4cd2013-10-26 10:50:59634void CrxUpdateService::UpdateComponent(CrxUpdateItem* workitem) {
[email protected]afa378f22013-12-02 03:37:54635 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]da37c1d2013-12-19 01:04:38640 const std::vector<GURL>* urls = NULL;
[email protected]61aca4cd2013-10-26 10:50:59641 if (CanTryDiffUpdate(workitem, *config_)) {
[email protected]da37c1d2013-12-19 01:04:38642 urls = &workitem->crx_diffurls;
[email protected]61aca4cd2013-10-26 10:50:59643 ChangeItemState(workitem, CrxUpdateItem::kDownloadingDiff);
644 } else {
[email protected]da37c1d2013-12-19 01:04:38645 urls = &workitem->crx_urls;
[email protected]61aca4cd2013-10-26 10:50:59646 ChangeItemState(workitem, CrxUpdateItem::kDownloading);
647 }
[email protected]3cb2a4f2013-12-07 21:54:34648
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]3a0092d2013-12-18 03:04:35653 crx_downloader_.reset(CrxDownloader::Create(
[email protected]3cb2a4f2013-12-07 21:54:34654 is_background_download,
[email protected]afa378f22013-12-02 03:37:54655 config_->RequestContext(),
656 blocking_task_runner_,
657 base::Bind(&CrxUpdateService::DownloadComplete,
658 base::Unretained(this),
659 base::Passed(&crx_context))));
[email protected]da37c1d2013-12-19 01:04:38660 crx_downloader_->StartDownload(*urls);
[email protected]61aca4cd2013-10-26 10:50:59661}
662
[email protected]93e8e2c2014-01-04 12:29:23663void CrxUpdateService::UpdateCheckComplete(
664 int error,
665 const std::string& error_message,
[email protected]055981f2014-01-17 20:22:32666 const UpdateResponse::Results& results) {
[email protected]e8f96ff2011-08-03 05:07:33667 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23668 update_checker_.reset();
669 if (!error)
670 OnUpdateCheckSucceeded(results);
[email protected]652f4272013-11-13 09:23:41671 else
[email protected]93e8e2c2014-01-04 12:29:23672 OnUpdateCheckFailed(error, error_message);
[email protected]e8f96ff2011-08-03 05:07:33673}
674
[email protected]93e8e2c2014-01-04 12:29:23675// 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.
680void CrxUpdateService::OnUpdateCheckSucceeded(
[email protected]055981f2014-01-17 20:22:32681 const UpdateResponse::Results& results) {
[email protected]2cddef42013-11-22 08:23:22682 size_t num_updates_pending = 0;
[email protected]e8f96ff2011-08-03 05:07:33683 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]055981f2014-01-17 20:22:32684 std::vector<UpdateResponse::Result>::const_iterator it;
[email protected]e8f96ff2011-08-03 05:07:33685 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]2cddef42013-11-22 08:23:22690 if (crx->status != CrxUpdateItem::kChecking) {
691 NOTREACHED();
[email protected]e8f96ff2011-08-03 05:07:33692 continue; // Not updating this component now.
[email protected]2cddef42013-11-22 08:23:22693 }
[email protected]e8f96ff2011-08-03 05:07:33694
[email protected]2cddef42013-11-22 08:23:22695 if (it->manifest.version.empty()) {
[email protected]cf442612011-08-09 20:20:12696 // No version means no update available.
[email protected]61aca4cd2013-10-26 10:50:59697 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]cf442612011-08-09 20:20:12698 continue;
[email protected]e8f96ff2011-08-03 05:07:33699 }
[email protected]2cddef42013-11-22 08:23:22700
701 if (!IsVersionNewer(crx->component.version, it->manifest.version)) {
702 // The component is up to date.
[email protected]61aca4cd2013-10-26 10:50:59703 ChangeItemState(crx, CrxUpdateItem::kUpToDate);
[email protected]cf442612011-08-09 20:20:12704 continue;
[email protected]e8f96ff2011-08-03 05:07:33705 }
[email protected]2cddef42013-11-22 08:23:22706
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]61aca4cd2013-10-26 10:50:59710 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]cf442612011-08-09 20:20:12711 continue;
712 }
[email protected]e8f96ff2011-08-03 05:07:33713 }
[email protected]2cddef42013-11-22 08:23:22714
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]c5e4a2222014-01-03 16:06:13722 crx->next_version = Version(it->manifest.version);
[email protected]2cddef42013-11-22 08:23:22723
[email protected]055981f2014-01-17 20:22:32724 typedef UpdateResponse::Result::Manifest::Package Package;
[email protected]2cddef42013-11-22 08:23:22725 const Package& package(it->manifest.packages[0]);
726 crx->next_fp = package.fingerprint;
727
[email protected]da37c1d2013-12-19 01:04:38728 // 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]2cddef42013-11-22 08:23:22739
[email protected]61aca4cd2013-10-26 10:50:59740 ChangeItemState(crx, CrxUpdateItem::kCanUpdate);
[email protected]2cddef42013-11-22 08:23:22741 ++num_updates_pending;
[email protected]e8f96ff2011-08-03 05:07:33742 }
[email protected]cf442612011-08-09 20:20:12743
[email protected]2cddef42013-11-22 08:23:22744 // All components that are not included in the update response are
745 // considered up to date.
[email protected]cf442612011-08-09 20:20:12746 ChangeItemStatus(CrxUpdateItem::kChecking, CrxUpdateItem::kUpToDate);
747
[email protected]32a6c8382013-08-20 00:29:20748 // 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]2cddef42013-11-22 08:23:22750 ScheduleNextRun(num_updates_pending > 0 ? kStepDelayShort : kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33751}
752
[email protected]93e8e2c2014-01-04 12:29:23753// TODO: record UMA stats.
754void CrxUpdateService::OnUpdateCheckFailed(int error,
755 const std::string& error_message) {
[email protected]e8f96ff2011-08-03 05:07:33756 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23757 DCHECK(error);
[email protected]e8f96ff2011-08-03 05:07:33758 size_t count = ChangeItemStatus(CrxUpdateItem::kChecking,
759 CrxUpdateItem::kNoUpdate);
760 DCHECK_GT(count, 0ul);
[email protected]32a6c8382013-08-20 00:29:20761 ScheduleNextRun(kStepDelayLong);
[email protected]e8f96ff2011-08-03 05:07:33762}
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]3cb2a4f2013-12-07 21:54:34767void CrxUpdateService::DownloadComplete(
768 scoped_ptr<CRXContext> crx_context,
[email protected]3a0092d2013-12-18 03:04:35769 const CrxDownloader::Result& download_result) {
[email protected]e8f96ff2011-08-03 05:07:33770 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]64f39fa12013-09-03 21:49:37771
[email protected]64f39fa12013-09-03 21:49:37772 CrxUpdateItem* crx = FindUpdateItemById(crx_context->id);
[email protected]e3e696d32013-06-21 20:41:36773 DCHECK(crx->status == CrxUpdateItem::kDownloadingDiff ||
774 crx->status == CrxUpdateItem::kDownloading);
775
[email protected]3a0092d2013-12-18 03:04:35776 AppendDownloadMetrics(crx_downloader_->download_metrics(),
777 &crx->download_metrics);
778
[email protected]3cb2a4f2013-12-07 21:54:34779 if (download_result.error) {
[email protected]e3e696d32013-06-21 20:41:36780 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
[email protected]7b0529242013-07-20 05:45:46781 crx->diff_error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34782 crx->diff_error_code = download_result.error;
[email protected]e630ba62013-06-22 15:22:34783 crx->diff_update_failed = true;
[email protected]e3e696d32013-06-21 20:41:36784 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
785 CrxUpdateItem::kCanUpdate);
786 DCHECK_EQ(count, 1ul);
[email protected]afa378f22013-12-02 03:37:54787 crx_downloader_.reset();
[email protected]7b0529242013-07-20 05:45:46788
[email protected]32a6c8382013-08-20 00:29:20789 ScheduleNextRun(kStepDelayShort);
[email protected]e3e696d32013-06-21 20:41:36790 return;
791 }
[email protected]7b0529242013-07-20 05:45:46792 crx->error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34793 crx->error_code = download_result.error;
[email protected]e8f96ff2011-08-03 05:07:33794 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloading,
795 CrxUpdateItem::kNoUpdate);
796 DCHECK_EQ(count, 1ul);
[email protected]afa378f22013-12-02 03:37:54797 crx_downloader_.reset();
[email protected]e3e696d32013-06-21 20:41:36798
[email protected]7b0529242013-07-20 05:45:46799 // 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]32a6c8382013-08-20 00:29:20803 // Move on to the next update, if there is one available.
804 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33805 } else {
[email protected]e3e696d32013-06-21 20:41:36806 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]e8f96ff2011-08-03 05:07:33814 DCHECK_EQ(count, 1ul);
[email protected]afa378f22013-12-02 03:37:54815 crx_downloader_.reset();
[email protected]cf442612011-08-09 20:20:12816
[email protected]44da56e2011-11-21 19:59:14817 // Why unretained? See comment at top of file.
[email protected]8f5f2ea2013-10-31 09:39:10818 blocking_task_runner_->PostDelayedTask(
[email protected]73251e72012-03-04 02:10:33819 FROM_HERE,
[email protected]44da56e2011-11-21 19:59:14820 base::Bind(&CrxUpdateService::Install,
821 base::Unretained(this),
[email protected]afa378f22013-12-02 03:37:54822 base::Passed(&crx_context),
[email protected]3cb2a4f2013-12-07 21:54:34823 download_result.response),
[email protected]73251e72012-03-04 02:10:33824 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]e8f96ff2011-08-03 05:07:33825 }
[email protected]e8f96ff2011-08-03 05:07:33826}
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]afa378f22013-12-02 03:37:54832void CrxUpdateService::Install(scoped_ptr<CRXContext> context,
[email protected]650b2d52013-02-10 03:41:45833 const base::FilePath& crx_path) {
[email protected]8f5f2ea2013-10-31 09:39:10834 // This function owns the file at |crx_path| and the |context| object.
[email protected]e3e696d32013-06-21 20:41:36835 ComponentUnpacker unpacker(context->pk_hash,
836 crx_path,
837 context->fingerprint,
838 component_patcher_.get(),
839 context->installer);
[email protected]055981f2014-01-17 20:22:32840 if (!DeleteFileAndEmptyParentDirectory(crx_path))
[email protected]e8f96ff2011-08-03 05:07:33841 NOTREACHED() << crx_path.value();
[email protected]2e2c5292013-12-17 03:48:40842
[email protected]44da56e2011-11-21 19:59:14843 // Why unretained? See comment at top of file.
[email protected]73251e72012-03-04 02:10:33844 BrowserThread::PostDelayedTask(
845 BrowserThread::UI,
846 FROM_HERE,
[email protected]44da56e2011-11-21 19:59:14847 base::Bind(&CrxUpdateService::DoneInstalling, base::Unretained(this),
[email protected]e3e696d32013-06-21 20:41:36848 context->id, unpacker.error(), unpacker.extended_error()),
[email protected]73251e72012-03-04 02:10:33849 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]e8f96ff2011-08-03 05:07:33850}
851
852// Installation has been completed. Adjust the component status and
[email protected]7b0529242013-07-20 05:45:46853// schedule the next check. Schedule a short delay before trying the full
854// update when the differential update failed.
[email protected]07f93af12011-08-17 20:57:22855void CrxUpdateService::DoneInstalling(const std::string& component_id,
[email protected]e3e696d32013-06-21 20:41:36856 ComponentUnpacker::Error error,
857 int extra_code) {
[email protected]e8f96ff2011-08-03 05:07:33858 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]07f93af12011-08-17 20:57:22859
[email protected]7b0529242013-07-20 05:45:46860 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]07f93af12011-08-17 20:57:22874 CrxUpdateItem* item = FindUpdateItemById(component_id);
[email protected]7b0529242013-07-20 05:45:46875 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]1ea21ad2013-09-02 04:20:39879 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]e3e696d32013-06-21 20:41:36885 }
[email protected]e3e696d32013-06-21 20:41:36886
[email protected]7b0529242013-07-20 05:45:46887 if (is_success) {
[email protected]61aca4cd2013-10-26 10:50:59888 ChangeItemState(item, CrxUpdateItem::kUpdated);
[email protected]07f93af12011-08-17 20:57:22889 item->component.version = item->next_version;
[email protected]e3e696d32013-06-21 20:41:36890 item->component.fingerprint = item->next_fp;
[email protected]7b0529242013-07-20 05:45:46891 } else {
[email protected]61aca4cd2013-10-26 10:50:59892 ChangeItemState(item, CrxUpdateItem::kNoUpdate);
[email protected]7b0529242013-07-20 05:45:46893 item->error_category = error_category;
894 item->error_code = error;
895 item->extra_code1 = extra_code;
[email protected]e3e696d32013-06-21 20:41:36896 }
[email protected]07f93af12011-08-17 20:57:22897
[email protected]7b0529242013-07-20 05:45:46898 ping_manager_->OnUpdateComplete(item);
[email protected]360b8bb2011-09-01 21:48:06899
[email protected]32a6c8382013-08-20 00:29:20900 // Move on to the next update, if there is one available.
901 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33902}
903
[email protected]85e61d52013-08-01 22:23:42904void 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]00a77fa2013-11-02 04:18:46914content::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
930void 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
945CUResourceThrottle::CUResourceThrottle(const net::URLRequest* request)
946 : state_(NEW) {
947 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
948}
949
950CUResourceThrottle::~CUResourceThrottle() {
951 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
952}
953
954void CUResourceThrottle::WillStartRequest(bool* defer) {
955 if (state_ != UNBLOCKED) {
956 state_ = BLOCKED;
957 *defer = true;
958 } else {
959 *defer = false;
960 }
961}
962
963void CUResourceThrottle::WillRedirectRequest(const GURL& new_url, bool* defer) {
964 WillStartRequest(defer);
965}
966
[email protected]f8fe5cf2013-12-04 20:11:53967const char* CUResourceThrottle::GetNameForLogging() const {
968 return "ComponentUpdateResourceThrottle";
969}
970
[email protected]00a77fa2013-11-02 04:18:46971void CUResourceThrottle::Unblock() {
972 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
973 if (state_ == BLOCKED)
974 controller()->Resume();
975 state_ = UNBLOCKED;
976}
977
[email protected]e8f96ff2011-08-03 05:07:33978// The component update factory. Using the component updater as a singleton
979// is the job of the browser process.
980ComponentUpdateService* ComponentUpdateServiceFactory(
981 ComponentUpdateService::Configurator* config) {
982 DCHECK(config);
983 return new CrxUpdateService(config);
984}
[email protected]2cddef42013-11-22 08:23:22985
[email protected]055981f2014-01-17 20:22:32986} // namespace component_updater
987