blob: 66d1f6e75ab6118fda19b626dfa3fdcb75153112 [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]78efe2e92014-08-08 15:53:2213#include "base/callback.h"
[email protected]e3e696d32013-06-21 20:41:3614#include "base/compiler_specific.h"
[email protected]e8f96ff2011-08-03 05:07:3315#include "base/file_util.h"
[email protected]57999812013-02-24 05:40:5216#include "base/files/file_path.h"
[email protected]e8f96ff2011-08-03 05:07:3317#include "base/logging.h"
[email protected]7226b33c2011-08-18 08:44:2218#include "base/memory/scoped_ptr.h"
[email protected]e93632052014-07-28 23:51:0919#include "base/message_loop/message_loop_proxy.h"
[email protected]d3268fe2014-04-25 02:14:2320#include "base/observer_list.h"
[email protected]f5d27e32014-01-31 06:48:5321#include "base/sequenced_task_runner.h"
[email protected]e8f96ff2011-08-03 05:07:3322#include "base/stl_util.h"
[email protected]8f5f2ea2013-10-31 09:39:1023#include "base/threading/sequenced_worker_pool.h"
[email protected]ed6fb982014-07-23 16:56:5224#include "base/threading/thread_checker.h"
[email protected]41a17c52013-06-28 00:27:5325#include "base/timer/timer.h"
[email protected]e260af72014-08-05 07:52:3926#include "chrome/browser/component_updater/component_patcher_operation.h"
[email protected]e8f96ff2011-08-03 05:07:3327#include "chrome/browser/component_updater/component_unpacker.h"
[email protected]655043812014-06-24 01:50:3628#include "chrome/browser/component_updater/component_updater_configurator.h"
[email protected]7b0529242013-07-20 05:45:4629#include "chrome/browser/component_updater/component_updater_ping_manager.h"
[email protected]2cddef42013-11-22 08:23:2230#include "chrome/browser/component_updater/component_updater_utils.h"
[email protected]afa378f22013-12-02 03:37:5431#include "chrome/browser/component_updater/crx_downloader.h"
[email protected]7b0529242013-07-20 05:45:4632#include "chrome/browser/component_updater/crx_update_item.h"
[email protected]93e8e2c2014-01-04 12:29:2333#include "chrome/browser/component_updater/update_checker.h"
[email protected]6268d3a2013-11-27 01:28:0934#include "chrome/browser/component_updater/update_response.h"
[email protected]761fa4702013-07-02 15:25:1535#include "url/gurl.h"
[email protected]e8f96ff2011-08-03 05:07:3336
[email protected]055981f2014-01-17 20:22:3237namespace component_updater {
[email protected]3a0092d2013-12-18 03:04:3538
[email protected]44da56e2011-11-21 19:59:1439// The component updater is designed to live until process shutdown, so
40// base::Bind() calls are not refcounted.
41
[email protected]e8f96ff2011-08-03 05:07:3342namespace {
[email protected]e3e696d32013-06-21 20:41:3643
[email protected]2cddef42013-11-22 08:23:2244// Returns true if the |proposed| version is newer than |current| version.
[email protected]c5e4a2222014-01-03 16:06:1345bool IsVersionNewer(const Version& current, const std::string& proposed) {
46 Version proposed_ver(proposed);
[email protected]2cddef42013-11-22 08:23:2247 return proposed_ver.IsValid() && current.CompareTo(proposed_ver) < 0;
[email protected]e8f96ff2011-08-03 05:07:3348}
49
[email protected]e3e696d32013-06-21 20:41:3650// Returns true if a differential update is available, it has not failed yet,
51// and the configuration allows it.
52bool CanTryDiffUpdate(const CrxUpdateItem* update_item,
[email protected]655043812014-06-24 01:50:3653 const Configurator& config) {
[email protected]d0c8b8b42014-05-06 05:11:4554 return HasDiffUpdate(update_item) && !update_item->diff_update_failed &&
[email protected]e3e696d32013-06-21 20:41:3655 config.DeltasEnabled();
56}
57
[email protected]3a0092d2013-12-18 03:04:3558void AppendDownloadMetrics(
59 const std::vector<CrxDownloader::DownloadMetrics>& source,
60 std::vector<CrxDownloader::DownloadMetrics>* destination) {
61 destination->insert(destination->end(), source.begin(), source.end());
62}
63
[email protected]7b0529242013-07-20 05:45:4664} // namespace
65
66CrxUpdateItem::CrxUpdateItem()
67 : status(kNew),
[email protected]61aca4cd2013-10-26 10:50:5968 on_demand(false),
[email protected]7b0529242013-07-20 05:45:4669 diff_update_failed(false),
70 error_category(0),
71 error_code(0),
72 extra_code1(0),
73 diff_error_category(0),
74 diff_error_code(0),
75 diff_extra_code1(0) {
76}
77
78CrxUpdateItem::~CrxUpdateItem() {
79}
[email protected]86550a42013-06-21 15:20:4980
[email protected]dc06f0b2013-01-23 20:03:1681CrxComponent::CrxComponent()
[email protected]d0c8b8b42014-05-06 05:11:4582 : installer(NULL), allow_background_download(true) {
[email protected]dc06f0b2013-01-23 20:03:1683}
84
85CrxComponent::~CrxComponent() {
86}
[email protected]e8f96ff2011-08-03 05:07:3387
[email protected]e8f96ff2011-08-03 05:07:3388//////////////////////////////////////////////////////////////////////////////
89// The one and only implementation of the ComponentUpdateService interface. In
90// charge of running the show. The main method is ProcessPendingItems() which
[email protected]50b01392012-09-01 03:33:1391// is called periodically to do the upgrades/installs or the update checks.
[email protected]e8f96ff2011-08-03 05:07:3392// An important consideration here is to be as "low impact" as we can to the
93// rest of the browser, so even if we have many components registered and
[email protected]f1050432012-02-15 01:35:4694// eligible for update, we only do one thing at a time with pauses in between
[email protected]e8f96ff2011-08-03 05:07:3395// the tasks. Also when we do network requests there is only one |url_fetcher_|
[email protected]e3e696d32013-06-21 20:41:3696// in flight at a time.
[email protected]e8f96ff2011-08-03 05:07:3397// There are no locks in this code, the main structure |work_items_| is mutated
[email protected]ed6fb982014-07-23 16:56:5298// only from the main thread. The unpack and installation is done in a blocking
[email protected]74be2642014-02-07 09:40:3799// pool thread. The network requests are done in the IO thread or in the file
[email protected]e8f96ff2011-08-03 05:07:33100// thread.
[email protected]504830a2014-05-20 21:53:54101class CrxUpdateService : public ComponentUpdateService, public OnDemandUpdater {
[email protected]e8f96ff2011-08-03 05:07:33102 public:
[email protected]655043812014-06-24 01:50:36103 explicit CrxUpdateService(Configurator* config);
[email protected]e8f96ff2011-08-03 05:07:33104 virtual ~CrxUpdateService();
105
106 // Overrides for ComponentUpdateService.
[email protected]d3268fe2014-04-25 02:14:23107 virtual void AddObserver(Observer* observer) OVERRIDE;
108 virtual void RemoveObserver(Observer* observer) OVERRIDE;
[email protected]e8f96ff2011-08-03 05:07:33109 virtual Status Start() OVERRIDE;
110 virtual Status Stop() OVERRIDE;
111 virtual Status RegisterComponent(const CrxComponent& component) OVERRIDE;
[email protected]68bf09e2014-06-03 00:10:56112 virtual std::vector<std::string> GetComponentIDs() const OVERRIDE;
[email protected]504830a2014-05-20 21:53:54113 virtual OnDemandUpdater& GetOnDemandUpdater() OVERRIDE;
[email protected]78efe2e92014-08-08 15:53:22114 virtual void MaybeThrottle(const std::string& crx_id,
115 const base::Closure& callback) OVERRIDE;
[email protected]ed6fb982014-07-23 16:56:52116 virtual scoped_refptr<base::SequencedTaskRunner> GetSequencedTaskRunner()
117 OVERRIDE;
[email protected]504830a2014-05-20 21:53:54118
[email protected]28ea9ac2014-05-03 22:07:18119 // Context for a crx download url request.
120 struct CRXContext {
121 ComponentInstaller* installer;
122 std::vector<uint8> pk_hash;
123 std::string id;
124 std::string fingerprint;
125 CRXContext() : installer(NULL) {}
126 };
[email protected]e8f96ff2011-08-03 05:07:33127
[email protected]e8f96ff2011-08-03 05:07:33128 private:
[email protected]7b0529242013-07-20 05:45:46129 enum ErrorCategory {
130 kErrorNone = 0,
131 kNetworkError,
132 kUnpackError,
133 kInstallError,
134 };
135
[email protected]32a6c8382013-08-20 00:29:20136 enum StepDelayInterval {
137 kStepDelayShort = 0,
138 kStepDelayMedium,
139 kStepDelayLong,
140 };
141
[email protected]78efe2e92014-08-08 15:53:22142 // Overrides for ComponentUpdateService.
143 virtual bool GetComponentDetails(const std::string& component_id,
144 CrxUpdateItem* item) const OVERRIDE;
145
146 // Overrides for OnDemandUpdater.
147 virtual Status OnDemandUpdate(const std::string& component_id) OVERRIDE;
148
[email protected]055981f2014-01-17 20:22:32149 void UpdateCheckComplete(int error,
150 const std::string& error_message,
151 const UpdateResponse::Results& results);
152 void OnUpdateCheckSucceeded(const UpdateResponse::Results& results);
[email protected]93e8e2c2014-01-04 12:29:23153 void OnUpdateCheckFailed(int error, const std::string& error_message);
[email protected]e8f96ff2011-08-03 05:07:33154
[email protected]8a5ebd432014-05-02 00:21:22155 void DownloadProgress(const std::string& component_id,
156 const CrxDownloader::Result& download_result);
157
158 void DownloadComplete(scoped_ptr<CRXContext> crx_context,
159 const CrxDownloader::Result& download_result);
[email protected]afa378f22013-12-02 03:37:54160
[email protected]00a77fa2013-11-02 04:18:46161 Status OnDemandUpdateInternal(CrxUpdateItem* item);
[email protected]5b53f932014-06-06 10:26:54162 Status OnDemandUpdateWithCooldown(CrxUpdateItem* item);
[email protected]00a77fa2013-11-02 04:18:46163
[email protected]e8f96ff2011-08-03 05:07:33164 void ProcessPendingItems();
165
[email protected]93e8e2c2014-01-04 12:29:23166 // Find a component that is ready to update.
167 CrxUpdateItem* FindReadyComponent() const;
168
169 // Prepares the components for an update check and initiates the request.
170 // Returns true if an update check request has been made. Returns false if
171 // no update check was needed or an error occured.
172 bool CheckForUpdates();
[email protected]61aca4cd2013-10-26 10:50:59173
174 void UpdateComponent(CrxUpdateItem* workitem);
175
[email protected]32a6c8382013-08-20 00:29:20176 void ScheduleNextRun(StepDelayInterval step_delay);
[email protected]e8f96ff2011-08-03 05:07:33177
[email protected]6268d3a2013-11-27 01:28:09178 void ParseResponse(const std::string& xml);
[email protected]e8f96ff2011-08-03 05:07:33179
[email protected]afa378f22013-12-02 03:37:54180 void Install(scoped_ptr<CRXContext> context, const base::FilePath& crx_path);
[email protected]e8f96ff2011-08-03 05:07:33181
[email protected]f5d27e32014-01-31 06:48:53182 void EndUnpacking(const std::string& component_id,
183 const base::FilePath& crx_path,
184 ComponentUnpacker::Error error,
185 int extended_error);
186
[email protected]07f93af12011-08-17 20:57:22187 void DoneInstalling(const std::string& component_id,
[email protected]e3e696d32013-06-21 20:41:36188 ComponentUnpacker::Error error,
189 int extended_error);
[email protected]e8f96ff2011-08-03 05:07:33190
[email protected]61aca4cd2013-10-26 10:50:59191 void ChangeItemState(CrxUpdateItem* item, CrxUpdateItem::Status to);
192
[email protected]d0c8b8b42014-05-06 05:11:45193 size_t ChangeItemStatus(CrxUpdateItem::Status from, CrxUpdateItem::Status to);
[email protected]e8f96ff2011-08-03 05:07:33194
[email protected]68bf09e2014-06-03 00:10:56195 CrxUpdateItem* FindUpdateItemById(const std::string& id) const;
[email protected]e8f96ff2011-08-03 05:07:33196
[email protected]d3268fe2014-04-25 02:14:23197 void NotifyObservers(Observer::Events event, const std::string& id);
[email protected]85e61d52013-08-01 22:23:42198
[email protected]61aca4cd2013-10-26 10:50:59199 bool HasOnDemandItems() const;
200
[email protected]68bf09e2014-06-03 00:10:56201 Status GetServiceStatus(const CrxUpdateItem::Status status);
202
[email protected]655043812014-06-24 01:50:36203 scoped_ptr<Configurator> config_;
[email protected]e3e696d32013-06-21 20:41:36204
[email protected]055981f2014-01-17 20:22:32205 scoped_ptr<UpdateChecker> update_checker_;
[email protected]e8f96ff2011-08-03 05:07:33206
[email protected]055981f2014-01-17 20:22:32207 scoped_ptr<PingManager> ping_manager_;
[email protected]7b0529242013-07-20 05:45:46208
[email protected]94a481b2014-03-28 19:41:55209 scoped_refptr<ComponentUnpacker> unpacker_;
[email protected]f5d27e32014-01-31 06:48:53210
[email protected]055981f2014-01-17 20:22:32211 scoped_ptr<CrxDownloader> crx_downloader_;
[email protected]afa378f22013-12-02 03:37:54212
[email protected]86550a42013-06-21 15:20:49213 // A collection of every work item.
[email protected]e3e696d32013-06-21 20:41:36214 typedef std::vector<CrxUpdateItem*> UpdateItems;
[email protected]e8f96ff2011-08-03 05:07:33215 UpdateItems work_items_;
216
217 base::OneShotTimer<CrxUpdateService> timer_;
218
[email protected]ed6fb982014-07-23 16:56:52219 base::ThreadChecker thread_checker_;
220
[email protected]e93632052014-07-28 23:51:09221 // Used to post responses back to the main thread.
222 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
223
[email protected]8f5f2ea2013-10-31 09:39:10224 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
225
[email protected]e8f96ff2011-08-03 05:07:33226 bool running_;
227
[email protected]d3268fe2014-04-25 02:14:23228 ObserverList<Observer> observer_list_;
229
[email protected]e8f96ff2011-08-03 05:07:33230 DISALLOW_COPY_AND_ASSIGN(CrxUpdateService);
231};
232
[email protected]e8f96ff2011-08-03 05:07:33233//////////////////////////////////////////////////////////////////////////////
234
[email protected]655043812014-06-24 01:50:36235CrxUpdateService::CrxUpdateService(Configurator* config)
[email protected]e8f96ff2011-08-03 05:07:33236 : config_(config),
[email protected]09974032014-06-27 07:42:08237 ping_manager_(new PingManager(*config)),
[email protected]e93632052014-07-28 23:51:09238 main_task_runner_(base::MessageLoopProxy::current()),
[email protected]ed6fb982014-07-23 16:56:52239 blocking_task_runner_(config->GetSequencedTaskRunner()),
[email protected]e8f96ff2011-08-03 05:07:33240 running_(false) {
[email protected]1ea21ad2013-09-02 04:20:39241}
[email protected]e8f96ff2011-08-03 05:07:33242
243CrxUpdateService::~CrxUpdateService() {
[email protected]ed6fb982014-07-23 16:56:52244 // Because we are a singleton, at this point only the main thread should be
[email protected]e8f96ff2011-08-03 05:07:33245 // alive, this simplifies the management of the work that could be in
246 // flight in other threads.
247 Stop();
248 STLDeleteElements(&work_items_);
[email protected]1ea21ad2013-09-02 04:20:39249}
[email protected]e8f96ff2011-08-03 05:07:33250
[email protected]d3268fe2014-04-25 02:14:23251void CrxUpdateService::AddObserver(Observer* observer) {
[email protected]ed6fb982014-07-23 16:56:52252 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]d3268fe2014-04-25 02:14:23253 observer_list_.AddObserver(observer);
254}
255
256void CrxUpdateService::RemoveObserver(Observer* observer) {
[email protected]ed6fb982014-07-23 16:56:52257 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]d3268fe2014-04-25 02:14:23258 observer_list_.RemoveObserver(observer);
259}
260
[email protected]e8f96ff2011-08-03 05:07:33261ComponentUpdateService::Status CrxUpdateService::Start() {
262 // Note that RegisterComponent will call Start() when the first
263 // component is registered, so it can be called twice. This way
264 // we avoid scheduling the timer if there is no work to do.
[email protected]fb53e652014-04-30 11:27:19265 VLOG(1) << "CrxUpdateService starting up";
[email protected]e8f96ff2011-08-03 05:07:33266 running_ = true;
267 if (work_items_.empty())
268 return kOk;
269
[email protected]d3268fe2014-04-25 02:14:23270 NotifyObservers(Observer::COMPONENT_UPDATER_STARTED, "");
[email protected]85e61d52013-08-01 22:23:42271
[email protected]fb53e652014-04-30 11:27:19272 VLOG(1) << "First update attempt will take place in "
273 << config_->InitialDelay() << " seconds";
[email protected]d0c8b8b42014-05-06 05:11:45274 timer_.Start(FROM_HERE,
275 base::TimeDelta::FromSeconds(config_->InitialDelay()),
276 this,
277 &CrxUpdateService::ProcessPendingItems);
[email protected]e8f96ff2011-08-03 05:07:33278 return kOk;
279}
280
281// Stop the main check + update loop. In flight operations will be
282// completed.
283ComponentUpdateService::Status CrxUpdateService::Stop() {
[email protected]fb53e652014-04-30 11:27:19284 VLOG(1) << "CrxUpdateService stopping";
[email protected]e8f96ff2011-08-03 05:07:33285 running_ = false;
286 timer_.Stop();
287 return kOk;
288}
289
[email protected]61aca4cd2013-10-26 10:50:59290bool CrxUpdateService::HasOnDemandItems() const {
291 class Helper {
292 public:
[email protected]d0c8b8b42014-05-06 05:11:45293 static bool IsOnDemand(CrxUpdateItem* item) { return item->on_demand; }
[email protected]61aca4cd2013-10-26 10:50:59294 };
295 return std::find_if(work_items_.begin(),
296 work_items_.end(),
297 Helper::IsOnDemand) != work_items_.end();
298}
299
[email protected]ccb4feef2013-02-14 06:16:47300// This function sets the timer which will call ProcessPendingItems() or
[email protected]61aca4cd2013-10-26 10:50:59301// ProcessRequestedItem() if there is an on_demand item. There
[email protected]32a6c8382013-08-20 00:29:20302// are three kinds of waits:
303// - a short delay, when there is immediate work to be done.
304// - a medium delay, when there are updates to be applied within the current
305// update cycle, or there are components that are still unchecked.
306// - a long delay when a full check/update cycle has completed for all
307// components.
308void CrxUpdateService::ScheduleNextRun(StepDelayInterval step_delay) {
[email protected]ed6fb982014-07-23 16:56:52309 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]93e8e2c2014-01-04 12:29:23310 DCHECK(!update_checker_);
[email protected]e8f96ff2011-08-03 05:07:33311 CHECK(!timer_.IsRunning());
312 // It could be the case that Stop() had been called while a url request
313 // or unpacking was in flight, if so we arrive here but |running_| is
314 // false. In that case do not loop again.
315 if (!running_)
316 return;
317
[email protected]ccb4feef2013-02-14 06:16:47318 // Keep the delay short if in the middle of an update (step_delay),
319 // or there are new requested_work_items_ that have not been processed yet.
[email protected]32a6c8382013-08-20 00:29:20320 int64 delay_seconds = 0;
[email protected]61aca4cd2013-10-26 10:50:59321 if (!HasOnDemandItems()) {
[email protected]32a6c8382013-08-20 00:29:20322 switch (step_delay) {
323 case kStepDelayShort:
324 delay_seconds = config_->StepDelay();
325 break;
326 case kStepDelayMedium:
327 delay_seconds = config_->StepDelayMedium();
328 break;
329 case kStepDelayLong:
330 delay_seconds = config_->NextCheckDelay();
331 break;
332 }
333 } else {
334 delay_seconds = config_->StepDelay();
335 }
[email protected]cf442612011-08-09 20:20:12336
[email protected]32a6c8382013-08-20 00:29:20337 if (step_delay != kStepDelayShort) {
[email protected]d3268fe2014-04-25 02:14:23338 NotifyObservers(Observer::COMPONENT_UPDATER_SLEEPING, "");
[email protected]85e61d52013-08-01 22:23:42339
[email protected]e8f96ff2011-08-03 05:07:33340 // Zero is only used for unit tests.
[email protected]32a6c8382013-08-20 00:29:20341 if (0 == delay_seconds)
[email protected]e8f96ff2011-08-03 05:07:33342 return;
343 }
344
[email protected]fb53e652014-04-30 11:27:19345 VLOG(1) << "Scheduling next run to occur in " << delay_seconds << " seconds";
[email protected]d0c8b8b42014-05-06 05:11:45346 timer_.Start(FROM_HERE,
347 base::TimeDelta::FromSeconds(delay_seconds),
348 this,
349 &CrxUpdateService::ProcessPendingItems);
[email protected]e8f96ff2011-08-03 05:07:33350}
351
352// Given a extension-like component id, find the associated component.
[email protected]68bf09e2014-06-03 00:10:56353CrxUpdateItem* CrxUpdateService::FindUpdateItemById(
354 const std::string& id) const {
[email protected]ed6fb982014-07-23 16:56:52355 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]e8f96ff2011-08-03 05:07:33356 CrxUpdateItem::FindById finder(id);
[email protected]68bf09e2014-06-03 00:10:56357 UpdateItems::const_iterator it =
[email protected]d0c8b8b42014-05-06 05:11:45358 std::find_if(work_items_.begin(), work_items_.end(), finder);
[email protected]93e8e2c2014-01-04 12:29:23359 return it != work_items_.end() ? *it : NULL;
[email protected]e8f96ff2011-08-03 05:07:33360}
361
[email protected]61aca4cd2013-10-26 10:50:59362// Changes a component's status, clearing on_demand and firing notifications as
363// necessary. By convention, this is the only function that can change a
364// CrxUpdateItem's |status|.
365// TODO(waffles): Do we want to add DCHECKS for valid state transitions here?
366void CrxUpdateService::ChangeItemState(CrxUpdateItem* item,
367 CrxUpdateItem::Status to) {
[email protected]ed6fb982014-07-23 16:56:52368 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]d0c8b8b42014-05-06 05:11:45369 if (to == CrxUpdateItem::kNoUpdate || to == CrxUpdateItem::kUpdated ||
[email protected]61aca4cd2013-10-26 10:50:59370 to == CrxUpdateItem::kUpToDate) {
371 item->on_demand = false;
372 }
373
374 item->status = to;
375
[email protected]d3268fe2014-04-25 02:14:23376 switch (to) {
377 case CrxUpdateItem::kCanUpdate:
378 NotifyObservers(Observer::COMPONENT_UPDATE_FOUND, item->id);
379 break;
380 case CrxUpdateItem::kUpdatingDiff:
381 case CrxUpdateItem::kUpdating:
382 NotifyObservers(Observer::COMPONENT_UPDATE_READY, item->id);
383 break;
384 case CrxUpdateItem::kUpdated:
385 NotifyObservers(Observer::COMPONENT_UPDATED, item->id);
386 break;
387 case CrxUpdateItem::kUpToDate:
388 case CrxUpdateItem::kNoUpdate:
389 NotifyObservers(Observer::COMPONENT_NOT_UPDATED, item->id);
390 break;
391 case CrxUpdateItem::kNew:
392 case CrxUpdateItem::kChecking:
393 case CrxUpdateItem::kDownloading:
394 case CrxUpdateItem::kDownloadingDiff:
395 case CrxUpdateItem::kLastStatus:
396 // No notification for these states.
397 break;
[email protected]61aca4cd2013-10-26 10:50:59398 }
[email protected]00a77fa2013-11-02 04:18:46399
400 // Free possible pending network requests.
[email protected]d0c8b8b42014-05-06 05:11:45401 if ((to == CrxUpdateItem::kUpdated) || (to == CrxUpdateItem::kUpToDate) ||
[email protected]00a77fa2013-11-02 04:18:46402 (to == CrxUpdateItem::kNoUpdate)) {
[email protected]78efe2e92014-08-08 15:53:22403 for (std::vector<base::Closure>::iterator it =
404 item->ready_callbacks.begin();
405 it != item->ready_callbacks.end();
406 ++it) {
407 it->Run();
408 }
409 item->ready_callbacks.clear();
[email protected]00a77fa2013-11-02 04:18:46410 }
[email protected]61aca4cd2013-10-26 10:50:59411}
412
[email protected]e8f96ff2011-08-03 05:07:33413// Changes all the components in |work_items_| that have |from| status to
[email protected]e3e696d32013-06-21 20:41:36414// |to| status and returns how many have been changed.
[email protected]e8f96ff2011-08-03 05:07:33415size_t CrxUpdateService::ChangeItemStatus(CrxUpdateItem::Status from,
416 CrxUpdateItem::Status to) {
[email protected]ed6fb982014-07-23 16:56:52417 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]e8f96ff2011-08-03 05:07:33418 size_t count = 0;
419 for (UpdateItems::iterator it = work_items_.begin();
[email protected]d0c8b8b42014-05-06 05:11:45420 it != work_items_.end();
421 ++it) {
[email protected]e8f96ff2011-08-03 05:07:33422 CrxUpdateItem* item = *it;
[email protected]93e8e2c2014-01-04 12:29:23423 if (item->status == from) {
424 ChangeItemState(item, to);
425 ++count;
426 }
[email protected]e8f96ff2011-08-03 05:07:33427 }
428 return count;
429}
430
431// Adds a component to be checked for upgrades. If the component exists it
432// it will be replaced and the return code is kReplaced.
[email protected]e8f96ff2011-08-03 05:07:33433ComponentUpdateService::Status CrxUpdateService::RegisterComponent(
434 const CrxComponent& component) {
[email protected]ed6fb982014-07-23 16:56:52435 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]d0c8b8b42014-05-06 05:11:45436 if (component.pk_hash.empty() || !component.version.IsValid() ||
[email protected]e8f96ff2011-08-03 05:07:33437 !component.installer)
438 return kError;
439
[email protected]055981f2014-01-17 20:22:32440 std::string id(GetCrxComponentID(component));
[email protected]93e8e2c2014-01-04 12:29:23441 CrxUpdateItem* uit = FindUpdateItemById(id);
[email protected]e8f96ff2011-08-03 05:07:33442 if (uit) {
443 uit->component = component;
444 return kReplaced;
445 }
446
447 uit = new CrxUpdateItem;
448 uit->id.swap(id);
449 uit->component = component;
[email protected]e3e696d32013-06-21 20:41:36450
[email protected]e8f96ff2011-08-03 05:07:33451 work_items_.push_back(uit);
[email protected]10bc0222014-06-11 13:44:38452
[email protected]e8f96ff2011-08-03 05:07:33453 // If this is the first component registered we call Start to
[email protected]10bc0222014-06-11 13:44:38454 // schedule the first timer. Otherwise, reset the timer to trigger another
455 // pass over the work items, if the component updater is sleeping, fact
456 // indicated by a running timer. If the timer is not running, it means that
457 // the service is busy updating something, and in that case, this component
458 // will be picked up at the next pass.
459 if (running_) {
460 if (work_items_.size() == 1) {
461 Start();
462 } else if (timer_.IsRunning()) {
463 timer_.Start(FROM_HERE,
464 base::TimeDelta::FromSeconds(config_->InitialDelay()),
465 this,
466 &CrxUpdateService::ProcessPendingItems);
467 }
468 }
[email protected]e8f96ff2011-08-03 05:07:33469
470 return kOk;
471}
472
[email protected]68bf09e2014-06-03 00:10:56473std::vector<std::string> CrxUpdateService::GetComponentIDs() const {
[email protected]ed6fb982014-07-23 16:56:52474 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]68bf09e2014-06-03 00:10:56475 std::vector<std::string> component_ids;
[email protected]2e919ddd2013-08-21 05:05:17476 for (UpdateItems::const_iterator it = work_items_.begin();
[email protected]d0c8b8b42014-05-06 05:11:45477 it != work_items_.end();
478 ++it) {
[email protected]2e919ddd2013-08-21 05:05:17479 const CrxUpdateItem* item = *it;
[email protected]68bf09e2014-06-03 00:10:56480 component_ids.push_back(item->id);
[email protected]2e919ddd2013-08-21 05:05:17481 }
[email protected]68bf09e2014-06-03 00:10:56482 return component_ids;
483}
484
[email protected]78efe2e92014-08-08 15:53:22485OnDemandUpdater& CrxUpdateService::GetOnDemandUpdater() {
486 return *this;
487}
488
489void CrxUpdateService::MaybeThrottle(const std::string& crx_id,
490 const base::Closure& callback) {
491 DCHECK(thread_checker_.CalledOnValidThread());
492 // Check if we can on-demand update, else unblock the request anyway.
493 CrxUpdateItem* item = FindUpdateItemById(crx_id);
494 Status status = OnDemandUpdateWithCooldown(item);
495 if (status == kOk || status == kInProgress) {
496 item->ready_callbacks.push_back(callback);
497 return;
498 }
499 callback.Run();
500}
501
502scoped_refptr<base::SequencedTaskRunner>
503CrxUpdateService::GetSequencedTaskRunner() {
504 return config_->GetSequencedTaskRunner();
505}
506
[email protected]f392e372014-06-12 07:25:57507bool CrxUpdateService::GetComponentDetails(const std::string& component_id,
508 CrxUpdateItem* item) const {
[email protected]ed6fb982014-07-23 16:56:52509 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]f392e372014-06-12 07:25:57510 const CrxUpdateItem* crx_update_item(FindUpdateItemById(component_id));
511 if (crx_update_item)
512 *item = *crx_update_item;
513 return crx_update_item != NULL;
[email protected]2e919ddd2013-08-21 05:05:17514}
515
[email protected]78efe2e92014-08-08 15:53:22516// Start the process of checking for an update, for a particular component
517// that was previously registered.
518// |component_id| is a value returned from GetCrxComponentID().
519ComponentUpdateService::Status CrxUpdateService::OnDemandUpdate(
520 const std::string& component_id) {
521 return OnDemandUpdateInternal(FindUpdateItemById(component_id));
[email protected]ed6fb982014-07-23 16:56:52522}
523
[email protected]93e8e2c2014-01-04 12:29:23524// This is the main loop of the component updater. It updates one component
525// at a time if updates are available. Otherwise, it does an update check or
526// takes a long sleep until the loop runs again.
[email protected]e8f96ff2011-08-03 05:07:33527void CrxUpdateService::ProcessPendingItems() {
[email protected]ed6fb982014-07-23 16:56:52528 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]93e8e2c2014-01-04 12:29:23529
[email protected]61aca4cd2013-10-26 10:50:59530 CrxUpdateItem* ready_upgrade = FindReadyComponent();
531 if (ready_upgrade) {
532 UpdateComponent(ready_upgrade);
[email protected]e8f96ff2011-08-03 05:07:33533 return;
534 }
[email protected]93e8e2c2014-01-04 12:29:23535
536 if (!CheckForUpdates())
537 ScheduleNextRun(kStepDelayLong);
[email protected]61aca4cd2013-10-26 10:50:59538}
539
[email protected]93e8e2c2014-01-04 12:29:23540CrxUpdateItem* CrxUpdateService::FindReadyComponent() const {
[email protected]c4b4cfa2014-03-10 18:55:00541 class Helper {
542 public:
543 static bool IsReadyOnDemand(CrxUpdateItem* item) {
544 return item->on_demand && IsReady(item);
[email protected]61aca4cd2013-10-26 10:50:59545 }
[email protected]c4b4cfa2014-03-10 18:55:00546 static bool IsReady(CrxUpdateItem* item) {
547 return item->status == CrxUpdateItem::kCanUpdate;
548 }
549 };
[email protected]61aca4cd2013-10-26 10:50:59550
[email protected]c4b4cfa2014-03-10 18:55:00551 std::vector<CrxUpdateItem*>::const_iterator it = std::find_if(
552 work_items_.begin(), work_items_.end(), Helper::IsReadyOnDemand);
553 if (it != work_items_.end())
554 return *it;
555 it = std::find_if(work_items_.begin(), work_items_.end(), Helper::IsReady);
556 if (it != work_items_.end())
557 return *it;
[email protected]61aca4cd2013-10-26 10:50:59558 return NULL;
559}
560
[email protected]93e8e2c2014-01-04 12:29:23561// Prepares the components for an update check and initiates the request.
[email protected]21a9c9a2014-02-19 19:37:11562// On demand components are always included in the update check request.
563// Otherwise, only include components that have not been checked recently.
[email protected]93e8e2c2014-01-04 12:29:23564bool CrxUpdateService::CheckForUpdates() {
[email protected]21a9c9a2014-02-19 19:37:11565 const base::TimeDelta minimum_recheck_wait_time =
566 base::TimeDelta::FromSeconds(config_->MinimumReCheckWait());
567 const base::Time now(base::Time::Now());
568
[email protected]93e8e2c2014-01-04 12:29:23569 std::vector<CrxUpdateItem*> items_to_check;
570 for (size_t i = 0; i != work_items_.size(); ++i) {
571 CrxUpdateItem* item = work_items_[i];
572 DCHECK(item->status == CrxUpdateItem::kNew ||
573 item->status == CrxUpdateItem::kNoUpdate ||
574 item->status == CrxUpdateItem::kUpToDate ||
575 item->status == CrxUpdateItem::kUpdated);
576
[email protected]21a9c9a2014-02-19 19:37:11577 const base::TimeDelta time_since_last_checked(now - item->last_check);
578
579 if (!item->on_demand &&
580 time_since_last_checked < minimum_recheck_wait_time) {
[email protected]fb53e652014-04-30 11:27:19581 VLOG(1) << "Skipping check for component update: id=" << item->id
582 << ", time_since_last_checked="
583 << time_since_last_checked.InSeconds()
584 << " seconds: too soon to check for an update";
[email protected]21a9c9a2014-02-19 19:37:11585 continue;
586 }
587
[email protected]fb53e652014-04-30 11:27:19588 VLOG(1) << "Scheduling update check for component id=" << item->id
589 << ", time_since_last_checked="
[email protected]d0c8b8b42014-05-06 05:11:45590 << time_since_last_checked.InSeconds() << " seconds";
[email protected]fb53e652014-04-30 11:27:19591
[email protected]21a9c9a2014-02-19 19:37:11592 item->last_check = now;
[email protected]93e8e2c2014-01-04 12:29:23593 item->crx_urls.clear();
594 item->crx_diffurls.clear();
595 item->previous_version = item->component.version;
596 item->next_version = Version();
597 item->previous_fp = item->component.fingerprint;
598 item->next_fp.clear();
599 item->diff_update_failed = false;
600 item->error_category = 0;
601 item->error_code = 0;
602 item->extra_code1 = 0;
603 item->diff_error_category = 0;
604 item->diff_error_code = 0;
605 item->diff_extra_code1 = 0;
606 item->download_metrics.clear();
607
608 items_to_check.push_back(item);
[email protected]3a10c572014-07-04 06:57:47609
610 ChangeItemState(item, CrxUpdateItem::kChecking);
[email protected]93e8e2c2014-01-04 12:29:23611 }
612
613 if (items_to_check.empty())
614 return false;
615
[email protected]d0c8b8b42014-05-06 05:11:45616 update_checker_ =
[email protected]09974032014-06-27 07:42:08617 UpdateChecker::Create(*config_,
[email protected]d0c8b8b42014-05-06 05:11:45618 base::Bind(&CrxUpdateService::UpdateCheckComplete,
619 base::Unretained(this))).Pass();
[email protected]93e8e2c2014-01-04 12:29:23620 return update_checker_->CheckForUpdates(items_to_check,
621 config_->ExtraRequestParams());
622}
623
[email protected]61aca4cd2013-10-26 10:50:59624void CrxUpdateService::UpdateComponent(CrxUpdateItem* workitem) {
[email protected]afa378f22013-12-02 03:37:54625 scoped_ptr<CRXContext> crx_context(new CRXContext);
626 crx_context->pk_hash = workitem->component.pk_hash;
627 crx_context->id = workitem->id;
628 crx_context->installer = workitem->component.installer;
629 crx_context->fingerprint = workitem->next_fp;
[email protected]da37c1d2013-12-19 01:04:38630 const std::vector<GURL>* urls = NULL;
[email protected]cfd13e52014-02-05 09:35:07631 bool allow_background_download = false;
[email protected]61aca4cd2013-10-26 10:50:59632 if (CanTryDiffUpdate(workitem, *config_)) {
[email protected]da37c1d2013-12-19 01:04:38633 urls = &workitem->crx_diffurls;
[email protected]61aca4cd2013-10-26 10:50:59634 ChangeItemState(workitem, CrxUpdateItem::kDownloadingDiff);
635 } else {
[email protected]cfd13e52014-02-05 09:35:07636 // Background downloads are enabled only for selected components and
637 // only for full downloads (see issue 340448).
638 allow_background_download = workitem->component.allow_background_download;
[email protected]da37c1d2013-12-19 01:04:38639 urls = &workitem->crx_urls;
[email protected]61aca4cd2013-10-26 10:50:59640 ChangeItemState(workitem, CrxUpdateItem::kDownloading);
641 }
[email protected]3cb2a4f2013-12-07 21:54:34642
643 // On demand component updates are always downloaded in foreground.
[email protected]d0c8b8b42014-05-06 05:11:45644 const bool is_background_download = !workitem->on_demand &&
645 allow_background_download &&
646 config_->UseBackgroundDownloader();
[email protected]3cb2a4f2013-12-07 21:54:34647
[email protected]ed6fb982014-07-23 16:56:52648 crx_downloader_.reset(
649 CrxDownloader::Create(is_background_download,
650 config_->RequestContext(),
651 blocking_task_runner_,
652 config_->GetSingleThreadTaskRunner()));
[email protected]8a5ebd432014-05-02 00:21:22653 crx_downloader_->set_progress_callback(
654 base::Bind(&CrxUpdateService::DownloadProgress,
655 base::Unretained(this),
656 crx_context->id));
[email protected]1b6587dc52014-04-26 00:38:55657 crx_downloader_->StartDownload(*urls,
658 base::Bind(&CrxUpdateService::DownloadComplete,
659 base::Unretained(this),
660 base::Passed(&crx_context)));
[email protected]61aca4cd2013-10-26 10:50:59661}
662
[email protected]93e8e2c2014-01-04 12:29:23663void CrxUpdateService::UpdateCheckComplete(
664 int error,
665 const std::string& error_message,
[email protected]055981f2014-01-17 20:22:32666 const UpdateResponse::Results& results) {
[email protected]ed6fb982014-07-23 16:56:52667 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]93e8e2c2014-01-04 12:29:23668 update_checker_.reset();
669 if (!error)
670 OnUpdateCheckSucceeded(results);
[email protected]652f4272013-11-13 09:23:41671 else
[email protected]93e8e2c2014-01-04 12:29:23672 OnUpdateCheckFailed(error, error_message);
[email protected]e8f96ff2011-08-03 05:07:33673}
674
[email protected]93e8e2c2014-01-04 12:29:23675// Handles a valid Omaha update check response by matching the results with
676// the registered components which were checked for updates.
677// If updates are found, prepare the components for the actual version upgrade.
678// One of these components will be drafted for the upgrade next time
679// ProcessPendingItems is called.
680void CrxUpdateService::OnUpdateCheckSucceeded(
[email protected]055981f2014-01-17 20:22:32681 const UpdateResponse::Results& results) {
[email protected]2cddef42013-11-22 08:23:22682 size_t num_updates_pending = 0;
[email protected]ed6fb982014-07-23 16:56:52683 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]fb53e652014-04-30 11:27:19684 VLOG(1) << "Update check succeeded.";
[email protected]055981f2014-01-17 20:22:32685 std::vector<UpdateResponse::Result>::const_iterator it;
[email protected]e8f96ff2011-08-03 05:07:33686 for (it = results.list.begin(); it != results.list.end(); ++it) {
687 CrxUpdateItem* crx = FindUpdateItemById(it->extension_id);
688 if (!crx)
689 continue;
690
[email protected]2cddef42013-11-22 08:23:22691 if (crx->status != CrxUpdateItem::kChecking) {
692 NOTREACHED();
[email protected]e8f96ff2011-08-03 05:07:33693 continue; // Not updating this component now.
[email protected]2cddef42013-11-22 08:23:22694 }
[email protected]e8f96ff2011-08-03 05:07:33695
[email protected]2cddef42013-11-22 08:23:22696 if (it->manifest.version.empty()) {
[email protected]cf442612011-08-09 20:20:12697 // No version means no update available.
[email protected]61aca4cd2013-10-26 10:50:59698 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]fb53e652014-04-30 11:27:19699 VLOG(1) << "No update available for component: " << crx->id;
[email protected]cf442612011-08-09 20:20:12700 continue;
[email protected]e8f96ff2011-08-03 05:07:33701 }
[email protected]2cddef42013-11-22 08:23:22702
703 if (!IsVersionNewer(crx->component.version, it->manifest.version)) {
704 // The component is up to date.
[email protected]61aca4cd2013-10-26 10:50:59705 ChangeItemState(crx, CrxUpdateItem::kUpToDate);
[email protected]fb53e652014-04-30 11:27:19706 VLOG(1) << "Component already up-to-date: " << crx->id;
[email protected]cf442612011-08-09 20:20:12707 continue;
[email protected]e8f96ff2011-08-03 05:07:33708 }
[email protected]2cddef42013-11-22 08:23:22709
710 if (!it->manifest.browser_min_version.empty()) {
[email protected]6a8ab1d2014-07-10 22:47:39711 if (IsVersionNewer(config_->GetBrowserVersion(),
712 it->manifest.browser_min_version)) {
[email protected]2cddef42013-11-22 08:23:22713 // The component is not compatible with this Chrome version.
[email protected]fb53e652014-04-30 11:27:19714 VLOG(1) << "Ignoring incompatible component: " << crx->id;
[email protected]61aca4cd2013-10-26 10:50:59715 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]cf442612011-08-09 20:20:12716 continue;
717 }
[email protected]e8f96ff2011-08-03 05:07:33718 }
[email protected]2cddef42013-11-22 08:23:22719
720 if (it->manifest.packages.size() != 1) {
721 // Assume one and only one package per component.
[email protected]fb53e652014-04-30 11:27:19722 VLOG(1) << "Ignoring multiple packages for component: " << crx->id;
[email protected]2cddef42013-11-22 08:23:22723 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
724 continue;
725 }
726
727 // Parse the members of the result and queue an upgrade for this component.
[email protected]c5e4a2222014-01-03 16:06:13728 crx->next_version = Version(it->manifest.version);
[email protected]2cddef42013-11-22 08:23:22729
[email protected]fb53e652014-04-30 11:27:19730 VLOG(1) << "Update found for component: " << crx->id;
731
[email protected]055981f2014-01-17 20:22:32732 typedef UpdateResponse::Result::Manifest::Package Package;
[email protected]2cddef42013-11-22 08:23:22733 const Package& package(it->manifest.packages[0]);
734 crx->next_fp = package.fingerprint;
735
[email protected]da37c1d2013-12-19 01:04:38736 // Resolve the urls by combining the base urls with the package names.
737 for (size_t i = 0; i != it->crx_urls.size(); ++i) {
738 const GURL url(it->crx_urls[i].Resolve(package.name));
739 if (url.is_valid())
[email protected]d0c8b8b42014-05-06 05:11:45740 crx->crx_urls.push_back(url);
[email protected]da37c1d2013-12-19 01:04:38741 }
742 for (size_t i = 0; i != it->crx_diffurls.size(); ++i) {
743 const GURL url(it->crx_diffurls[i].Resolve(package.namediff));
744 if (url.is_valid())
[email protected]d0c8b8b42014-05-06 05:11:45745 crx->crx_diffurls.push_back(url);
[email protected]da37c1d2013-12-19 01:04:38746 }
[email protected]2cddef42013-11-22 08:23:22747
[email protected]61aca4cd2013-10-26 10:50:59748 ChangeItemState(crx, CrxUpdateItem::kCanUpdate);
[email protected]2cddef42013-11-22 08:23:22749 ++num_updates_pending;
[email protected]e8f96ff2011-08-03 05:07:33750 }
[email protected]cf442612011-08-09 20:20:12751
[email protected]2cddef42013-11-22 08:23:22752 // All components that are not included in the update response are
753 // considered up to date.
[email protected]cf442612011-08-09 20:20:12754 ChangeItemStatus(CrxUpdateItem::kChecking, CrxUpdateItem::kUpToDate);
755
[email protected]32a6c8382013-08-20 00:29:20756 // If there are updates pending we do a short wait, otherwise we take
757 // a longer delay until we check the components again.
[email protected]21a9c9a2014-02-19 19:37:11758 ScheduleNextRun(num_updates_pending > 0 ? kStepDelayShort : kStepDelayLong);
[email protected]e8f96ff2011-08-03 05:07:33759}
760
[email protected]93e8e2c2014-01-04 12:29:23761void CrxUpdateService::OnUpdateCheckFailed(int error,
762 const std::string& error_message) {
[email protected]ed6fb982014-07-23 16:56:52763 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]93e8e2c2014-01-04 12:29:23764 DCHECK(error);
[email protected]d0c8b8b42014-05-06 05:11:45765 size_t count =
766 ChangeItemStatus(CrxUpdateItem::kChecking, CrxUpdateItem::kNoUpdate);
[email protected]e8f96ff2011-08-03 05:07:33767 DCHECK_GT(count, 0ul);
[email protected]fb53e652014-04-30 11:27:19768 VLOG(1) << "Update check failed.";
[email protected]32a6c8382013-08-20 00:29:20769 ScheduleNextRun(kStepDelayLong);
[email protected]e8f96ff2011-08-03 05:07:33770}
771
[email protected]8a5ebd432014-05-02 00:21:22772// Called when progress is being made downloading a CRX. The progress may
773// not monotonically increase due to how the CRX downloader switches between
774// different downloaders and fallback urls.
775void CrxUpdateService::DownloadProgress(
776 const std::string& component_id,
777 const CrxDownloader::Result& download_result) {
[email protected]ed6fb982014-07-23 16:56:52778 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]8a5ebd432014-05-02 00:21:22779 NotifyObservers(Observer::COMPONENT_UPDATE_DOWNLOADING, component_id);
780}
781
[email protected]e8f96ff2011-08-03 05:07:33782// Called when the CRX package has been downloaded to a temporary location.
783// Here we fire the notifications and schedule the component-specific installer
784// to be called in the file thread.
[email protected]3cb2a4f2013-12-07 21:54:34785void CrxUpdateService::DownloadComplete(
786 scoped_ptr<CRXContext> crx_context,
[email protected]3a0092d2013-12-18 03:04:35787 const CrxDownloader::Result& download_result) {
[email protected]ed6fb982014-07-23 16:56:52788 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]64f39fa12013-09-03 21:49:37789
[email protected]64f39fa12013-09-03 21:49:37790 CrxUpdateItem* crx = FindUpdateItemById(crx_context->id);
[email protected]e3e696d32013-06-21 20:41:36791 DCHECK(crx->status == CrxUpdateItem::kDownloadingDiff ||
792 crx->status == CrxUpdateItem::kDownloading);
793
[email protected]3a0092d2013-12-18 03:04:35794 AppendDownloadMetrics(crx_downloader_->download_metrics(),
795 &crx->download_metrics);
796
[email protected]8a5ebd432014-05-02 00:21:22797 crx_downloader_.reset();
798
[email protected]3cb2a4f2013-12-07 21:54:34799 if (download_result.error) {
[email protected]e3e696d32013-06-21 20:41:36800 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
[email protected]7b0529242013-07-20 05:45:46801 crx->diff_error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34802 crx->diff_error_code = download_result.error;
[email protected]e630ba62013-06-22 15:22:34803 crx->diff_update_failed = true;
[email protected]e3e696d32013-06-21 20:41:36804 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
805 CrxUpdateItem::kCanUpdate);
806 DCHECK_EQ(count, 1ul);
[email protected]7b0529242013-07-20 05:45:46807
[email protected]32a6c8382013-08-20 00:29:20808 ScheduleNextRun(kStepDelayShort);
[email protected]e3e696d32013-06-21 20:41:36809 return;
810 }
[email protected]7b0529242013-07-20 05:45:46811 crx->error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34812 crx->error_code = download_result.error;
[email protected]d0c8b8b42014-05-06 05:11:45813 size_t count =
814 ChangeItemStatus(CrxUpdateItem::kDownloading, CrxUpdateItem::kNoUpdate);
[email protected]e8f96ff2011-08-03 05:07:33815 DCHECK_EQ(count, 1ul);
[email protected]e3e696d32013-06-21 20:41:36816
[email protected]7b0529242013-07-20 05:45:46817 // At this point, since both the differential and the full downloads failed,
818 // the update for this component has finished with an error.
819 ping_manager_->OnUpdateComplete(crx);
820
[email protected]32a6c8382013-08-20 00:29:20821 // Move on to the next update, if there is one available.
822 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33823 } else {
[email protected]e3e696d32013-06-21 20:41:36824 size_t count = 0;
825 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
826 count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
827 CrxUpdateItem::kUpdatingDiff);
828 } else {
829 count = ChangeItemStatus(CrxUpdateItem::kDownloading,
830 CrxUpdateItem::kUpdating);
831 }
[email protected]e8f96ff2011-08-03 05:07:33832 DCHECK_EQ(count, 1ul);
[email protected]cf442612011-08-09 20:20:12833
[email protected]44da56e2011-11-21 19:59:14834 // Why unretained? See comment at top of file.
[email protected]8f5f2ea2013-10-31 09:39:10835 blocking_task_runner_->PostDelayedTask(
[email protected]73251e72012-03-04 02:10:33836 FROM_HERE,
[email protected]44da56e2011-11-21 19:59:14837 base::Bind(&CrxUpdateService::Install,
838 base::Unretained(this),
[email protected]afa378f22013-12-02 03:37:54839 base::Passed(&crx_context),
[email protected]3cb2a4f2013-12-07 21:54:34840 download_result.response),
[email protected]73251e72012-03-04 02:10:33841 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]e8f96ff2011-08-03 05:07:33842 }
[email protected]e8f96ff2011-08-03 05:07:33843}
844
845// Install consists of digital signature verification, unpacking and then
846// calling the component specific installer. All that is handled by the
[email protected]f5d27e32014-01-31 06:48:53847// |unpacker_|. If there is an error this function is in charge of deleting
[email protected]e8f96ff2011-08-03 05:07:33848// the files created.
[email protected]afa378f22013-12-02 03:37:54849void CrxUpdateService::Install(scoped_ptr<CRXContext> context,
[email protected]650b2d52013-02-10 03:41:45850 const base::FilePath& crx_path) {
[email protected]8f5f2ea2013-10-31 09:39:10851 // This function owns the file at |crx_path| and the |context| object.
[email protected]94a481b2014-03-28 19:41:55852 unpacker_ = new ComponentUnpacker(context->pk_hash,
853 crx_path,
854 context->fingerprint,
855 context->installer,
[email protected]e260af72014-08-05 07:52:39856 config_->CreateOutOfProcessPatcher(),
[email protected]94a481b2014-03-28 19:41:55857 blocking_task_runner_);
[email protected]f5d27e32014-01-31 06:48:53858 unpacker_->Unpack(base::Bind(&CrxUpdateService::EndUnpacking,
859 base::Unretained(this),
860 context->id,
861 crx_path));
862}
863
864void CrxUpdateService::EndUnpacking(const std::string& component_id,
865 const base::FilePath& crx_path,
866 ComponentUnpacker::Error error,
867 int extended_error) {
[email protected]055981f2014-01-17 20:22:32868 if (!DeleteFileAndEmptyParentDirectory(crx_path))
[email protected]e8f96ff2011-08-03 05:07:33869 NOTREACHED() << crx_path.value();
[email protected]e93632052014-07-28 23:51:09870 main_task_runner_->PostDelayedTask(
[email protected]73251e72012-03-04 02:10:33871 FROM_HERE,
[email protected]d0c8b8b42014-05-06 05:11:45872 base::Bind(&CrxUpdateService::DoneInstalling,
873 base::Unretained(this),
874 component_id,
875 error,
876 extended_error),
[email protected]73251e72012-03-04 02:10:33877 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]94a481b2014-03-28 19:41:55878 // Reset the unpacker last, otherwise we free our own arguments.
879 unpacker_ = NULL;
[email protected]e8f96ff2011-08-03 05:07:33880}
881
882// Installation has been completed. Adjust the component status and
[email protected]7b0529242013-07-20 05:45:46883// schedule the next check. Schedule a short delay before trying the full
884// update when the differential update failed.
[email protected]07f93af12011-08-17 20:57:22885void CrxUpdateService::DoneInstalling(const std::string& component_id,
[email protected]e3e696d32013-06-21 20:41:36886 ComponentUnpacker::Error error,
887 int extra_code) {
[email protected]ed6fb982014-07-23 16:56:52888 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]07f93af12011-08-17 20:57:22889
[email protected]7b0529242013-07-20 05:45:46890 ErrorCategory error_category = kErrorNone;
891 switch (error) {
892 case ComponentUnpacker::kNone:
893 break;
894 case ComponentUnpacker::kInstallerError:
895 error_category = kInstallError;
896 break;
897 default:
898 error_category = kUnpackError;
899 break;
900 }
901
902 const bool is_success = error == ComponentUnpacker::kNone;
903
[email protected]07f93af12011-08-17 20:57:22904 CrxUpdateItem* item = FindUpdateItemById(component_id);
[email protected]7b0529242013-07-20 05:45:46905 if (item->status == CrxUpdateItem::kUpdatingDiff && !is_success) {
906 item->diff_error_category = error_category;
907 item->diff_error_code = error;
908 item->diff_extra_code1 = extra_code;
[email protected]1ea21ad2013-09-02 04:20:39909 item->diff_update_failed = true;
910 size_t count = ChangeItemStatus(CrxUpdateItem::kUpdatingDiff,
911 CrxUpdateItem::kCanUpdate);
912 DCHECK_EQ(count, 1ul);
913 ScheduleNextRun(kStepDelayShort);
914 return;
[email protected]d0c8b8b42014-05-06 05:11:45915 }
[email protected]e3e696d32013-06-21 20:41:36916
[email protected]7b0529242013-07-20 05:45:46917 if (is_success) {
[email protected]07f93af12011-08-17 20:57:22918 item->component.version = item->next_version;
[email protected]e3e696d32013-06-21 20:41:36919 item->component.fingerprint = item->next_fp;
[email protected]3a10c572014-07-04 06:57:47920 ChangeItemState(item, CrxUpdateItem::kUpdated);
[email protected]7b0529242013-07-20 05:45:46921 } else {
[email protected]7b0529242013-07-20 05:45:46922 item->error_category = error_category;
923 item->error_code = error;
924 item->extra_code1 = extra_code;
[email protected]3a10c572014-07-04 06:57:47925 ChangeItemState(item, CrxUpdateItem::kNoUpdate);
[email protected]e3e696d32013-06-21 20:41:36926 }
[email protected]07f93af12011-08-17 20:57:22927
[email protected]7b0529242013-07-20 05:45:46928 ping_manager_->OnUpdateComplete(item);
[email protected]360b8bb2011-09-01 21:48:06929
[email protected]32a6c8382013-08-20 00:29:20930 // Move on to the next update, if there is one available.
931 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33932}
933
[email protected]d3268fe2014-04-25 02:14:23934void CrxUpdateService::NotifyObservers(Observer::Events event,
935 const std::string& id) {
[email protected]ed6fb982014-07-23 16:56:52936 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]d3268fe2014-04-25 02:14:23937 FOR_EACH_OBSERVER(Observer, observer_list_, OnEvent(event, id));
[email protected]85e61d52013-08-01 22:23:42938}
939
[email protected]5b53f932014-06-06 10:26:54940ComponentUpdateService::Status CrxUpdateService::OnDemandUpdateWithCooldown(
[email protected]504830a2014-05-20 21:53:54941 CrxUpdateItem* uit) {
942 if (!uit)
943 return kError;
944
945 // Check if the request is too soon.
946 base::TimeDelta delta = base::Time::Now() - uit->last_check;
947 if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay()))
948 return kError;
949
[email protected]5b53f932014-06-06 10:26:54950 return OnDemandUpdateInternal(uit);
951}
952
953ComponentUpdateService::Status CrxUpdateService::OnDemandUpdateInternal(
954 CrxUpdateItem* uit) {
955 if (!uit)
956 return kError;
957
[email protected]68bf09e2014-06-03 00:10:56958 uit->on_demand = true;
[email protected]504830a2014-05-20 21:53:54959
[email protected]3a10c572014-07-04 06:57:47960 // If there is an update available for this item, then continue processing
961 // the update. This is an artifact of how update checks are done: in addition
962 // to the on-demand item, the update check may include other items as well.
963 if (uit->status != CrxUpdateItem::kCanUpdate) {
964 Status service_status = GetServiceStatus(uit->status);
965 // If the item is already in the process of being updated, there is
966 // no point in this call, so return kInProgress.
967 if (service_status == kInProgress)
968 return service_status;
969
970 // Otherwise the item was already checked a while back (or it is new),
971 // set its status to kNew to give it a slightly higher priority.
972 ChangeItemState(uit, CrxUpdateItem::kNew);
973 }
974
[email protected]504830a2014-05-20 21:53:54975 // In case the current delay is long, set the timer to a shorter value
976 // to get the ball rolling.
977 if (timer_.IsRunning()) {
978 timer_.Stop();
979 timer_.Start(FROM_HERE,
980 base::TimeDelta::FromSeconds(config_->StepDelay()),
981 this,
982 &CrxUpdateService::ProcessPendingItems);
983 }
984
985 return kOk;
986}
987
[email protected]68bf09e2014-06-03 00:10:56988ComponentUpdateService::Status CrxUpdateService::GetServiceStatus(
989 CrxUpdateItem::Status status) {
990 switch (status) {
991 case CrxUpdateItem::kChecking:
992 case CrxUpdateItem::kCanUpdate:
993 case CrxUpdateItem::kDownloadingDiff:
994 case CrxUpdateItem::kDownloading:
995 case CrxUpdateItem::kUpdatingDiff:
996 case CrxUpdateItem::kUpdating:
997 return kInProgress;
998 case CrxUpdateItem::kNew:
999 case CrxUpdateItem::kUpdated:
1000 case CrxUpdateItem::kUpToDate:
1001 case CrxUpdateItem::kNoUpdate:
1002 return kOk;
1003 case CrxUpdateItem::kLastStatus:
1004 NOTREACHED() << status;
1005 }
1006 return kError;
1007}
1008
[email protected]00a77fa2013-11-02 04:18:461009///////////////////////////////////////////////////////////////////////////////
1010
[email protected]e8f96ff2011-08-03 05:07:331011// The component update factory. Using the component updater as a singleton
1012// is the job of the browser process.
[email protected]655043812014-06-24 01:50:361013ComponentUpdateService* ComponentUpdateServiceFactory(Configurator* config) {
[email protected]e8f96ff2011-08-03 05:07:331014 DCHECK(config);
1015 return new CrxUpdateService(config);
1016}
[email protected]2cddef42013-11-22 08:23:221017
[email protected]055981f2014-01-17 20:22:321018} // namespace component_updater