blob: faecf0bd37864e52016a56826c4d7dd6ed4f3b9e [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]d3268fe2014-04-25 02:14:2318#include "base/observer_list.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]ed6fb982014-07-23 16:56:5222#include "base/threading/thread_checker.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]655043812014-06-24 01:50:3626#include "chrome/browser/component_updater/component_updater_configurator.h"
[email protected]7b0529242013-07-20 05:45:4627#include "chrome/browser/component_updater/component_updater_ping_manager.h"
[email protected]2cddef42013-11-22 08:23:2228#include "chrome/browser/component_updater/component_updater_utils.h"
[email protected]afa378f22013-12-02 03:37:5429#include "chrome/browser/component_updater/crx_downloader.h"
[email protected]7b0529242013-07-20 05:45:4630#include "chrome/browser/component_updater/crx_update_item.h"
[email protected]93e8e2c2014-01-04 12:29:2331#include "chrome/browser/component_updater/update_checker.h"
[email protected]6268d3a2013-11-27 01:28:0932#include "chrome/browser/component_updater/update_response.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,
[email protected]655043812014-06-24 01:50:3656 const 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]ed6fb982014-07-23 16:56:52143// only from the main thread. The unpack and installation is done in a blocking
[email protected]74be2642014-02-07 09:40:37144// 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:
[email protected]655043812014-06-24 01:50:36148 explicit CrxUpdateService(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;
[email protected]f392e372014-06-12 07:25:57158 virtual bool GetComponentDetails(const std::string& component_id,
159 CrxUpdateItem* item) const OVERRIDE;
[email protected]504830a2014-05-20 21:53:54160 virtual OnDemandUpdater& GetOnDemandUpdater() OVERRIDE;
[email protected]ed6fb982014-07-23 16:56:52161 virtual scoped_refptr<base::SequencedTaskRunner> GetSequencedTaskRunner()
162 OVERRIDE;
[email protected]504830a2014-05-20 21:53:54163
164 // Overrides for OnDemandUpdater.
[email protected]00a77fa2013-11-02 04:18:46165 virtual content::ResourceThrottle* GetOnDemandResourceThrottle(
[email protected]d0c8b8b42014-05-06 05:11:45166 net::URLRequest* request,
167 const std::string& crx_id) OVERRIDE;
[email protected]504830a2014-05-20 21:53:54168 virtual Status OnDemandUpdate(const std::string& component_id) OVERRIDE;
[email protected]e8f96ff2011-08-03 05:07:33169
[email protected]28ea9ac2014-05-03 22:07:18170 // Context for a crx download url request.
171 struct CRXContext {
172 ComponentInstaller* installer;
173 std::vector<uint8> pk_hash;
174 std::string id;
175 std::string fingerprint;
176 CRXContext() : installer(NULL) {}
177 };
[email protected]e8f96ff2011-08-03 05:07:33178
[email protected]e8f96ff2011-08-03 05:07:33179 private:
[email protected]7b0529242013-07-20 05:45:46180 enum ErrorCategory {
181 kErrorNone = 0,
182 kNetworkError,
183 kUnpackError,
184 kInstallError,
185 };
186
[email protected]32a6c8382013-08-20 00:29:20187 enum StepDelayInterval {
188 kStepDelayShort = 0,
189 kStepDelayMedium,
190 kStepDelayLong,
191 };
192
[email protected]055981f2014-01-17 20:22:32193 void UpdateCheckComplete(int error,
194 const std::string& error_message,
195 const UpdateResponse::Results& results);
196 void OnUpdateCheckSucceeded(const UpdateResponse::Results& results);
[email protected]93e8e2c2014-01-04 12:29:23197 void OnUpdateCheckFailed(int error, const std::string& error_message);
[email protected]e8f96ff2011-08-03 05:07:33198
[email protected]8a5ebd432014-05-02 00:21:22199 void DownloadProgress(const std::string& component_id,
200 const CrxDownloader::Result& download_result);
201
202 void DownloadComplete(scoped_ptr<CRXContext> crx_context,
203 const CrxDownloader::Result& download_result);
[email protected]afa378f22013-12-02 03:37:54204
[email protected]00a77fa2013-11-02 04:18:46205 Status OnDemandUpdateInternal(CrxUpdateItem* item);
[email protected]5b53f932014-06-06 10:26:54206 Status OnDemandUpdateWithCooldown(CrxUpdateItem* item);
[email protected]00a77fa2013-11-02 04:18:46207
[email protected]e8f96ff2011-08-03 05:07:33208 void ProcessPendingItems();
209
[email protected]93e8e2c2014-01-04 12:29:23210 // Find a component that is ready to update.
211 CrxUpdateItem* FindReadyComponent() const;
212
213 // Prepares the components for an update check and initiates the request.
214 // Returns true if an update check request has been made. Returns false if
215 // no update check was needed or an error occured.
216 bool CheckForUpdates();
[email protected]61aca4cd2013-10-26 10:50:59217
218 void UpdateComponent(CrxUpdateItem* workitem);
219
[email protected]32a6c8382013-08-20 00:29:20220 void ScheduleNextRun(StepDelayInterval step_delay);
[email protected]e8f96ff2011-08-03 05:07:33221
[email protected]6268d3a2013-11-27 01:28:09222 void ParseResponse(const std::string& xml);
[email protected]e8f96ff2011-08-03 05:07:33223
[email protected]afa378f22013-12-02 03:37:54224 void Install(scoped_ptr<CRXContext> context, const base::FilePath& crx_path);
[email protected]e8f96ff2011-08-03 05:07:33225
[email protected]f5d27e32014-01-31 06:48:53226 void EndUnpacking(const std::string& component_id,
227 const base::FilePath& crx_path,
228 ComponentUnpacker::Error error,
229 int extended_error);
230
[email protected]07f93af12011-08-17 20:57:22231 void DoneInstalling(const std::string& component_id,
[email protected]e3e696d32013-06-21 20:41:36232 ComponentUnpacker::Error error,
233 int extended_error);
[email protected]e8f96ff2011-08-03 05:07:33234
[email protected]61aca4cd2013-10-26 10:50:59235 void ChangeItemState(CrxUpdateItem* item, CrxUpdateItem::Status to);
236
[email protected]d0c8b8b42014-05-06 05:11:45237 size_t ChangeItemStatus(CrxUpdateItem::Status from, CrxUpdateItem::Status to);
[email protected]e8f96ff2011-08-03 05:07:33238
[email protected]68bf09e2014-06-03 00:10:56239 CrxUpdateItem* FindUpdateItemById(const std::string& id) const;
[email protected]e8f96ff2011-08-03 05:07:33240
[email protected]d3268fe2014-04-25 02:14:23241 void NotifyObservers(Observer::Events event, const std::string& id);
[email protected]85e61d52013-08-01 22:23:42242
[email protected]61aca4cd2013-10-26 10:50:59243 bool HasOnDemandItems() const;
244
[email protected]00a77fa2013-11-02 04:18:46245 void OnNewResourceThrottle(base::WeakPtr<CUResourceThrottle> rt,
246 const std::string& crx_id);
247
[email protected]68bf09e2014-06-03 00:10:56248 Status GetServiceStatus(const CrxUpdateItem::Status status);
249
[email protected]655043812014-06-24 01:50:36250 scoped_ptr<Configurator> config_;
[email protected]e3e696d32013-06-21 20:41:36251
[email protected]055981f2014-01-17 20:22:32252 scoped_ptr<UpdateChecker> update_checker_;
[email protected]e8f96ff2011-08-03 05:07:33253
[email protected]055981f2014-01-17 20:22:32254 scoped_ptr<PingManager> ping_manager_;
[email protected]7b0529242013-07-20 05:45:46255
[email protected]94a481b2014-03-28 19:41:55256 scoped_refptr<ComponentUnpacker> unpacker_;
[email protected]f5d27e32014-01-31 06:48:53257
[email protected]055981f2014-01-17 20:22:32258 scoped_ptr<CrxDownloader> crx_downloader_;
[email protected]afa378f22013-12-02 03:37:54259
[email protected]86550a42013-06-21 15:20:49260 // A collection of every work item.
[email protected]e3e696d32013-06-21 20:41:36261 typedef std::vector<CrxUpdateItem*> UpdateItems;
[email protected]e8f96ff2011-08-03 05:07:33262 UpdateItems work_items_;
263
264 base::OneShotTimer<CrxUpdateService> timer_;
265
[email protected]ed6fb982014-07-23 16:56:52266 base::ThreadChecker thread_checker_;
267
[email protected]8f5f2ea2013-10-31 09:39:10268 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
269
[email protected]e8f96ff2011-08-03 05:07:33270 bool running_;
271
[email protected]d3268fe2014-04-25 02:14:23272 ObserverList<Observer> observer_list_;
273
[email protected]e8f96ff2011-08-03 05:07:33274 DISALLOW_COPY_AND_ASSIGN(CrxUpdateService);
275};
276
[email protected]e8f96ff2011-08-03 05:07:33277//////////////////////////////////////////////////////////////////////////////
278
[email protected]655043812014-06-24 01:50:36279CrxUpdateService::CrxUpdateService(Configurator* config)
[email protected]e8f96ff2011-08-03 05:07:33280 : config_(config),
[email protected]09974032014-06-27 07:42:08281 ping_manager_(new PingManager(*config)),
[email protected]ed6fb982014-07-23 16:56:52282 blocking_task_runner_(config->GetSequencedTaskRunner()),
[email protected]e8f96ff2011-08-03 05:07:33283 running_(false) {
[email protected]1ea21ad2013-09-02 04:20:39284}
[email protected]e8f96ff2011-08-03 05:07:33285
286CrxUpdateService::~CrxUpdateService() {
[email protected]ed6fb982014-07-23 16:56:52287 // Because we are a singleton, at this point only the main thread should be
[email protected]e8f96ff2011-08-03 05:07:33288 // alive, this simplifies the management of the work that could be in
289 // flight in other threads.
290 Stop();
291 STLDeleteElements(&work_items_);
[email protected]1ea21ad2013-09-02 04:20:39292}
[email protected]e8f96ff2011-08-03 05:07:33293
[email protected]d3268fe2014-04-25 02:14:23294void CrxUpdateService::AddObserver(Observer* observer) {
[email protected]ed6fb982014-07-23 16:56:52295 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]d3268fe2014-04-25 02:14:23296 observer_list_.AddObserver(observer);
297}
298
299void CrxUpdateService::RemoveObserver(Observer* observer) {
[email protected]ed6fb982014-07-23 16:56:52300 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]d3268fe2014-04-25 02:14:23301 observer_list_.RemoveObserver(observer);
302}
303
[email protected]e8f96ff2011-08-03 05:07:33304ComponentUpdateService::Status CrxUpdateService::Start() {
305 // Note that RegisterComponent will call Start() when the first
306 // component is registered, so it can be called twice. This way
307 // we avoid scheduling the timer if there is no work to do.
[email protected]fb53e652014-04-30 11:27:19308 VLOG(1) << "CrxUpdateService starting up";
[email protected]e8f96ff2011-08-03 05:07:33309 running_ = true;
310 if (work_items_.empty())
311 return kOk;
312
[email protected]d3268fe2014-04-25 02:14:23313 NotifyObservers(Observer::COMPONENT_UPDATER_STARTED, "");
[email protected]85e61d52013-08-01 22:23:42314
[email protected]fb53e652014-04-30 11:27:19315 VLOG(1) << "First update attempt will take place in "
316 << config_->InitialDelay() << " seconds";
[email protected]d0c8b8b42014-05-06 05:11:45317 timer_.Start(FROM_HERE,
318 base::TimeDelta::FromSeconds(config_->InitialDelay()),
319 this,
320 &CrxUpdateService::ProcessPendingItems);
[email protected]e8f96ff2011-08-03 05:07:33321 return kOk;
322}
323
324// Stop the main check + update loop. In flight operations will be
325// completed.
326ComponentUpdateService::Status CrxUpdateService::Stop() {
[email protected]fb53e652014-04-30 11:27:19327 VLOG(1) << "CrxUpdateService stopping";
[email protected]e8f96ff2011-08-03 05:07:33328 running_ = false;
329 timer_.Stop();
330 return kOk;
331}
332
[email protected]61aca4cd2013-10-26 10:50:59333bool CrxUpdateService::HasOnDemandItems() const {
334 class Helper {
335 public:
[email protected]d0c8b8b42014-05-06 05:11:45336 static bool IsOnDemand(CrxUpdateItem* item) { return item->on_demand; }
[email protected]61aca4cd2013-10-26 10:50:59337 };
338 return std::find_if(work_items_.begin(),
339 work_items_.end(),
340 Helper::IsOnDemand) != work_items_.end();
341}
342
[email protected]ccb4feef2013-02-14 06:16:47343// This function sets the timer which will call ProcessPendingItems() or
[email protected]61aca4cd2013-10-26 10:50:59344// ProcessRequestedItem() if there is an on_demand item. There
[email protected]32a6c8382013-08-20 00:29:20345// are three kinds of waits:
346// - a short delay, when there is immediate work to be done.
347// - a medium delay, when there are updates to be applied within the current
348// update cycle, or there are components that are still unchecked.
349// - a long delay when a full check/update cycle has completed for all
350// components.
351void CrxUpdateService::ScheduleNextRun(StepDelayInterval step_delay) {
[email protected]ed6fb982014-07-23 16:56:52352 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]93e8e2c2014-01-04 12:29:23353 DCHECK(!update_checker_);
[email protected]e8f96ff2011-08-03 05:07:33354 CHECK(!timer_.IsRunning());
355 // It could be the case that Stop() had been called while a url request
356 // or unpacking was in flight, if so we arrive here but |running_| is
357 // false. In that case do not loop again.
358 if (!running_)
359 return;
360
[email protected]ccb4feef2013-02-14 06:16:47361 // Keep the delay short if in the middle of an update (step_delay),
362 // or there are new requested_work_items_ that have not been processed yet.
[email protected]32a6c8382013-08-20 00:29:20363 int64 delay_seconds = 0;
[email protected]61aca4cd2013-10-26 10:50:59364 if (!HasOnDemandItems()) {
[email protected]32a6c8382013-08-20 00:29:20365 switch (step_delay) {
366 case kStepDelayShort:
367 delay_seconds = config_->StepDelay();
368 break;
369 case kStepDelayMedium:
370 delay_seconds = config_->StepDelayMedium();
371 break;
372 case kStepDelayLong:
373 delay_seconds = config_->NextCheckDelay();
374 break;
375 }
376 } else {
377 delay_seconds = config_->StepDelay();
378 }
[email protected]cf442612011-08-09 20:20:12379
[email protected]32a6c8382013-08-20 00:29:20380 if (step_delay != kStepDelayShort) {
[email protected]d3268fe2014-04-25 02:14:23381 NotifyObservers(Observer::COMPONENT_UPDATER_SLEEPING, "");
[email protected]85e61d52013-08-01 22:23:42382
[email protected]e8f96ff2011-08-03 05:07:33383 // Zero is only used for unit tests.
[email protected]32a6c8382013-08-20 00:29:20384 if (0 == delay_seconds)
[email protected]e8f96ff2011-08-03 05:07:33385 return;
386 }
387
[email protected]fb53e652014-04-30 11:27:19388 VLOG(1) << "Scheduling next run to occur in " << delay_seconds << " seconds";
[email protected]d0c8b8b42014-05-06 05:11:45389 timer_.Start(FROM_HERE,
390 base::TimeDelta::FromSeconds(delay_seconds),
391 this,
392 &CrxUpdateService::ProcessPendingItems);
[email protected]e8f96ff2011-08-03 05:07:33393}
394
395// Given a extension-like component id, find the associated component.
[email protected]68bf09e2014-06-03 00:10:56396CrxUpdateItem* CrxUpdateService::FindUpdateItemById(
397 const std::string& id) const {
[email protected]ed6fb982014-07-23 16:56:52398 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]e8f96ff2011-08-03 05:07:33399 CrxUpdateItem::FindById finder(id);
[email protected]68bf09e2014-06-03 00:10:56400 UpdateItems::const_iterator it =
[email protected]d0c8b8b42014-05-06 05:11:45401 std::find_if(work_items_.begin(), work_items_.end(), finder);
[email protected]93e8e2c2014-01-04 12:29:23402 return it != work_items_.end() ? *it : NULL;
[email protected]e8f96ff2011-08-03 05:07:33403}
404
[email protected]61aca4cd2013-10-26 10:50:59405// Changes a component's status, clearing on_demand and firing notifications as
406// necessary. By convention, this is the only function that can change a
407// CrxUpdateItem's |status|.
408// TODO(waffles): Do we want to add DCHECKS for valid state transitions here?
409void CrxUpdateService::ChangeItemState(CrxUpdateItem* item,
410 CrxUpdateItem::Status to) {
[email protected]ed6fb982014-07-23 16:56:52411 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]d0c8b8b42014-05-06 05:11:45412 if (to == CrxUpdateItem::kNoUpdate || to == CrxUpdateItem::kUpdated ||
[email protected]61aca4cd2013-10-26 10:50:59413 to == CrxUpdateItem::kUpToDate) {
414 item->on_demand = false;
415 }
416
417 item->status = to;
418
[email protected]d3268fe2014-04-25 02:14:23419 switch (to) {
420 case CrxUpdateItem::kCanUpdate:
421 NotifyObservers(Observer::COMPONENT_UPDATE_FOUND, item->id);
422 break;
423 case CrxUpdateItem::kUpdatingDiff:
424 case CrxUpdateItem::kUpdating:
425 NotifyObservers(Observer::COMPONENT_UPDATE_READY, item->id);
426 break;
427 case CrxUpdateItem::kUpdated:
428 NotifyObservers(Observer::COMPONENT_UPDATED, item->id);
429 break;
430 case CrxUpdateItem::kUpToDate:
431 case CrxUpdateItem::kNoUpdate:
432 NotifyObservers(Observer::COMPONENT_NOT_UPDATED, item->id);
433 break;
434 case CrxUpdateItem::kNew:
435 case CrxUpdateItem::kChecking:
436 case CrxUpdateItem::kDownloading:
437 case CrxUpdateItem::kDownloadingDiff:
438 case CrxUpdateItem::kLastStatus:
439 // No notification for these states.
440 break;
[email protected]61aca4cd2013-10-26 10:50:59441 }
[email protected]00a77fa2013-11-02 04:18:46442
443 // Free possible pending network requests.
[email protected]d0c8b8b42014-05-06 05:11:45444 if ((to == CrxUpdateItem::kUpdated) || (to == CrxUpdateItem::kUpToDate) ||
[email protected]00a77fa2013-11-02 04:18:46445 (to == CrxUpdateItem::kNoUpdate)) {
446 UnblockandReapAllThrottles(&item->throttles);
447 }
[email protected]61aca4cd2013-10-26 10:50:59448}
449
[email protected]e8f96ff2011-08-03 05:07:33450// Changes all the components in |work_items_| that have |from| status to
[email protected]e3e696d32013-06-21 20:41:36451// |to| status and returns how many have been changed.
[email protected]e8f96ff2011-08-03 05:07:33452size_t CrxUpdateService::ChangeItemStatus(CrxUpdateItem::Status from,
453 CrxUpdateItem::Status to) {
[email protected]ed6fb982014-07-23 16:56:52454 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]e8f96ff2011-08-03 05:07:33455 size_t count = 0;
456 for (UpdateItems::iterator it = work_items_.begin();
[email protected]d0c8b8b42014-05-06 05:11:45457 it != work_items_.end();
458 ++it) {
[email protected]e8f96ff2011-08-03 05:07:33459 CrxUpdateItem* item = *it;
[email protected]93e8e2c2014-01-04 12:29:23460 if (item->status == from) {
461 ChangeItemState(item, to);
462 ++count;
463 }
[email protected]e8f96ff2011-08-03 05:07:33464 }
465 return count;
466}
467
468// Adds a component to be checked for upgrades. If the component exists it
469// it will be replaced and the return code is kReplaced.
[email protected]e8f96ff2011-08-03 05:07:33470ComponentUpdateService::Status CrxUpdateService::RegisterComponent(
471 const CrxComponent& component) {
[email protected]ed6fb982014-07-23 16:56:52472 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]d0c8b8b42014-05-06 05:11:45473 if (component.pk_hash.empty() || !component.version.IsValid() ||
[email protected]e8f96ff2011-08-03 05:07:33474 !component.installer)
475 return kError;
476
[email protected]055981f2014-01-17 20:22:32477 std::string id(GetCrxComponentID(component));
[email protected]93e8e2c2014-01-04 12:29:23478 CrxUpdateItem* uit = FindUpdateItemById(id);
[email protected]e8f96ff2011-08-03 05:07:33479 if (uit) {
480 uit->component = component;
481 return kReplaced;
482 }
483
484 uit = new CrxUpdateItem;
485 uit->id.swap(id);
486 uit->component = component;
[email protected]e3e696d32013-06-21 20:41:36487
[email protected]e8f96ff2011-08-03 05:07:33488 work_items_.push_back(uit);
[email protected]10bc0222014-06-11 13:44:38489
[email protected]e8f96ff2011-08-03 05:07:33490 // If this is the first component registered we call Start to
[email protected]10bc0222014-06-11 13:44:38491 // schedule the first timer. Otherwise, reset the timer to trigger another
492 // pass over the work items, if the component updater is sleeping, fact
493 // indicated by a running timer. If the timer is not running, it means that
494 // the service is busy updating something, and in that case, this component
495 // will be picked up at the next pass.
496 if (running_) {
497 if (work_items_.size() == 1) {
498 Start();
499 } else if (timer_.IsRunning()) {
500 timer_.Start(FROM_HERE,
501 base::TimeDelta::FromSeconds(config_->InitialDelay()),
502 this,
503 &CrxUpdateService::ProcessPendingItems);
504 }
505 }
[email protected]e8f96ff2011-08-03 05:07:33506
507 return kOk;
508}
509
[email protected]68bf09e2014-06-03 00:10:56510std::vector<std::string> CrxUpdateService::GetComponentIDs() const {
[email protected]ed6fb982014-07-23 16:56:52511 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]68bf09e2014-06-03 00:10:56512 std::vector<std::string> component_ids;
[email protected]2e919ddd2013-08-21 05:05:17513 for (UpdateItems::const_iterator it = work_items_.begin();
[email protected]d0c8b8b42014-05-06 05:11:45514 it != work_items_.end();
515 ++it) {
[email protected]2e919ddd2013-08-21 05:05:17516 const CrxUpdateItem* item = *it;
[email protected]68bf09e2014-06-03 00:10:56517 component_ids.push_back(item->id);
[email protected]2e919ddd2013-08-21 05:05:17518 }
[email protected]68bf09e2014-06-03 00:10:56519 return component_ids;
520}
521
[email protected]f392e372014-06-12 07:25:57522bool CrxUpdateService::GetComponentDetails(const std::string& component_id,
523 CrxUpdateItem* item) const {
[email protected]ed6fb982014-07-23 16:56:52524 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]f392e372014-06-12 07:25:57525 const CrxUpdateItem* crx_update_item(FindUpdateItemById(component_id));
526 if (crx_update_item)
527 *item = *crx_update_item;
528 return crx_update_item != NULL;
[email protected]2e919ddd2013-08-21 05:05:17529}
530
[email protected]504830a2014-05-20 21:53:54531OnDemandUpdater& CrxUpdateService::GetOnDemandUpdater() {
532 return *this;
533}
534
[email protected]ed6fb982014-07-23 16:56:52535scoped_refptr<base::SequencedTaskRunner>
536CrxUpdateService::GetSequencedTaskRunner() {
537 return config_->GetSequencedTaskRunner();
538}
539
[email protected]93e8e2c2014-01-04 12:29:23540// This is the main loop of the component updater. It updates one component
541// at a time if updates are available. Otherwise, it does an update check or
542// takes a long sleep until the loop runs again.
[email protected]e8f96ff2011-08-03 05:07:33543void CrxUpdateService::ProcessPendingItems() {
[email protected]ed6fb982014-07-23 16:56:52544 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]93e8e2c2014-01-04 12:29:23545
[email protected]61aca4cd2013-10-26 10:50:59546 CrxUpdateItem* ready_upgrade = FindReadyComponent();
547 if (ready_upgrade) {
548 UpdateComponent(ready_upgrade);
[email protected]e8f96ff2011-08-03 05:07:33549 return;
550 }
[email protected]93e8e2c2014-01-04 12:29:23551
552 if (!CheckForUpdates())
553 ScheduleNextRun(kStepDelayLong);
[email protected]61aca4cd2013-10-26 10:50:59554}
555
[email protected]93e8e2c2014-01-04 12:29:23556CrxUpdateItem* CrxUpdateService::FindReadyComponent() const {
[email protected]c4b4cfa2014-03-10 18:55:00557 class Helper {
558 public:
559 static bool IsReadyOnDemand(CrxUpdateItem* item) {
560 return item->on_demand && IsReady(item);
[email protected]61aca4cd2013-10-26 10:50:59561 }
[email protected]c4b4cfa2014-03-10 18:55:00562 static bool IsReady(CrxUpdateItem* item) {
563 return item->status == CrxUpdateItem::kCanUpdate;
564 }
565 };
[email protected]61aca4cd2013-10-26 10:50:59566
[email protected]c4b4cfa2014-03-10 18:55:00567 std::vector<CrxUpdateItem*>::const_iterator it = std::find_if(
568 work_items_.begin(), work_items_.end(), Helper::IsReadyOnDemand);
569 if (it != work_items_.end())
570 return *it;
571 it = std::find_if(work_items_.begin(), work_items_.end(), Helper::IsReady);
572 if (it != work_items_.end())
573 return *it;
[email protected]61aca4cd2013-10-26 10:50:59574 return NULL;
575}
576
[email protected]93e8e2c2014-01-04 12:29:23577// Prepares the components for an update check and initiates the request.
[email protected]21a9c9a2014-02-19 19:37:11578// On demand components are always included in the update check request.
579// Otherwise, only include components that have not been checked recently.
[email protected]93e8e2c2014-01-04 12:29:23580bool CrxUpdateService::CheckForUpdates() {
[email protected]21a9c9a2014-02-19 19:37:11581 const base::TimeDelta minimum_recheck_wait_time =
582 base::TimeDelta::FromSeconds(config_->MinimumReCheckWait());
583 const base::Time now(base::Time::Now());
584
[email protected]93e8e2c2014-01-04 12:29:23585 std::vector<CrxUpdateItem*> items_to_check;
586 for (size_t i = 0; i != work_items_.size(); ++i) {
587 CrxUpdateItem* item = work_items_[i];
588 DCHECK(item->status == CrxUpdateItem::kNew ||
589 item->status == CrxUpdateItem::kNoUpdate ||
590 item->status == CrxUpdateItem::kUpToDate ||
591 item->status == CrxUpdateItem::kUpdated);
592
[email protected]21a9c9a2014-02-19 19:37:11593 const base::TimeDelta time_since_last_checked(now - item->last_check);
594
595 if (!item->on_demand &&
596 time_since_last_checked < minimum_recheck_wait_time) {
[email protected]fb53e652014-04-30 11:27:19597 VLOG(1) << "Skipping check for component update: id=" << item->id
598 << ", time_since_last_checked="
599 << time_since_last_checked.InSeconds()
600 << " seconds: too soon to check for an update";
[email protected]21a9c9a2014-02-19 19:37:11601 continue;
602 }
603
[email protected]fb53e652014-04-30 11:27:19604 VLOG(1) << "Scheduling update check for component id=" << item->id
605 << ", time_since_last_checked="
[email protected]d0c8b8b42014-05-06 05:11:45606 << time_since_last_checked.InSeconds() << " seconds";
[email protected]fb53e652014-04-30 11:27:19607
[email protected]21a9c9a2014-02-19 19:37:11608 item->last_check = now;
[email protected]93e8e2c2014-01-04 12:29:23609 item->crx_urls.clear();
610 item->crx_diffurls.clear();
611 item->previous_version = item->component.version;
612 item->next_version = Version();
613 item->previous_fp = item->component.fingerprint;
614 item->next_fp.clear();
615 item->diff_update_failed = false;
616 item->error_category = 0;
617 item->error_code = 0;
618 item->extra_code1 = 0;
619 item->diff_error_category = 0;
620 item->diff_error_code = 0;
621 item->diff_extra_code1 = 0;
622 item->download_metrics.clear();
623
624 items_to_check.push_back(item);
[email protected]3a10c572014-07-04 06:57:47625
626 ChangeItemState(item, CrxUpdateItem::kChecking);
[email protected]93e8e2c2014-01-04 12:29:23627 }
628
629 if (items_to_check.empty())
630 return false;
631
[email protected]d0c8b8b42014-05-06 05:11:45632 update_checker_ =
[email protected]09974032014-06-27 07:42:08633 UpdateChecker::Create(*config_,
[email protected]d0c8b8b42014-05-06 05:11:45634 base::Bind(&CrxUpdateService::UpdateCheckComplete,
635 base::Unretained(this))).Pass();
[email protected]93e8e2c2014-01-04 12:29:23636 return update_checker_->CheckForUpdates(items_to_check,
637 config_->ExtraRequestParams());
638}
639
[email protected]61aca4cd2013-10-26 10:50:59640void CrxUpdateService::UpdateComponent(CrxUpdateItem* workitem) {
[email protected]afa378f22013-12-02 03:37:54641 scoped_ptr<CRXContext> crx_context(new CRXContext);
642 crx_context->pk_hash = workitem->component.pk_hash;
643 crx_context->id = workitem->id;
644 crx_context->installer = workitem->component.installer;
645 crx_context->fingerprint = workitem->next_fp;
[email protected]da37c1d2013-12-19 01:04:38646 const std::vector<GURL>* urls = NULL;
[email protected]cfd13e52014-02-05 09:35:07647 bool allow_background_download = false;
[email protected]61aca4cd2013-10-26 10:50:59648 if (CanTryDiffUpdate(workitem, *config_)) {
[email protected]da37c1d2013-12-19 01:04:38649 urls = &workitem->crx_diffurls;
[email protected]61aca4cd2013-10-26 10:50:59650 ChangeItemState(workitem, CrxUpdateItem::kDownloadingDiff);
651 } else {
[email protected]cfd13e52014-02-05 09:35:07652 // Background downloads are enabled only for selected components and
653 // only for full downloads (see issue 340448).
654 allow_background_download = workitem->component.allow_background_download;
[email protected]da37c1d2013-12-19 01:04:38655 urls = &workitem->crx_urls;
[email protected]61aca4cd2013-10-26 10:50:59656 ChangeItemState(workitem, CrxUpdateItem::kDownloading);
657 }
[email protected]3cb2a4f2013-12-07 21:54:34658
659 // On demand component updates are always downloaded in foreground.
[email protected]d0c8b8b42014-05-06 05:11:45660 const bool is_background_download = !workitem->on_demand &&
661 allow_background_download &&
662 config_->UseBackgroundDownloader();
[email protected]3cb2a4f2013-12-07 21:54:34663
[email protected]ed6fb982014-07-23 16:56:52664 crx_downloader_.reset(
665 CrxDownloader::Create(is_background_download,
666 config_->RequestContext(),
667 blocking_task_runner_,
668 config_->GetSingleThreadTaskRunner()));
[email protected]8a5ebd432014-05-02 00:21:22669 crx_downloader_->set_progress_callback(
670 base::Bind(&CrxUpdateService::DownloadProgress,
671 base::Unretained(this),
672 crx_context->id));
[email protected]1b6587dc52014-04-26 00:38:55673 crx_downloader_->StartDownload(*urls,
674 base::Bind(&CrxUpdateService::DownloadComplete,
675 base::Unretained(this),
676 base::Passed(&crx_context)));
[email protected]61aca4cd2013-10-26 10:50:59677}
678
[email protected]93e8e2c2014-01-04 12:29:23679void CrxUpdateService::UpdateCheckComplete(
680 int error,
681 const std::string& error_message,
[email protected]055981f2014-01-17 20:22:32682 const UpdateResponse::Results& results) {
[email protected]ed6fb982014-07-23 16:56:52683 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]93e8e2c2014-01-04 12:29:23684 update_checker_.reset();
685 if (!error)
686 OnUpdateCheckSucceeded(results);
[email protected]652f4272013-11-13 09:23:41687 else
[email protected]93e8e2c2014-01-04 12:29:23688 OnUpdateCheckFailed(error, error_message);
[email protected]e8f96ff2011-08-03 05:07:33689}
690
[email protected]93e8e2c2014-01-04 12:29:23691// Handles a valid Omaha update check response by matching the results with
692// the registered components which were checked for updates.
693// If updates are found, prepare the components for the actual version upgrade.
694// One of these components will be drafted for the upgrade next time
695// ProcessPendingItems is called.
696void CrxUpdateService::OnUpdateCheckSucceeded(
[email protected]055981f2014-01-17 20:22:32697 const UpdateResponse::Results& results) {
[email protected]2cddef42013-11-22 08:23:22698 size_t num_updates_pending = 0;
[email protected]ed6fb982014-07-23 16:56:52699 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]fb53e652014-04-30 11:27:19700 VLOG(1) << "Update check succeeded.";
[email protected]055981f2014-01-17 20:22:32701 std::vector<UpdateResponse::Result>::const_iterator it;
[email protected]e8f96ff2011-08-03 05:07:33702 for (it = results.list.begin(); it != results.list.end(); ++it) {
703 CrxUpdateItem* crx = FindUpdateItemById(it->extension_id);
704 if (!crx)
705 continue;
706
[email protected]2cddef42013-11-22 08:23:22707 if (crx->status != CrxUpdateItem::kChecking) {
708 NOTREACHED();
[email protected]e8f96ff2011-08-03 05:07:33709 continue; // Not updating this component now.
[email protected]2cddef42013-11-22 08:23:22710 }
[email protected]e8f96ff2011-08-03 05:07:33711
[email protected]2cddef42013-11-22 08:23:22712 if (it->manifest.version.empty()) {
[email protected]cf442612011-08-09 20:20:12713 // No version means no update available.
[email protected]61aca4cd2013-10-26 10:50:59714 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]fb53e652014-04-30 11:27:19715 VLOG(1) << "No update available for component: " << crx->id;
[email protected]cf442612011-08-09 20:20:12716 continue;
[email protected]e8f96ff2011-08-03 05:07:33717 }
[email protected]2cddef42013-11-22 08:23:22718
719 if (!IsVersionNewer(crx->component.version, it->manifest.version)) {
720 // The component is up to date.
[email protected]61aca4cd2013-10-26 10:50:59721 ChangeItemState(crx, CrxUpdateItem::kUpToDate);
[email protected]fb53e652014-04-30 11:27:19722 VLOG(1) << "Component already up-to-date: " << crx->id;
[email protected]cf442612011-08-09 20:20:12723 continue;
[email protected]e8f96ff2011-08-03 05:07:33724 }
[email protected]2cddef42013-11-22 08:23:22725
726 if (!it->manifest.browser_min_version.empty()) {
[email protected]6a8ab1d2014-07-10 22:47:39727 if (IsVersionNewer(config_->GetBrowserVersion(),
728 it->manifest.browser_min_version)) {
[email protected]2cddef42013-11-22 08:23:22729 // The component is not compatible with this Chrome version.
[email protected]fb53e652014-04-30 11:27:19730 VLOG(1) << "Ignoring incompatible component: " << crx->id;
[email protected]61aca4cd2013-10-26 10:50:59731 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]cf442612011-08-09 20:20:12732 continue;
733 }
[email protected]e8f96ff2011-08-03 05:07:33734 }
[email protected]2cddef42013-11-22 08:23:22735
736 if (it->manifest.packages.size() != 1) {
737 // Assume one and only one package per component.
[email protected]fb53e652014-04-30 11:27:19738 VLOG(1) << "Ignoring multiple packages for component: " << crx->id;
[email protected]2cddef42013-11-22 08:23:22739 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
740 continue;
741 }
742
743 // Parse the members of the result and queue an upgrade for this component.
[email protected]c5e4a2222014-01-03 16:06:13744 crx->next_version = Version(it->manifest.version);
[email protected]2cddef42013-11-22 08:23:22745
[email protected]fb53e652014-04-30 11:27:19746 VLOG(1) << "Update found for component: " << crx->id;
747
[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())
[email protected]d0c8b8b42014-05-06 05:11:45756 crx->crx_urls.push_back(url);
[email protected]da37c1d2013-12-19 01:04:38757 }
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())
[email protected]d0c8b8b42014-05-06 05:11:45761 crx->crx_diffurls.push_back(url);
[email protected]da37c1d2013-12-19 01:04:38762 }
[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:23777void CrxUpdateService::OnUpdateCheckFailed(int error,
778 const std::string& error_message) {
[email protected]ed6fb982014-07-23 16:56:52779 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]93e8e2c2014-01-04 12:29:23780 DCHECK(error);
[email protected]d0c8b8b42014-05-06 05:11:45781 size_t count =
782 ChangeItemStatus(CrxUpdateItem::kChecking, CrxUpdateItem::kNoUpdate);
[email protected]e8f96ff2011-08-03 05:07:33783 DCHECK_GT(count, 0ul);
[email protected]fb53e652014-04-30 11:27:19784 VLOG(1) << "Update check failed.";
[email protected]32a6c8382013-08-20 00:29:20785 ScheduleNextRun(kStepDelayLong);
[email protected]e8f96ff2011-08-03 05:07:33786}
787
[email protected]8a5ebd432014-05-02 00:21:22788// Called when progress is being made downloading a CRX. The progress may
789// not monotonically increase due to how the CRX downloader switches between
790// different downloaders and fallback urls.
791void CrxUpdateService::DownloadProgress(
792 const std::string& component_id,
793 const CrxDownloader::Result& download_result) {
[email protected]ed6fb982014-07-23 16:56:52794 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]8a5ebd432014-05-02 00:21:22795 NotifyObservers(Observer::COMPONENT_UPDATE_DOWNLOADING, component_id);
796}
797
[email protected]e8f96ff2011-08-03 05:07:33798// Called when the CRX package has been downloaded to a temporary location.
799// Here we fire the notifications and schedule the component-specific installer
800// to be called in the file thread.
[email protected]3cb2a4f2013-12-07 21:54:34801void CrxUpdateService::DownloadComplete(
802 scoped_ptr<CRXContext> crx_context,
[email protected]3a0092d2013-12-18 03:04:35803 const CrxDownloader::Result& download_result) {
[email protected]ed6fb982014-07-23 16:56:52804 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]64f39fa12013-09-03 21:49:37805
[email protected]64f39fa12013-09-03 21:49:37806 CrxUpdateItem* crx = FindUpdateItemById(crx_context->id);
[email protected]e3e696d32013-06-21 20:41:36807 DCHECK(crx->status == CrxUpdateItem::kDownloadingDiff ||
808 crx->status == CrxUpdateItem::kDownloading);
809
[email protected]3a0092d2013-12-18 03:04:35810 AppendDownloadMetrics(crx_downloader_->download_metrics(),
811 &crx->download_metrics);
812
[email protected]8a5ebd432014-05-02 00:21:22813 crx_downloader_.reset();
814
[email protected]3cb2a4f2013-12-07 21:54:34815 if (download_result.error) {
[email protected]e3e696d32013-06-21 20:41:36816 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
[email protected]7b0529242013-07-20 05:45:46817 crx->diff_error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34818 crx->diff_error_code = download_result.error;
[email protected]e630ba62013-06-22 15:22:34819 crx->diff_update_failed = true;
[email protected]e3e696d32013-06-21 20:41:36820 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
821 CrxUpdateItem::kCanUpdate);
822 DCHECK_EQ(count, 1ul);
[email protected]7b0529242013-07-20 05:45:46823
[email protected]32a6c8382013-08-20 00:29:20824 ScheduleNextRun(kStepDelayShort);
[email protected]e3e696d32013-06-21 20:41:36825 return;
826 }
[email protected]7b0529242013-07-20 05:45:46827 crx->error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34828 crx->error_code = download_result.error;
[email protected]d0c8b8b42014-05-06 05:11:45829 size_t count =
830 ChangeItemStatus(CrxUpdateItem::kDownloading, CrxUpdateItem::kNoUpdate);
[email protected]e8f96ff2011-08-03 05:07:33831 DCHECK_EQ(count, 1ul);
[email protected]e3e696d32013-06-21 20:41:36832
[email protected]7b0529242013-07-20 05:45:46833 // At this point, since both the differential and the full downloads failed,
834 // the update for this component has finished with an error.
835 ping_manager_->OnUpdateComplete(crx);
836
[email protected]32a6c8382013-08-20 00:29:20837 // Move on to the next update, if there is one available.
838 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33839 } else {
[email protected]e3e696d32013-06-21 20:41:36840 size_t count = 0;
841 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
842 count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
843 CrxUpdateItem::kUpdatingDiff);
844 } else {
845 count = ChangeItemStatus(CrxUpdateItem::kDownloading,
846 CrxUpdateItem::kUpdating);
847 }
[email protected]e8f96ff2011-08-03 05:07:33848 DCHECK_EQ(count, 1ul);
[email protected]cf442612011-08-09 20:20:12849
[email protected]44da56e2011-11-21 19:59:14850 // Why unretained? See comment at top of file.
[email protected]8f5f2ea2013-10-31 09:39:10851 blocking_task_runner_->PostDelayedTask(
[email protected]73251e72012-03-04 02:10:33852 FROM_HERE,
[email protected]44da56e2011-11-21 19:59:14853 base::Bind(&CrxUpdateService::Install,
854 base::Unretained(this),
[email protected]afa378f22013-12-02 03:37:54855 base::Passed(&crx_context),
[email protected]3cb2a4f2013-12-07 21:54:34856 download_result.response),
[email protected]73251e72012-03-04 02:10:33857 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]e8f96ff2011-08-03 05:07:33858 }
[email protected]e8f96ff2011-08-03 05:07:33859}
860
861// Install consists of digital signature verification, unpacking and then
862// calling the component specific installer. All that is handled by the
[email protected]f5d27e32014-01-31 06:48:53863// |unpacker_|. If there is an error this function is in charge of deleting
[email protected]e8f96ff2011-08-03 05:07:33864// the files created.
[email protected]afa378f22013-12-02 03:37:54865void CrxUpdateService::Install(scoped_ptr<CRXContext> context,
[email protected]650b2d52013-02-10 03:41:45866 const base::FilePath& crx_path) {
[email protected]8f5f2ea2013-10-31 09:39:10867 // This function owns the file at |crx_path| and the |context| object.
[email protected]94a481b2014-03-28 19:41:55868 unpacker_ = new ComponentUnpacker(context->pk_hash,
869 crx_path,
870 context->fingerprint,
871 context->installer,
872 config_->InProcess(),
873 blocking_task_runner_);
[email protected]f5d27e32014-01-31 06:48:53874 unpacker_->Unpack(base::Bind(&CrxUpdateService::EndUnpacking,
875 base::Unretained(this),
876 context->id,
877 crx_path));
878}
879
880void CrxUpdateService::EndUnpacking(const std::string& component_id,
881 const base::FilePath& crx_path,
882 ComponentUnpacker::Error error,
883 int extended_error) {
[email protected]055981f2014-01-17 20:22:32884 if (!DeleteFileAndEmptyParentDirectory(crx_path))
[email protected]e8f96ff2011-08-03 05:07:33885 NOTREACHED() << crx_path.value();
[email protected]73251e72012-03-04 02:10:33886 BrowserThread::PostDelayedTask(
887 BrowserThread::UI,
888 FROM_HERE,
[email protected]d0c8b8b42014-05-06 05:11:45889 base::Bind(&CrxUpdateService::DoneInstalling,
890 base::Unretained(this),
891 component_id,
892 error,
893 extended_error),
[email protected]73251e72012-03-04 02:10:33894 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]94a481b2014-03-28 19:41:55895 // Reset the unpacker last, otherwise we free our own arguments.
896 unpacker_ = NULL;
[email protected]e8f96ff2011-08-03 05:07:33897}
898
899// Installation has been completed. Adjust the component status and
[email protected]7b0529242013-07-20 05:45:46900// schedule the next check. Schedule a short delay before trying the full
901// update when the differential update failed.
[email protected]07f93af12011-08-17 20:57:22902void CrxUpdateService::DoneInstalling(const std::string& component_id,
[email protected]e3e696d32013-06-21 20:41:36903 ComponentUnpacker::Error error,
904 int extra_code) {
[email protected]ed6fb982014-07-23 16:56:52905 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]07f93af12011-08-17 20:57:22906
[email protected]7b0529242013-07-20 05:45:46907 ErrorCategory error_category = kErrorNone;
908 switch (error) {
909 case ComponentUnpacker::kNone:
910 break;
911 case ComponentUnpacker::kInstallerError:
912 error_category = kInstallError;
913 break;
914 default:
915 error_category = kUnpackError;
916 break;
917 }
918
919 const bool is_success = error == ComponentUnpacker::kNone;
920
[email protected]07f93af12011-08-17 20:57:22921 CrxUpdateItem* item = FindUpdateItemById(component_id);
[email protected]7b0529242013-07-20 05:45:46922 if (item->status == CrxUpdateItem::kUpdatingDiff && !is_success) {
923 item->diff_error_category = error_category;
924 item->diff_error_code = error;
925 item->diff_extra_code1 = extra_code;
[email protected]1ea21ad2013-09-02 04:20:39926 item->diff_update_failed = true;
927 size_t count = ChangeItemStatus(CrxUpdateItem::kUpdatingDiff,
928 CrxUpdateItem::kCanUpdate);
929 DCHECK_EQ(count, 1ul);
930 ScheduleNextRun(kStepDelayShort);
931 return;
[email protected]d0c8b8b42014-05-06 05:11:45932 }
[email protected]e3e696d32013-06-21 20:41:36933
[email protected]7b0529242013-07-20 05:45:46934 if (is_success) {
[email protected]07f93af12011-08-17 20:57:22935 item->component.version = item->next_version;
[email protected]e3e696d32013-06-21 20:41:36936 item->component.fingerprint = item->next_fp;
[email protected]3a10c572014-07-04 06:57:47937 ChangeItemState(item, CrxUpdateItem::kUpdated);
[email protected]7b0529242013-07-20 05:45:46938 } else {
[email protected]7b0529242013-07-20 05:45:46939 item->error_category = error_category;
940 item->error_code = error;
941 item->extra_code1 = extra_code;
[email protected]3a10c572014-07-04 06:57:47942 ChangeItemState(item, CrxUpdateItem::kNoUpdate);
[email protected]e3e696d32013-06-21 20:41:36943 }
[email protected]07f93af12011-08-17 20:57:22944
[email protected]7b0529242013-07-20 05:45:46945 ping_manager_->OnUpdateComplete(item);
[email protected]360b8bb2011-09-01 21:48:06946
[email protected]32a6c8382013-08-20 00:29:20947 // Move on to the next update, if there is one available.
948 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33949}
950
[email protected]d3268fe2014-04-25 02:14:23951void CrxUpdateService::NotifyObservers(Observer::Events event,
952 const std::string& id) {
[email protected]ed6fb982014-07-23 16:56:52953 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]d3268fe2014-04-25 02:14:23954 FOR_EACH_OBSERVER(Observer, observer_list_, OnEvent(event, id));
[email protected]85e61d52013-08-01 22:23:42955}
956
[email protected]00a77fa2013-11-02 04:18:46957content::ResourceThrottle* CrxUpdateService::GetOnDemandResourceThrottle(
[email protected]d0c8b8b42014-05-06 05:11:45958 net::URLRequest* request,
959 const std::string& crx_id) {
[email protected]00a77fa2013-11-02 04:18:46960 // We give the raw pointer to the caller, who will delete it at will
961 // and we keep for ourselves a weak pointer to it so we can post tasks
962 // from the UI thread without having to track lifetime directly.
963 CUResourceThrottle* rt = new CUResourceThrottle(request);
[email protected]d0c8b8b42014-05-06 05:11:45964 BrowserThread::PostTask(BrowserThread::UI,
965 FROM_HERE,
966 base::Bind(&CrxUpdateService::OnNewResourceThrottle,
967 base::Unretained(this),
968 rt->AsWeakPtr(),
969 crx_id));
[email protected]00a77fa2013-11-02 04:18:46970 return rt;
971}
972
973void CrxUpdateService::OnNewResourceThrottle(
[email protected]d0c8b8b42014-05-06 05:11:45974 base::WeakPtr<CUResourceThrottle> rt,
975 const std::string& crx_id) {
[email protected]ed6fb982014-07-23 16:56:52976 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]00a77fa2013-11-02 04:18:46977 // Check if we can on-demand update, else unblock the request anyway.
978 CrxUpdateItem* item = FindUpdateItemById(crx_id);
[email protected]5b53f932014-06-06 10:26:54979 Status status = OnDemandUpdateWithCooldown(item);
[email protected]00a77fa2013-11-02 04:18:46980 if (status == kOk || status == kInProgress) {
981 item->throttles.push_back(rt);
982 return;
983 }
984 UnblockResourceThrottle(rt);
985}
986
[email protected]68bf09e2014-06-03 00:10:56987// Start the process of checking for an update, for a particular component
988// that was previously registered.
989// |component_id| is a value returned from GetCrxComponentID().
[email protected]504830a2014-05-20 21:53:54990ComponentUpdateService::Status CrxUpdateService::OnDemandUpdate(
991 const std::string& component_id) {
992 return OnDemandUpdateInternal(FindUpdateItemById(component_id));
993}
994
[email protected]5b53f932014-06-06 10:26:54995ComponentUpdateService::Status CrxUpdateService::OnDemandUpdateWithCooldown(
[email protected]504830a2014-05-20 21:53:54996 CrxUpdateItem* uit) {
997 if (!uit)
998 return kError;
999
1000 // Check if the request is too soon.
1001 base::TimeDelta delta = base::Time::Now() - uit->last_check;
1002 if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay()))
1003 return kError;
1004
[email protected]5b53f932014-06-06 10:26:541005 return OnDemandUpdateInternal(uit);
1006}
1007
1008ComponentUpdateService::Status CrxUpdateService::OnDemandUpdateInternal(
1009 CrxUpdateItem* uit) {
1010 if (!uit)
1011 return kError;
1012
[email protected]68bf09e2014-06-03 00:10:561013 uit->on_demand = true;
[email protected]504830a2014-05-20 21:53:541014
[email protected]3a10c572014-07-04 06:57:471015 // If there is an update available for this item, then continue processing
1016 // the update. This is an artifact of how update checks are done: in addition
1017 // to the on-demand item, the update check may include other items as well.
1018 if (uit->status != CrxUpdateItem::kCanUpdate) {
1019 Status service_status = GetServiceStatus(uit->status);
1020 // If the item is already in the process of being updated, there is
1021 // no point in this call, so return kInProgress.
1022 if (service_status == kInProgress)
1023 return service_status;
1024
1025 // Otherwise the item was already checked a while back (or it is new),
1026 // set its status to kNew to give it a slightly higher priority.
1027 ChangeItemState(uit, CrxUpdateItem::kNew);
1028 }
1029
[email protected]504830a2014-05-20 21:53:541030 // In case the current delay is long, set the timer to a shorter value
1031 // to get the ball rolling.
1032 if (timer_.IsRunning()) {
1033 timer_.Stop();
1034 timer_.Start(FROM_HERE,
1035 base::TimeDelta::FromSeconds(config_->StepDelay()),
1036 this,
1037 &CrxUpdateService::ProcessPendingItems);
1038 }
1039
1040 return kOk;
1041}
1042
[email protected]68bf09e2014-06-03 00:10:561043ComponentUpdateService::Status CrxUpdateService::GetServiceStatus(
1044 CrxUpdateItem::Status status) {
1045 switch (status) {
1046 case CrxUpdateItem::kChecking:
1047 case CrxUpdateItem::kCanUpdate:
1048 case CrxUpdateItem::kDownloadingDiff:
1049 case CrxUpdateItem::kDownloading:
1050 case CrxUpdateItem::kUpdatingDiff:
1051 case CrxUpdateItem::kUpdating:
1052 return kInProgress;
1053 case CrxUpdateItem::kNew:
1054 case CrxUpdateItem::kUpdated:
1055 case CrxUpdateItem::kUpToDate:
1056 case CrxUpdateItem::kNoUpdate:
1057 return kOk;
1058 case CrxUpdateItem::kLastStatus:
1059 NOTREACHED() << status;
1060 }
1061 return kError;
1062}
1063
[email protected]00a77fa2013-11-02 04:18:461064///////////////////////////////////////////////////////////////////////////////
1065
1066CUResourceThrottle::CUResourceThrottle(const net::URLRequest* request)
1067 : state_(NEW) {
1068 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1069}
1070
1071CUResourceThrottle::~CUResourceThrottle() {
1072 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1073}
1074
1075void CUResourceThrottle::WillStartRequest(bool* defer) {
1076 if (state_ != UNBLOCKED) {
1077 state_ = BLOCKED;
1078 *defer = true;
1079 } else {
1080 *defer = false;
1081 }
1082}
1083
1084void CUResourceThrottle::WillRedirectRequest(const GURL& new_url, bool* defer) {
1085 WillStartRequest(defer);
1086}
1087
[email protected]f8fe5cf2013-12-04 20:11:531088const char* CUResourceThrottle::GetNameForLogging() const {
1089 return "ComponentUpdateResourceThrottle";
1090}
1091
[email protected]00a77fa2013-11-02 04:18:461092void CUResourceThrottle::Unblock() {
1093 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1094 if (state_ == BLOCKED)
1095 controller()->Resume();
1096 state_ = UNBLOCKED;
1097}
1098
[email protected]e8f96ff2011-08-03 05:07:331099// The component update factory. Using the component updater as a singleton
1100// is the job of the browser process.
[email protected]655043812014-06-24 01:50:361101ComponentUpdateService* ComponentUpdateServiceFactory(Configurator* config) {
[email protected]e8f96ff2011-08-03 05:07:331102 DCHECK(config);
1103 return new CrxUpdateService(config);
1104}
[email protected]2cddef42013-11-22 08:23:221105
[email protected]055981f2014-01-17 20:22:321106} // namespace component_updater