blob: fa04986f6829511953201f2f4fcabcf416b4cc2b [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]c4b4cfa2014-03-10 18:55:00577 class Helper {
578 public:
579 static bool IsReadyOnDemand(CrxUpdateItem* item) {
580 return item->on_demand && IsReady(item);
[email protected]61aca4cd2013-10-26 10:50:59581 }
[email protected]c4b4cfa2014-03-10 18:55:00582 static bool IsReady(CrxUpdateItem* item) {
583 return item->status == CrxUpdateItem::kCanUpdate;
584 }
585 };
[email protected]61aca4cd2013-10-26 10:50:59586
[email protected]c4b4cfa2014-03-10 18:55:00587 std::vector<CrxUpdateItem*>::const_iterator it = std::find_if(
588 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;
[email protected]61aca4cd2013-10-26 10:50:59594 return NULL;
595}
596
[email protected]93e8e2c2014-01-04 12:29:23597// Prepares the components for an update check and initiates the request.
[email protected]21a9c9a2014-02-19 19:37:11598// On demand components are always included in the update check request.
599// Otherwise, only include components that have not been checked recently.
[email protected]93e8e2c2014-01-04 12:29:23600bool CrxUpdateService::CheckForUpdates() {
[email protected]21a9c9a2014-02-19 19:37:11601 const base::TimeDelta minimum_recheck_wait_time =
602 base::TimeDelta::FromSeconds(config_->MinimumReCheckWait());
603 const base::Time now(base::Time::Now());
604
[email protected]93e8e2c2014-01-04 12:29:23605 std::vector<CrxUpdateItem*> items_to_check;
606 for (size_t i = 0; i != work_items_.size(); ++i) {
607 CrxUpdateItem* item = work_items_[i];
608 DCHECK(item->status == CrxUpdateItem::kNew ||
609 item->status == CrxUpdateItem::kNoUpdate ||
610 item->status == CrxUpdateItem::kUpToDate ||
611 item->status == CrxUpdateItem::kUpdated);
612
[email protected]21a9c9a2014-02-19 19:37:11613 const base::TimeDelta time_since_last_checked(now - item->last_check);
614
615 if (!item->on_demand &&
616 time_since_last_checked < minimum_recheck_wait_time) {
617 continue;
618 }
619
[email protected]93e8e2c2014-01-04 12:29:23620 ChangeItemState(item, CrxUpdateItem::kChecking);
621
[email protected]21a9c9a2014-02-19 19:37:11622 item->last_check = now;
[email protected]93e8e2c2014-01-04 12:29:23623 item->crx_urls.clear();
624 item->crx_diffurls.clear();
625 item->previous_version = item->component.version;
626 item->next_version = Version();
627 item->previous_fp = item->component.fingerprint;
628 item->next_fp.clear();
629 item->diff_update_failed = false;
630 item->error_category = 0;
631 item->error_code = 0;
632 item->extra_code1 = 0;
633 item->diff_error_category = 0;
634 item->diff_error_code = 0;
635 item->diff_extra_code1 = 0;
636 item->download_metrics.clear();
637
638 items_to_check.push_back(item);
639 }
640
641 if (items_to_check.empty())
642 return false;
643
[email protected]055981f2014-01-17 20:22:32644 update_checker_ = UpdateChecker::Create(
[email protected]93e8e2c2014-01-04 12:29:23645 config_->UpdateUrl(),
646 config_->RequestContext(),
647 base::Bind(&CrxUpdateService::UpdateCheckComplete,
648 base::Unretained(this))).Pass();
649 return update_checker_->CheckForUpdates(items_to_check,
650 config_->ExtraRequestParams());
651}
652
[email protected]61aca4cd2013-10-26 10:50:59653void CrxUpdateService::UpdateComponent(CrxUpdateItem* workitem) {
[email protected]afa378f22013-12-02 03:37:54654 scoped_ptr<CRXContext> crx_context(new CRXContext);
655 crx_context->pk_hash = workitem->component.pk_hash;
656 crx_context->id = workitem->id;
657 crx_context->installer = workitem->component.installer;
658 crx_context->fingerprint = workitem->next_fp;
[email protected]da37c1d2013-12-19 01:04:38659 const std::vector<GURL>* urls = NULL;
[email protected]cfd13e52014-02-05 09:35:07660 bool allow_background_download = false;
[email protected]61aca4cd2013-10-26 10:50:59661 if (CanTryDiffUpdate(workitem, *config_)) {
[email protected]da37c1d2013-12-19 01:04:38662 urls = &workitem->crx_diffurls;
[email protected]61aca4cd2013-10-26 10:50:59663 ChangeItemState(workitem, CrxUpdateItem::kDownloadingDiff);
664 } else {
[email protected]cfd13e52014-02-05 09:35:07665 // Background downloads are enabled only for selected components and
666 // only for full downloads (see issue 340448).
667 allow_background_download = workitem->component.allow_background_download;
[email protected]da37c1d2013-12-19 01:04:38668 urls = &workitem->crx_urls;
[email protected]61aca4cd2013-10-26 10:50:59669 ChangeItemState(workitem, CrxUpdateItem::kDownloading);
670 }
[email protected]3cb2a4f2013-12-07 21:54:34671
672 // On demand component updates are always downloaded in foreground.
[email protected]cfd13e52014-02-05 09:35:07673 const bool is_background_download =
674 !workitem->on_demand && allow_background_download &&
675 config_->UseBackgroundDownloader();
[email protected]3cb2a4f2013-12-07 21:54:34676
[email protected]3a0092d2013-12-18 03:04:35677 crx_downloader_.reset(CrxDownloader::Create(
[email protected]3cb2a4f2013-12-07 21:54:34678 is_background_download,
[email protected]afa378f22013-12-02 03:37:54679 config_->RequestContext(),
680 blocking_task_runner_,
681 base::Bind(&CrxUpdateService::DownloadComplete,
682 base::Unretained(this),
683 base::Passed(&crx_context))));
[email protected]da37c1d2013-12-19 01:04:38684 crx_downloader_->StartDownload(*urls);
[email protected]61aca4cd2013-10-26 10:50:59685}
686
[email protected]93e8e2c2014-01-04 12:29:23687void CrxUpdateService::UpdateCheckComplete(
688 int error,
689 const std::string& error_message,
[email protected]055981f2014-01-17 20:22:32690 const UpdateResponse::Results& results) {
[email protected]e8f96ff2011-08-03 05:07:33691 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23692 update_checker_.reset();
693 if (!error)
694 OnUpdateCheckSucceeded(results);
[email protected]652f4272013-11-13 09:23:41695 else
[email protected]93e8e2c2014-01-04 12:29:23696 OnUpdateCheckFailed(error, error_message);
[email protected]e8f96ff2011-08-03 05:07:33697}
698
[email protected]93e8e2c2014-01-04 12:29:23699// Handles a valid Omaha update check response by matching the results with
700// the registered components which were checked for updates.
701// If updates are found, prepare the components for the actual version upgrade.
702// One of these components will be drafted for the upgrade next time
703// ProcessPendingItems is called.
704void CrxUpdateService::OnUpdateCheckSucceeded(
[email protected]055981f2014-01-17 20:22:32705 const UpdateResponse::Results& results) {
[email protected]2cddef42013-11-22 08:23:22706 size_t num_updates_pending = 0;
[email protected]e8f96ff2011-08-03 05:07:33707 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]055981f2014-01-17 20:22:32708 std::vector<UpdateResponse::Result>::const_iterator it;
[email protected]e8f96ff2011-08-03 05:07:33709 for (it = results.list.begin(); it != results.list.end(); ++it) {
710 CrxUpdateItem* crx = FindUpdateItemById(it->extension_id);
711 if (!crx)
712 continue;
713
[email protected]2cddef42013-11-22 08:23:22714 if (crx->status != CrxUpdateItem::kChecking) {
715 NOTREACHED();
[email protected]e8f96ff2011-08-03 05:07:33716 continue; // Not updating this component now.
[email protected]2cddef42013-11-22 08:23:22717 }
[email protected]e8f96ff2011-08-03 05:07:33718
[email protected]2cddef42013-11-22 08:23:22719 if (it->manifest.version.empty()) {
[email protected]cf442612011-08-09 20:20:12720 // No version means no update available.
[email protected]61aca4cd2013-10-26 10:50:59721 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]cf442612011-08-09 20:20:12722 continue;
[email protected]e8f96ff2011-08-03 05:07:33723 }
[email protected]2cddef42013-11-22 08:23:22724
725 if (!IsVersionNewer(crx->component.version, it->manifest.version)) {
726 // The component is up to date.
[email protected]61aca4cd2013-10-26 10:50:59727 ChangeItemState(crx, CrxUpdateItem::kUpToDate);
[email protected]cf442612011-08-09 20:20:12728 continue;
[email protected]e8f96ff2011-08-03 05:07:33729 }
[email protected]2cddef42013-11-22 08:23:22730
731 if (!it->manifest.browser_min_version.empty()) {
732 if (IsVersionNewer(chrome_version_, it->manifest.browser_min_version)) {
733 // The component is not compatible with this Chrome version.
[email protected]61aca4cd2013-10-26 10:50:59734 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]cf442612011-08-09 20:20:12735 continue;
736 }
[email protected]e8f96ff2011-08-03 05:07:33737 }
[email protected]2cddef42013-11-22 08:23:22738
739 if (it->manifest.packages.size() != 1) {
740 // Assume one and only one package per component.
741 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
742 continue;
743 }
744
745 // Parse the members of the result and queue an upgrade for this component.
[email protected]c5e4a2222014-01-03 16:06:13746 crx->next_version = Version(it->manifest.version);
[email protected]2cddef42013-11-22 08:23:22747
[email protected]055981f2014-01-17 20:22:32748 typedef UpdateResponse::Result::Manifest::Package Package;
[email protected]2cddef42013-11-22 08:23:22749 const Package& package(it->manifest.packages[0]);
750 crx->next_fp = package.fingerprint;
751
[email protected]da37c1d2013-12-19 01:04:38752 // Resolve the urls by combining the base urls with the package names.
753 for (size_t i = 0; i != it->crx_urls.size(); ++i) {
754 const GURL url(it->crx_urls[i].Resolve(package.name));
755 if (url.is_valid())
756 crx->crx_urls.push_back(url);
757 }
758 for (size_t i = 0; i != it->crx_diffurls.size(); ++i) {
759 const GURL url(it->crx_diffurls[i].Resolve(package.namediff));
760 if (url.is_valid())
761 crx->crx_diffurls.push_back(url);
762 }
[email protected]2cddef42013-11-22 08:23:22763
[email protected]61aca4cd2013-10-26 10:50:59764 ChangeItemState(crx, CrxUpdateItem::kCanUpdate);
[email protected]2cddef42013-11-22 08:23:22765 ++num_updates_pending;
[email protected]e8f96ff2011-08-03 05:07:33766 }
[email protected]cf442612011-08-09 20:20:12767
[email protected]2cddef42013-11-22 08:23:22768 // All components that are not included in the update response are
769 // considered up to date.
[email protected]cf442612011-08-09 20:20:12770 ChangeItemStatus(CrxUpdateItem::kChecking, CrxUpdateItem::kUpToDate);
771
[email protected]32a6c8382013-08-20 00:29:20772 // If there are updates pending we do a short wait, otherwise we take
773 // a longer delay until we check the components again.
[email protected]21a9c9a2014-02-19 19:37:11774 ScheduleNextRun(num_updates_pending > 0 ? kStepDelayShort : kStepDelayLong);
[email protected]e8f96ff2011-08-03 05:07:33775}
776
[email protected]93e8e2c2014-01-04 12:29:23777// TODO: record UMA stats.
778void CrxUpdateService::OnUpdateCheckFailed(int error,
779 const std::string& error_message) {
[email protected]e8f96ff2011-08-03 05:07:33780 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23781 DCHECK(error);
[email protected]e8f96ff2011-08-03 05:07:33782 size_t count = ChangeItemStatus(CrxUpdateItem::kChecking,
783 CrxUpdateItem::kNoUpdate);
784 DCHECK_GT(count, 0ul);
[email protected]32a6c8382013-08-20 00:29:20785 ScheduleNextRun(kStepDelayLong);
[email protected]e8f96ff2011-08-03 05:07:33786}
787
788// Called when the CRX package has been downloaded to a temporary location.
789// Here we fire the notifications and schedule the component-specific installer
790// to be called in the file thread.
[email protected]3cb2a4f2013-12-07 21:54:34791void CrxUpdateService::DownloadComplete(
792 scoped_ptr<CRXContext> crx_context,
[email protected]3a0092d2013-12-18 03:04:35793 const CrxDownloader::Result& download_result) {
[email protected]e8f96ff2011-08-03 05:07:33794 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]64f39fa12013-09-03 21:49:37795
[email protected]64f39fa12013-09-03 21:49:37796 CrxUpdateItem* crx = FindUpdateItemById(crx_context->id);
[email protected]e3e696d32013-06-21 20:41:36797 DCHECK(crx->status == CrxUpdateItem::kDownloadingDiff ||
798 crx->status == CrxUpdateItem::kDownloading);
799
[email protected]3a0092d2013-12-18 03:04:35800 AppendDownloadMetrics(crx_downloader_->download_metrics(),
801 &crx->download_metrics);
802
[email protected]3cb2a4f2013-12-07 21:54:34803 if (download_result.error) {
[email protected]e3e696d32013-06-21 20:41:36804 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
[email protected]7b0529242013-07-20 05:45:46805 crx->diff_error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34806 crx->diff_error_code = download_result.error;
[email protected]e630ba62013-06-22 15:22:34807 crx->diff_update_failed = true;
[email protected]e3e696d32013-06-21 20:41:36808 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
809 CrxUpdateItem::kCanUpdate);
810 DCHECK_EQ(count, 1ul);
[email protected]afa378f22013-12-02 03:37:54811 crx_downloader_.reset();
[email protected]7b0529242013-07-20 05:45:46812
[email protected]32a6c8382013-08-20 00:29:20813 ScheduleNextRun(kStepDelayShort);
[email protected]e3e696d32013-06-21 20:41:36814 return;
815 }
[email protected]7b0529242013-07-20 05:45:46816 crx->error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34817 crx->error_code = download_result.error;
[email protected]e8f96ff2011-08-03 05:07:33818 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloading,
819 CrxUpdateItem::kNoUpdate);
820 DCHECK_EQ(count, 1ul);
[email protected]afa378f22013-12-02 03:37:54821 crx_downloader_.reset();
[email protected]e3e696d32013-06-21 20:41:36822
[email protected]7b0529242013-07-20 05:45:46823 // At this point, since both the differential and the full downloads failed,
824 // the update for this component has finished with an error.
825 ping_manager_->OnUpdateComplete(crx);
826
[email protected]32a6c8382013-08-20 00:29:20827 // Move on to the next update, if there is one available.
828 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33829 } else {
[email protected]e3e696d32013-06-21 20:41:36830 size_t count = 0;
831 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
832 count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
833 CrxUpdateItem::kUpdatingDiff);
834 } else {
835 count = ChangeItemStatus(CrxUpdateItem::kDownloading,
836 CrxUpdateItem::kUpdating);
837 }
[email protected]e8f96ff2011-08-03 05:07:33838 DCHECK_EQ(count, 1ul);
[email protected]afa378f22013-12-02 03:37:54839 crx_downloader_.reset();
[email protected]cf442612011-08-09 20:20:12840
[email protected]44da56e2011-11-21 19:59:14841 // Why unretained? See comment at top of file.
[email protected]8f5f2ea2013-10-31 09:39:10842 blocking_task_runner_->PostDelayedTask(
[email protected]73251e72012-03-04 02:10:33843 FROM_HERE,
[email protected]44da56e2011-11-21 19:59:14844 base::Bind(&CrxUpdateService::Install,
845 base::Unretained(this),
[email protected]afa378f22013-12-02 03:37:54846 base::Passed(&crx_context),
[email protected]3cb2a4f2013-12-07 21:54:34847 download_result.response),
[email protected]73251e72012-03-04 02:10:33848 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]e8f96ff2011-08-03 05:07:33849 }
[email protected]e8f96ff2011-08-03 05:07:33850}
851
852// Install consists of digital signature verification, unpacking and then
853// calling the component specific installer. All that is handled by the
[email protected]f5d27e32014-01-31 06:48:53854// |unpacker_|. If there is an error this function is in charge of deleting
[email protected]e8f96ff2011-08-03 05:07:33855// the files created.
[email protected]afa378f22013-12-02 03:37:54856void CrxUpdateService::Install(scoped_ptr<CRXContext> context,
[email protected]650b2d52013-02-10 03:41:45857 const base::FilePath& crx_path) {
[email protected]8f5f2ea2013-10-31 09:39:10858 // This function owns the file at |crx_path| and the |context| object.
[email protected]f5d27e32014-01-31 06:48:53859 unpacker_.reset(new ComponentUnpacker(context->pk_hash,
860 crx_path,
861 context->fingerprint,
862 component_patcher_.get(),
863 context->installer,
864 blocking_task_runner_));
865 unpacker_->Unpack(base::Bind(&CrxUpdateService::EndUnpacking,
866 base::Unretained(this),
867 context->id,
868 crx_path));
869}
870
871void CrxUpdateService::EndUnpacking(const std::string& component_id,
872 const base::FilePath& crx_path,
873 ComponentUnpacker::Error error,
874 int extended_error) {
[email protected]055981f2014-01-17 20:22:32875 if (!DeleteFileAndEmptyParentDirectory(crx_path))
[email protected]e8f96ff2011-08-03 05:07:33876 NOTREACHED() << crx_path.value();
[email protected]73251e72012-03-04 02:10:33877 BrowserThread::PostDelayedTask(
878 BrowserThread::UI,
879 FROM_HERE,
[email protected]44da56e2011-11-21 19:59:14880 base::Bind(&CrxUpdateService::DoneInstalling, base::Unretained(this),
[email protected]f5d27e32014-01-31 06:48:53881 component_id, error, extended_error),
[email protected]73251e72012-03-04 02:10:33882 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]e8f96ff2011-08-03 05:07:33883}
884
885// Installation has been completed. Adjust the component status and
[email protected]7b0529242013-07-20 05:45:46886// schedule the next check. Schedule a short delay before trying the full
887// update when the differential update failed.
[email protected]07f93af12011-08-17 20:57:22888void CrxUpdateService::DoneInstalling(const std::string& component_id,
[email protected]e3e696d32013-06-21 20:41:36889 ComponentUnpacker::Error error,
890 int extra_code) {
[email protected]e8f96ff2011-08-03 05:07:33891 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]07f93af12011-08-17 20:57:22892
[email protected]7b0529242013-07-20 05:45:46893 ErrorCategory error_category = kErrorNone;
894 switch (error) {
895 case ComponentUnpacker::kNone:
896 break;
897 case ComponentUnpacker::kInstallerError:
898 error_category = kInstallError;
899 break;
900 default:
901 error_category = kUnpackError;
902 break;
903 }
904
905 const bool is_success = error == ComponentUnpacker::kNone;
906
[email protected]07f93af12011-08-17 20:57:22907 CrxUpdateItem* item = FindUpdateItemById(component_id);
[email protected]7b0529242013-07-20 05:45:46908 if (item->status == CrxUpdateItem::kUpdatingDiff && !is_success) {
909 item->diff_error_category = error_category;
910 item->diff_error_code = error;
911 item->diff_extra_code1 = extra_code;
[email protected]1ea21ad2013-09-02 04:20:39912 item->diff_update_failed = true;
913 size_t count = ChangeItemStatus(CrxUpdateItem::kUpdatingDiff,
914 CrxUpdateItem::kCanUpdate);
915 DCHECK_EQ(count, 1ul);
916 ScheduleNextRun(kStepDelayShort);
917 return;
[email protected]e3e696d32013-06-21 20:41:36918 }
[email protected]e3e696d32013-06-21 20:41:36919
[email protected]7b0529242013-07-20 05:45:46920 if (is_success) {
[email protected]61aca4cd2013-10-26 10:50:59921 ChangeItemState(item, CrxUpdateItem::kUpdated);
[email protected]07f93af12011-08-17 20:57:22922 item->component.version = item->next_version;
[email protected]e3e696d32013-06-21 20:41:36923 item->component.fingerprint = item->next_fp;
[email protected]7b0529242013-07-20 05:45:46924 } else {
[email protected]61aca4cd2013-10-26 10:50:59925 ChangeItemState(item, CrxUpdateItem::kNoUpdate);
[email protected]7b0529242013-07-20 05:45:46926 item->error_category = error_category;
927 item->error_code = error;
928 item->extra_code1 = extra_code;
[email protected]e3e696d32013-06-21 20:41:36929 }
[email protected]07f93af12011-08-17 20:57:22930
[email protected]7b0529242013-07-20 05:45:46931 ping_manager_->OnUpdateComplete(item);
[email protected]360b8bb2011-09-01 21:48:06932
[email protected]32a6c8382013-08-20 00:29:20933 // Move on to the next update, if there is one available.
934 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33935}
936
[email protected]85e61d52013-08-01 22:23:42937void CrxUpdateService::NotifyComponentObservers(
938 ComponentObserver::Events event, int extra) const {
939 for (UpdateItems::const_iterator it = work_items_.begin();
940 it != work_items_.end(); ++it) {
941 ComponentObserver* observer = (*it)->component.observer;
942 if (observer)
943 observer->OnEvent(event, 0);
944 }
945}
946
[email protected]00a77fa2013-11-02 04:18:46947content::ResourceThrottle* CrxUpdateService::GetOnDemandResourceThrottle(
948 net::URLRequest* request, const std::string& crx_id) {
949 // We give the raw pointer to the caller, who will delete it at will
950 // and we keep for ourselves a weak pointer to it so we can post tasks
951 // from the UI thread without having to track lifetime directly.
952 CUResourceThrottle* rt = new CUResourceThrottle(request);
953 BrowserThread::PostTask(
954 BrowserThread::UI,
955 FROM_HERE,
956 base::Bind(&CrxUpdateService::OnNewResourceThrottle,
957 base::Unretained(this),
958 rt->AsWeakPtr(),
959 crx_id));
960 return rt;
961}
962
963void CrxUpdateService::OnNewResourceThrottle(
964 base::WeakPtr<CUResourceThrottle> rt, const std::string& crx_id) {
965 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
966 // Check if we can on-demand update, else unblock the request anyway.
967 CrxUpdateItem* item = FindUpdateItemById(crx_id);
968 Status status = OnDemandUpdateInternal(item);
969 if (status == kOk || status == kInProgress) {
970 item->throttles.push_back(rt);
971 return;
972 }
973 UnblockResourceThrottle(rt);
974}
975
976///////////////////////////////////////////////////////////////////////////////
977
978CUResourceThrottle::CUResourceThrottle(const net::URLRequest* request)
979 : state_(NEW) {
980 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
981}
982
983CUResourceThrottle::~CUResourceThrottle() {
984 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
985}
986
987void CUResourceThrottle::WillStartRequest(bool* defer) {
988 if (state_ != UNBLOCKED) {
989 state_ = BLOCKED;
990 *defer = true;
991 } else {
992 *defer = false;
993 }
994}
995
996void CUResourceThrottle::WillRedirectRequest(const GURL& new_url, bool* defer) {
997 WillStartRequest(defer);
998}
999
[email protected]f8fe5cf2013-12-04 20:11:531000const char* CUResourceThrottle::GetNameForLogging() const {
1001 return "ComponentUpdateResourceThrottle";
1002}
1003
[email protected]00a77fa2013-11-02 04:18:461004void CUResourceThrottle::Unblock() {
1005 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1006 if (state_ == BLOCKED)
1007 controller()->Resume();
1008 state_ = UNBLOCKED;
1009}
1010
[email protected]e8f96ff2011-08-03 05:07:331011// The component update factory. Using the component updater as a singleton
1012// is the job of the browser process.
1013ComponentUpdateService* ComponentUpdateServiceFactory(
1014 ComponentUpdateService::Configurator* config) {
1015 DCHECK(config);
1016 return new CrxUpdateService(config);
1017}
[email protected]2cddef42013-11-22 08:23:221018
[email protected]055981f2014-01-17 20:22:321019} // namespace component_updater
1020