blob: 92f138146a36ea879970299155a644d411092c3b [file] [log] [blame]
[email protected]de0fdca22014-08-19 05:26:091// Copyright 2014 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
[email protected]de0fdca22014-08-19 05:26:095#include "components/component_updater/component_updater_service.h"
[email protected]e8f96ff2011-08-03 05:07:336
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"
sorin395c2ac2014-09-16 21:31:0713#include "base/bind_helpers.h"
[email protected]78efe2e92014-08-08 15:53:2214#include "base/callback.h"
[email protected]e3e696d32013-06-21 20:41:3615#include "base/compiler_specific.h"
[email protected]57999812013-02-24 05:40:5216#include "base/files/file_path.h"
thestig819adcc82014-09-10 22:24:5317#include "base/files/file_util.h"
[email protected]e8f96ff2011-08-03 05:07:3318#include "base/logging.h"
sorin5cb1f5492014-09-23 04:07:4419#include "base/macros.h"
[email protected]7226b33c2011-08-18 08:44:2220#include "base/memory/scoped_ptr.h"
[email protected]e93632052014-07-28 23:51:0921#include "base/message_loop/message_loop_proxy.h"
[email protected]d3268fe2014-04-25 02:14:2322#include "base/observer_list.h"
[email protected]f5d27e32014-01-31 06:48:5323#include "base/sequenced_task_runner.h"
[email protected]e8f96ff2011-08-03 05:07:3324#include "base/stl_util.h"
[email protected]8f5f2ea2013-10-31 09:39:1025#include "base/threading/sequenced_worker_pool.h"
[email protected]ed6fb982014-07-23 16:56:5226#include "base/threading/thread_checker.h"
[email protected]41a17c52013-06-28 00:27:5327#include "base/timer/timer.h"
[email protected]de0fdca22014-08-19 05:26:0928#include "components/component_updater/component_patcher_operation.h"
29#include "components/component_updater/component_unpacker.h"
30#include "components/component_updater/component_updater_configurator.h"
31#include "components/component_updater/component_updater_ping_manager.h"
32#include "components/component_updater/component_updater_utils.h"
33#include "components/component_updater/crx_downloader.h"
34#include "components/component_updater/crx_update_item.h"
35#include "components/component_updater/update_checker.h"
36#include "components/component_updater/update_response.h"
[email protected]761fa4702013-07-02 15:25:1537#include "url/gurl.h"
[email protected]e8f96ff2011-08-03 05:07:3338
[email protected]055981f2014-01-17 20:22:3239namespace component_updater {
[email protected]3a0092d2013-12-18 03:04:3540
[email protected]44da56e2011-11-21 19:59:1441// The component updater is designed to live until process shutdown, so
42// base::Bind() calls are not refcounted.
43
[email protected]e8f96ff2011-08-03 05:07:3344namespace {
[email protected]e3e696d32013-06-21 20:41:3645
[email protected]2cddef42013-11-22 08:23:2246// Returns true if the |proposed| version is newer than |current| version.
[email protected]c5e4a2222014-01-03 16:06:1347bool IsVersionNewer(const Version& current, const std::string& proposed) {
48 Version proposed_ver(proposed);
[email protected]2cddef42013-11-22 08:23:2249 return proposed_ver.IsValid() && current.CompareTo(proposed_ver) < 0;
[email protected]e8f96ff2011-08-03 05:07:3350}
51
[email protected]e3e696d32013-06-21 20:41:3652// Returns true if a differential update is available, it has not failed yet,
53// and the configuration allows it.
54bool CanTryDiffUpdate(const CrxUpdateItem* update_item,
[email protected]655043812014-06-24 01:50:3655 const Configurator& config) {
[email protected]d0c8b8b42014-05-06 05:11:4556 return HasDiffUpdate(update_item) && !update_item->diff_update_failed &&
[email protected]e3e696d32013-06-21 20:41:3657 config.DeltasEnabled();
58}
59
[email protected]3a0092d2013-12-18 03:04:3560void AppendDownloadMetrics(
61 const std::vector<CrxDownloader::DownloadMetrics>& source,
62 std::vector<CrxDownloader::DownloadMetrics>* destination) {
63 destination->insert(destination->end(), source.begin(), source.end());
64}
65
[email protected]7b0529242013-07-20 05:45:4666} // namespace
67
68CrxUpdateItem::CrxUpdateItem()
69 : status(kNew),
[email protected]61aca4cd2013-10-26 10:50:5970 on_demand(false),
[email protected]7b0529242013-07-20 05:45:4671 diff_update_failed(false),
72 error_category(0),
73 error_code(0),
74 extra_code1(0),
75 diff_error_category(0),
76 diff_error_code(0),
77 diff_extra_code1(0) {
78}
79
80CrxUpdateItem::~CrxUpdateItem() {
81}
[email protected]86550a42013-06-21 15:20:4982
[email protected]dc06f0b2013-01-23 20:03:1683CrxComponent::CrxComponent()
[email protected]d0c8b8b42014-05-06 05:11:4584 : installer(NULL), allow_background_download(true) {
[email protected]dc06f0b2013-01-23 20:03:1685}
86
87CrxComponent::~CrxComponent() {
88}
[email protected]e8f96ff2011-08-03 05:07:3389
90//////////////////////////////////////////////////////////////////////////////
91// The one and only implementation of the ComponentUpdateService interface. In
92// charge of running the show. The main method is ProcessPendingItems() which
[email protected]50b01392012-09-01 03:33:1393// is called periodically to do the upgrades/installs or the update checks.
[email protected]e8f96ff2011-08-03 05:07:3394// An important consideration here is to be as "low impact" as we can to the
95// rest of the browser, so even if we have many components registered and
[email protected]f1050432012-02-15 01:35:4696// eligible for update, we only do one thing at a time with pauses in between
[email protected]e8f96ff2011-08-03 05:07:3397// the tasks. Also when we do network requests there is only one |url_fetcher_|
[email protected]e3e696d32013-06-21 20:41:3698// in flight at a time.
[email protected]e8f96ff2011-08-03 05:07:3399// There are no locks in this code, the main structure |work_items_| is mutated
[email protected]ed6fb982014-07-23 16:56:52100// only from the main thread. The unpack and installation is done in a blocking
[email protected]74be2642014-02-07 09:40:37101// pool thread. The network requests are done in the IO thread or in the file
[email protected]e8f96ff2011-08-03 05:07:33102// thread.
[email protected]504830a2014-05-20 21:53:54103class CrxUpdateService : public ComponentUpdateService, public OnDemandUpdater {
[email protected]e8f96ff2011-08-03 05:07:33104 public:
[email protected]655043812014-06-24 01:50:36105 explicit CrxUpdateService(Configurator* config);
dcheng00ea022b2014-10-21 11:24:56106 ~CrxUpdateService() override;
[email protected]e8f96ff2011-08-03 05:07:33107
108 // Overrides for ComponentUpdateService.
dcheng00ea022b2014-10-21 11:24:56109 void AddObserver(Observer* observer) override;
110 void RemoveObserver(Observer* observer) override;
111 Status Start() override;
112 Status Stop() override;
113 Status RegisterComponent(const CrxComponent& component) override;
114 std::vector<std::string> GetComponentIDs() const override;
115 OnDemandUpdater& GetOnDemandUpdater() override;
116 void MaybeThrottle(const std::string& crx_id,
117 const base::Closure& callback) override;
118 scoped_refptr<base::SequencedTaskRunner> GetSequencedTaskRunner() override;
[email protected]504830a2014-05-20 21:53:54119
[email protected]28ea9ac2014-05-03 22:07:18120 // Context for a crx download url request.
121 struct CRXContext {
122 ComponentInstaller* installer;
sorin5cb1f5492014-09-23 04:07:44123 std::vector<uint8_t> pk_hash;
[email protected]28ea9ac2014-05-03 22:07:18124 std::string id;
125 std::string fingerprint;
126 CRXContext() : installer(NULL) {}
127 };
[email protected]e8f96ff2011-08-03 05:07:33128
[email protected]e8f96ff2011-08-03 05:07:33129 private:
[email protected]7b0529242013-07-20 05:45:46130 enum ErrorCategory {
131 kErrorNone = 0,
132 kNetworkError,
133 kUnpackError,
134 kInstallError,
135 };
136
[email protected]32a6c8382013-08-20 00:29:20137 enum StepDelayInterval {
138 kStepDelayShort = 0,
139 kStepDelayMedium,
140 kStepDelayLong,
141 };
142
[email protected]78efe2e92014-08-08 15:53:22143 // Overrides for ComponentUpdateService.
dcheng00ea022b2014-10-21 11:24:56144 bool GetComponentDetails(const std::string& component_id,
145 CrxUpdateItem* item) const override;
[email protected]78efe2e92014-08-08 15:53:22146
147 // Overrides for OnDemandUpdater.
dcheng00ea022b2014-10-21 11:24:56148 Status OnDemandUpdate(const std::string& component_id) override;
[email protected]78efe2e92014-08-08 15:53:22149
sorin395c2ac2014-09-16 21:31:07150 void UpdateCheckComplete(const GURL& original_url,
151 int error,
[email protected]055981f2014-01-17 20:22:32152 const std::string& error_message,
153 const UpdateResponse::Results& results);
154 void OnUpdateCheckSucceeded(const UpdateResponse::Results& results);
[email protected]93e8e2c2014-01-04 12:29:23155 void OnUpdateCheckFailed(int error, const std::string& error_message);
[email protected]e8f96ff2011-08-03 05:07:33156
[email protected]8a5ebd432014-05-02 00:21:22157 void DownloadProgress(const std::string& component_id,
158 const CrxDownloader::Result& download_result);
159
160 void DownloadComplete(scoped_ptr<CRXContext> crx_context,
161 const CrxDownloader::Result& download_result);
[email protected]afa378f22013-12-02 03:37:54162
[email protected]00a77fa2013-11-02 04:18:46163 Status OnDemandUpdateInternal(CrxUpdateItem* item);
[email protected]5b53f932014-06-06 10:26:54164 Status OnDemandUpdateWithCooldown(CrxUpdateItem* item);
[email protected]00a77fa2013-11-02 04:18:46165
[email protected]e8f96ff2011-08-03 05:07:33166 void ProcessPendingItems();
167
[email protected]93e8e2c2014-01-04 12:29:23168 // Find a component that is ready to update.
169 CrxUpdateItem* FindReadyComponent() const;
170
171 // Prepares the components for an update check and initiates the request.
172 // Returns true if an update check request has been made. Returns false if
173 // no update check was needed or an error occured.
174 bool CheckForUpdates();
[email protected]61aca4cd2013-10-26 10:50:59175
176 void UpdateComponent(CrxUpdateItem* workitem);
177
[email protected]32a6c8382013-08-20 00:29:20178 void ScheduleNextRun(StepDelayInterval step_delay);
[email protected]e8f96ff2011-08-03 05:07:33179
[email protected]6268d3a2013-11-27 01:28:09180 void ParseResponse(const std::string& xml);
[email protected]e8f96ff2011-08-03 05:07:33181
[email protected]afa378f22013-12-02 03:37:54182 void Install(scoped_ptr<CRXContext> context, const base::FilePath& crx_path);
[email protected]e8f96ff2011-08-03 05:07:33183
[email protected]f5d27e32014-01-31 06:48:53184 void EndUnpacking(const std::string& component_id,
185 const base::FilePath& crx_path,
186 ComponentUnpacker::Error error,
187 int extended_error);
188
[email protected]07f93af12011-08-17 20:57:22189 void DoneInstalling(const std::string& component_id,
[email protected]e3e696d32013-06-21 20:41:36190 ComponentUnpacker::Error error,
191 int extended_error);
[email protected]e8f96ff2011-08-03 05:07:33192
[email protected]61aca4cd2013-10-26 10:50:59193 void ChangeItemState(CrxUpdateItem* item, CrxUpdateItem::Status to);
194
[email protected]d0c8b8b42014-05-06 05:11:45195 size_t ChangeItemStatus(CrxUpdateItem::Status from, CrxUpdateItem::Status to);
[email protected]e8f96ff2011-08-03 05:07:33196
[email protected]68bf09e2014-06-03 00:10:56197 CrxUpdateItem* FindUpdateItemById(const std::string& id) const;
[email protected]e8f96ff2011-08-03 05:07:33198
[email protected]d3268fe2014-04-25 02:14:23199 void NotifyObservers(Observer::Events event, const std::string& id);
[email protected]85e61d52013-08-01 22:23:42200
[email protected]61aca4cd2013-10-26 10:50:59201 bool HasOnDemandItems() const;
202
[email protected]68bf09e2014-06-03 00:10:56203 Status GetServiceStatus(const CrxUpdateItem::Status status);
204
[email protected]655043812014-06-24 01:50:36205 scoped_ptr<Configurator> config_;
[email protected]e3e696d32013-06-21 20:41:36206
[email protected]055981f2014-01-17 20:22:32207 scoped_ptr<UpdateChecker> update_checker_;
[email protected]e8f96ff2011-08-03 05:07:33208
[email protected]055981f2014-01-17 20:22:32209 scoped_ptr<PingManager> ping_manager_;
[email protected]7b0529242013-07-20 05:45:46210
[email protected]94a481b2014-03-28 19:41:55211 scoped_refptr<ComponentUnpacker> unpacker_;
[email protected]f5d27e32014-01-31 06:48:53212
[email protected]055981f2014-01-17 20:22:32213 scoped_ptr<CrxDownloader> crx_downloader_;
[email protected]afa378f22013-12-02 03:37:54214
[email protected]86550a42013-06-21 15:20:49215 // A collection of every work item.
[email protected]e3e696d32013-06-21 20:41:36216 typedef std::vector<CrxUpdateItem*> UpdateItems;
[email protected]e8f96ff2011-08-03 05:07:33217 UpdateItems work_items_;
218
219 base::OneShotTimer<CrxUpdateService> timer_;
220
[email protected]ed6fb982014-07-23 16:56:52221 base::ThreadChecker thread_checker_;
222
[email protected]e93632052014-07-28 23:51:09223 // Used to post responses back to the main thread.
224 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
225
[email protected]8f5f2ea2013-10-31 09:39:10226 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
227
[email protected]e8f96ff2011-08-03 05:07:33228 bool running_;
229
[email protected]d3268fe2014-04-25 02:14:23230 ObserverList<Observer> observer_list_;
231
[email protected]e8f96ff2011-08-03 05:07:33232 DISALLOW_COPY_AND_ASSIGN(CrxUpdateService);
233};
234
[email protected]e8f96ff2011-08-03 05:07:33235//////////////////////////////////////////////////////////////////////////////
236
[email protected]655043812014-06-24 01:50:36237CrxUpdateService::CrxUpdateService(Configurator* config)
[email protected]e8f96ff2011-08-03 05:07:33238 : config_(config),
[email protected]09974032014-06-27 07:42:08239 ping_manager_(new PingManager(*config)),
[email protected]e93632052014-07-28 23:51:09240 main_task_runner_(base::MessageLoopProxy::current()),
[email protected]ed6fb982014-07-23 16:56:52241 blocking_task_runner_(config->GetSequencedTaskRunner()),
[email protected]e8f96ff2011-08-03 05:07:33242 running_(false) {
[email protected]1ea21ad2013-09-02 04:20:39243}
[email protected]e8f96ff2011-08-03 05:07:33244
245CrxUpdateService::~CrxUpdateService() {
[email protected]ed6fb982014-07-23 16:56:52246 // Because we are a singleton, at this point only the main thread should be
[email protected]e8f96ff2011-08-03 05:07:33247 // alive, this simplifies the management of the work that could be in
248 // flight in other threads.
249 Stop();
250 STLDeleteElements(&work_items_);
[email protected]1ea21ad2013-09-02 04:20:39251}
[email protected]e8f96ff2011-08-03 05:07:33252
[email protected]d3268fe2014-04-25 02:14:23253void CrxUpdateService::AddObserver(Observer* observer) {
[email protected]ed6fb982014-07-23 16:56:52254 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]d3268fe2014-04-25 02:14:23255 observer_list_.AddObserver(observer);
256}
257
258void CrxUpdateService::RemoveObserver(Observer* observer) {
[email protected]ed6fb982014-07-23 16:56:52259 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]d3268fe2014-04-25 02:14:23260 observer_list_.RemoveObserver(observer);
261}
262
[email protected]e8f96ff2011-08-03 05:07:33263ComponentUpdateService::Status CrxUpdateService::Start() {
264 // Note that RegisterComponent will call Start() when the first
265 // component is registered, so it can be called twice. This way
266 // we avoid scheduling the timer if there is no work to do.
[email protected]fb53e652014-04-30 11:27:19267 VLOG(1) << "CrxUpdateService starting up";
[email protected]e8f96ff2011-08-03 05:07:33268 running_ = true;
269 if (work_items_.empty())
270 return kOk;
271
[email protected]d3268fe2014-04-25 02:14:23272 NotifyObservers(Observer::COMPONENT_UPDATER_STARTED, "");
[email protected]85e61d52013-08-01 22:23:42273
[email protected]fb53e652014-04-30 11:27:19274 VLOG(1) << "First update attempt will take place in "
275 << config_->InitialDelay() << " seconds";
[email protected]d0c8b8b42014-05-06 05:11:45276 timer_.Start(FROM_HERE,
277 base::TimeDelta::FromSeconds(config_->InitialDelay()),
278 this,
279 &CrxUpdateService::ProcessPendingItems);
[email protected]e8f96ff2011-08-03 05:07:33280 return kOk;
281}
282
283// Stop the main check + update loop. In flight operations will be
284// completed.
285ComponentUpdateService::Status CrxUpdateService::Stop() {
[email protected]fb53e652014-04-30 11:27:19286 VLOG(1) << "CrxUpdateService stopping";
[email protected]e8f96ff2011-08-03 05:07:33287 running_ = false;
288 timer_.Stop();
289 return kOk;
290}
291
[email protected]61aca4cd2013-10-26 10:50:59292bool CrxUpdateService::HasOnDemandItems() const {
293 class Helper {
294 public:
[email protected]d0c8b8b42014-05-06 05:11:45295 static bool IsOnDemand(CrxUpdateItem* item) { return item->on_demand; }
[email protected]61aca4cd2013-10-26 10:50:59296 };
297 return std::find_if(work_items_.begin(),
298 work_items_.end(),
299 Helper::IsOnDemand) != work_items_.end();
300}
301
[email protected]ccb4feef2013-02-14 06:16:47302// This function sets the timer which will call ProcessPendingItems() or
[email protected]61aca4cd2013-10-26 10:50:59303// ProcessRequestedItem() if there is an on_demand item. There
[email protected]32a6c8382013-08-20 00:29:20304// are three kinds of waits:
305// - a short delay, when there is immediate work to be done.
306// - a medium delay, when there are updates to be applied within the current
307// update cycle, or there are components that are still unchecked.
308// - a long delay when a full check/update cycle has completed for all
309// components.
310void CrxUpdateService::ScheduleNextRun(StepDelayInterval step_delay) {
[email protected]ed6fb982014-07-23 16:56:52311 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]93e8e2c2014-01-04 12:29:23312 DCHECK(!update_checker_);
[email protected]e8f96ff2011-08-03 05:07:33313 CHECK(!timer_.IsRunning());
314 // It could be the case that Stop() had been called while a url request
315 // or unpacking was in flight, if so we arrive here but |running_| is
316 // false. In that case do not loop again.
317 if (!running_)
318 return;
319
[email protected]ccb4feef2013-02-14 06:16:47320 // Keep the delay short if in the middle of an update (step_delay),
321 // or there are new requested_work_items_ that have not been processed yet.
sorin5cb1f5492014-09-23 04:07:44322 int64_t delay_seconds = 0;
[email protected]61aca4cd2013-10-26 10:50:59323 if (!HasOnDemandItems()) {
[email protected]32a6c8382013-08-20 00:29:20324 switch (step_delay) {
325 case kStepDelayShort:
326 delay_seconds = config_->StepDelay();
327 break;
328 case kStepDelayMedium:
329 delay_seconds = config_->StepDelayMedium();
330 break;
331 case kStepDelayLong:
332 delay_seconds = config_->NextCheckDelay();
333 break;
334 }
335 } else {
336 delay_seconds = config_->StepDelay();
337 }
[email protected]cf442612011-08-09 20:20:12338
[email protected]32a6c8382013-08-20 00:29:20339 if (step_delay != kStepDelayShort) {
[email protected]d3268fe2014-04-25 02:14:23340 NotifyObservers(Observer::COMPONENT_UPDATER_SLEEPING, "");
[email protected]85e61d52013-08-01 22:23:42341
[email protected]e8f96ff2011-08-03 05:07:33342 // Zero is only used for unit tests.
[email protected]32a6c8382013-08-20 00:29:20343 if (0 == delay_seconds)
[email protected]e8f96ff2011-08-03 05:07:33344 return;
345 }
346
[email protected]fb53e652014-04-30 11:27:19347 VLOG(1) << "Scheduling next run to occur in " << delay_seconds << " seconds";
[email protected]d0c8b8b42014-05-06 05:11:45348 timer_.Start(FROM_HERE,
349 base::TimeDelta::FromSeconds(delay_seconds),
350 this,
351 &CrxUpdateService::ProcessPendingItems);
[email protected]e8f96ff2011-08-03 05:07:33352}
353
354// Given a extension-like component id, find the associated component.
[email protected]68bf09e2014-06-03 00:10:56355CrxUpdateItem* CrxUpdateService::FindUpdateItemById(
356 const std::string& id) const {
[email protected]ed6fb982014-07-23 16:56:52357 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]e8f96ff2011-08-03 05:07:33358 CrxUpdateItem::FindById finder(id);
[email protected]68bf09e2014-06-03 00:10:56359 UpdateItems::const_iterator it =
[email protected]d0c8b8b42014-05-06 05:11:45360 std::find_if(work_items_.begin(), work_items_.end(), finder);
[email protected]93e8e2c2014-01-04 12:29:23361 return it != work_items_.end() ? *it : NULL;
[email protected]e8f96ff2011-08-03 05:07:33362}
363
[email protected]61aca4cd2013-10-26 10:50:59364// Changes a component's status, clearing on_demand and firing notifications as
365// necessary. By convention, this is the only function that can change a
366// CrxUpdateItem's |status|.
367// TODO(waffles): Do we want to add DCHECKS for valid state transitions here?
368void CrxUpdateService::ChangeItemState(CrxUpdateItem* item,
369 CrxUpdateItem::Status to) {
[email protected]ed6fb982014-07-23 16:56:52370 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]d0c8b8b42014-05-06 05:11:45371 if (to == CrxUpdateItem::kNoUpdate || to == CrxUpdateItem::kUpdated ||
[email protected]61aca4cd2013-10-26 10:50:59372 to == CrxUpdateItem::kUpToDate) {
373 item->on_demand = false;
374 }
375
376 item->status = to;
377
[email protected]d3268fe2014-04-25 02:14:23378 switch (to) {
379 case CrxUpdateItem::kCanUpdate:
380 NotifyObservers(Observer::COMPONENT_UPDATE_FOUND, item->id);
381 break;
382 case CrxUpdateItem::kUpdatingDiff:
383 case CrxUpdateItem::kUpdating:
384 NotifyObservers(Observer::COMPONENT_UPDATE_READY, item->id);
385 break;
386 case CrxUpdateItem::kUpdated:
387 NotifyObservers(Observer::COMPONENT_UPDATED, item->id);
388 break;
389 case CrxUpdateItem::kUpToDate:
390 case CrxUpdateItem::kNoUpdate:
391 NotifyObservers(Observer::COMPONENT_NOT_UPDATED, item->id);
392 break;
393 case CrxUpdateItem::kNew:
394 case CrxUpdateItem::kChecking:
395 case CrxUpdateItem::kDownloading:
396 case CrxUpdateItem::kDownloadingDiff:
397 case CrxUpdateItem::kLastStatus:
398 // No notification for these states.
399 break;
[email protected]61aca4cd2013-10-26 10:50:59400 }
[email protected]00a77fa2013-11-02 04:18:46401
402 // Free possible pending network requests.
[email protected]d0c8b8b42014-05-06 05:11:45403 if ((to == CrxUpdateItem::kUpdated) || (to == CrxUpdateItem::kUpToDate) ||
[email protected]00a77fa2013-11-02 04:18:46404 (to == CrxUpdateItem::kNoUpdate)) {
[email protected]78efe2e92014-08-08 15:53:22405 for (std::vector<base::Closure>::iterator it =
406 item->ready_callbacks.begin();
407 it != item->ready_callbacks.end();
408 ++it) {
409 it->Run();
410 }
411 item->ready_callbacks.clear();
[email protected]00a77fa2013-11-02 04:18:46412 }
[email protected]61aca4cd2013-10-26 10:50:59413}
414
[email protected]e8f96ff2011-08-03 05:07:33415// Changes all the components in |work_items_| that have |from| status to
[email protected]e3e696d32013-06-21 20:41:36416// |to| status and returns how many have been changed.
[email protected]e8f96ff2011-08-03 05:07:33417size_t CrxUpdateService::ChangeItemStatus(CrxUpdateItem::Status from,
418 CrxUpdateItem::Status to) {
[email protected]ed6fb982014-07-23 16:56:52419 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]e8f96ff2011-08-03 05:07:33420 size_t count = 0;
421 for (UpdateItems::iterator it = work_items_.begin();
[email protected]d0c8b8b42014-05-06 05:11:45422 it != work_items_.end();
423 ++it) {
[email protected]e8f96ff2011-08-03 05:07:33424 CrxUpdateItem* item = *it;
[email protected]93e8e2c2014-01-04 12:29:23425 if (item->status == from) {
426 ChangeItemState(item, to);
427 ++count;
428 }
[email protected]e8f96ff2011-08-03 05:07:33429 }
430 return count;
431}
432
433// Adds a component to be checked for upgrades. If the component exists it
434// it will be replaced and the return code is kReplaced.
[email protected]e8f96ff2011-08-03 05:07:33435ComponentUpdateService::Status CrxUpdateService::RegisterComponent(
436 const CrxComponent& component) {
[email protected]ed6fb982014-07-23 16:56:52437 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]d0c8b8b42014-05-06 05:11:45438 if (component.pk_hash.empty() || !component.version.IsValid() ||
[email protected]e8f96ff2011-08-03 05:07:33439 !component.installer)
440 return kError;
441
[email protected]055981f2014-01-17 20:22:32442 std::string id(GetCrxComponentID(component));
[email protected]93e8e2c2014-01-04 12:29:23443 CrxUpdateItem* uit = FindUpdateItemById(id);
[email protected]e8f96ff2011-08-03 05:07:33444 if (uit) {
445 uit->component = component;
446 return kReplaced;
447 }
448
449 uit = new CrxUpdateItem;
450 uit->id.swap(id);
451 uit->component = component;
[email protected]e3e696d32013-06-21 20:41:36452
[email protected]e8f96ff2011-08-03 05:07:33453 work_items_.push_back(uit);
[email protected]10bc0222014-06-11 13:44:38454
[email protected]e8f96ff2011-08-03 05:07:33455 // If this is the first component registered we call Start to
[email protected]10bc0222014-06-11 13:44:38456 // schedule the first timer. Otherwise, reset the timer to trigger another
457 // pass over the work items, if the component updater is sleeping, fact
458 // indicated by a running timer. If the timer is not running, it means that
459 // the service is busy updating something, and in that case, this component
460 // will be picked up at the next pass.
461 if (running_) {
462 if (work_items_.size() == 1) {
463 Start();
464 } else if (timer_.IsRunning()) {
465 timer_.Start(FROM_HERE,
466 base::TimeDelta::FromSeconds(config_->InitialDelay()),
467 this,
468 &CrxUpdateService::ProcessPendingItems);
469 }
470 }
[email protected]e8f96ff2011-08-03 05:07:33471
472 return kOk;
473}
474
[email protected]68bf09e2014-06-03 00:10:56475std::vector<std::string> CrxUpdateService::GetComponentIDs() const {
[email protected]ed6fb982014-07-23 16:56:52476 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]68bf09e2014-06-03 00:10:56477 std::vector<std::string> component_ids;
[email protected]2e919ddd2013-08-21 05:05:17478 for (UpdateItems::const_iterator it = work_items_.begin();
[email protected]d0c8b8b42014-05-06 05:11:45479 it != work_items_.end();
480 ++it) {
[email protected]2e919ddd2013-08-21 05:05:17481 const CrxUpdateItem* item = *it;
[email protected]68bf09e2014-06-03 00:10:56482 component_ids.push_back(item->id);
[email protected]2e919ddd2013-08-21 05:05:17483 }
[email protected]68bf09e2014-06-03 00:10:56484 return component_ids;
485}
486
[email protected]78efe2e92014-08-08 15:53:22487OnDemandUpdater& CrxUpdateService::GetOnDemandUpdater() {
488 return *this;
489}
490
491void CrxUpdateService::MaybeThrottle(const std::string& crx_id,
492 const base::Closure& callback) {
493 DCHECK(thread_checker_.CalledOnValidThread());
494 // Check if we can on-demand update, else unblock the request anyway.
495 CrxUpdateItem* item = FindUpdateItemById(crx_id);
496 Status status = OnDemandUpdateWithCooldown(item);
497 if (status == kOk || status == kInProgress) {
498 item->ready_callbacks.push_back(callback);
499 return;
500 }
501 callback.Run();
502}
503
504scoped_refptr<base::SequencedTaskRunner>
505CrxUpdateService::GetSequencedTaskRunner() {
506 return config_->GetSequencedTaskRunner();
507}
508
[email protected]f392e372014-06-12 07:25:57509bool CrxUpdateService::GetComponentDetails(const std::string& component_id,
510 CrxUpdateItem* item) const {
[email protected]ed6fb982014-07-23 16:56:52511 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]f392e372014-06-12 07:25:57512 const CrxUpdateItem* crx_update_item(FindUpdateItemById(component_id));
513 if (crx_update_item)
514 *item = *crx_update_item;
515 return crx_update_item != NULL;
[email protected]2e919ddd2013-08-21 05:05:17516}
517
[email protected]78efe2e92014-08-08 15:53:22518// Start the process of checking for an update, for a particular component
519// that was previously registered.
520// |component_id| is a value returned from GetCrxComponentID().
521ComponentUpdateService::Status CrxUpdateService::OnDemandUpdate(
522 const std::string& component_id) {
523 return OnDemandUpdateInternal(FindUpdateItemById(component_id));
[email protected]ed6fb982014-07-23 16:56:52524}
525
[email protected]93e8e2c2014-01-04 12:29:23526// This is the main loop of the component updater. It updates one component
527// at a time if updates are available. Otherwise, it does an update check or
528// takes a long sleep until the loop runs again.
[email protected]e8f96ff2011-08-03 05:07:33529void CrxUpdateService::ProcessPendingItems() {
[email protected]ed6fb982014-07-23 16:56:52530 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]93e8e2c2014-01-04 12:29:23531
[email protected]61aca4cd2013-10-26 10:50:59532 CrxUpdateItem* ready_upgrade = FindReadyComponent();
533 if (ready_upgrade) {
534 UpdateComponent(ready_upgrade);
[email protected]e8f96ff2011-08-03 05:07:33535 return;
536 }
[email protected]93e8e2c2014-01-04 12:29:23537
538 if (!CheckForUpdates())
539 ScheduleNextRun(kStepDelayLong);
[email protected]61aca4cd2013-10-26 10:50:59540}
541
[email protected]93e8e2c2014-01-04 12:29:23542CrxUpdateItem* CrxUpdateService::FindReadyComponent() const {
[email protected]c4b4cfa2014-03-10 18:55:00543 class Helper {
544 public:
545 static bool IsReadyOnDemand(CrxUpdateItem* item) {
546 return item->on_demand && IsReady(item);
[email protected]61aca4cd2013-10-26 10:50:59547 }
[email protected]c4b4cfa2014-03-10 18:55:00548 static bool IsReady(CrxUpdateItem* item) {
549 return item->status == CrxUpdateItem::kCanUpdate;
550 }
551 };
[email protected]61aca4cd2013-10-26 10:50:59552
[email protected]c4b4cfa2014-03-10 18:55:00553 std::vector<CrxUpdateItem*>::const_iterator it = std::find_if(
554 work_items_.begin(), work_items_.end(), Helper::IsReadyOnDemand);
555 if (it != work_items_.end())
556 return *it;
557 it = std::find_if(work_items_.begin(), work_items_.end(), Helper::IsReady);
558 if (it != work_items_.end())
559 return *it;
[email protected]61aca4cd2013-10-26 10:50:59560 return NULL;
561}
562
[email protected]93e8e2c2014-01-04 12:29:23563// Prepares the components for an update check and initiates the request.
[email protected]21a9c9a2014-02-19 19:37:11564// On demand components are always included in the update check request.
565// Otherwise, only include components that have not been checked recently.
[email protected]93e8e2c2014-01-04 12:29:23566bool CrxUpdateService::CheckForUpdates() {
[email protected]21a9c9a2014-02-19 19:37:11567 const base::TimeDelta minimum_recheck_wait_time =
568 base::TimeDelta::FromSeconds(config_->MinimumReCheckWait());
569 const base::Time now(base::Time::Now());
570
[email protected]93e8e2c2014-01-04 12:29:23571 std::vector<CrxUpdateItem*> items_to_check;
572 for (size_t i = 0; i != work_items_.size(); ++i) {
573 CrxUpdateItem* item = work_items_[i];
574 DCHECK(item->status == CrxUpdateItem::kNew ||
575 item->status == CrxUpdateItem::kNoUpdate ||
576 item->status == CrxUpdateItem::kUpToDate ||
577 item->status == CrxUpdateItem::kUpdated);
578
[email protected]21a9c9a2014-02-19 19:37:11579 const base::TimeDelta time_since_last_checked(now - item->last_check);
580
581 if (!item->on_demand &&
582 time_since_last_checked < minimum_recheck_wait_time) {
[email protected]fb53e652014-04-30 11:27:19583 VLOG(1) << "Skipping check for component update: id=" << item->id
584 << ", time_since_last_checked="
585 << time_since_last_checked.InSeconds()
586 << " seconds: too soon to check for an update";
[email protected]21a9c9a2014-02-19 19:37:11587 continue;
588 }
589
[email protected]fb53e652014-04-30 11:27:19590 VLOG(1) << "Scheduling update check for component id=" << item->id
591 << ", time_since_last_checked="
[email protected]d0c8b8b42014-05-06 05:11:45592 << time_since_last_checked.InSeconds() << " seconds";
[email protected]fb53e652014-04-30 11:27:19593
[email protected]21a9c9a2014-02-19 19:37:11594 item->last_check = now;
[email protected]93e8e2c2014-01-04 12:29:23595 item->crx_urls.clear();
596 item->crx_diffurls.clear();
597 item->previous_version = item->component.version;
598 item->next_version = Version();
599 item->previous_fp = item->component.fingerprint;
600 item->next_fp.clear();
601 item->diff_update_failed = false;
602 item->error_category = 0;
603 item->error_code = 0;
604 item->extra_code1 = 0;
605 item->diff_error_category = 0;
606 item->diff_error_code = 0;
607 item->diff_extra_code1 = 0;
608 item->download_metrics.clear();
609
610 items_to_check.push_back(item);
[email protected]3a10c572014-07-04 06:57:47611
612 ChangeItemState(item, CrxUpdateItem::kChecking);
[email protected]93e8e2c2014-01-04 12:29:23613 }
614
615 if (items_to_check.empty())
616 return false;
617
sorin395c2ac2014-09-16 21:31:07618 update_checker_ = UpdateChecker::Create(*config_).Pass();
619 return update_checker_->CheckForUpdates(
620 items_to_check,
621 config_->ExtraRequestParams(),
622 base::Bind(&CrxUpdateService::UpdateCheckComplete,
623 base::Unretained(this)));
[email protected]93e8e2c2014-01-04 12:29:23624}
625
[email protected]61aca4cd2013-10-26 10:50:59626void CrxUpdateService::UpdateComponent(CrxUpdateItem* workitem) {
[email protected]afa378f22013-12-02 03:37:54627 scoped_ptr<CRXContext> crx_context(new CRXContext);
628 crx_context->pk_hash = workitem->component.pk_hash;
629 crx_context->id = workitem->id;
630 crx_context->installer = workitem->component.installer;
631 crx_context->fingerprint = workitem->next_fp;
[email protected]da37c1d2013-12-19 01:04:38632 const std::vector<GURL>* urls = NULL;
[email protected]cfd13e52014-02-05 09:35:07633 bool allow_background_download = false;
[email protected]61aca4cd2013-10-26 10:50:59634 if (CanTryDiffUpdate(workitem, *config_)) {
[email protected]da37c1d2013-12-19 01:04:38635 urls = &workitem->crx_diffurls;
[email protected]61aca4cd2013-10-26 10:50:59636 ChangeItemState(workitem, CrxUpdateItem::kDownloadingDiff);
637 } else {
[email protected]cfd13e52014-02-05 09:35:07638 // Background downloads are enabled only for selected components and
639 // only for full downloads (see issue 340448).
640 allow_background_download = workitem->component.allow_background_download;
[email protected]da37c1d2013-12-19 01:04:38641 urls = &workitem->crx_urls;
[email protected]61aca4cd2013-10-26 10:50:59642 ChangeItemState(workitem, CrxUpdateItem::kDownloading);
643 }
[email protected]3cb2a4f2013-12-07 21:54:34644
645 // On demand component updates are always downloaded in foreground.
[email protected]d0c8b8b42014-05-06 05:11:45646 const bool is_background_download = !workitem->on_demand &&
647 allow_background_download &&
648 config_->UseBackgroundDownloader();
[email protected]3cb2a4f2013-12-07 21:54:34649
[email protected]ed6fb982014-07-23 16:56:52650 crx_downloader_.reset(
651 CrxDownloader::Create(is_background_download,
652 config_->RequestContext(),
653 blocking_task_runner_,
654 config_->GetSingleThreadTaskRunner()));
[email protected]8a5ebd432014-05-02 00:21:22655 crx_downloader_->set_progress_callback(
656 base::Bind(&CrxUpdateService::DownloadProgress,
657 base::Unretained(this),
658 crx_context->id));
[email protected]1b6587dc52014-04-26 00:38:55659 crx_downloader_->StartDownload(*urls,
660 base::Bind(&CrxUpdateService::DownloadComplete,
661 base::Unretained(this),
662 base::Passed(&crx_context)));
[email protected]61aca4cd2013-10-26 10:50:59663}
664
[email protected]93e8e2c2014-01-04 12:29:23665void CrxUpdateService::UpdateCheckComplete(
sorin395c2ac2014-09-16 21:31:07666 const GURL& original_url,
[email protected]93e8e2c2014-01-04 12:29:23667 int error,
668 const std::string& error_message,
[email protected]055981f2014-01-17 20:22:32669 const UpdateResponse::Results& results) {
[email protected]ed6fb982014-07-23 16:56:52670 DCHECK(thread_checker_.CalledOnValidThread());
sorin395c2ac2014-09-16 21:31:07671 VLOG(1) << "Update check completed from: " << original_url.spec();
[email protected]93e8e2c2014-01-04 12:29:23672 update_checker_.reset();
673 if (!error)
674 OnUpdateCheckSucceeded(results);
[email protected]652f4272013-11-13 09:23:41675 else
[email protected]93e8e2c2014-01-04 12:29:23676 OnUpdateCheckFailed(error, error_message);
[email protected]e8f96ff2011-08-03 05:07:33677}
678
[email protected]93e8e2c2014-01-04 12:29:23679// Handles a valid Omaha update check response by matching the results with
680// the registered components which were checked for updates.
681// If updates are found, prepare the components for the actual version upgrade.
682// One of these components will be drafted for the upgrade next time
683// ProcessPendingItems is called.
684void CrxUpdateService::OnUpdateCheckSucceeded(
[email protected]055981f2014-01-17 20:22:32685 const UpdateResponse::Results& results) {
[email protected]2cddef42013-11-22 08:23:22686 size_t num_updates_pending = 0;
[email protected]ed6fb982014-07-23 16:56:52687 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]fb53e652014-04-30 11:27:19688 VLOG(1) << "Update check succeeded.";
[email protected]055981f2014-01-17 20:22:32689 std::vector<UpdateResponse::Result>::const_iterator it;
[email protected]e8f96ff2011-08-03 05:07:33690 for (it = results.list.begin(); it != results.list.end(); ++it) {
691 CrxUpdateItem* crx = FindUpdateItemById(it->extension_id);
692 if (!crx)
693 continue;
694
[email protected]2cddef42013-11-22 08:23:22695 if (crx->status != CrxUpdateItem::kChecking) {
696 NOTREACHED();
[email protected]e8f96ff2011-08-03 05:07:33697 continue; // Not updating this component now.
[email protected]2cddef42013-11-22 08:23:22698 }
[email protected]e8f96ff2011-08-03 05:07:33699
[email protected]2cddef42013-11-22 08:23:22700 if (it->manifest.version.empty()) {
[email protected]cf442612011-08-09 20:20:12701 // No version means no update available.
[email protected]61aca4cd2013-10-26 10:50:59702 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]fb53e652014-04-30 11:27:19703 VLOG(1) << "No update available for component: " << crx->id;
[email protected]cf442612011-08-09 20:20:12704 continue;
[email protected]e8f96ff2011-08-03 05:07:33705 }
[email protected]2cddef42013-11-22 08:23:22706
707 if (!IsVersionNewer(crx->component.version, it->manifest.version)) {
708 // The component is up to date.
[email protected]61aca4cd2013-10-26 10:50:59709 ChangeItemState(crx, CrxUpdateItem::kUpToDate);
[email protected]fb53e652014-04-30 11:27:19710 VLOG(1) << "Component already up-to-date: " << crx->id;
[email protected]cf442612011-08-09 20:20:12711 continue;
[email protected]e8f96ff2011-08-03 05:07:33712 }
[email protected]2cddef42013-11-22 08:23:22713
714 if (!it->manifest.browser_min_version.empty()) {
[email protected]6a8ab1d2014-07-10 22:47:39715 if (IsVersionNewer(config_->GetBrowserVersion(),
716 it->manifest.browser_min_version)) {
[email protected]2cddef42013-11-22 08:23:22717 // The component is not compatible with this Chrome version.
[email protected]fb53e652014-04-30 11:27:19718 VLOG(1) << "Ignoring incompatible component: " << crx->id;
[email protected]61aca4cd2013-10-26 10:50:59719 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]cf442612011-08-09 20:20:12720 continue;
721 }
[email protected]e8f96ff2011-08-03 05:07:33722 }
[email protected]2cddef42013-11-22 08:23:22723
724 if (it->manifest.packages.size() != 1) {
725 // Assume one and only one package per component.
[email protected]fb53e652014-04-30 11:27:19726 VLOG(1) << "Ignoring multiple packages for component: " << crx->id;
[email protected]2cddef42013-11-22 08:23:22727 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
728 continue;
729 }
730
731 // Parse the members of the result and queue an upgrade for this component.
[email protected]c5e4a2222014-01-03 16:06:13732 crx->next_version = Version(it->manifest.version);
[email protected]2cddef42013-11-22 08:23:22733
[email protected]fb53e652014-04-30 11:27:19734 VLOG(1) << "Update found for component: " << crx->id;
735
[email protected]055981f2014-01-17 20:22:32736 typedef UpdateResponse::Result::Manifest::Package Package;
[email protected]2cddef42013-11-22 08:23:22737 const Package& package(it->manifest.packages[0]);
738 crx->next_fp = package.fingerprint;
739
[email protected]da37c1d2013-12-19 01:04:38740 // Resolve the urls by combining the base urls with the package names.
741 for (size_t i = 0; i != it->crx_urls.size(); ++i) {
742 const GURL url(it->crx_urls[i].Resolve(package.name));
743 if (url.is_valid())
[email protected]d0c8b8b42014-05-06 05:11:45744 crx->crx_urls.push_back(url);
[email protected]da37c1d2013-12-19 01:04:38745 }
746 for (size_t i = 0; i != it->crx_diffurls.size(); ++i) {
747 const GURL url(it->crx_diffurls[i].Resolve(package.namediff));
748 if (url.is_valid())
[email protected]d0c8b8b42014-05-06 05:11:45749 crx->crx_diffurls.push_back(url);
[email protected]da37c1d2013-12-19 01:04:38750 }
[email protected]2cddef42013-11-22 08:23:22751
[email protected]61aca4cd2013-10-26 10:50:59752 ChangeItemState(crx, CrxUpdateItem::kCanUpdate);
[email protected]2cddef42013-11-22 08:23:22753 ++num_updates_pending;
[email protected]e8f96ff2011-08-03 05:07:33754 }
[email protected]cf442612011-08-09 20:20:12755
[email protected]2cddef42013-11-22 08:23:22756 // All components that are not included in the update response are
757 // considered up to date.
[email protected]cf442612011-08-09 20:20:12758 ChangeItemStatus(CrxUpdateItem::kChecking, CrxUpdateItem::kUpToDate);
759
[email protected]32a6c8382013-08-20 00:29:20760 // If there are updates pending we do a short wait, otherwise we take
761 // a longer delay until we check the components again.
[email protected]21a9c9a2014-02-19 19:37:11762 ScheduleNextRun(num_updates_pending > 0 ? kStepDelayShort : kStepDelayLong);
[email protected]e8f96ff2011-08-03 05:07:33763}
764
[email protected]93e8e2c2014-01-04 12:29:23765void CrxUpdateService::OnUpdateCheckFailed(int error,
766 const std::string& error_message) {
[email protected]ed6fb982014-07-23 16:56:52767 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]93e8e2c2014-01-04 12:29:23768 DCHECK(error);
[email protected]d0c8b8b42014-05-06 05:11:45769 size_t count =
770 ChangeItemStatus(CrxUpdateItem::kChecking, CrxUpdateItem::kNoUpdate);
[email protected]e8f96ff2011-08-03 05:07:33771 DCHECK_GT(count, 0ul);
[email protected]fb53e652014-04-30 11:27:19772 VLOG(1) << "Update check failed.";
[email protected]32a6c8382013-08-20 00:29:20773 ScheduleNextRun(kStepDelayLong);
[email protected]e8f96ff2011-08-03 05:07:33774}
775
[email protected]8a5ebd432014-05-02 00:21:22776// Called when progress is being made downloading a CRX. The progress may
777// not monotonically increase due to how the CRX downloader switches between
778// different downloaders and fallback urls.
779void CrxUpdateService::DownloadProgress(
780 const std::string& component_id,
781 const CrxDownloader::Result& download_result) {
[email protected]ed6fb982014-07-23 16:56:52782 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]8a5ebd432014-05-02 00:21:22783 NotifyObservers(Observer::COMPONENT_UPDATE_DOWNLOADING, component_id);
784}
785
[email protected]e8f96ff2011-08-03 05:07:33786// Called when the CRX package has been downloaded to a temporary location.
787// Here we fire the notifications and schedule the component-specific installer
788// to be called in the file thread.
[email protected]3cb2a4f2013-12-07 21:54:34789void CrxUpdateService::DownloadComplete(
790 scoped_ptr<CRXContext> crx_context,
[email protected]3a0092d2013-12-18 03:04:35791 const CrxDownloader::Result& download_result) {
[email protected]ed6fb982014-07-23 16:56:52792 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]64f39fa12013-09-03 21:49:37793
[email protected]64f39fa12013-09-03 21:49:37794 CrxUpdateItem* crx = FindUpdateItemById(crx_context->id);
[email protected]e3e696d32013-06-21 20:41:36795 DCHECK(crx->status == CrxUpdateItem::kDownloadingDiff ||
796 crx->status == CrxUpdateItem::kDownloading);
797
[email protected]3a0092d2013-12-18 03:04:35798 AppendDownloadMetrics(crx_downloader_->download_metrics(),
799 &crx->download_metrics);
800
[email protected]8a5ebd432014-05-02 00:21:22801 crx_downloader_.reset();
802
[email protected]3cb2a4f2013-12-07 21:54:34803 if (download_result.error) {
[email protected]e3e696d32013-06-21 20:41:36804 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
[email protected]7b0529242013-07-20 05:45:46805 crx->diff_error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34806 crx->diff_error_code = download_result.error;
[email protected]e630ba62013-06-22 15:22:34807 crx->diff_update_failed = true;
[email protected]e3e696d32013-06-21 20:41:36808 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
809 CrxUpdateItem::kCanUpdate);
810 DCHECK_EQ(count, 1ul);
[email protected]7b0529242013-07-20 05:45:46811
[email protected]32a6c8382013-08-20 00:29:20812 ScheduleNextRun(kStepDelayShort);
[email protected]e3e696d32013-06-21 20:41:36813 return;
814 }
[email protected]7b0529242013-07-20 05:45:46815 crx->error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34816 crx->error_code = download_result.error;
[email protected]d0c8b8b42014-05-06 05:11:45817 size_t count =
818 ChangeItemStatus(CrxUpdateItem::kDownloading, CrxUpdateItem::kNoUpdate);
[email protected]e8f96ff2011-08-03 05:07:33819 DCHECK_EQ(count, 1ul);
[email protected]e3e696d32013-06-21 20:41:36820
[email protected]7b0529242013-07-20 05:45:46821 // At this point, since both the differential and the full downloads failed,
822 // the update for this component has finished with an error.
823 ping_manager_->OnUpdateComplete(crx);
824
[email protected]32a6c8382013-08-20 00:29:20825 // Move on to the next update, if there is one available.
826 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33827 } else {
[email protected]e3e696d32013-06-21 20:41:36828 size_t count = 0;
829 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
830 count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
831 CrxUpdateItem::kUpdatingDiff);
832 } else {
833 count = ChangeItemStatus(CrxUpdateItem::kDownloading,
834 CrxUpdateItem::kUpdating);
835 }
[email protected]e8f96ff2011-08-03 05:07:33836 DCHECK_EQ(count, 1ul);
[email protected]cf442612011-08-09 20:20:12837
[email protected]44da56e2011-11-21 19:59:14838 // Why unretained? See comment at top of file.
[email protected]8f5f2ea2013-10-31 09:39:10839 blocking_task_runner_->PostDelayedTask(
[email protected]73251e72012-03-04 02:10:33840 FROM_HERE,
[email protected]44da56e2011-11-21 19:59:14841 base::Bind(&CrxUpdateService::Install,
842 base::Unretained(this),
[email protected]afa378f22013-12-02 03:37:54843 base::Passed(&crx_context),
[email protected]3cb2a4f2013-12-07 21:54:34844 download_result.response),
[email protected]73251e72012-03-04 02:10:33845 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]e8f96ff2011-08-03 05:07:33846 }
[email protected]e8f96ff2011-08-03 05:07:33847}
848
849// Install consists of digital signature verification, unpacking and then
850// calling the component specific installer. All that is handled by the
[email protected]f5d27e32014-01-31 06:48:53851// |unpacker_|. If there is an error this function is in charge of deleting
[email protected]e8f96ff2011-08-03 05:07:33852// the files created.
[email protected]afa378f22013-12-02 03:37:54853void CrxUpdateService::Install(scoped_ptr<CRXContext> context,
[email protected]650b2d52013-02-10 03:41:45854 const base::FilePath& crx_path) {
[email protected]8f5f2ea2013-10-31 09:39:10855 // This function owns the file at |crx_path| and the |context| object.
[email protected]94a481b2014-03-28 19:41:55856 unpacker_ = new ComponentUnpacker(context->pk_hash,
857 crx_path,
858 context->fingerprint,
859 context->installer,
[email protected]e260af72014-08-05 07:52:39860 config_->CreateOutOfProcessPatcher(),
[email protected]94a481b2014-03-28 19:41:55861 blocking_task_runner_);
[email protected]f5d27e32014-01-31 06:48:53862 unpacker_->Unpack(base::Bind(&CrxUpdateService::EndUnpacking,
863 base::Unretained(this),
864 context->id,
865 crx_path));
866}
867
868void CrxUpdateService::EndUnpacking(const std::string& component_id,
869 const base::FilePath& crx_path,
870 ComponentUnpacker::Error error,
871 int extended_error) {
[email protected]055981f2014-01-17 20:22:32872 if (!DeleteFileAndEmptyParentDirectory(crx_path))
[email protected]e8f96ff2011-08-03 05:07:33873 NOTREACHED() << crx_path.value();
[email protected]e93632052014-07-28 23:51:09874 main_task_runner_->PostDelayedTask(
[email protected]73251e72012-03-04 02:10:33875 FROM_HERE,
[email protected]d0c8b8b42014-05-06 05:11:45876 base::Bind(&CrxUpdateService::DoneInstalling,
877 base::Unretained(this),
878 component_id,
879 error,
880 extended_error),
[email protected]73251e72012-03-04 02:10:33881 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]94a481b2014-03-28 19:41:55882 // Reset the unpacker last, otherwise we free our own arguments.
883 unpacker_ = NULL;
[email protected]e8f96ff2011-08-03 05:07:33884}
885
886// Installation has been completed. Adjust the component status and
[email protected]7b0529242013-07-20 05:45:46887// schedule the next check. Schedule a short delay before trying the full
888// update when the differential update failed.
[email protected]07f93af12011-08-17 20:57:22889void CrxUpdateService::DoneInstalling(const std::string& component_id,
[email protected]e3e696d32013-06-21 20:41:36890 ComponentUnpacker::Error error,
891 int extra_code) {
[email protected]ed6fb982014-07-23 16:56:52892 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]07f93af12011-08-17 20:57:22893
[email protected]7b0529242013-07-20 05:45:46894 ErrorCategory error_category = kErrorNone;
895 switch (error) {
896 case ComponentUnpacker::kNone:
897 break;
898 case ComponentUnpacker::kInstallerError:
899 error_category = kInstallError;
900 break;
901 default:
902 error_category = kUnpackError;
903 break;
904 }
905
906 const bool is_success = error == ComponentUnpacker::kNone;
907
[email protected]07f93af12011-08-17 20:57:22908 CrxUpdateItem* item = FindUpdateItemById(component_id);
[email protected]7b0529242013-07-20 05:45:46909 if (item->status == CrxUpdateItem::kUpdatingDiff && !is_success) {
910 item->diff_error_category = error_category;
911 item->diff_error_code = error;
912 item->diff_extra_code1 = extra_code;
[email protected]1ea21ad2013-09-02 04:20:39913 item->diff_update_failed = true;
914 size_t count = ChangeItemStatus(CrxUpdateItem::kUpdatingDiff,
915 CrxUpdateItem::kCanUpdate);
916 DCHECK_EQ(count, 1ul);
917 ScheduleNextRun(kStepDelayShort);
918 return;
[email protected]d0c8b8b42014-05-06 05:11:45919 }
[email protected]e3e696d32013-06-21 20:41:36920
[email protected]7b0529242013-07-20 05:45:46921 if (is_success) {
[email protected]07f93af12011-08-17 20:57:22922 item->component.version = item->next_version;
[email protected]e3e696d32013-06-21 20:41:36923 item->component.fingerprint = item->next_fp;
[email protected]3a10c572014-07-04 06:57:47924 ChangeItemState(item, CrxUpdateItem::kUpdated);
[email protected]7b0529242013-07-20 05:45:46925 } else {
[email protected]7b0529242013-07-20 05:45:46926 item->error_category = error_category;
927 item->error_code = error;
928 item->extra_code1 = extra_code;
[email protected]3a10c572014-07-04 06:57:47929 ChangeItemState(item, CrxUpdateItem::kNoUpdate);
[email protected]e3e696d32013-06-21 20:41:36930 }
[email protected]07f93af12011-08-17 20:57:22931
[email protected]7b0529242013-07-20 05:45:46932 ping_manager_->OnUpdateComplete(item);
[email protected]360b8bb2011-09-01 21:48:06933
[email protected]32a6c8382013-08-20 00:29:20934 // Move on to the next update, if there is one available.
935 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33936}
937
[email protected]d3268fe2014-04-25 02:14:23938void CrxUpdateService::NotifyObservers(Observer::Events event,
939 const std::string& id) {
[email protected]ed6fb982014-07-23 16:56:52940 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]d3268fe2014-04-25 02:14:23941 FOR_EACH_OBSERVER(Observer, observer_list_, OnEvent(event, id));
[email protected]85e61d52013-08-01 22:23:42942}
943
[email protected]5b53f932014-06-06 10:26:54944ComponentUpdateService::Status CrxUpdateService::OnDemandUpdateWithCooldown(
[email protected]504830a2014-05-20 21:53:54945 CrxUpdateItem* uit) {
946 if (!uit)
947 return kError;
948
949 // Check if the request is too soon.
950 base::TimeDelta delta = base::Time::Now() - uit->last_check;
951 if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay()))
952 return kError;
953
[email protected]5b53f932014-06-06 10:26:54954 return OnDemandUpdateInternal(uit);
955}
956
957ComponentUpdateService::Status CrxUpdateService::OnDemandUpdateInternal(
958 CrxUpdateItem* uit) {
959 if (!uit)
960 return kError;
961
[email protected]68bf09e2014-06-03 00:10:56962 uit->on_demand = true;
[email protected]504830a2014-05-20 21:53:54963
[email protected]3a10c572014-07-04 06:57:47964 // If there is an update available for this item, then continue processing
965 // the update. This is an artifact of how update checks are done: in addition
966 // to the on-demand item, the update check may include other items as well.
967 if (uit->status != CrxUpdateItem::kCanUpdate) {
968 Status service_status = GetServiceStatus(uit->status);
969 // If the item is already in the process of being updated, there is
970 // no point in this call, so return kInProgress.
971 if (service_status == kInProgress)
972 return service_status;
973
974 // Otherwise the item was already checked a while back (or it is new),
975 // set its status to kNew to give it a slightly higher priority.
976 ChangeItemState(uit, CrxUpdateItem::kNew);
977 }
978
[email protected]504830a2014-05-20 21:53:54979 // In case the current delay is long, set the timer to a shorter value
980 // to get the ball rolling.
981 if (timer_.IsRunning()) {
982 timer_.Stop();
983 timer_.Start(FROM_HERE,
984 base::TimeDelta::FromSeconds(config_->StepDelay()),
985 this,
986 &CrxUpdateService::ProcessPendingItems);
987 }
988
989 return kOk;
990}
991
[email protected]68bf09e2014-06-03 00:10:56992ComponentUpdateService::Status CrxUpdateService::GetServiceStatus(
993 CrxUpdateItem::Status status) {
994 switch (status) {
995 case CrxUpdateItem::kChecking:
996 case CrxUpdateItem::kCanUpdate:
997 case CrxUpdateItem::kDownloadingDiff:
998 case CrxUpdateItem::kDownloading:
999 case CrxUpdateItem::kUpdatingDiff:
1000 case CrxUpdateItem::kUpdating:
1001 return kInProgress;
1002 case CrxUpdateItem::kNew:
1003 case CrxUpdateItem::kUpdated:
1004 case CrxUpdateItem::kUpToDate:
1005 case CrxUpdateItem::kNoUpdate:
1006 return kOk;
1007 case CrxUpdateItem::kLastStatus:
1008 NOTREACHED() << status;
1009 }
1010 return kError;
1011}
1012
[email protected]00a77fa2013-11-02 04:18:461013///////////////////////////////////////////////////////////////////////////////
1014
[email protected]e8f96ff2011-08-03 05:07:331015// The component update factory. Using the component updater as a singleton
1016// is the job of the browser process.
[email protected]655043812014-06-24 01:50:361017ComponentUpdateService* ComponentUpdateServiceFactory(Configurator* config) {
[email protected]e8f96ff2011-08-03 05:07:331018 DCHECK(config);
1019 return new CrxUpdateService(config);
1020}
[email protected]2cddef42013-11-22 08:23:221021
[email protected]055981f2014-01-17 20:22:321022} // namespace component_updater