blob: 135bca66d96796515ab5fc8781a8c879ba034189 [file] [log] [blame]
[email protected]93e8e2c2014-01-04 12:29:231// Copyright 2012 The Chromium Authors. All rights reserved.
[email protected]e8f96ff2011-08-03 05:07:332// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/component_updater/component_updater_service.h"
6
7#include <algorithm>
[email protected]ccb4feef2013-02-14 06:16:478#include <set>
[email protected]e8f96ff2011-08-03 05:07:339#include <vector>
10
11#include "base/at_exit.h"
[email protected]44da56e2011-11-21 19:59:1412#include "base/bind.h"
[email protected]e3e696d32013-06-21 20:41:3613#include "base/compiler_specific.h"
[email protected]e8f96ff2011-08-03 05:07:3314#include "base/file_util.h"
[email protected]57999812013-02-24 05:40:5215#include "base/files/file_path.h"
[email protected]e8f96ff2011-08-03 05:07:3316#include "base/logging.h"
[email protected]7226b33c2011-08-18 08:44:2217#include "base/memory/scoped_ptr.h"
[email protected]00a77fa2013-11-02 04:18:4618#include "base/memory/weak_ptr.h"
[email protected]d3268fe2014-04-25 02:14:2319#include "base/observer_list.h"
[email protected]f5d27e32014-01-31 06:48:5320#include "base/sequenced_task_runner.h"
[email protected]e8f96ff2011-08-03 05:07:3321#include "base/stl_util.h"
[email protected]8f5f2ea2013-10-31 09:39:1022#include "base/threading/sequenced_worker_pool.h"
[email protected]41a17c52013-06-28 00:27:5323#include "base/timer/timer.h"
[email protected]e8f96ff2011-08-03 05:07:3324#include "chrome/browser/browser_process.h"
25#include "chrome/browser/component_updater/component_unpacker.h"
[email protected]7b0529242013-07-20 05:45:4626#include "chrome/browser/component_updater/component_updater_ping_manager.h"
[email protected]2cddef42013-11-22 08:23:2227#include "chrome/browser/component_updater/component_updater_utils.h"
[email protected]afa378f22013-12-02 03:37:5428#include "chrome/browser/component_updater/crx_downloader.h"
[email protected]7b0529242013-07-20 05:45:4629#include "chrome/browser/component_updater/crx_update_item.h"
[email protected]93e8e2c2014-01-04 12:29:2330#include "chrome/browser/component_updater/update_checker.h"
[email protected]6268d3a2013-11-27 01:28:0931#include "chrome/browser/component_updater/update_response.h"
[email protected]e8f96ff2011-08-03 05:07:3332#include "chrome/common/chrome_version_info.h"
[email protected]b7b63872013-01-03 02:41:1933#include "content/public/browser/browser_thread.h"
[email protected]00a77fa2013-11-02 04:18:4634#include "content/public/browser/resource_controller.h"
35#include "content/public/browser/resource_throttle.h"
[email protected]761fa4702013-07-02 15:25:1536#include "url/gurl.h"
[email protected]e8f96ff2011-08-03 05:07:3337
[email protected]631bb742011-11-02 11:29:3938using content::BrowserThread;
39
[email protected]055981f2014-01-17 20:22:3240namespace component_updater {
[email protected]3a0092d2013-12-18 03:04:3541
[email protected]44da56e2011-11-21 19:59:1442// The component updater is designed to live until process shutdown, so
43// base::Bind() calls are not refcounted.
44
[email protected]e8f96ff2011-08-03 05:07:3345namespace {
[email protected]e3e696d32013-06-21 20:41:3646
[email protected]2cddef42013-11-22 08:23:2247// Returns true if the |proposed| version is newer than |current| version.
[email protected]c5e4a2222014-01-03 16:06:1348bool IsVersionNewer(const Version& current, const std::string& proposed) {
49 Version proposed_ver(proposed);
[email protected]2cddef42013-11-22 08:23:2250 return proposed_ver.IsValid() && current.CompareTo(proposed_ver) < 0;
[email protected]e8f96ff2011-08-03 05:07:3351}
52
[email protected]e3e696d32013-06-21 20:41:3653// Returns true if a differential update is available, it has not failed yet,
54// and the configuration allows it.
55bool CanTryDiffUpdate(const CrxUpdateItem* update_item,
56 const ComponentUpdateService::Configurator& config) {
[email protected]d0c8b8b42014-05-06 05:11:4557 return HasDiffUpdate(update_item) && !update_item->diff_update_failed &&
[email protected]e3e696d32013-06-21 20:41:3658 config.DeltasEnabled();
59}
60
[email protected]3a0092d2013-12-18 03:04:3561void AppendDownloadMetrics(
62 const std::vector<CrxDownloader::DownloadMetrics>& source,
63 std::vector<CrxDownloader::DownloadMetrics>* destination) {
64 destination->insert(destination->end(), source.begin(), source.end());
65}
66
[email protected]7b0529242013-07-20 05:45:4667} // namespace
68
69CrxUpdateItem::CrxUpdateItem()
70 : status(kNew),
[email protected]61aca4cd2013-10-26 10:50:5971 on_demand(false),
[email protected]7b0529242013-07-20 05:45:4672 diff_update_failed(false),
73 error_category(0),
74 error_code(0),
75 extra_code1(0),
76 diff_error_category(0),
77 diff_error_code(0),
78 diff_extra_code1(0) {
79}
80
81CrxUpdateItem::~CrxUpdateItem() {
82}
[email protected]86550a42013-06-21 15:20:4983
[email protected]dc06f0b2013-01-23 20:03:1684CrxComponent::CrxComponent()
[email protected]d0c8b8b42014-05-06 05:11:4585 : installer(NULL), allow_background_download(true) {
[email protected]dc06f0b2013-01-23 20:03:1686}
87
88CrxComponent::~CrxComponent() {
89}
[email protected]e8f96ff2011-08-03 05:07:3390
[email protected]00a77fa2013-11-02 04:18:4691///////////////////////////////////////////////////////////////////////////////
92// In charge of blocking url requests until the |crx_id| component has been
93// updated. This class is touched solely from the IO thread. The UI thread
94// can post tasks to it via weak pointers. By default the request is blocked
95// unless the CrxUpdateService calls Unblock().
96// The lifetime is controlled by Chrome's resource loader so the component
97// updater cannot touch objects from this class except via weak pointers.
[email protected]d0c8b8b42014-05-06 05:11:4598class CUResourceThrottle : public content::ResourceThrottle,
99 public base::SupportsWeakPtr<CUResourceThrottle> {
[email protected]00a77fa2013-11-02 04:18:46100 public:
101 explicit CUResourceThrottle(const net::URLRequest* request);
102 virtual ~CUResourceThrottle();
[email protected]2cddef42013-11-22 08:23:22103
[email protected]00a77fa2013-11-02 04:18:46104 // Overriden from ResourceThrottle.
105 virtual void WillStartRequest(bool* defer) OVERRIDE;
106 virtual void WillRedirectRequest(const GURL& new_url, bool* defer) OVERRIDE;
[email protected]f8fe5cf2013-12-04 20:11:53107 virtual const char* GetNameForLogging() const OVERRIDE;
[email protected]00a77fa2013-11-02 04:18:46108
109 // Component updater calls this function via PostTask to unblock the request.
110 void Unblock();
111
112 typedef std::vector<base::WeakPtr<CUResourceThrottle> > WeakPtrVector;
113
114 private:
[email protected]d0c8b8b42014-05-06 05:11:45115 enum State { NEW, BLOCKED, UNBLOCKED };
[email protected]00a77fa2013-11-02 04:18:46116
[email protected]2cddef42013-11-22 08:23:22117 State state_;
[email protected]00a77fa2013-11-02 04:18:46118};
119
120void UnblockResourceThrottle(base::WeakPtr<CUResourceThrottle> rt) {
[email protected]d0c8b8b42014-05-06 05:11:45121 BrowserThread::PostTask(BrowserThread::IO,
122 FROM_HERE,
123 base::Bind(&CUResourceThrottle::Unblock, rt));
[email protected]00a77fa2013-11-02 04:18:46124}
125
126void UnblockandReapAllThrottles(CUResourceThrottle::WeakPtrVector* throttles) {
127 CUResourceThrottle::WeakPtrVector::iterator it;
128 for (it = throttles->begin(); it != throttles->end(); ++it)
129 UnblockResourceThrottle(*it);
130 throttles->clear();
131}
132
[email protected]e8f96ff2011-08-03 05:07:33133//////////////////////////////////////////////////////////////////////////////
134// The one and only implementation of the ComponentUpdateService interface. In
135// charge of running the show. The main method is ProcessPendingItems() which
[email protected]50b01392012-09-01 03:33:13136// is called periodically to do the upgrades/installs or the update checks.
[email protected]e8f96ff2011-08-03 05:07:33137// An important consideration here is to be as "low impact" as we can to the
138// rest of the browser, so even if we have many components registered and
[email protected]f1050432012-02-15 01:35:46139// eligible for update, we only do one thing at a time with pauses in between
[email protected]e8f96ff2011-08-03 05:07:33140// the tasks. Also when we do network requests there is only one |url_fetcher_|
[email protected]e3e696d32013-06-21 20:41:36141// in flight at a time.
[email protected]e8f96ff2011-08-03 05:07:33142// There are no locks in this code, the main structure |work_items_| is mutated
[email protected]74be2642014-02-07 09:40:37143// only from the UI thread. The unpack and installation is done in a blocking
144// pool thread. The network requests are done in the IO thread or in the file
[email protected]e8f96ff2011-08-03 05:07:33145// thread.
[email protected]504830a2014-05-20 21:53:54146class CrxUpdateService : public ComponentUpdateService, public OnDemandUpdater {
[email protected]e8f96ff2011-08-03 05:07:33147 public:
148 explicit CrxUpdateService(ComponentUpdateService::Configurator* config);
[email protected]e8f96ff2011-08-03 05:07:33149 virtual ~CrxUpdateService();
150
151 // Overrides for ComponentUpdateService.
[email protected]d3268fe2014-04-25 02:14:23152 virtual void AddObserver(Observer* observer) OVERRIDE;
153 virtual void RemoveObserver(Observer* observer) OVERRIDE;
[email protected]e8f96ff2011-08-03 05:07:33154 virtual Status Start() OVERRIDE;
155 virtual Status Stop() OVERRIDE;
156 virtual Status RegisterComponent(const CrxComponent& component) OVERRIDE;
[email protected]68bf09e2014-06-03 00:10:56157 virtual std::vector<std::string> GetComponentIDs() const OVERRIDE;
158 virtual CrxUpdateItem* GetComponentDetails(
159 const std::string& component_id) const OVERRIDE;
[email protected]504830a2014-05-20 21:53:54160 virtual OnDemandUpdater& GetOnDemandUpdater() OVERRIDE;
161
162 // Overrides for OnDemandUpdater.
[email protected]00a77fa2013-11-02 04:18:46163 virtual content::ResourceThrottle* GetOnDemandResourceThrottle(
[email protected]d0c8b8b42014-05-06 05:11:45164 net::URLRequest* request,
165 const std::string& crx_id) OVERRIDE;
[email protected]504830a2014-05-20 21:53:54166 virtual Status OnDemandUpdate(const std::string& component_id) OVERRIDE;
[email protected]e8f96ff2011-08-03 05:07:33167
[email protected]28ea9ac2014-05-03 22:07:18168 // Context for a crx download url request.
169 struct CRXContext {
170 ComponentInstaller* installer;
171 std::vector<uint8> pk_hash;
172 std::string id;
173 std::string fingerprint;
174 CRXContext() : installer(NULL) {}
175 };
[email protected]e8f96ff2011-08-03 05:07:33176
[email protected]e8f96ff2011-08-03 05:07:33177 private:
[email protected]7b0529242013-07-20 05:45:46178 enum ErrorCategory {
179 kErrorNone = 0,
180 kNetworkError,
181 kUnpackError,
182 kInstallError,
183 };
184
[email protected]32a6c8382013-08-20 00:29:20185 enum StepDelayInterval {
186 kStepDelayShort = 0,
187 kStepDelayMedium,
188 kStepDelayLong,
189 };
190
[email protected]055981f2014-01-17 20:22:32191 void UpdateCheckComplete(int error,
192 const std::string& error_message,
193 const UpdateResponse::Results& results);
194 void OnUpdateCheckSucceeded(const UpdateResponse::Results& results);
[email protected]93e8e2c2014-01-04 12:29:23195 void OnUpdateCheckFailed(int error, const std::string& error_message);
[email protected]e8f96ff2011-08-03 05:07:33196
[email protected]8a5ebd432014-05-02 00:21:22197 void DownloadProgress(const std::string& component_id,
198 const CrxDownloader::Result& download_result);
199
200 void DownloadComplete(scoped_ptr<CRXContext> crx_context,
201 const CrxDownloader::Result& download_result);
[email protected]afa378f22013-12-02 03:37:54202
[email protected]00a77fa2013-11-02 04:18:46203 Status OnDemandUpdateInternal(CrxUpdateItem* item);
[email protected]5b53f932014-06-06 10:26:54204 Status OnDemandUpdateWithCooldown(CrxUpdateItem* item);
[email protected]00a77fa2013-11-02 04:18:46205
[email protected]e8f96ff2011-08-03 05:07:33206 void ProcessPendingItems();
207
[email protected]93e8e2c2014-01-04 12:29:23208 // Find a component that is ready to update.
209 CrxUpdateItem* FindReadyComponent() const;
210
211 // Prepares the components for an update check and initiates the request.
212 // Returns true if an update check request has been made. Returns false if
213 // no update check was needed or an error occured.
214 bool CheckForUpdates();
[email protected]61aca4cd2013-10-26 10:50:59215
216 void UpdateComponent(CrxUpdateItem* workitem);
217
[email protected]32a6c8382013-08-20 00:29:20218 void ScheduleNextRun(StepDelayInterval step_delay);
[email protected]e8f96ff2011-08-03 05:07:33219
[email protected]6268d3a2013-11-27 01:28:09220 void ParseResponse(const std::string& xml);
[email protected]e8f96ff2011-08-03 05:07:33221
[email protected]afa378f22013-12-02 03:37:54222 void Install(scoped_ptr<CRXContext> context, const base::FilePath& crx_path);
[email protected]e8f96ff2011-08-03 05:07:33223
[email protected]f5d27e32014-01-31 06:48:53224 void EndUnpacking(const std::string& component_id,
225 const base::FilePath& crx_path,
226 ComponentUnpacker::Error error,
227 int extended_error);
228
[email protected]07f93af12011-08-17 20:57:22229 void DoneInstalling(const std::string& component_id,
[email protected]e3e696d32013-06-21 20:41:36230 ComponentUnpacker::Error error,
231 int extended_error);
[email protected]e8f96ff2011-08-03 05:07:33232
[email protected]61aca4cd2013-10-26 10:50:59233 void ChangeItemState(CrxUpdateItem* item, CrxUpdateItem::Status to);
234
[email protected]d0c8b8b42014-05-06 05:11:45235 size_t ChangeItemStatus(CrxUpdateItem::Status from, CrxUpdateItem::Status to);
[email protected]e8f96ff2011-08-03 05:07:33236
[email protected]68bf09e2014-06-03 00:10:56237 CrxUpdateItem* FindUpdateItemById(const std::string& id) const;
[email protected]e8f96ff2011-08-03 05:07:33238
[email protected]d3268fe2014-04-25 02:14:23239 void NotifyObservers(Observer::Events event, const std::string& id);
[email protected]85e61d52013-08-01 22:23:42240
[email protected]61aca4cd2013-10-26 10:50:59241 bool HasOnDemandItems() const;
242
[email protected]00a77fa2013-11-02 04:18:46243 void OnNewResourceThrottle(base::WeakPtr<CUResourceThrottle> rt,
244 const std::string& crx_id);
245
[email protected]68bf09e2014-06-03 00:10:56246 Status GetServiceStatus(const CrxUpdateItem::Status status);
247
[email protected]e3e696d32013-06-21 20:41:36248 scoped_ptr<ComponentUpdateService::Configurator> config_;
249
[email protected]055981f2014-01-17 20:22:32250 scoped_ptr<UpdateChecker> update_checker_;
[email protected]e8f96ff2011-08-03 05:07:33251
[email protected]055981f2014-01-17 20:22:32252 scoped_ptr<PingManager> ping_manager_;
[email protected]7b0529242013-07-20 05:45:46253
[email protected]94a481b2014-03-28 19:41:55254 scoped_refptr<ComponentUnpacker> unpacker_;
[email protected]f5d27e32014-01-31 06:48:53255
[email protected]055981f2014-01-17 20:22:32256 scoped_ptr<CrxDownloader> crx_downloader_;
[email protected]afa378f22013-12-02 03:37:54257
[email protected]86550a42013-06-21 15:20:49258 // A collection of every work item.
[email protected]e3e696d32013-06-21 20:41:36259 typedef std::vector<CrxUpdateItem*> UpdateItems;
[email protected]e8f96ff2011-08-03 05:07:33260 UpdateItems work_items_;
261
262 base::OneShotTimer<CrxUpdateService> timer_;
263
[email protected]8f5f2ea2013-10-31 09:39:10264 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
265
[email protected]c5e4a2222014-01-03 16:06:13266 const Version chrome_version_;
[email protected]e8f96ff2011-08-03 05:07:33267
268 bool running_;
269
[email protected]d3268fe2014-04-25 02:14:23270 ObserverList<Observer> observer_list_;
271
[email protected]e8f96ff2011-08-03 05:07:33272 DISALLOW_COPY_AND_ASSIGN(CrxUpdateService);
273};
274
[email protected]e8f96ff2011-08-03 05:07:33275//////////////////////////////////////////////////////////////////////////////
276
[email protected]e3e696d32013-06-21 20:41:36277CrxUpdateService::CrxUpdateService(ComponentUpdateService::Configurator* config)
[email protected]e8f96ff2011-08-03 05:07:33278 : config_(config),
[email protected]d0c8b8b42014-05-06 05:11:45279 ping_manager_(
280 new PingManager(config->PingUrl(), config->RequestContext())),
281 blocking_task_runner_(
282 BrowserThread::GetBlockingPool()->
283 GetSequencedTaskRunnerWithShutdownBehavior(
284 BrowserThread::GetBlockingPool()->GetSequenceToken(),
285 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)),
[email protected]e8f96ff2011-08-03 05:07:33286 chrome_version_(chrome::VersionInfo().Version()),
287 running_(false) {
[email protected]1ea21ad2013-09-02 04:20:39288}
[email protected]e8f96ff2011-08-03 05:07:33289
290CrxUpdateService::~CrxUpdateService() {
291 // Because we are a singleton, at this point only the UI thread should be
292 // alive, this simplifies the management of the work that could be in
293 // flight in other threads.
294 Stop();
295 STLDeleteElements(&work_items_);
[email protected]1ea21ad2013-09-02 04:20:39296}
[email protected]e8f96ff2011-08-03 05:07:33297
[email protected]d3268fe2014-04-25 02:14:23298void CrxUpdateService::AddObserver(Observer* observer) {
299 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
300 observer_list_.AddObserver(observer);
301}
302
303void CrxUpdateService::RemoveObserver(Observer* observer) {
304 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
305 observer_list_.RemoveObserver(observer);
306}
307
[email protected]e8f96ff2011-08-03 05:07:33308ComponentUpdateService::Status CrxUpdateService::Start() {
309 // Note that RegisterComponent will call Start() when the first
310 // component is registered, so it can be called twice. This way
311 // we avoid scheduling the timer if there is no work to do.
[email protected]fb53e652014-04-30 11:27:19312 VLOG(1) << "CrxUpdateService starting up";
[email protected]e8f96ff2011-08-03 05:07:33313 running_ = true;
314 if (work_items_.empty())
315 return kOk;
316
[email protected]d3268fe2014-04-25 02:14:23317 NotifyObservers(Observer::COMPONENT_UPDATER_STARTED, "");
[email protected]85e61d52013-08-01 22:23:42318
[email protected]fb53e652014-04-30 11:27:19319 VLOG(1) << "First update attempt will take place in "
320 << config_->InitialDelay() << " seconds";
[email protected]d0c8b8b42014-05-06 05:11:45321 timer_.Start(FROM_HERE,
322 base::TimeDelta::FromSeconds(config_->InitialDelay()),
323 this,
324 &CrxUpdateService::ProcessPendingItems);
[email protected]e8f96ff2011-08-03 05:07:33325 return kOk;
326}
327
328// Stop the main check + update loop. In flight operations will be
329// completed.
330ComponentUpdateService::Status CrxUpdateService::Stop() {
[email protected]fb53e652014-04-30 11:27:19331 VLOG(1) << "CrxUpdateService stopping";
[email protected]e8f96ff2011-08-03 05:07:33332 running_ = false;
333 timer_.Stop();
334 return kOk;
335}
336
[email protected]61aca4cd2013-10-26 10:50:59337bool CrxUpdateService::HasOnDemandItems() const {
338 class Helper {
339 public:
[email protected]d0c8b8b42014-05-06 05:11:45340 static bool IsOnDemand(CrxUpdateItem* item) { return item->on_demand; }
[email protected]61aca4cd2013-10-26 10:50:59341 };
342 return std::find_if(work_items_.begin(),
343 work_items_.end(),
344 Helper::IsOnDemand) != work_items_.end();
345}
346
[email protected]ccb4feef2013-02-14 06:16:47347// This function sets the timer which will call ProcessPendingItems() or
[email protected]61aca4cd2013-10-26 10:50:59348// ProcessRequestedItem() if there is an on_demand item. There
[email protected]32a6c8382013-08-20 00:29:20349// are three kinds of waits:
350// - a short delay, when there is immediate work to be done.
351// - a medium delay, when there are updates to be applied within the current
352// update cycle, or there are components that are still unchecked.
353// - a long delay when a full check/update cycle has completed for all
354// components.
355void CrxUpdateService::ScheduleNextRun(StepDelayInterval step_delay) {
[email protected]e8f96ff2011-08-03 05:07:33356 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23357 DCHECK(!update_checker_);
[email protected]e8f96ff2011-08-03 05:07:33358 CHECK(!timer_.IsRunning());
359 // It could be the case that Stop() had been called while a url request
360 // or unpacking was in flight, if so we arrive here but |running_| is
361 // false. In that case do not loop again.
362 if (!running_)
363 return;
364
[email protected]ccb4feef2013-02-14 06:16:47365 // Keep the delay short if in the middle of an update (step_delay),
366 // or there are new requested_work_items_ that have not been processed yet.
[email protected]32a6c8382013-08-20 00:29:20367 int64 delay_seconds = 0;
[email protected]61aca4cd2013-10-26 10:50:59368 if (!HasOnDemandItems()) {
[email protected]32a6c8382013-08-20 00:29:20369 switch (step_delay) {
370 case kStepDelayShort:
371 delay_seconds = config_->StepDelay();
372 break;
373 case kStepDelayMedium:
374 delay_seconds = config_->StepDelayMedium();
375 break;
376 case kStepDelayLong:
377 delay_seconds = config_->NextCheckDelay();
378 break;
379 }
380 } else {
381 delay_seconds = config_->StepDelay();
382 }
[email protected]cf442612011-08-09 20:20:12383
[email protected]32a6c8382013-08-20 00:29:20384 if (step_delay != kStepDelayShort) {
[email protected]d3268fe2014-04-25 02:14:23385 NotifyObservers(Observer::COMPONENT_UPDATER_SLEEPING, "");
[email protected]85e61d52013-08-01 22:23:42386
[email protected]e8f96ff2011-08-03 05:07:33387 // Zero is only used for unit tests.
[email protected]32a6c8382013-08-20 00:29:20388 if (0 == delay_seconds)
[email protected]e8f96ff2011-08-03 05:07:33389 return;
390 }
391
[email protected]fb53e652014-04-30 11:27:19392 VLOG(1) << "Scheduling next run to occur in " << delay_seconds << " seconds";
[email protected]d0c8b8b42014-05-06 05:11:45393 timer_.Start(FROM_HERE,
394 base::TimeDelta::FromSeconds(delay_seconds),
395 this,
396 &CrxUpdateService::ProcessPendingItems);
[email protected]e8f96ff2011-08-03 05:07:33397}
398
399// Given a extension-like component id, find the associated component.
[email protected]68bf09e2014-06-03 00:10:56400CrxUpdateItem* CrxUpdateService::FindUpdateItemById(
401 const std::string& id) const {
[email protected]e8f96ff2011-08-03 05:07:33402 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
403 CrxUpdateItem::FindById finder(id);
[email protected]68bf09e2014-06-03 00:10:56404 UpdateItems::const_iterator it =
[email protected]d0c8b8b42014-05-06 05:11:45405 std::find_if(work_items_.begin(), work_items_.end(), finder);
[email protected]93e8e2c2014-01-04 12:29:23406 return it != work_items_.end() ? *it : NULL;
[email protected]e8f96ff2011-08-03 05:07:33407}
408
[email protected]61aca4cd2013-10-26 10:50:59409// Changes a component's status, clearing on_demand and firing notifications as
410// necessary. By convention, this is the only function that can change a
411// CrxUpdateItem's |status|.
412// TODO(waffles): Do we want to add DCHECKS for valid state transitions here?
413void CrxUpdateService::ChangeItemState(CrxUpdateItem* item,
414 CrxUpdateItem::Status to) {
415 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d0c8b8b42014-05-06 05:11:45416 if (to == CrxUpdateItem::kNoUpdate || to == CrxUpdateItem::kUpdated ||
[email protected]61aca4cd2013-10-26 10:50:59417 to == CrxUpdateItem::kUpToDate) {
418 item->on_demand = false;
419 }
420
421 item->status = to;
422
[email protected]d3268fe2014-04-25 02:14:23423 switch (to) {
424 case CrxUpdateItem::kCanUpdate:
425 NotifyObservers(Observer::COMPONENT_UPDATE_FOUND, item->id);
426 break;
427 case CrxUpdateItem::kUpdatingDiff:
428 case CrxUpdateItem::kUpdating:
429 NotifyObservers(Observer::COMPONENT_UPDATE_READY, item->id);
430 break;
431 case CrxUpdateItem::kUpdated:
432 NotifyObservers(Observer::COMPONENT_UPDATED, item->id);
433 break;
434 case CrxUpdateItem::kUpToDate:
435 case CrxUpdateItem::kNoUpdate:
436 NotifyObservers(Observer::COMPONENT_NOT_UPDATED, item->id);
437 break;
438 case CrxUpdateItem::kNew:
439 case CrxUpdateItem::kChecking:
440 case CrxUpdateItem::kDownloading:
441 case CrxUpdateItem::kDownloadingDiff:
442 case CrxUpdateItem::kLastStatus:
443 // No notification for these states.
444 break;
[email protected]61aca4cd2013-10-26 10:50:59445 }
[email protected]00a77fa2013-11-02 04:18:46446
447 // Free possible pending network requests.
[email protected]d0c8b8b42014-05-06 05:11:45448 if ((to == CrxUpdateItem::kUpdated) || (to == CrxUpdateItem::kUpToDate) ||
[email protected]00a77fa2013-11-02 04:18:46449 (to == CrxUpdateItem::kNoUpdate)) {
450 UnblockandReapAllThrottles(&item->throttles);
451 }
[email protected]61aca4cd2013-10-26 10:50:59452}
453
[email protected]e8f96ff2011-08-03 05:07:33454// Changes all the components in |work_items_| that have |from| status to
[email protected]e3e696d32013-06-21 20:41:36455// |to| status and returns how many have been changed.
[email protected]e8f96ff2011-08-03 05:07:33456size_t CrxUpdateService::ChangeItemStatus(CrxUpdateItem::Status from,
457 CrxUpdateItem::Status to) {
458 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
459 size_t count = 0;
460 for (UpdateItems::iterator it = work_items_.begin();
[email protected]d0c8b8b42014-05-06 05:11:45461 it != work_items_.end();
462 ++it) {
[email protected]e8f96ff2011-08-03 05:07:33463 CrxUpdateItem* item = *it;
[email protected]93e8e2c2014-01-04 12:29:23464 if (item->status == from) {
465 ChangeItemState(item, to);
466 ++count;
467 }
[email protected]e8f96ff2011-08-03 05:07:33468 }
469 return count;
470}
471
472// Adds a component to be checked for upgrades. If the component exists it
473// it will be replaced and the return code is kReplaced.
[email protected]e8f96ff2011-08-03 05:07:33474ComponentUpdateService::Status CrxUpdateService::RegisterComponent(
475 const CrxComponent& component) {
476 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]d0c8b8b42014-05-06 05:11:45477 if (component.pk_hash.empty() || !component.version.IsValid() ||
[email protected]e8f96ff2011-08-03 05:07:33478 !component.installer)
479 return kError;
480
[email protected]055981f2014-01-17 20:22:32481 std::string id(GetCrxComponentID(component));
[email protected]93e8e2c2014-01-04 12:29:23482 CrxUpdateItem* uit = FindUpdateItemById(id);
[email protected]e8f96ff2011-08-03 05:07:33483 if (uit) {
484 uit->component = component;
485 return kReplaced;
486 }
487
488 uit = new CrxUpdateItem;
489 uit->id.swap(id);
490 uit->component = component;
[email protected]e3e696d32013-06-21 20:41:36491
[email protected]e8f96ff2011-08-03 05:07:33492 work_items_.push_back(uit);
[email protected]10bc0222014-06-11 13:44:38493
[email protected]e8f96ff2011-08-03 05:07:33494 // If this is the first component registered we call Start to
[email protected]10bc0222014-06-11 13:44:38495 // schedule the first timer. Otherwise, reset the timer to trigger another
496 // pass over the work items, if the component updater is sleeping, fact
497 // indicated by a running timer. If the timer is not running, it means that
498 // the service is busy updating something, and in that case, this component
499 // will be picked up at the next pass.
500 if (running_) {
501 if (work_items_.size() == 1) {
502 Start();
503 } else if (timer_.IsRunning()) {
504 timer_.Start(FROM_HERE,
505 base::TimeDelta::FromSeconds(config_->InitialDelay()),
506 this,
507 &CrxUpdateService::ProcessPendingItems);
508 }
509 }
[email protected]e8f96ff2011-08-03 05:07:33510
511 return kOk;
512}
513
[email protected]68bf09e2014-06-03 00:10:56514std::vector<std::string> CrxUpdateService::GetComponentIDs() const {
[email protected]2e919ddd2013-08-21 05:05:17515 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]68bf09e2014-06-03 00:10:56516 std::vector<std::string> component_ids;
[email protected]2e919ddd2013-08-21 05:05:17517 for (UpdateItems::const_iterator it = work_items_.begin();
[email protected]d0c8b8b42014-05-06 05:11:45518 it != work_items_.end();
519 ++it) {
[email protected]2e919ddd2013-08-21 05:05:17520 const CrxUpdateItem* item = *it;
[email protected]68bf09e2014-06-03 00:10:56521 component_ids.push_back(item->id);
[email protected]2e919ddd2013-08-21 05:05:17522 }
[email protected]68bf09e2014-06-03 00:10:56523 return component_ids;
524}
525
526CrxUpdateItem* CrxUpdateService::GetComponentDetails(
527 const std::string& component_id) const {
528 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
529 return FindUpdateItemById(component_id);
[email protected]2e919ddd2013-08-21 05:05:17530}
531
[email protected]504830a2014-05-20 21:53:54532OnDemandUpdater& CrxUpdateService::GetOnDemandUpdater() {
533 return *this;
534}
535
[email protected]93e8e2c2014-01-04 12:29:23536// This is the main loop of the component updater. It updates one component
537// at a time if updates are available. Otherwise, it does an update check or
538// takes a long sleep until the loop runs again.
[email protected]e8f96ff2011-08-03 05:07:33539void CrxUpdateService::ProcessPendingItems() {
540 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23541
[email protected]61aca4cd2013-10-26 10:50:59542 CrxUpdateItem* ready_upgrade = FindReadyComponent();
543 if (ready_upgrade) {
544 UpdateComponent(ready_upgrade);
[email protected]e8f96ff2011-08-03 05:07:33545 return;
546 }
[email protected]93e8e2c2014-01-04 12:29:23547
548 if (!CheckForUpdates())
549 ScheduleNextRun(kStepDelayLong);
[email protected]61aca4cd2013-10-26 10:50:59550}
551
[email protected]93e8e2c2014-01-04 12:29:23552CrxUpdateItem* CrxUpdateService::FindReadyComponent() const {
[email protected]c4b4cfa2014-03-10 18:55:00553 class Helper {
554 public:
555 static bool IsReadyOnDemand(CrxUpdateItem* item) {
556 return item->on_demand && IsReady(item);
[email protected]61aca4cd2013-10-26 10:50:59557 }
[email protected]c4b4cfa2014-03-10 18:55:00558 static bool IsReady(CrxUpdateItem* item) {
559 return item->status == CrxUpdateItem::kCanUpdate;
560 }
561 };
[email protected]61aca4cd2013-10-26 10:50:59562
[email protected]c4b4cfa2014-03-10 18:55:00563 std::vector<CrxUpdateItem*>::const_iterator it = std::find_if(
564 work_items_.begin(), work_items_.end(), Helper::IsReadyOnDemand);
565 if (it != work_items_.end())
566 return *it;
567 it = std::find_if(work_items_.begin(), work_items_.end(), Helper::IsReady);
568 if (it != work_items_.end())
569 return *it;
[email protected]61aca4cd2013-10-26 10:50:59570 return NULL;
571}
572
[email protected]93e8e2c2014-01-04 12:29:23573// Prepares the components for an update check and initiates the request.
[email protected]21a9c9a2014-02-19 19:37:11574// On demand components are always included in the update check request.
575// Otherwise, only include components that have not been checked recently.
[email protected]93e8e2c2014-01-04 12:29:23576bool CrxUpdateService::CheckForUpdates() {
[email protected]21a9c9a2014-02-19 19:37:11577 const base::TimeDelta minimum_recheck_wait_time =
578 base::TimeDelta::FromSeconds(config_->MinimumReCheckWait());
579 const base::Time now(base::Time::Now());
580
[email protected]93e8e2c2014-01-04 12:29:23581 std::vector<CrxUpdateItem*> items_to_check;
582 for (size_t i = 0; i != work_items_.size(); ++i) {
583 CrxUpdateItem* item = work_items_[i];
584 DCHECK(item->status == CrxUpdateItem::kNew ||
585 item->status == CrxUpdateItem::kNoUpdate ||
586 item->status == CrxUpdateItem::kUpToDate ||
587 item->status == CrxUpdateItem::kUpdated);
588
[email protected]21a9c9a2014-02-19 19:37:11589 const base::TimeDelta time_since_last_checked(now - item->last_check);
590
591 if (!item->on_demand &&
592 time_since_last_checked < minimum_recheck_wait_time) {
[email protected]fb53e652014-04-30 11:27:19593 VLOG(1) << "Skipping check for component update: id=" << item->id
594 << ", time_since_last_checked="
595 << time_since_last_checked.InSeconds()
596 << " seconds: too soon to check for an update";
[email protected]21a9c9a2014-02-19 19:37:11597 continue;
598 }
599
[email protected]fb53e652014-04-30 11:27:19600 VLOG(1) << "Scheduling update check for component id=" << item->id
601 << ", time_since_last_checked="
[email protected]d0c8b8b42014-05-06 05:11:45602 << time_since_last_checked.InSeconds() << " seconds";
[email protected]fb53e652014-04-30 11:27:19603
[email protected]93e8e2c2014-01-04 12:29:23604 ChangeItemState(item, CrxUpdateItem::kChecking);
605
[email protected]21a9c9a2014-02-19 19:37:11606 item->last_check = now;
[email protected]93e8e2c2014-01-04 12:29:23607 item->crx_urls.clear();
608 item->crx_diffurls.clear();
609 item->previous_version = item->component.version;
610 item->next_version = Version();
611 item->previous_fp = item->component.fingerprint;
612 item->next_fp.clear();
613 item->diff_update_failed = false;
614 item->error_category = 0;
615 item->error_code = 0;
616 item->extra_code1 = 0;
617 item->diff_error_category = 0;
618 item->diff_error_code = 0;
619 item->diff_extra_code1 = 0;
620 item->download_metrics.clear();
621
622 items_to_check.push_back(item);
623 }
624
625 if (items_to_check.empty())
626 return false;
627
[email protected]d0c8b8b42014-05-06 05:11:45628 update_checker_ =
629 UpdateChecker::Create(config_->UpdateUrl(),
630 config_->RequestContext(),
631 base::Bind(&CrxUpdateService::UpdateCheckComplete,
632 base::Unretained(this))).Pass();
[email protected]93e8e2c2014-01-04 12:29:23633 return update_checker_->CheckForUpdates(items_to_check,
634 config_->ExtraRequestParams());
635}
636
[email protected]61aca4cd2013-10-26 10:50:59637void CrxUpdateService::UpdateComponent(CrxUpdateItem* workitem) {
[email protected]afa378f22013-12-02 03:37:54638 scoped_ptr<CRXContext> crx_context(new CRXContext);
639 crx_context->pk_hash = workitem->component.pk_hash;
640 crx_context->id = workitem->id;
641 crx_context->installer = workitem->component.installer;
642 crx_context->fingerprint = workitem->next_fp;
[email protected]da37c1d2013-12-19 01:04:38643 const std::vector<GURL>* urls = NULL;
[email protected]cfd13e52014-02-05 09:35:07644 bool allow_background_download = false;
[email protected]61aca4cd2013-10-26 10:50:59645 if (CanTryDiffUpdate(workitem, *config_)) {
[email protected]da37c1d2013-12-19 01:04:38646 urls = &workitem->crx_diffurls;
[email protected]61aca4cd2013-10-26 10:50:59647 ChangeItemState(workitem, CrxUpdateItem::kDownloadingDiff);
648 } else {
[email protected]cfd13e52014-02-05 09:35:07649 // Background downloads are enabled only for selected components and
650 // only for full downloads (see issue 340448).
651 allow_background_download = workitem->component.allow_background_download;
[email protected]da37c1d2013-12-19 01:04:38652 urls = &workitem->crx_urls;
[email protected]61aca4cd2013-10-26 10:50:59653 ChangeItemState(workitem, CrxUpdateItem::kDownloading);
654 }
[email protected]3cb2a4f2013-12-07 21:54:34655
656 // On demand component updates are always downloaded in foreground.
[email protected]d0c8b8b42014-05-06 05:11:45657 const bool is_background_download = !workitem->on_demand &&
658 allow_background_download &&
659 config_->UseBackgroundDownloader();
[email protected]3cb2a4f2013-12-07 21:54:34660
[email protected]1b6587dc52014-04-26 00:38:55661 crx_downloader_.reset(CrxDownloader::Create(is_background_download,
662 config_->RequestContext(),
663 blocking_task_runner_));
[email protected]8a5ebd432014-05-02 00:21:22664 crx_downloader_->set_progress_callback(
665 base::Bind(&CrxUpdateService::DownloadProgress,
666 base::Unretained(this),
667 crx_context->id));
[email protected]1b6587dc52014-04-26 00:38:55668 crx_downloader_->StartDownload(*urls,
669 base::Bind(&CrxUpdateService::DownloadComplete,
670 base::Unretained(this),
671 base::Passed(&crx_context)));
[email protected]61aca4cd2013-10-26 10:50:59672}
673
[email protected]93e8e2c2014-01-04 12:29:23674void CrxUpdateService::UpdateCheckComplete(
675 int error,
676 const std::string& error_message,
[email protected]055981f2014-01-17 20:22:32677 const UpdateResponse::Results& results) {
[email protected]e8f96ff2011-08-03 05:07:33678 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23679 update_checker_.reset();
680 if (!error)
681 OnUpdateCheckSucceeded(results);
[email protected]652f4272013-11-13 09:23:41682 else
[email protected]93e8e2c2014-01-04 12:29:23683 OnUpdateCheckFailed(error, error_message);
[email protected]e8f96ff2011-08-03 05:07:33684}
685
[email protected]93e8e2c2014-01-04 12:29:23686// Handles a valid Omaha update check response by matching the results with
687// the registered components which were checked for updates.
688// If updates are found, prepare the components for the actual version upgrade.
689// One of these components will be drafted for the upgrade next time
690// ProcessPendingItems is called.
691void CrxUpdateService::OnUpdateCheckSucceeded(
[email protected]055981f2014-01-17 20:22:32692 const UpdateResponse::Results& results) {
[email protected]2cddef42013-11-22 08:23:22693 size_t num_updates_pending = 0;
[email protected]e8f96ff2011-08-03 05:07:33694 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]fb53e652014-04-30 11:27:19695 VLOG(1) << "Update check succeeded.";
[email protected]055981f2014-01-17 20:22:32696 std::vector<UpdateResponse::Result>::const_iterator it;
[email protected]e8f96ff2011-08-03 05:07:33697 for (it = results.list.begin(); it != results.list.end(); ++it) {
698 CrxUpdateItem* crx = FindUpdateItemById(it->extension_id);
699 if (!crx)
700 continue;
701
[email protected]2cddef42013-11-22 08:23:22702 if (crx->status != CrxUpdateItem::kChecking) {
703 NOTREACHED();
[email protected]e8f96ff2011-08-03 05:07:33704 continue; // Not updating this component now.
[email protected]2cddef42013-11-22 08:23:22705 }
[email protected]e8f96ff2011-08-03 05:07:33706
[email protected]2cddef42013-11-22 08:23:22707 if (it->manifest.version.empty()) {
[email protected]cf442612011-08-09 20:20:12708 // No version means no update available.
[email protected]61aca4cd2013-10-26 10:50:59709 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]fb53e652014-04-30 11:27:19710 VLOG(1) << "No update available for component: " << crx->id;
[email protected]cf442612011-08-09 20:20:12711 continue;
[email protected]e8f96ff2011-08-03 05:07:33712 }
[email protected]2cddef42013-11-22 08:23:22713
714 if (!IsVersionNewer(crx->component.version, it->manifest.version)) {
715 // The component is up to date.
[email protected]61aca4cd2013-10-26 10:50:59716 ChangeItemState(crx, CrxUpdateItem::kUpToDate);
[email protected]fb53e652014-04-30 11:27:19717 VLOG(1) << "Component already up-to-date: " << crx->id;
[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]fb53e652014-04-30 11:27:19724 VLOG(1) << "Ignoring incompatible component: " << crx->id;
[email protected]61aca4cd2013-10-26 10:50:59725 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]cf442612011-08-09 20:20:12726 continue;
727 }
[email protected]e8f96ff2011-08-03 05:07:33728 }
[email protected]2cddef42013-11-22 08:23:22729
730 if (it->manifest.packages.size() != 1) {
731 // Assume one and only one package per component.
[email protected]fb53e652014-04-30 11:27:19732 VLOG(1) << "Ignoring multiple packages for component: " << crx->id;
[email protected]2cddef42013-11-22 08:23:22733 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
734 continue;
735 }
736
737 // Parse the members of the result and queue an upgrade for this component.
[email protected]c5e4a2222014-01-03 16:06:13738 crx->next_version = Version(it->manifest.version);
[email protected]2cddef42013-11-22 08:23:22739
[email protected]fb53e652014-04-30 11:27:19740 VLOG(1) << "Update found for component: " << crx->id;
741
[email protected]055981f2014-01-17 20:22:32742 typedef UpdateResponse::Result::Manifest::Package Package;
[email protected]2cddef42013-11-22 08:23:22743 const Package& package(it->manifest.packages[0]);
744 crx->next_fp = package.fingerprint;
745
[email protected]da37c1d2013-12-19 01:04:38746 // Resolve the urls by combining the base urls with the package names.
747 for (size_t i = 0; i != it->crx_urls.size(); ++i) {
748 const GURL url(it->crx_urls[i].Resolve(package.name));
749 if (url.is_valid())
[email protected]d0c8b8b42014-05-06 05:11:45750 crx->crx_urls.push_back(url);
[email protected]da37c1d2013-12-19 01:04:38751 }
752 for (size_t i = 0; i != it->crx_diffurls.size(); ++i) {
753 const GURL url(it->crx_diffurls[i].Resolve(package.namediff));
754 if (url.is_valid())
[email protected]d0c8b8b42014-05-06 05:11:45755 crx->crx_diffurls.push_back(url);
[email protected]da37c1d2013-12-19 01:04:38756 }
[email protected]2cddef42013-11-22 08:23:22757
[email protected]61aca4cd2013-10-26 10:50:59758 ChangeItemState(crx, CrxUpdateItem::kCanUpdate);
[email protected]2cddef42013-11-22 08:23:22759 ++num_updates_pending;
[email protected]e8f96ff2011-08-03 05:07:33760 }
[email protected]cf442612011-08-09 20:20:12761
[email protected]2cddef42013-11-22 08:23:22762 // All components that are not included in the update response are
763 // considered up to date.
[email protected]cf442612011-08-09 20:20:12764 ChangeItemStatus(CrxUpdateItem::kChecking, CrxUpdateItem::kUpToDate);
765
[email protected]32a6c8382013-08-20 00:29:20766 // If there are updates pending we do a short wait, otherwise we take
767 // a longer delay until we check the components again.
[email protected]21a9c9a2014-02-19 19:37:11768 ScheduleNextRun(num_updates_pending > 0 ? kStepDelayShort : kStepDelayLong);
[email protected]e8f96ff2011-08-03 05:07:33769}
770
[email protected]93e8e2c2014-01-04 12:29:23771void CrxUpdateService::OnUpdateCheckFailed(int error,
772 const std::string& error_message) {
[email protected]e8f96ff2011-08-03 05:07:33773 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23774 DCHECK(error);
[email protected]d0c8b8b42014-05-06 05:11:45775 size_t count =
776 ChangeItemStatus(CrxUpdateItem::kChecking, CrxUpdateItem::kNoUpdate);
[email protected]e8f96ff2011-08-03 05:07:33777 DCHECK_GT(count, 0ul);
[email protected]fb53e652014-04-30 11:27:19778 VLOG(1) << "Update check failed.";
[email protected]32a6c8382013-08-20 00:29:20779 ScheduleNextRun(kStepDelayLong);
[email protected]e8f96ff2011-08-03 05:07:33780}
781
[email protected]8a5ebd432014-05-02 00:21:22782// Called when progress is being made downloading a CRX. The progress may
783// not monotonically increase due to how the CRX downloader switches between
784// different downloaders and fallback urls.
785void CrxUpdateService::DownloadProgress(
786 const std::string& component_id,
787 const CrxDownloader::Result& download_result) {
788 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
789 NotifyObservers(Observer::COMPONENT_UPDATE_DOWNLOADING, component_id);
790}
791
[email protected]e8f96ff2011-08-03 05:07:33792// Called when the CRX package has been downloaded to a temporary location.
793// Here we fire the notifications and schedule the component-specific installer
794// to be called in the file thread.
[email protected]3cb2a4f2013-12-07 21:54:34795void CrxUpdateService::DownloadComplete(
796 scoped_ptr<CRXContext> crx_context,
[email protected]3a0092d2013-12-18 03:04:35797 const CrxDownloader::Result& download_result) {
[email protected]e8f96ff2011-08-03 05:07:33798 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]64f39fa12013-09-03 21:49:37799
[email protected]64f39fa12013-09-03 21:49:37800 CrxUpdateItem* crx = FindUpdateItemById(crx_context->id);
[email protected]e3e696d32013-06-21 20:41:36801 DCHECK(crx->status == CrxUpdateItem::kDownloadingDiff ||
802 crx->status == CrxUpdateItem::kDownloading);
803
[email protected]3a0092d2013-12-18 03:04:35804 AppendDownloadMetrics(crx_downloader_->download_metrics(),
805 &crx->download_metrics);
806
[email protected]8a5ebd432014-05-02 00:21:22807 crx_downloader_.reset();
808
[email protected]3cb2a4f2013-12-07 21:54:34809 if (download_result.error) {
[email protected]e3e696d32013-06-21 20:41:36810 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
[email protected]7b0529242013-07-20 05:45:46811 crx->diff_error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34812 crx->diff_error_code = download_result.error;
[email protected]e630ba62013-06-22 15:22:34813 crx->diff_update_failed = true;
[email protected]e3e696d32013-06-21 20:41:36814 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
815 CrxUpdateItem::kCanUpdate);
816 DCHECK_EQ(count, 1ul);
[email protected]7b0529242013-07-20 05:45:46817
[email protected]32a6c8382013-08-20 00:29:20818 ScheduleNextRun(kStepDelayShort);
[email protected]e3e696d32013-06-21 20:41:36819 return;
820 }
[email protected]7b0529242013-07-20 05:45:46821 crx->error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34822 crx->error_code = download_result.error;
[email protected]d0c8b8b42014-05-06 05:11:45823 size_t count =
824 ChangeItemStatus(CrxUpdateItem::kDownloading, CrxUpdateItem::kNoUpdate);
[email protected]e8f96ff2011-08-03 05:07:33825 DCHECK_EQ(count, 1ul);
[email protected]e3e696d32013-06-21 20:41:36826
[email protected]7b0529242013-07-20 05:45:46827 // At this point, since both the differential and the full downloads failed,
828 // the update for this component has finished with an error.
829 ping_manager_->OnUpdateComplete(crx);
830
[email protected]32a6c8382013-08-20 00:29:20831 // Move on to the next update, if there is one available.
832 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33833 } else {
[email protected]e3e696d32013-06-21 20:41:36834 size_t count = 0;
835 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
836 count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
837 CrxUpdateItem::kUpdatingDiff);
838 } else {
839 count = ChangeItemStatus(CrxUpdateItem::kDownloading,
840 CrxUpdateItem::kUpdating);
841 }
[email protected]e8f96ff2011-08-03 05:07:33842 DCHECK_EQ(count, 1ul);
[email protected]cf442612011-08-09 20:20:12843
[email protected]44da56e2011-11-21 19:59:14844 // Why unretained? See comment at top of file.
[email protected]8f5f2ea2013-10-31 09:39:10845 blocking_task_runner_->PostDelayedTask(
[email protected]73251e72012-03-04 02:10:33846 FROM_HERE,
[email protected]44da56e2011-11-21 19:59:14847 base::Bind(&CrxUpdateService::Install,
848 base::Unretained(this),
[email protected]afa378f22013-12-02 03:37:54849 base::Passed(&crx_context),
[email protected]3cb2a4f2013-12-07 21:54:34850 download_result.response),
[email protected]73251e72012-03-04 02:10:33851 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]e8f96ff2011-08-03 05:07:33852 }
[email protected]e8f96ff2011-08-03 05:07:33853}
854
855// Install consists of digital signature verification, unpacking and then
856// calling the component specific installer. All that is handled by the
[email protected]f5d27e32014-01-31 06:48:53857// |unpacker_|. If there is an error this function is in charge of deleting
[email protected]e8f96ff2011-08-03 05:07:33858// the files created.
[email protected]afa378f22013-12-02 03:37:54859void CrxUpdateService::Install(scoped_ptr<CRXContext> context,
[email protected]650b2d52013-02-10 03:41:45860 const base::FilePath& crx_path) {
[email protected]8f5f2ea2013-10-31 09:39:10861 // This function owns the file at |crx_path| and the |context| object.
[email protected]94a481b2014-03-28 19:41:55862 unpacker_ = new ComponentUnpacker(context->pk_hash,
863 crx_path,
864 context->fingerprint,
865 context->installer,
866 config_->InProcess(),
867 blocking_task_runner_);
[email protected]f5d27e32014-01-31 06:48:53868 unpacker_->Unpack(base::Bind(&CrxUpdateService::EndUnpacking,
869 base::Unretained(this),
870 context->id,
871 crx_path));
872}
873
874void CrxUpdateService::EndUnpacking(const std::string& component_id,
875 const base::FilePath& crx_path,
876 ComponentUnpacker::Error error,
877 int extended_error) {
[email protected]055981f2014-01-17 20:22:32878 if (!DeleteFileAndEmptyParentDirectory(crx_path))
[email protected]e8f96ff2011-08-03 05:07:33879 NOTREACHED() << crx_path.value();
[email protected]73251e72012-03-04 02:10:33880 BrowserThread::PostDelayedTask(
881 BrowserThread::UI,
882 FROM_HERE,
[email protected]d0c8b8b42014-05-06 05:11:45883 base::Bind(&CrxUpdateService::DoneInstalling,
884 base::Unretained(this),
885 component_id,
886 error,
887 extended_error),
[email protected]73251e72012-03-04 02:10:33888 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]94a481b2014-03-28 19:41:55889 // Reset the unpacker last, otherwise we free our own arguments.
890 unpacker_ = NULL;
[email protected]e8f96ff2011-08-03 05:07:33891}
892
893// Installation has been completed. Adjust the component status and
[email protected]7b0529242013-07-20 05:45:46894// schedule the next check. Schedule a short delay before trying the full
895// update when the differential update failed.
[email protected]07f93af12011-08-17 20:57:22896void CrxUpdateService::DoneInstalling(const std::string& component_id,
[email protected]e3e696d32013-06-21 20:41:36897 ComponentUnpacker::Error error,
898 int extra_code) {
[email protected]e8f96ff2011-08-03 05:07:33899 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]07f93af12011-08-17 20:57:22900
[email protected]7b0529242013-07-20 05:45:46901 ErrorCategory error_category = kErrorNone;
902 switch (error) {
903 case ComponentUnpacker::kNone:
904 break;
905 case ComponentUnpacker::kInstallerError:
906 error_category = kInstallError;
907 break;
908 default:
909 error_category = kUnpackError;
910 break;
911 }
912
913 const bool is_success = error == ComponentUnpacker::kNone;
914
[email protected]07f93af12011-08-17 20:57:22915 CrxUpdateItem* item = FindUpdateItemById(component_id);
[email protected]7b0529242013-07-20 05:45:46916 if (item->status == CrxUpdateItem::kUpdatingDiff && !is_success) {
917 item->diff_error_category = error_category;
918 item->diff_error_code = error;
919 item->diff_extra_code1 = extra_code;
[email protected]1ea21ad2013-09-02 04:20:39920 item->diff_update_failed = true;
921 size_t count = ChangeItemStatus(CrxUpdateItem::kUpdatingDiff,
922 CrxUpdateItem::kCanUpdate);
923 DCHECK_EQ(count, 1ul);
924 ScheduleNextRun(kStepDelayShort);
925 return;
[email protected]d0c8b8b42014-05-06 05:11:45926 }
[email protected]e3e696d32013-06-21 20:41:36927
[email protected]7b0529242013-07-20 05:45:46928 if (is_success) {
[email protected]61aca4cd2013-10-26 10:50:59929 ChangeItemState(item, CrxUpdateItem::kUpdated);
[email protected]07f93af12011-08-17 20:57:22930 item->component.version = item->next_version;
[email protected]e3e696d32013-06-21 20:41:36931 item->component.fingerprint = item->next_fp;
[email protected]7b0529242013-07-20 05:45:46932 } else {
[email protected]61aca4cd2013-10-26 10:50:59933 ChangeItemState(item, CrxUpdateItem::kNoUpdate);
[email protected]7b0529242013-07-20 05:45:46934 item->error_category = error_category;
935 item->error_code = error;
936 item->extra_code1 = extra_code;
[email protected]e3e696d32013-06-21 20:41:36937 }
[email protected]07f93af12011-08-17 20:57:22938
[email protected]7b0529242013-07-20 05:45:46939 ping_manager_->OnUpdateComplete(item);
[email protected]360b8bb2011-09-01 21:48:06940
[email protected]32a6c8382013-08-20 00:29:20941 // Move on to the next update, if there is one available.
942 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33943}
944
[email protected]d3268fe2014-04-25 02:14:23945void CrxUpdateService::NotifyObservers(Observer::Events event,
946 const std::string& id) {
947 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
948 FOR_EACH_OBSERVER(Observer, observer_list_, OnEvent(event, id));
[email protected]85e61d52013-08-01 22:23:42949}
950
[email protected]00a77fa2013-11-02 04:18:46951content::ResourceThrottle* CrxUpdateService::GetOnDemandResourceThrottle(
[email protected]d0c8b8b42014-05-06 05:11:45952 net::URLRequest* request,
953 const std::string& crx_id) {
[email protected]00a77fa2013-11-02 04:18:46954 // We give the raw pointer to the caller, who will delete it at will
955 // and we keep for ourselves a weak pointer to it so we can post tasks
956 // from the UI thread without having to track lifetime directly.
957 CUResourceThrottle* rt = new CUResourceThrottle(request);
[email protected]d0c8b8b42014-05-06 05:11:45958 BrowserThread::PostTask(BrowserThread::UI,
959 FROM_HERE,
960 base::Bind(&CrxUpdateService::OnNewResourceThrottle,
961 base::Unretained(this),
962 rt->AsWeakPtr(),
963 crx_id));
[email protected]00a77fa2013-11-02 04:18:46964 return rt;
965}
966
967void CrxUpdateService::OnNewResourceThrottle(
[email protected]d0c8b8b42014-05-06 05:11:45968 base::WeakPtr<CUResourceThrottle> rt,
969 const std::string& crx_id) {
[email protected]00a77fa2013-11-02 04:18:46970 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
971 // Check if we can on-demand update, else unblock the request anyway.
972 CrxUpdateItem* item = FindUpdateItemById(crx_id);
[email protected]5b53f932014-06-06 10:26:54973 Status status = OnDemandUpdateWithCooldown(item);
[email protected]00a77fa2013-11-02 04:18:46974 if (status == kOk || status == kInProgress) {
975 item->throttles.push_back(rt);
976 return;
977 }
978 UnblockResourceThrottle(rt);
979}
980
[email protected]68bf09e2014-06-03 00:10:56981// Start the process of checking for an update, for a particular component
982// that was previously registered.
983// |component_id| is a value returned from GetCrxComponentID().
[email protected]504830a2014-05-20 21:53:54984ComponentUpdateService::Status CrxUpdateService::OnDemandUpdate(
985 const std::string& component_id) {
986 return OnDemandUpdateInternal(FindUpdateItemById(component_id));
987}
988
[email protected]5b53f932014-06-06 10:26:54989ComponentUpdateService::Status CrxUpdateService::OnDemandUpdateWithCooldown(
[email protected]504830a2014-05-20 21:53:54990 CrxUpdateItem* uit) {
991 if (!uit)
992 return kError;
993
994 // Check if the request is too soon.
995 base::TimeDelta delta = base::Time::Now() - uit->last_check;
996 if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay()))
997 return kError;
998
[email protected]5b53f932014-06-06 10:26:54999 return OnDemandUpdateInternal(uit);
1000}
1001
1002ComponentUpdateService::Status CrxUpdateService::OnDemandUpdateInternal(
1003 CrxUpdateItem* uit) {
1004 if (!uit)
1005 return kError;
1006
[email protected]68bf09e2014-06-03 00:10:561007 Status service_status = GetServiceStatus(uit->status);
1008 // If the item is already in the process of being updated, there is
1009 // no point in this call, so return kInProgress.
1010 if (service_status == kInProgress)
1011 return service_status;
1012
1013 // Otherwise the item was already checked a while back (or it is new),
1014 // set its status to kNew to give it a slightly higher priority.
1015 ChangeItemState(uit, CrxUpdateItem::kNew);
1016 uit->on_demand = true;
[email protected]504830a2014-05-20 21:53:541017
1018 // In case the current delay is long, set the timer to a shorter value
1019 // to get the ball rolling.
1020 if (timer_.IsRunning()) {
1021 timer_.Stop();
1022 timer_.Start(FROM_HERE,
1023 base::TimeDelta::FromSeconds(config_->StepDelay()),
1024 this,
1025 &CrxUpdateService::ProcessPendingItems);
1026 }
1027
1028 return kOk;
1029}
1030
[email protected]68bf09e2014-06-03 00:10:561031ComponentUpdateService::Status CrxUpdateService::GetServiceStatus(
1032 CrxUpdateItem::Status status) {
1033 switch (status) {
1034 case CrxUpdateItem::kChecking:
1035 case CrxUpdateItem::kCanUpdate:
1036 case CrxUpdateItem::kDownloadingDiff:
1037 case CrxUpdateItem::kDownloading:
1038 case CrxUpdateItem::kUpdatingDiff:
1039 case CrxUpdateItem::kUpdating:
1040 return kInProgress;
1041 case CrxUpdateItem::kNew:
1042 case CrxUpdateItem::kUpdated:
1043 case CrxUpdateItem::kUpToDate:
1044 case CrxUpdateItem::kNoUpdate:
1045 return kOk;
1046 case CrxUpdateItem::kLastStatus:
1047 NOTREACHED() << status;
1048 }
1049 return kError;
1050}
1051
[email protected]00a77fa2013-11-02 04:18:461052///////////////////////////////////////////////////////////////////////////////
1053
1054CUResourceThrottle::CUResourceThrottle(const net::URLRequest* request)
1055 : state_(NEW) {
1056 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1057}
1058
1059CUResourceThrottle::~CUResourceThrottle() {
1060 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1061}
1062
1063void CUResourceThrottle::WillStartRequest(bool* defer) {
1064 if (state_ != UNBLOCKED) {
1065 state_ = BLOCKED;
1066 *defer = true;
1067 } else {
1068 *defer = false;
1069 }
1070}
1071
1072void CUResourceThrottle::WillRedirectRequest(const GURL& new_url, bool* defer) {
1073 WillStartRequest(defer);
1074}
1075
[email protected]f8fe5cf2013-12-04 20:11:531076const char* CUResourceThrottle::GetNameForLogging() const {
1077 return "ComponentUpdateResourceThrottle";
1078}
1079
[email protected]00a77fa2013-11-02 04:18:461080void CUResourceThrottle::Unblock() {
1081 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1082 if (state_ == BLOCKED)
1083 controller()->Resume();
1084 state_ = UNBLOCKED;
1085}
1086
[email protected]e8f96ff2011-08-03 05:07:331087// The component update factory. Using the component updater as a singleton
1088// is the job of the browser process.
1089ComponentUpdateService* ComponentUpdateServiceFactory(
1090 ComponentUpdateService::Configurator* config) {
1091 DCHECK(config);
1092 return new CrxUpdateService(config);
1093}
[email protected]2cddef42013-11-22 08:23:221094
[email protected]055981f2014-01-17 20:22:321095} // namespace component_updater