blob: 2095c0593a6ac4a2a91f7b5795a35a61249f920a [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]f5d27e32014-01-31 06:48:5319#include "base/sequenced_task_runner.h"
[email protected]e8f96ff2011-08-03 05:07:3320#include "base/stl_util.h"
[email protected]8f5f2ea2013-10-31 09:39:1021#include "base/threading/sequenced_worker_pool.h"
[email protected]41a17c52013-06-28 00:27:5322#include "base/timer/timer.h"
[email protected]e8f96ff2011-08-03 05:07:3323#include "chrome/browser/browser_process.h"
[email protected]e3e696d32013-06-21 20:41:3624#include "chrome/browser/component_updater/component_patcher.h"
[email protected]e8f96ff2011-08-03 05:07:3325#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 observer(NULL),
88 allow_background_download(true) {
[email protected]dc06f0b2013-01-23 20:03:1689}
90
91CrxComponent::~CrxComponent() {
92}
[email protected]e8f96ff2011-08-03 05:07:3393
[email protected]2e919ddd2013-08-21 05:05:1794CrxComponentInfo::CrxComponentInfo() {
95}
96
97CrxComponentInfo::~CrxComponentInfo() {
98}
99
[email protected]00a77fa2013-11-02 04:18:46100///////////////////////////////////////////////////////////////////////////////
101// In charge of blocking url requests until the |crx_id| component has been
102// updated. This class is touched solely from the IO thread. The UI thread
103// can post tasks to it via weak pointers. By default the request is blocked
104// unless the CrxUpdateService calls Unblock().
105// The lifetime is controlled by Chrome's resource loader so the component
106// updater cannot touch objects from this class except via weak pointers.
107class CUResourceThrottle
108 : public content::ResourceThrottle,
109 public base::SupportsWeakPtr<CUResourceThrottle> {
110 public:
111 explicit CUResourceThrottle(const net::URLRequest* request);
112 virtual ~CUResourceThrottle();
[email protected]2cddef42013-11-22 08:23:22113
[email protected]00a77fa2013-11-02 04:18:46114 // Overriden from ResourceThrottle.
115 virtual void WillStartRequest(bool* defer) OVERRIDE;
116 virtual void WillRedirectRequest(const GURL& new_url, bool* defer) OVERRIDE;
[email protected]f8fe5cf2013-12-04 20:11:53117 virtual const char* GetNameForLogging() const OVERRIDE;
[email protected]00a77fa2013-11-02 04:18:46118
119 // Component updater calls this function via PostTask to unblock the request.
120 void Unblock();
121
122 typedef std::vector<base::WeakPtr<CUResourceThrottle> > WeakPtrVector;
123
124 private:
[email protected]2cddef42013-11-22 08:23:22125 enum State {
126 NEW,
127 BLOCKED,
128 UNBLOCKED
129 };
[email protected]00a77fa2013-11-02 04:18:46130
[email protected]2cddef42013-11-22 08:23:22131 State state_;
[email protected]00a77fa2013-11-02 04:18:46132};
133
134void UnblockResourceThrottle(base::WeakPtr<CUResourceThrottle> rt) {
135 BrowserThread::PostTask(
136 BrowserThread::IO,
137 FROM_HERE,
138 base::Bind(&CUResourceThrottle::Unblock, rt));
139}
140
141void UnblockandReapAllThrottles(CUResourceThrottle::WeakPtrVector* throttles) {
142 CUResourceThrottle::WeakPtrVector::iterator it;
143 for (it = throttles->begin(); it != throttles->end(); ++it)
144 UnblockResourceThrottle(*it);
145 throttles->clear();
146}
147
[email protected]e8f96ff2011-08-03 05:07:33148//////////////////////////////////////////////////////////////////////////////
149// The one and only implementation of the ComponentUpdateService interface. In
150// charge of running the show. The main method is ProcessPendingItems() which
[email protected]50b01392012-09-01 03:33:13151// is called periodically to do the upgrades/installs or the update checks.
[email protected]e8f96ff2011-08-03 05:07:33152// An important consideration here is to be as "low impact" as we can to the
153// rest of the browser, so even if we have many components registered and
[email protected]f1050432012-02-15 01:35:46154// eligible for update, we only do one thing at a time with pauses in between
[email protected]e8f96ff2011-08-03 05:07:33155// the tasks. Also when we do network requests there is only one |url_fetcher_|
[email protected]e3e696d32013-06-21 20:41:36156// in flight at a time.
[email protected]e8f96ff2011-08-03 05:07:33157// There are no locks in this code, the main structure |work_items_| is mutated
[email protected]74be2642014-02-07 09:40:37158// only from the UI thread. The unpack and installation is done in a blocking
159// pool thread. The network requests are done in the IO thread or in the file
[email protected]e8f96ff2011-08-03 05:07:33160// thread.
161class CrxUpdateService : public ComponentUpdateService {
162 public:
163 explicit CrxUpdateService(ComponentUpdateService::Configurator* config);
[email protected]e8f96ff2011-08-03 05:07:33164 virtual ~CrxUpdateService();
165
166 // Overrides for ComponentUpdateService.
167 virtual Status Start() OVERRIDE;
168 virtual Status Stop() OVERRIDE;
169 virtual Status RegisterComponent(const CrxComponent& component) OVERRIDE;
[email protected]61aca4cd2013-10-26 10:50:59170 virtual Status OnDemandUpdate(const std::string& component_id) OVERRIDE;
[email protected]2e919ddd2013-08-21 05:05:17171 virtual void GetComponents(
172 std::vector<CrxComponentInfo>* components) OVERRIDE;
[email protected]00a77fa2013-11-02 04:18:46173 virtual content::ResourceThrottle* GetOnDemandResourceThrottle(
174 net::URLRequest* request, const std::string& crx_id) OVERRIDE;
[email protected]e8f96ff2011-08-03 05:07:33175
[email protected]93e8e2c2014-01-04 12:29:23176 // Context for a crx download url request.
177 struct CRXContext {
178 ComponentInstaller* installer;
179 std::vector<uint8> pk_hash;
180 std::string id;
181 std::string fingerprint;
182 CRXContext() : installer(NULL) {}
183 };
[email protected]e8f96ff2011-08-03 05:07:33184
[email protected]e8f96ff2011-08-03 05:07:33185 private:
[email protected]7b0529242013-07-20 05:45:46186 enum ErrorCategory {
187 kErrorNone = 0,
188 kNetworkError,
189 kUnpackError,
190 kInstallError,
191 };
192
[email protected]32a6c8382013-08-20 00:29:20193 enum StepDelayInterval {
194 kStepDelayShort = 0,
195 kStepDelayMedium,
196 kStepDelayLong,
197 };
198
[email protected]055981f2014-01-17 20:22:32199 void UpdateCheckComplete(int error,
200 const std::string& error_message,
201 const UpdateResponse::Results& results);
202 void OnUpdateCheckSucceeded(const UpdateResponse::Results& results);
[email protected]93e8e2c2014-01-04 12:29:23203 void OnUpdateCheckFailed(int error, const std::string& error_message);
[email protected]e8f96ff2011-08-03 05:07:33204
[email protected]3cb2a4f2013-12-07 21:54:34205 void DownloadComplete(
206 scoped_ptr<CRXContext> crx_context,
[email protected]3a0092d2013-12-18 03:04:35207 const CrxDownloader::Result& download_result);
[email protected]afa378f22013-12-02 03:37:54208
[email protected]00a77fa2013-11-02 04:18:46209 Status OnDemandUpdateInternal(CrxUpdateItem* item);
210
[email protected]e8f96ff2011-08-03 05:07:33211 void ProcessPendingItems();
212
[email protected]93e8e2c2014-01-04 12:29:23213 // Find a component that is ready to update.
214 CrxUpdateItem* FindReadyComponent() const;
215
216 // Prepares the components for an update check and initiates the request.
217 // Returns true if an update check request has been made. Returns false if
218 // no update check was needed or an error occured.
219 bool CheckForUpdates();
[email protected]61aca4cd2013-10-26 10:50:59220
221 void UpdateComponent(CrxUpdateItem* workitem);
222
[email protected]32a6c8382013-08-20 00:29:20223 void ScheduleNextRun(StepDelayInterval step_delay);
[email protected]e8f96ff2011-08-03 05:07:33224
[email protected]6268d3a2013-11-27 01:28:09225 void ParseResponse(const std::string& xml);
[email protected]e8f96ff2011-08-03 05:07:33226
[email protected]afa378f22013-12-02 03:37:54227 void Install(scoped_ptr<CRXContext> context, const base::FilePath& crx_path);
[email protected]e8f96ff2011-08-03 05:07:33228
[email protected]f5d27e32014-01-31 06:48:53229 void EndUnpacking(const std::string& component_id,
230 const base::FilePath& crx_path,
231 ComponentUnpacker::Error error,
232 int extended_error);
233
[email protected]07f93af12011-08-17 20:57:22234 void DoneInstalling(const std::string& component_id,
[email protected]e3e696d32013-06-21 20:41:36235 ComponentUnpacker::Error error,
236 int extended_error);
[email protected]e8f96ff2011-08-03 05:07:33237
[email protected]61aca4cd2013-10-26 10:50:59238 void ChangeItemState(CrxUpdateItem* item, CrxUpdateItem::Status to);
239
[email protected]e8f96ff2011-08-03 05:07:33240 size_t ChangeItemStatus(CrxUpdateItem::Status from,
241 CrxUpdateItem::Status to);
242
243 CrxUpdateItem* FindUpdateItemById(const std::string& id);
244
[email protected]85e61d52013-08-01 22:23:42245 void NotifyComponentObservers(ComponentObserver::Events event,
246 int extra) const;
247
[email protected]61aca4cd2013-10-26 10:50:59248 bool HasOnDemandItems() const;
249
[email protected]00a77fa2013-11-02 04:18:46250 void OnNewResourceThrottle(base::WeakPtr<CUResourceThrottle> rt,
251 const std::string& crx_id);
252
[email protected]e3e696d32013-06-21 20:41:36253 scoped_ptr<ComponentUpdateService::Configurator> config_;
254
255 scoped_ptr<ComponentPatcher> component_patcher_;
[email protected]e8f96ff2011-08-03 05:07:33256
[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]f5d27e32014-01-31 06:48:53261 scoped_ptr<ComponentUnpacker> unpacker_;
262
[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
277 DISALLOW_COPY_AND_ASSIGN(CrxUpdateService);
278};
279
[email protected]e8f96ff2011-08-03 05:07:33280//////////////////////////////////////////////////////////////////////////////
281
[email protected]e3e696d32013-06-21 20:41:36282CrxUpdateService::CrxUpdateService(ComponentUpdateService::Configurator* config)
[email protected]e8f96ff2011-08-03 05:07:33283 : config_(config),
[email protected]e3e696d32013-06-21 20:41:36284 component_patcher_(config->CreateComponentPatcher()),
[email protected]055981f2014-01-17 20:22:32285 ping_manager_(new PingManager(config->PingUrl(),
286 config->RequestContext())),
[email protected]8f5f2ea2013-10-31 09:39:10287 blocking_task_runner_(BrowserThread::GetBlockingPool()->
288 GetSequencedTaskRunnerWithShutdownBehavior(
289 BrowserThread::GetBlockingPool()->GetSequenceToken(),
290 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)),
[email protected]e8f96ff2011-08-03 05:07:33291 chrome_version_(chrome::VersionInfo().Version()),
292 running_(false) {
[email protected]1ea21ad2013-09-02 04:20:39293}
[email protected]e8f96ff2011-08-03 05:07:33294
295CrxUpdateService::~CrxUpdateService() {
296 // Because we are a singleton, at this point only the UI thread should be
297 // alive, this simplifies the management of the work that could be in
298 // flight in other threads.
299 Stop();
300 STLDeleteElements(&work_items_);
[email protected]1ea21ad2013-09-02 04:20:39301}
[email protected]e8f96ff2011-08-03 05:07:33302
303ComponentUpdateService::Status CrxUpdateService::Start() {
304 // Note that RegisterComponent will call Start() when the first
305 // component is registered, so it can be called twice. This way
306 // we avoid scheduling the timer if there is no work to do.
307 running_ = true;
308 if (work_items_.empty())
309 return kOk;
310
[email protected]85e61d52013-08-01 22:23:42311 NotifyComponentObservers(ComponentObserver::COMPONENT_UPDATER_STARTED, 0);
312
[email protected]d323a172011-09-02 18:23:02313 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(config_->InitialDelay()),
[email protected]e8f96ff2011-08-03 05:07:33314 this, &CrxUpdateService::ProcessPendingItems);
315 return kOk;
316}
317
318// Stop the main check + update loop. In flight operations will be
319// completed.
320ComponentUpdateService::Status CrxUpdateService::Stop() {
321 running_ = false;
322 timer_.Stop();
323 return kOk;
324}
325
[email protected]61aca4cd2013-10-26 10:50:59326bool CrxUpdateService::HasOnDemandItems() const {
327 class Helper {
328 public:
329 static bool IsOnDemand(CrxUpdateItem* item) {
330 return item->on_demand;
331 }
332 };
333 return std::find_if(work_items_.begin(),
334 work_items_.end(),
335 Helper::IsOnDemand) != work_items_.end();
336}
337
[email protected]ccb4feef2013-02-14 06:16:47338// This function sets the timer which will call ProcessPendingItems() or
[email protected]61aca4cd2013-10-26 10:50:59339// ProcessRequestedItem() if there is an on_demand item. There
[email protected]32a6c8382013-08-20 00:29:20340// are three kinds of waits:
341// - a short delay, when there is immediate work to be done.
342// - a medium delay, when there are updates to be applied within the current
343// update cycle, or there are components that are still unchecked.
344// - a long delay when a full check/update cycle has completed for all
345// components.
346void CrxUpdateService::ScheduleNextRun(StepDelayInterval step_delay) {
[email protected]e8f96ff2011-08-03 05:07:33347 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23348 DCHECK(!update_checker_);
[email protected]e8f96ff2011-08-03 05:07:33349 CHECK(!timer_.IsRunning());
350 // It could be the case that Stop() had been called while a url request
351 // or unpacking was in flight, if so we arrive here but |running_| is
352 // false. In that case do not loop again.
353 if (!running_)
354 return;
355
[email protected]ccb4feef2013-02-14 06:16:47356 // Keep the delay short if in the middle of an update (step_delay),
357 // or there are new requested_work_items_ that have not been processed yet.
[email protected]32a6c8382013-08-20 00:29:20358 int64 delay_seconds = 0;
[email protected]61aca4cd2013-10-26 10:50:59359 if (!HasOnDemandItems()) {
[email protected]32a6c8382013-08-20 00:29:20360 switch (step_delay) {
361 case kStepDelayShort:
362 delay_seconds = config_->StepDelay();
363 break;
364 case kStepDelayMedium:
365 delay_seconds = config_->StepDelayMedium();
366 break;
367 case kStepDelayLong:
368 delay_seconds = config_->NextCheckDelay();
369 break;
370 }
371 } else {
372 delay_seconds = config_->StepDelay();
373 }
[email protected]cf442612011-08-09 20:20:12374
[email protected]32a6c8382013-08-20 00:29:20375 if (step_delay != kStepDelayShort) {
[email protected]85e61d52013-08-01 22:23:42376 NotifyComponentObservers(ComponentObserver::COMPONENT_UPDATER_SLEEPING, 0);
377
[email protected]e8f96ff2011-08-03 05:07:33378 // Zero is only used for unit tests.
[email protected]32a6c8382013-08-20 00:29:20379 if (0 == delay_seconds)
[email protected]e8f96ff2011-08-03 05:07:33380 return;
381 }
382
[email protected]32a6c8382013-08-20 00:29:20383 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(delay_seconds),
[email protected]e8f96ff2011-08-03 05:07:33384 this, &CrxUpdateService::ProcessPendingItems);
385}
386
387// Given a extension-like component id, find the associated component.
388CrxUpdateItem* CrxUpdateService::FindUpdateItemById(const std::string& id) {
389 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
390 CrxUpdateItem::FindById finder(id);
391 UpdateItems::iterator it = std::find_if(work_items_.begin(),
392 work_items_.end(),
393 finder);
[email protected]93e8e2c2014-01-04 12:29:23394 return it != work_items_.end() ? *it : NULL;
[email protected]e8f96ff2011-08-03 05:07:33395}
396
[email protected]61aca4cd2013-10-26 10:50:59397// Changes a component's status, clearing on_demand and firing notifications as
398// necessary. By convention, this is the only function that can change a
399// CrxUpdateItem's |status|.
400// TODO(waffles): Do we want to add DCHECKS for valid state transitions here?
401void CrxUpdateService::ChangeItemState(CrxUpdateItem* item,
402 CrxUpdateItem::Status to) {
403 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
404 if (to == CrxUpdateItem::kNoUpdate ||
405 to == CrxUpdateItem::kUpdated ||
406 to == CrxUpdateItem::kUpToDate) {
407 item->on_demand = false;
408 }
409
410 item->status = to;
411
412 ComponentObserver* observer = item->component.observer;
413 if (observer) {
414 switch (to) {
415 case CrxUpdateItem::kCanUpdate:
416 observer->OnEvent(ComponentObserver::COMPONENT_UPDATE_FOUND, 0);
417 break;
418 case CrxUpdateItem::kUpdatingDiff:
419 case CrxUpdateItem::kUpdating:
420 observer->OnEvent(ComponentObserver::COMPONENT_UPDATE_READY, 0);
421 break;
422 case CrxUpdateItem::kUpdated:
423 observer->OnEvent(ComponentObserver::COMPONENT_UPDATED, 0);
424 break;
425 case CrxUpdateItem::kUpToDate:
426 case CrxUpdateItem::kNoUpdate:
427 observer->OnEvent(ComponentObserver::COMPONENT_NOT_UPDATED, 0);
428 break;
429 case CrxUpdateItem::kNew:
430 case CrxUpdateItem::kChecking:
431 case CrxUpdateItem::kDownloading:
432 case CrxUpdateItem::kDownloadingDiff:
433 case CrxUpdateItem::kLastStatus:
434 // No notification for these states.
435 break;
436 }
437 }
[email protected]00a77fa2013-11-02 04:18:46438
439 // Free possible pending network requests.
440 if ((to == CrxUpdateItem::kUpdated) ||
441 (to == CrxUpdateItem::kUpToDate) ||
442 (to == CrxUpdateItem::kNoUpdate)) {
443 UnblockandReapAllThrottles(&item->throttles);
444 }
[email protected]61aca4cd2013-10-26 10:50:59445}
446
[email protected]e8f96ff2011-08-03 05:07:33447// Changes all the components in |work_items_| that have |from| status to
[email protected]e3e696d32013-06-21 20:41:36448// |to| status and returns how many have been changed.
[email protected]e8f96ff2011-08-03 05:07:33449size_t CrxUpdateService::ChangeItemStatus(CrxUpdateItem::Status from,
450 CrxUpdateItem::Status to) {
451 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
452 size_t count = 0;
453 for (UpdateItems::iterator it = work_items_.begin();
454 it != work_items_.end(); ++it) {
455 CrxUpdateItem* item = *it;
[email protected]93e8e2c2014-01-04 12:29:23456 if (item->status == from) {
457 ChangeItemState(item, to);
458 ++count;
459 }
[email protected]e8f96ff2011-08-03 05:07:33460 }
461 return count;
462}
463
464// Adds a component to be checked for upgrades. If the component exists it
465// it will be replaced and the return code is kReplaced.
[email protected]e8f96ff2011-08-03 05:07:33466ComponentUpdateService::Status CrxUpdateService::RegisterComponent(
467 const CrxComponent& component) {
468 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
469 if (component.pk_hash.empty() ||
470 !component.version.IsValid() ||
471 !component.installer)
472 return kError;
473
[email protected]055981f2014-01-17 20:22:32474 std::string id(GetCrxComponentID(component));
[email protected]93e8e2c2014-01-04 12:29:23475 CrxUpdateItem* uit = FindUpdateItemById(id);
[email protected]e8f96ff2011-08-03 05:07:33476 if (uit) {
477 uit->component = component;
478 return kReplaced;
479 }
480
481 uit = new CrxUpdateItem;
482 uit->id.swap(id);
483 uit->component = component;
[email protected]e3e696d32013-06-21 20:41:36484
[email protected]e8f96ff2011-08-03 05:07:33485 work_items_.push_back(uit);
486 // If this is the first component registered we call Start to
487 // schedule the first timer.
488 if (running_ && (work_items_.size() == 1))
489 Start();
490
491 return kOk;
492}
493
[email protected]ccb4feef2013-02-14 06:16:47494// Start the process of checking for an update, for a particular component
495// that was previously registered.
[email protected]2e919ddd2013-08-21 05:05:17496// |component_id| is a value returned from GetCrxComponentID().
[email protected]61aca4cd2013-10-26 10:50:59497ComponentUpdateService::Status CrxUpdateService::OnDemandUpdate(
[email protected]2e919ddd2013-08-21 05:05:17498 const std::string& component_id) {
[email protected]00a77fa2013-11-02 04:18:46499 return OnDemandUpdateInternal(FindUpdateItemById(component_id));
500}
501
502ComponentUpdateService::Status CrxUpdateService::OnDemandUpdateInternal(
503 CrxUpdateItem* uit) {
[email protected]ccb4feef2013-02-14 06:16:47504 if (!uit)
505 return kError;
[email protected]2cddef42013-11-22 08:23:22506
[email protected]ccb4feef2013-02-14 06:16:47507 // Check if the request is too soon.
508 base::TimeDelta delta = base::Time::Now() - uit->last_check;
[email protected]e3e696d32013-06-21 20:41:36509 if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay()))
[email protected]ccb4feef2013-02-14 06:16:47510 return kError;
[email protected]ccb4feef2013-02-14 06:16:47511
512 switch (uit->status) {
513 // If the item is already in the process of being updated, there is
514 // no point in this call, so return kInProgress.
515 case CrxUpdateItem::kChecking:
516 case CrxUpdateItem::kCanUpdate:
[email protected]e3e696d32013-06-21 20:41:36517 case CrxUpdateItem::kDownloadingDiff:
[email protected]ccb4feef2013-02-14 06:16:47518 case CrxUpdateItem::kDownloading:
[email protected]e3e696d32013-06-21 20:41:36519 case CrxUpdateItem::kUpdatingDiff:
[email protected]ccb4feef2013-02-14 06:16:47520 case CrxUpdateItem::kUpdating:
521 return kInProgress;
522 // Otherwise the item was already checked a while back (or it is new),
523 // set its status to kNew to give it a slightly higher priority.
524 case CrxUpdateItem::kNew:
525 case CrxUpdateItem::kUpdated:
526 case CrxUpdateItem::kUpToDate:
527 case CrxUpdateItem::kNoUpdate:
[email protected]61aca4cd2013-10-26 10:50:59528 ChangeItemState(uit, CrxUpdateItem::kNew);
529 uit->on_demand = true;
[email protected]ccb4feef2013-02-14 06:16:47530 break;
531 case CrxUpdateItem::kLastStatus:
532 NOTREACHED() << uit->status;
533 }
534
535 // In case the current delay is long, set the timer to a shorter value
536 // to get the ball rolling.
537 if (timer_.IsRunning()) {
538 timer_.Stop();
539 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(config_->StepDelay()),
540 this, &CrxUpdateService::ProcessPendingItems);
541 }
542
543 return kOk;
544}
545
[email protected]2e919ddd2013-08-21 05:05:17546void CrxUpdateService::GetComponents(
547 std::vector<CrxComponentInfo>* components) {
548 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
549 for (UpdateItems::const_iterator it = work_items_.begin();
550 it != work_items_.end(); ++it) {
551 const CrxUpdateItem* item = *it;
552 CrxComponentInfo info;
[email protected]055981f2014-01-17 20:22:32553 info.id = GetCrxComponentID(item->component);
[email protected]2e919ddd2013-08-21 05:05:17554 info.version = item->component.version.GetString();
555 info.name = item->component.name;
556 components->push_back(info);
557 }
558}
559
[email protected]93e8e2c2014-01-04 12:29:23560// This is the main loop of the component updater. It updates one component
561// at a time if updates are available. Otherwise, it does an update check or
562// takes a long sleep until the loop runs again.
[email protected]e8f96ff2011-08-03 05:07:33563void CrxUpdateService::ProcessPendingItems() {
564 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23565
[email protected]61aca4cd2013-10-26 10:50:59566 CrxUpdateItem* ready_upgrade = FindReadyComponent();
567 if (ready_upgrade) {
568 UpdateComponent(ready_upgrade);
[email protected]e8f96ff2011-08-03 05:07:33569 return;
570 }
[email protected]93e8e2c2014-01-04 12:29:23571
572 if (!CheckForUpdates())
573 ScheduleNextRun(kStepDelayLong);
[email protected]61aca4cd2013-10-26 10:50:59574}
575
[email protected]93e8e2c2014-01-04 12:29:23576CrxUpdateItem* CrxUpdateService::FindReadyComponent() const {
[email protected]61aca4cd2013-10-26 10:50:59577 class Helper {
578 public:
579 static bool IsReadyOnDemand(CrxUpdateItem* item) {
580 return item->on_demand && IsReady(item);
581 }
582 static bool IsReady(CrxUpdateItem* item) {
583 return item->status == CrxUpdateItem::kCanUpdate;
584 }
585 };
586
[email protected]93e8e2c2014-01-04 12:29:23587 std::vector<CrxUpdateItem*>::const_iterator it = std::find_if(
[email protected]61aca4cd2013-10-26 10:50:59588 work_items_.begin(), work_items_.end(), Helper::IsReadyOnDemand);
589 if (it != work_items_.end())
590 return *it;
591 it = std::find_if(work_items_.begin(), work_items_.end(), Helper::IsReady);
592 if (it != work_items_.end())
593 return *it;
594 return NULL;
595}
596
[email protected]93e8e2c2014-01-04 12:29:23597// Prepares the components for an update check and initiates the request.
598bool CrxUpdateService::CheckForUpdates() {
599 // All components are selected for the update check, regardless of when they
600 // were last checked. More selective algorithms could be implemented in the
601 // future.
602 std::vector<CrxUpdateItem*> items_to_check;
603 for (size_t i = 0; i != work_items_.size(); ++i) {
604 CrxUpdateItem* item = work_items_[i];
605 DCHECK(item->status == CrxUpdateItem::kNew ||
606 item->status == CrxUpdateItem::kNoUpdate ||
607 item->status == CrxUpdateItem::kUpToDate ||
608 item->status == CrxUpdateItem::kUpdated);
609
610 ChangeItemState(item, CrxUpdateItem::kChecking);
611
612 item->last_check = base::Time::Now();
613 item->crx_urls.clear();
614 item->crx_diffurls.clear();
615 item->previous_version = item->component.version;
616 item->next_version = Version();
617 item->previous_fp = item->component.fingerprint;
618 item->next_fp.clear();
619 item->diff_update_failed = false;
620 item->error_category = 0;
621 item->error_code = 0;
622 item->extra_code1 = 0;
623 item->diff_error_category = 0;
624 item->diff_error_code = 0;
625 item->diff_extra_code1 = 0;
626 item->download_metrics.clear();
627
628 items_to_check.push_back(item);
629 }
630
631 if (items_to_check.empty())
632 return false;
633
[email protected]055981f2014-01-17 20:22:32634 update_checker_ = UpdateChecker::Create(
[email protected]93e8e2c2014-01-04 12:29:23635 config_->UpdateUrl(),
636 config_->RequestContext(),
637 base::Bind(&CrxUpdateService::UpdateCheckComplete,
638 base::Unretained(this))).Pass();
639 return update_checker_->CheckForUpdates(items_to_check,
640 config_->ExtraRequestParams());
641}
642
[email protected]61aca4cd2013-10-26 10:50:59643void CrxUpdateService::UpdateComponent(CrxUpdateItem* workitem) {
[email protected]afa378f22013-12-02 03:37:54644 scoped_ptr<CRXContext> crx_context(new CRXContext);
645 crx_context->pk_hash = workitem->component.pk_hash;
646 crx_context->id = workitem->id;
647 crx_context->installer = workitem->component.installer;
648 crx_context->fingerprint = workitem->next_fp;
[email protected]da37c1d2013-12-19 01:04:38649 const std::vector<GURL>* urls = NULL;
[email protected]cfd13e52014-02-05 09:35:07650 bool allow_background_download = false;
[email protected]61aca4cd2013-10-26 10:50:59651 if (CanTryDiffUpdate(workitem, *config_)) {
[email protected]da37c1d2013-12-19 01:04:38652 urls = &workitem->crx_diffurls;
[email protected]61aca4cd2013-10-26 10:50:59653 ChangeItemState(workitem, CrxUpdateItem::kDownloadingDiff);
654 } else {
[email protected]cfd13e52014-02-05 09:35:07655 // Background downloads are enabled only for selected components and
656 // only for full downloads (see issue 340448).
657 allow_background_download = workitem->component.allow_background_download;
[email protected]da37c1d2013-12-19 01:04:38658 urls = &workitem->crx_urls;
[email protected]61aca4cd2013-10-26 10:50:59659 ChangeItemState(workitem, CrxUpdateItem::kDownloading);
660 }
[email protected]3cb2a4f2013-12-07 21:54:34661
662 // On demand component updates are always downloaded in foreground.
[email protected]cfd13e52014-02-05 09:35:07663 const bool is_background_download =
664 !workitem->on_demand && allow_background_download &&
665 config_->UseBackgroundDownloader();
[email protected]3cb2a4f2013-12-07 21:54:34666
[email protected]3a0092d2013-12-18 03:04:35667 crx_downloader_.reset(CrxDownloader::Create(
[email protected]3cb2a4f2013-12-07 21:54:34668 is_background_download,
[email protected]afa378f22013-12-02 03:37:54669 config_->RequestContext(),
670 blocking_task_runner_,
671 base::Bind(&CrxUpdateService::DownloadComplete,
672 base::Unretained(this),
673 base::Passed(&crx_context))));
[email protected]da37c1d2013-12-19 01:04:38674 crx_downloader_->StartDownload(*urls);
[email protected]61aca4cd2013-10-26 10:50:59675}
676
[email protected]93e8e2c2014-01-04 12:29:23677void CrxUpdateService::UpdateCheckComplete(
678 int error,
679 const std::string& error_message,
[email protected]055981f2014-01-17 20:22:32680 const UpdateResponse::Results& results) {
[email protected]e8f96ff2011-08-03 05:07:33681 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23682 update_checker_.reset();
683 if (!error)
684 OnUpdateCheckSucceeded(results);
[email protected]652f4272013-11-13 09:23:41685 else
[email protected]93e8e2c2014-01-04 12:29:23686 OnUpdateCheckFailed(error, error_message);
[email protected]e8f96ff2011-08-03 05:07:33687}
688
[email protected]93e8e2c2014-01-04 12:29:23689// Handles a valid Omaha update check response by matching the results with
690// the registered components which were checked for updates.
691// If updates are found, prepare the components for the actual version upgrade.
692// One of these components will be drafted for the upgrade next time
693// ProcessPendingItems is called.
694void CrxUpdateService::OnUpdateCheckSucceeded(
[email protected]055981f2014-01-17 20:22:32695 const UpdateResponse::Results& results) {
[email protected]2cddef42013-11-22 08:23:22696 size_t num_updates_pending = 0;
[email protected]e8f96ff2011-08-03 05:07:33697 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]055981f2014-01-17 20:22:32698 std::vector<UpdateResponse::Result>::const_iterator it;
[email protected]e8f96ff2011-08-03 05:07:33699 for (it = results.list.begin(); it != results.list.end(); ++it) {
700 CrxUpdateItem* crx = FindUpdateItemById(it->extension_id);
701 if (!crx)
702 continue;
703
[email protected]2cddef42013-11-22 08:23:22704 if (crx->status != CrxUpdateItem::kChecking) {
705 NOTREACHED();
[email protected]e8f96ff2011-08-03 05:07:33706 continue; // Not updating this component now.
[email protected]2cddef42013-11-22 08:23:22707 }
[email protected]e8f96ff2011-08-03 05:07:33708
[email protected]2cddef42013-11-22 08:23:22709 if (it->manifest.version.empty()) {
[email protected]cf442612011-08-09 20:20:12710 // No version means no update available.
[email protected]61aca4cd2013-10-26 10:50:59711 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]cf442612011-08-09 20:20:12712 continue;
[email protected]e8f96ff2011-08-03 05:07:33713 }
[email protected]2cddef42013-11-22 08:23:22714
715 if (!IsVersionNewer(crx->component.version, it->manifest.version)) {
716 // The component is up to date.
[email protected]61aca4cd2013-10-26 10:50:59717 ChangeItemState(crx, CrxUpdateItem::kUpToDate);
[email protected]cf442612011-08-09 20:20:12718 continue;
[email protected]e8f96ff2011-08-03 05:07:33719 }
[email protected]2cddef42013-11-22 08:23:22720
721 if (!it->manifest.browser_min_version.empty()) {
722 if (IsVersionNewer(chrome_version_, it->manifest.browser_min_version)) {
723 // The component is not compatible with this Chrome version.
[email protected]61aca4cd2013-10-26 10:50:59724 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]cf442612011-08-09 20:20:12725 continue;
726 }
[email protected]e8f96ff2011-08-03 05:07:33727 }
[email protected]2cddef42013-11-22 08:23:22728
729 if (it->manifest.packages.size() != 1) {
730 // Assume one and only one package per component.
731 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
732 continue;
733 }
734
735 // Parse the members of the result and queue an upgrade for this component.
[email protected]c5e4a2222014-01-03 16:06:13736 crx->next_version = Version(it->manifest.version);
[email protected]2cddef42013-11-22 08:23:22737
[email protected]055981f2014-01-17 20:22:32738 typedef UpdateResponse::Result::Manifest::Package Package;
[email protected]2cddef42013-11-22 08:23:22739 const Package& package(it->manifest.packages[0]);
740 crx->next_fp = package.fingerprint;
741
[email protected]da37c1d2013-12-19 01:04:38742 // Resolve the urls by combining the base urls with the package names.
743 for (size_t i = 0; i != it->crx_urls.size(); ++i) {
744 const GURL url(it->crx_urls[i].Resolve(package.name));
745 if (url.is_valid())
746 crx->crx_urls.push_back(url);
747 }
748 for (size_t i = 0; i != it->crx_diffurls.size(); ++i) {
749 const GURL url(it->crx_diffurls[i].Resolve(package.namediff));
750 if (url.is_valid())
751 crx->crx_diffurls.push_back(url);
752 }
[email protected]2cddef42013-11-22 08:23:22753
[email protected]61aca4cd2013-10-26 10:50:59754 ChangeItemState(crx, CrxUpdateItem::kCanUpdate);
[email protected]2cddef42013-11-22 08:23:22755 ++num_updates_pending;
[email protected]e8f96ff2011-08-03 05:07:33756 }
[email protected]cf442612011-08-09 20:20:12757
[email protected]2cddef42013-11-22 08:23:22758 // All components that are not included in the update response are
759 // considered up to date.
[email protected]cf442612011-08-09 20:20:12760 ChangeItemStatus(CrxUpdateItem::kChecking, CrxUpdateItem::kUpToDate);
761
[email protected]32a6c8382013-08-20 00:29:20762 // If there are updates pending we do a short wait, otherwise we take
763 // a longer delay until we check the components again.
[email protected]2cddef42013-11-22 08:23:22764 ScheduleNextRun(num_updates_pending > 0 ? kStepDelayShort : kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33765}
766
[email protected]93e8e2c2014-01-04 12:29:23767// TODO: record UMA stats.
768void CrxUpdateService::OnUpdateCheckFailed(int error,
769 const std::string& error_message) {
[email protected]e8f96ff2011-08-03 05:07:33770 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23771 DCHECK(error);
[email protected]e8f96ff2011-08-03 05:07:33772 size_t count = ChangeItemStatus(CrxUpdateItem::kChecking,
773 CrxUpdateItem::kNoUpdate);
774 DCHECK_GT(count, 0ul);
[email protected]32a6c8382013-08-20 00:29:20775 ScheduleNextRun(kStepDelayLong);
[email protected]e8f96ff2011-08-03 05:07:33776}
777
778// Called when the CRX package has been downloaded to a temporary location.
779// Here we fire the notifications and schedule the component-specific installer
780// to be called in the file thread.
[email protected]3cb2a4f2013-12-07 21:54:34781void CrxUpdateService::DownloadComplete(
782 scoped_ptr<CRXContext> crx_context,
[email protected]3a0092d2013-12-18 03:04:35783 const CrxDownloader::Result& download_result) {
[email protected]e8f96ff2011-08-03 05:07:33784 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]64f39fa12013-09-03 21:49:37785
[email protected]64f39fa12013-09-03 21:49:37786 CrxUpdateItem* crx = FindUpdateItemById(crx_context->id);
[email protected]e3e696d32013-06-21 20:41:36787 DCHECK(crx->status == CrxUpdateItem::kDownloadingDiff ||
788 crx->status == CrxUpdateItem::kDownloading);
789
[email protected]3a0092d2013-12-18 03:04:35790 AppendDownloadMetrics(crx_downloader_->download_metrics(),
791 &crx->download_metrics);
792
[email protected]3cb2a4f2013-12-07 21:54:34793 if (download_result.error) {
[email protected]e3e696d32013-06-21 20:41:36794 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
[email protected]7b0529242013-07-20 05:45:46795 crx->diff_error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34796 crx->diff_error_code = download_result.error;
[email protected]e630ba62013-06-22 15:22:34797 crx->diff_update_failed = true;
[email protected]e3e696d32013-06-21 20:41:36798 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
799 CrxUpdateItem::kCanUpdate);
800 DCHECK_EQ(count, 1ul);
[email protected]afa378f22013-12-02 03:37:54801 crx_downloader_.reset();
[email protected]7b0529242013-07-20 05:45:46802
[email protected]32a6c8382013-08-20 00:29:20803 ScheduleNextRun(kStepDelayShort);
[email protected]e3e696d32013-06-21 20:41:36804 return;
805 }
[email protected]7b0529242013-07-20 05:45:46806 crx->error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34807 crx->error_code = download_result.error;
[email protected]e8f96ff2011-08-03 05:07:33808 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloading,
809 CrxUpdateItem::kNoUpdate);
810 DCHECK_EQ(count, 1ul);
[email protected]afa378f22013-12-02 03:37:54811 crx_downloader_.reset();
[email protected]e3e696d32013-06-21 20:41:36812
[email protected]7b0529242013-07-20 05:45:46813 // At this point, since both the differential and the full downloads failed,
814 // the update for this component has finished with an error.
815 ping_manager_->OnUpdateComplete(crx);
816
[email protected]32a6c8382013-08-20 00:29:20817 // Move on to the next update, if there is one available.
818 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33819 } else {
[email protected]e3e696d32013-06-21 20:41:36820 size_t count = 0;
821 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
822 count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
823 CrxUpdateItem::kUpdatingDiff);
824 } else {
825 count = ChangeItemStatus(CrxUpdateItem::kDownloading,
826 CrxUpdateItem::kUpdating);
827 }
[email protected]e8f96ff2011-08-03 05:07:33828 DCHECK_EQ(count, 1ul);
[email protected]afa378f22013-12-02 03:37:54829 crx_downloader_.reset();
[email protected]cf442612011-08-09 20:20:12830
[email protected]44da56e2011-11-21 19:59:14831 // Why unretained? See comment at top of file.
[email protected]8f5f2ea2013-10-31 09:39:10832 blocking_task_runner_->PostDelayedTask(
[email protected]73251e72012-03-04 02:10:33833 FROM_HERE,
[email protected]44da56e2011-11-21 19:59:14834 base::Bind(&CrxUpdateService::Install,
835 base::Unretained(this),
[email protected]afa378f22013-12-02 03:37:54836 base::Passed(&crx_context),
[email protected]3cb2a4f2013-12-07 21:54:34837 download_result.response),
[email protected]73251e72012-03-04 02:10:33838 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]e8f96ff2011-08-03 05:07:33839 }
[email protected]e8f96ff2011-08-03 05:07:33840}
841
842// Install consists of digital signature verification, unpacking and then
843// calling the component specific installer. All that is handled by the
[email protected]f5d27e32014-01-31 06:48:53844// |unpacker_|. If there is an error this function is in charge of deleting
[email protected]e8f96ff2011-08-03 05:07:33845// the files created.
[email protected]afa378f22013-12-02 03:37:54846void CrxUpdateService::Install(scoped_ptr<CRXContext> context,
[email protected]650b2d52013-02-10 03:41:45847 const base::FilePath& crx_path) {
[email protected]8f5f2ea2013-10-31 09:39:10848 // This function owns the file at |crx_path| and the |context| object.
[email protected]f5d27e32014-01-31 06:48:53849 unpacker_.reset(new ComponentUnpacker(context->pk_hash,
850 crx_path,
851 context->fingerprint,
852 component_patcher_.get(),
853 context->installer,
854 blocking_task_runner_));
855 unpacker_->Unpack(base::Bind(&CrxUpdateService::EndUnpacking,
856 base::Unretained(this),
857 context->id,
858 crx_path));
859}
860
861void CrxUpdateService::EndUnpacking(const std::string& component_id,
862 const base::FilePath& crx_path,
863 ComponentUnpacker::Error error,
864 int extended_error) {
[email protected]055981f2014-01-17 20:22:32865 if (!DeleteFileAndEmptyParentDirectory(crx_path))
[email protected]e8f96ff2011-08-03 05:07:33866 NOTREACHED() << crx_path.value();
[email protected]73251e72012-03-04 02:10:33867 BrowserThread::PostDelayedTask(
868 BrowserThread::UI,
869 FROM_HERE,
[email protected]44da56e2011-11-21 19:59:14870 base::Bind(&CrxUpdateService::DoneInstalling, base::Unretained(this),
[email protected]f5d27e32014-01-31 06:48:53871 component_id, error, extended_error),
[email protected]73251e72012-03-04 02:10:33872 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]e8f96ff2011-08-03 05:07:33873}
874
875// Installation has been completed. Adjust the component status and
[email protected]7b0529242013-07-20 05:45:46876// schedule the next check. Schedule a short delay before trying the full
877// update when the differential update failed.
[email protected]07f93af12011-08-17 20:57:22878void CrxUpdateService::DoneInstalling(const std::string& component_id,
[email protected]e3e696d32013-06-21 20:41:36879 ComponentUnpacker::Error error,
880 int extra_code) {
[email protected]e8f96ff2011-08-03 05:07:33881 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]07f93af12011-08-17 20:57:22882
[email protected]7b0529242013-07-20 05:45:46883 ErrorCategory error_category = kErrorNone;
884 switch (error) {
885 case ComponentUnpacker::kNone:
886 break;
887 case ComponentUnpacker::kInstallerError:
888 error_category = kInstallError;
889 break;
890 default:
891 error_category = kUnpackError;
892 break;
893 }
894
895 const bool is_success = error == ComponentUnpacker::kNone;
896
[email protected]07f93af12011-08-17 20:57:22897 CrxUpdateItem* item = FindUpdateItemById(component_id);
[email protected]7b0529242013-07-20 05:45:46898 if (item->status == CrxUpdateItem::kUpdatingDiff && !is_success) {
899 item->diff_error_category = error_category;
900 item->diff_error_code = error;
901 item->diff_extra_code1 = extra_code;
[email protected]1ea21ad2013-09-02 04:20:39902 item->diff_update_failed = true;
903 size_t count = ChangeItemStatus(CrxUpdateItem::kUpdatingDiff,
904 CrxUpdateItem::kCanUpdate);
905 DCHECK_EQ(count, 1ul);
906 ScheduleNextRun(kStepDelayShort);
907 return;
[email protected]e3e696d32013-06-21 20:41:36908 }
[email protected]e3e696d32013-06-21 20:41:36909
[email protected]7b0529242013-07-20 05:45:46910 if (is_success) {
[email protected]61aca4cd2013-10-26 10:50:59911 ChangeItemState(item, CrxUpdateItem::kUpdated);
[email protected]07f93af12011-08-17 20:57:22912 item->component.version = item->next_version;
[email protected]e3e696d32013-06-21 20:41:36913 item->component.fingerprint = item->next_fp;
[email protected]7b0529242013-07-20 05:45:46914 } else {
[email protected]61aca4cd2013-10-26 10:50:59915 ChangeItemState(item, CrxUpdateItem::kNoUpdate);
[email protected]7b0529242013-07-20 05:45:46916 item->error_category = error_category;
917 item->error_code = error;
918 item->extra_code1 = extra_code;
[email protected]e3e696d32013-06-21 20:41:36919 }
[email protected]07f93af12011-08-17 20:57:22920
[email protected]7b0529242013-07-20 05:45:46921 ping_manager_->OnUpdateComplete(item);
[email protected]360b8bb2011-09-01 21:48:06922
[email protected]32a6c8382013-08-20 00:29:20923 // Move on to the next update, if there is one available.
924 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33925}
926
[email protected]85e61d52013-08-01 22:23:42927void CrxUpdateService::NotifyComponentObservers(
928 ComponentObserver::Events event, int extra) const {
929 for (UpdateItems::const_iterator it = work_items_.begin();
930 it != work_items_.end(); ++it) {
931 ComponentObserver* observer = (*it)->component.observer;
932 if (observer)
933 observer->OnEvent(event, 0);
934 }
935}
936
[email protected]00a77fa2013-11-02 04:18:46937content::ResourceThrottle* CrxUpdateService::GetOnDemandResourceThrottle(
938 net::URLRequest* request, const std::string& crx_id) {
939 // We give the raw pointer to the caller, who will delete it at will
940 // and we keep for ourselves a weak pointer to it so we can post tasks
941 // from the UI thread without having to track lifetime directly.
942 CUResourceThrottle* rt = new CUResourceThrottle(request);
943 BrowserThread::PostTask(
944 BrowserThread::UI,
945 FROM_HERE,
946 base::Bind(&CrxUpdateService::OnNewResourceThrottle,
947 base::Unretained(this),
948 rt->AsWeakPtr(),
949 crx_id));
950 return rt;
951}
952
953void CrxUpdateService::OnNewResourceThrottle(
954 base::WeakPtr<CUResourceThrottle> rt, const std::string& crx_id) {
955 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
956 // Check if we can on-demand update, else unblock the request anyway.
957 CrxUpdateItem* item = FindUpdateItemById(crx_id);
958 Status status = OnDemandUpdateInternal(item);
959 if (status == kOk || status == kInProgress) {
960 item->throttles.push_back(rt);
961 return;
962 }
963 UnblockResourceThrottle(rt);
964}
965
966///////////////////////////////////////////////////////////////////////////////
967
968CUResourceThrottle::CUResourceThrottle(const net::URLRequest* request)
969 : state_(NEW) {
970 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
971}
972
973CUResourceThrottle::~CUResourceThrottle() {
974 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
975}
976
977void CUResourceThrottle::WillStartRequest(bool* defer) {
978 if (state_ != UNBLOCKED) {
979 state_ = BLOCKED;
980 *defer = true;
981 } else {
982 *defer = false;
983 }
984}
985
986void CUResourceThrottle::WillRedirectRequest(const GURL& new_url, bool* defer) {
987 WillStartRequest(defer);
988}
989
[email protected]f8fe5cf2013-12-04 20:11:53990const char* CUResourceThrottle::GetNameForLogging() const {
991 return "ComponentUpdateResourceThrottle";
992}
993
[email protected]00a77fa2013-11-02 04:18:46994void CUResourceThrottle::Unblock() {
995 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
996 if (state_ == BLOCKED)
997 controller()->Resume();
998 state_ = UNBLOCKED;
999}
1000
[email protected]e8f96ff2011-08-03 05:07:331001// The component update factory. Using the component updater as a singleton
1002// is the job of the browser process.
1003ComponentUpdateService* ComponentUpdateServiceFactory(
1004 ComponentUpdateService::Configurator* config) {
1005 DCHECK(config);
1006 return new CrxUpdateService(config);
1007}
[email protected]2cddef42013-11-22 08:23:221008
[email protected]055981f2014-01-17 20:22:321009} // namespace component_updater
1010