blob: 4bfc2d7edaea0092b856f221be16f3d58cd6c64a [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"
sorin52ac0882015-01-24 01:15:0028#include "components/update_client/component_patcher_operation.h"
29#include "components/update_client/component_unpacker.h"
30#include "components/update_client/configurator.h"
31#include "components/update_client/crx_downloader.h"
32#include "components/update_client/crx_update_item.h"
33#include "components/update_client/ping_manager.h"
34#include "components/update_client/update_checker.h"
35#include "components/update_client/update_client.h"
36#include "components/update_client/update_response.h"
37#include "components/update_client/utils.h"
[email protected]761fa4702013-07-02 15:25:1538#include "url/gurl.h"
[email protected]e8f96ff2011-08-03 05:07:3339
vasiliibc47245d82015-04-14 08:59:1240using update_client::ComponentInstaller;
sorin52ac0882015-01-24 01:15:0041using update_client::ComponentUnpacker;
42using update_client::Configurator;
43using update_client::CrxComponent;
44using update_client::CrxDownloader;
45using update_client::CrxUpdateItem;
46using update_client::PingManager;
47using update_client::UpdateChecker;
48using update_client::UpdateResponse;
49
[email protected]055981f2014-01-17 20:22:3250namespace component_updater {
[email protected]3a0092d2013-12-18 03:04:3551
[email protected]44da56e2011-11-21 19:59:1452// The component updater is designed to live until process shutdown, so
53// base::Bind() calls are not refcounted.
54
[email protected]e8f96ff2011-08-03 05:07:3355namespace {
[email protected]e3e696d32013-06-21 20:41:3656
[email protected]2cddef42013-11-22 08:23:2257// Returns true if the |proposed| version is newer than |current| version.
[email protected]c5e4a2222014-01-03 16:06:1358bool IsVersionNewer(const Version& current, const std::string& proposed) {
59 Version proposed_ver(proposed);
[email protected]2cddef42013-11-22 08:23:2260 return proposed_ver.IsValid() && current.CompareTo(proposed_ver) < 0;
[email protected]e8f96ff2011-08-03 05:07:3361}
62
[email protected]e3e696d32013-06-21 20:41:3663// Returns true if a differential update is available, it has not failed yet,
64// and the configuration allows it.
65bool CanTryDiffUpdate(const CrxUpdateItem* update_item,
[email protected]655043812014-06-24 01:50:3666 const Configurator& config) {
[email protected]d0c8b8b42014-05-06 05:11:4567 return HasDiffUpdate(update_item) && !update_item->diff_update_failed &&
[email protected]e3e696d32013-06-21 20:41:3668 config.DeltasEnabled();
69}
70
[email protected]3a0092d2013-12-18 03:04:3571void AppendDownloadMetrics(
72 const std::vector<CrxDownloader::DownloadMetrics>& source,
73 std::vector<CrxDownloader::DownloadMetrics>* destination) {
74 destination->insert(destination->end(), source.begin(), source.end());
75}
76
[email protected]7b0529242013-07-20 05:45:4677} // namespace
78
[email protected]e8f96ff2011-08-03 05:07:3379//////////////////////////////////////////////////////////////////////////////
80// The one and only implementation of the ComponentUpdateService interface. In
81// charge of running the show. The main method is ProcessPendingItems() which
[email protected]50b01392012-09-01 03:33:1382// is called periodically to do the upgrades/installs or the update checks.
[email protected]e8f96ff2011-08-03 05:07:3383// An important consideration here is to be as "low impact" as we can to the
84// rest of the browser, so even if we have many components registered and
[email protected]f1050432012-02-15 01:35:4685// eligible for update, we only do one thing at a time with pauses in between
[email protected]e8f96ff2011-08-03 05:07:3386// the tasks. Also when we do network requests there is only one |url_fetcher_|
[email protected]e3e696d32013-06-21 20:41:3687// in flight at a time.
[email protected]e8f96ff2011-08-03 05:07:3388// There are no locks in this code, the main structure |work_items_| is mutated
[email protected]ed6fb982014-07-23 16:56:5289// only from the main thread. The unpack and installation is done in a blocking
[email protected]74be2642014-02-07 09:40:3790// pool thread. The network requests are done in the IO thread or in the file
[email protected]e8f96ff2011-08-03 05:07:3391// thread.
[email protected]504830a2014-05-20 21:53:5492class CrxUpdateService : public ComponentUpdateService, public OnDemandUpdater {
[email protected]e8f96ff2011-08-03 05:07:3393 public:
vasiliibc47245d82015-04-14 08:59:1294 explicit CrxUpdateService(Configurator* config);
dcheng00ea022b2014-10-21 11:24:5695 ~CrxUpdateService() override;
[email protected]e8f96ff2011-08-03 05:07:3396
97 // Overrides for ComponentUpdateService.
dcheng00ea022b2014-10-21 11:24:5698 void AddObserver(Observer* observer) override;
99 void RemoveObserver(Observer* observer) override;
100 Status Start() override;
101 Status Stop() override;
102 Status RegisterComponent(const CrxComponent& component) override;
bauerb1f6657e72015-02-09 00:00:27103 Status UnregisterComponent(const std::string& crx_id) override;
dcheng00ea022b2014-10-21 11:24:56104 std::vector<std::string> GetComponentIDs() const override;
105 OnDemandUpdater& GetOnDemandUpdater() override;
106 void MaybeThrottle(const std::string& crx_id,
107 const base::Closure& callback) override;
108 scoped_refptr<base::SequencedTaskRunner> GetSequencedTaskRunner() override;
[email protected]504830a2014-05-20 21:53:54109
[email protected]28ea9ac2014-05-03 22:07:18110 // Context for a crx download url request.
111 struct CRXContext {
vasiliibc47245d82015-04-14 08:59:12112 scoped_refptr<ComponentInstaller> installer;
sorin5cb1f5492014-09-23 04:07:44113 std::vector<uint8_t> pk_hash;
[email protected]28ea9ac2014-05-03 22:07:18114 std::string id;
115 std::string fingerprint;
116 CRXContext() : installer(NULL) {}
117 };
[email protected]e8f96ff2011-08-03 05:07:33118
[email protected]e8f96ff2011-08-03 05:07:33119 private:
[email protected]7b0529242013-07-20 05:45:46120 enum ErrorCategory {
121 kErrorNone = 0,
122 kNetworkError,
123 kUnpackError,
124 kInstallError,
125 };
126
[email protected]32a6c8382013-08-20 00:29:20127 enum StepDelayInterval {
128 kStepDelayShort = 0,
129 kStepDelayMedium,
130 kStepDelayLong,
131 };
132
[email protected]78efe2e92014-08-08 15:53:22133 // Overrides for ComponentUpdateService.
dcheng00ea022b2014-10-21 11:24:56134 bool GetComponentDetails(const std::string& component_id,
135 CrxUpdateItem* item) const override;
[email protected]78efe2e92014-08-08 15:53:22136
137 // Overrides for OnDemandUpdater.
dcheng00ea022b2014-10-21 11:24:56138 Status OnDemandUpdate(const std::string& component_id) override;
[email protected]78efe2e92014-08-08 15:53:22139
sorin395c2ac2014-09-16 21:31:07140 void UpdateCheckComplete(const GURL& original_url,
141 int error,
[email protected]055981f2014-01-17 20:22:32142 const std::string& error_message,
143 const UpdateResponse::Results& results);
144 void OnUpdateCheckSucceeded(const UpdateResponse::Results& results);
[email protected]93e8e2c2014-01-04 12:29:23145 void OnUpdateCheckFailed(int error, const std::string& error_message);
[email protected]e8f96ff2011-08-03 05:07:33146
[email protected]8a5ebd432014-05-02 00:21:22147 void DownloadProgress(const std::string& component_id,
148 const CrxDownloader::Result& download_result);
149
150 void DownloadComplete(scoped_ptr<CRXContext> crx_context,
151 const CrxDownloader::Result& download_result);
[email protected]afa378f22013-12-02 03:37:54152
[email protected]00a77fa2013-11-02 04:18:46153 Status OnDemandUpdateInternal(CrxUpdateItem* item);
[email protected]5b53f932014-06-06 10:26:54154 Status OnDemandUpdateWithCooldown(CrxUpdateItem* item);
[email protected]00a77fa2013-11-02 04:18:46155
[email protected]e8f96ff2011-08-03 05:07:33156 void ProcessPendingItems();
157
bauerb1f6657e72015-02-09 00:00:27158 // Uninstall and remove all unregistered work items.
159 void UninstallUnregisteredItems();
160
[email protected]93e8e2c2014-01-04 12:29:23161 // Find a component that is ready to update.
162 CrxUpdateItem* FindReadyComponent() const;
163
164 // Prepares the components for an update check and initiates the request.
165 // Returns true if an update check request has been made. Returns false if
166 // no update check was needed or an error occured.
167 bool CheckForUpdates();
[email protected]61aca4cd2013-10-26 10:50:59168
169 void UpdateComponent(CrxUpdateItem* workitem);
170
[email protected]32a6c8382013-08-20 00:29:20171 void ScheduleNextRun(StepDelayInterval step_delay);
[email protected]e8f96ff2011-08-03 05:07:33172
[email protected]6268d3a2013-11-27 01:28:09173 void ParseResponse(const std::string& xml);
[email protected]e8f96ff2011-08-03 05:07:33174
[email protected]afa378f22013-12-02 03:37:54175 void Install(scoped_ptr<CRXContext> context, const base::FilePath& crx_path);
[email protected]e8f96ff2011-08-03 05:07:33176
[email protected]f5d27e32014-01-31 06:48:53177 void EndUnpacking(const std::string& component_id,
178 const base::FilePath& crx_path,
179 ComponentUnpacker::Error error,
180 int extended_error);
181
[email protected]07f93af12011-08-17 20:57:22182 void DoneInstalling(const std::string& component_id,
[email protected]e3e696d32013-06-21 20:41:36183 ComponentUnpacker::Error error,
184 int extended_error);
[email protected]e8f96ff2011-08-03 05:07:33185
vasiliibc47245d82015-04-14 08:59:12186 void ChangeItemState(CrxUpdateItem* item, CrxUpdateItem::Status to);
[email protected]61aca4cd2013-10-26 10:50:59187
vasiliibc47245d82015-04-14 08:59:12188 size_t ChangeItemStatus(CrxUpdateItem::Status from, CrxUpdateItem::Status to);
[email protected]e8f96ff2011-08-03 05:07:33189
[email protected]68bf09e2014-06-03 00:10:56190 CrxUpdateItem* FindUpdateItemById(const std::string& id) const;
[email protected]e8f96ff2011-08-03 05:07:33191
[email protected]d3268fe2014-04-25 02:14:23192 void NotifyObservers(Observer::Events event, const std::string& id);
[email protected]85e61d52013-08-01 22:23:42193
[email protected]61aca4cd2013-10-26 10:50:59194 bool HasOnDemandItems() const;
195
vasiliibc47245d82015-04-14 08:59:12196 Status GetServiceStatus(const CrxUpdateItem::Status status);
[email protected]68bf09e2014-06-03 00:10:56197
vasiliibc47245d82015-04-14 08:59:12198 scoped_ptr<Configurator> config_;
[email protected]e3e696d32013-06-21 20:41:36199
[email protected]055981f2014-01-17 20:22:32200 scoped_ptr<UpdateChecker> update_checker_;
[email protected]e8f96ff2011-08-03 05:07:33201
[email protected]055981f2014-01-17 20:22:32202 scoped_ptr<PingManager> ping_manager_;
[email protected]7b0529242013-07-20 05:45:46203
[email protected]94a481b2014-03-28 19:41:55204 scoped_refptr<ComponentUnpacker> unpacker_;
[email protected]f5d27e32014-01-31 06:48:53205
[email protected]055981f2014-01-17 20:22:32206 scoped_ptr<CrxDownloader> crx_downloader_;
[email protected]afa378f22013-12-02 03:37:54207
[email protected]86550a42013-06-21 15:20:49208 // A collection of every work item.
[email protected]e3e696d32013-06-21 20:41:36209 typedef std::vector<CrxUpdateItem*> UpdateItems;
[email protected]e8f96ff2011-08-03 05:07:33210 UpdateItems work_items_;
211
212 base::OneShotTimer<CrxUpdateService> timer_;
213
[email protected]ed6fb982014-07-23 16:56:52214 base::ThreadChecker thread_checker_;
215
[email protected]e93632052014-07-28 23:51:09216 // Used to post responses back to the main thread.
217 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
218
[email protected]8f5f2ea2013-10-31 09:39:10219 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
220
[email protected]e8f96ff2011-08-03 05:07:33221 bool running_;
222
[email protected]d3268fe2014-04-25 02:14:23223 ObserverList<Observer> observer_list_;
224
[email protected]e8f96ff2011-08-03 05:07:33225 DISALLOW_COPY_AND_ASSIGN(CrxUpdateService);
226};
227
[email protected]e8f96ff2011-08-03 05:07:33228//////////////////////////////////////////////////////////////////////////////
229
vasiliibc47245d82015-04-14 08:59:12230CrxUpdateService::CrxUpdateService(Configurator* config)
[email protected]e8f96ff2011-08-03 05:07:33231 : config_(config),
[email protected]09974032014-06-27 07:42:08232 ping_manager_(new PingManager(*config)),
[email protected]e93632052014-07-28 23:51:09233 main_task_runner_(base::MessageLoopProxy::current()),
[email protected]ed6fb982014-07-23 16:56:52234 blocking_task_runner_(config->GetSequencedTaskRunner()),
[email protected]e8f96ff2011-08-03 05:07:33235 running_(false) {
[email protected]1ea21ad2013-09-02 04:20:39236}
[email protected]e8f96ff2011-08-03 05:07:33237
238CrxUpdateService::~CrxUpdateService() {
[email protected]ed6fb982014-07-23 16:56:52239 // Because we are a singleton, at this point only the main thread should be
[email protected]e8f96ff2011-08-03 05:07:33240 // alive, this simplifies the management of the work that could be in
241 // flight in other threads.
242 Stop();
243 STLDeleteElements(&work_items_);
[email protected]1ea21ad2013-09-02 04:20:39244}
[email protected]e8f96ff2011-08-03 05:07:33245
[email protected]d3268fe2014-04-25 02:14:23246void CrxUpdateService::AddObserver(Observer* observer) {
[email protected]ed6fb982014-07-23 16:56:52247 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]d3268fe2014-04-25 02:14:23248 observer_list_.AddObserver(observer);
249}
250
251void CrxUpdateService::RemoveObserver(Observer* observer) {
[email protected]ed6fb982014-07-23 16:56:52252 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]d3268fe2014-04-25 02:14:23253 observer_list_.RemoveObserver(observer);
254}
255
[email protected]e8f96ff2011-08-03 05:07:33256ComponentUpdateService::Status CrxUpdateService::Start() {
257 // Note that RegisterComponent will call Start() when the first
258 // component is registered, so it can be called twice. This way
259 // we avoid scheduling the timer if there is no work to do.
[email protected]fb53e652014-04-30 11:27:19260 VLOG(1) << "CrxUpdateService starting up";
[email protected]e8f96ff2011-08-03 05:07:33261 running_ = true;
262 if (work_items_.empty())
vasiliibc47245d82015-04-14 08:59:12263 return kOk;
[email protected]e8f96ff2011-08-03 05:07:33264
vasiliibc47245d82015-04-14 08:59:12265 NotifyObservers(Observer::COMPONENT_UPDATER_STARTED, "");
[email protected]85e61d52013-08-01 22:23:42266
[email protected]fb53e652014-04-30 11:27:19267 VLOG(1) << "First update attempt will take place in "
268 << config_->InitialDelay() << " seconds";
[email protected]d0c8b8b42014-05-06 05:11:45269 timer_.Start(FROM_HERE,
270 base::TimeDelta::FromSeconds(config_->InitialDelay()),
271 this,
272 &CrxUpdateService::ProcessPendingItems);
vasiliibc47245d82015-04-14 08:59:12273 return kOk;
[email protected]e8f96ff2011-08-03 05:07:33274}
275
276// Stop the main check + update loop. In flight operations will be
277// completed.
278ComponentUpdateService::Status CrxUpdateService::Stop() {
[email protected]fb53e652014-04-30 11:27:19279 VLOG(1) << "CrxUpdateService stopping";
[email protected]e8f96ff2011-08-03 05:07:33280 running_ = false;
281 timer_.Stop();
vasiliibc47245d82015-04-14 08:59:12282 return kOk;
[email protected]e8f96ff2011-08-03 05:07:33283}
284
[email protected]61aca4cd2013-10-26 10:50:59285bool CrxUpdateService::HasOnDemandItems() const {
286 class Helper {
287 public:
[email protected]d0c8b8b42014-05-06 05:11:45288 static bool IsOnDemand(CrxUpdateItem* item) { return item->on_demand; }
[email protected]61aca4cd2013-10-26 10:50:59289 };
290 return std::find_if(work_items_.begin(),
291 work_items_.end(),
292 Helper::IsOnDemand) != work_items_.end();
293}
294
[email protected]ccb4feef2013-02-14 06:16:47295// This function sets the timer which will call ProcessPendingItems() or
[email protected]61aca4cd2013-10-26 10:50:59296// ProcessRequestedItem() if there is an on_demand item. There
[email protected]32a6c8382013-08-20 00:29:20297// are three kinds of waits:
298// - a short delay, when there is immediate work to be done.
299// - a medium delay, when there are updates to be applied within the current
300// update cycle, or there are components that are still unchecked.
301// - a long delay when a full check/update cycle has completed for all
302// components.
303void CrxUpdateService::ScheduleNextRun(StepDelayInterval step_delay) {
[email protected]ed6fb982014-07-23 16:56:52304 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]93e8e2c2014-01-04 12:29:23305 DCHECK(!update_checker_);
[email protected]e8f96ff2011-08-03 05:07:33306 CHECK(!timer_.IsRunning());
307 // It could be the case that Stop() had been called while a url request
308 // or unpacking was in flight, if so we arrive here but |running_| is
309 // false. In that case do not loop again.
310 if (!running_)
311 return;
312
[email protected]ccb4feef2013-02-14 06:16:47313 // Keep the delay short if in the middle of an update (step_delay),
314 // or there are new requested_work_items_ that have not been processed yet.
sorin5cb1f5492014-09-23 04:07:44315 int64_t delay_seconds = 0;
[email protected]61aca4cd2013-10-26 10:50:59316 if (!HasOnDemandItems()) {
[email protected]32a6c8382013-08-20 00:29:20317 switch (step_delay) {
318 case kStepDelayShort:
319 delay_seconds = config_->StepDelay();
320 break;
321 case kStepDelayMedium:
322 delay_seconds = config_->StepDelayMedium();
323 break;
324 case kStepDelayLong:
325 delay_seconds = config_->NextCheckDelay();
326 break;
327 }
328 } else {
329 delay_seconds = config_->StepDelay();
330 }
[email protected]cf442612011-08-09 20:20:12331
[email protected]32a6c8382013-08-20 00:29:20332 if (step_delay != kStepDelayShort) {
vasiliibc47245d82015-04-14 08:59:12333 NotifyObservers(Observer::COMPONENT_UPDATER_SLEEPING, "");
[email protected]85e61d52013-08-01 22:23:42334
[email protected]e8f96ff2011-08-03 05:07:33335 // Zero is only used for unit tests.
[email protected]32a6c8382013-08-20 00:29:20336 if (0 == delay_seconds)
[email protected]e8f96ff2011-08-03 05:07:33337 return;
338 }
339
[email protected]fb53e652014-04-30 11:27:19340 VLOG(1) << "Scheduling next run to occur in " << delay_seconds << " seconds";
[email protected]d0c8b8b42014-05-06 05:11:45341 timer_.Start(FROM_HERE,
342 base::TimeDelta::FromSeconds(delay_seconds),
343 this,
344 &CrxUpdateService::ProcessPendingItems);
[email protected]e8f96ff2011-08-03 05:07:33345}
346
347// Given a extension-like component id, find the associated component.
[email protected]68bf09e2014-06-03 00:10:56348CrxUpdateItem* CrxUpdateService::FindUpdateItemById(
349 const std::string& id) const {
[email protected]ed6fb982014-07-23 16:56:52350 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]e8f96ff2011-08-03 05:07:33351 CrxUpdateItem::FindById finder(id);
[email protected]68bf09e2014-06-03 00:10:56352 UpdateItems::const_iterator it =
[email protected]d0c8b8b42014-05-06 05:11:45353 std::find_if(work_items_.begin(), work_items_.end(), finder);
[email protected]93e8e2c2014-01-04 12:29:23354 return it != work_items_.end() ? *it : NULL;
[email protected]e8f96ff2011-08-03 05:07:33355}
356
vasiliibc47245d82015-04-14 08:59:12357// Changes a component's status, clearing on_demand and firing notifications as
[email protected]61aca4cd2013-10-26 10:50:59358// necessary. By convention, this is the only function that can change a
vasiliibc47245d82015-04-14 08:59:12359// CrxUpdateItem's |status|.
[email protected]61aca4cd2013-10-26 10:50:59360// TODO(waffles): Do we want to add DCHECKS for valid state transitions here?
361void CrxUpdateService::ChangeItemState(CrxUpdateItem* item,
vasiliibc47245d82015-04-14 08:59:12362 CrxUpdateItem::Status to) {
[email protected]ed6fb982014-07-23 16:56:52363 DCHECK(thread_checker_.CalledOnValidThread());
vasiliibc47245d82015-04-14 08:59:12364 if (to == CrxUpdateItem::kNoUpdate || to == CrxUpdateItem::kUpdated ||
365 to == CrxUpdateItem::kUpToDate) {
[email protected]61aca4cd2013-10-26 10:50:59366 item->on_demand = false;
367 }
368
vasiliibc47245d82015-04-14 08:59:12369 item->status = to;
[email protected]61aca4cd2013-10-26 10:50:59370
[email protected]d3268fe2014-04-25 02:14:23371 switch (to) {
vasiliibc47245d82015-04-14 08:59:12372 case CrxUpdateItem::kCanUpdate:
373 NotifyObservers(Observer::COMPONENT_UPDATE_FOUND, item->id);
[email protected]d3268fe2014-04-25 02:14:23374 break;
vasiliibc47245d82015-04-14 08:59:12375 case CrxUpdateItem::kUpdatingDiff:
376 case CrxUpdateItem::kUpdating:
377 NotifyObservers(Observer::COMPONENT_UPDATE_READY, item->id);
[email protected]d3268fe2014-04-25 02:14:23378 break;
vasiliibc47245d82015-04-14 08:59:12379 case CrxUpdateItem::kUpdated:
380 NotifyObservers(Observer::COMPONENT_UPDATED, item->id);
[email protected]d3268fe2014-04-25 02:14:23381 break;
vasiliibc47245d82015-04-14 08:59:12382 case CrxUpdateItem::kUpToDate:
383 case CrxUpdateItem::kNoUpdate:
384 NotifyObservers(Observer::COMPONENT_NOT_UPDATED, item->id);
[email protected]d3268fe2014-04-25 02:14:23385 break;
vasiliibc47245d82015-04-14 08:59:12386 case CrxUpdateItem::kNew:
387 case CrxUpdateItem::kChecking:
388 case CrxUpdateItem::kDownloading:
389 case CrxUpdateItem::kDownloadingDiff:
390 case CrxUpdateItem::kLastStatus:
[email protected]d3268fe2014-04-25 02:14:23391 // No notification for these states.
392 break;
[email protected]61aca4cd2013-10-26 10:50:59393 }
[email protected]00a77fa2013-11-02 04:18:46394
395 // Free possible pending network requests.
vasiliibc47245d82015-04-14 08:59:12396 if ((to == CrxUpdateItem::kUpdated) || (to == CrxUpdateItem::kUpToDate) ||
397 (to == CrxUpdateItem::kNoUpdate)) {
[email protected]78efe2e92014-08-08 15:53:22398 for (std::vector<base::Closure>::iterator it =
399 item->ready_callbacks.begin();
400 it != item->ready_callbacks.end();
401 ++it) {
402 it->Run();
403 }
404 item->ready_callbacks.clear();
[email protected]00a77fa2013-11-02 04:18:46405 }
[email protected]61aca4cd2013-10-26 10:50:59406}
407
vasiliibc47245d82015-04-14 08:59:12408// Changes all the components in |work_items_| that have |from| status to
409// |to| status and returns how many have been changed.
410size_t CrxUpdateService::ChangeItemStatus(CrxUpdateItem::Status from,
411 CrxUpdateItem::Status to) {
[email protected]ed6fb982014-07-23 16:56:52412 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]e8f96ff2011-08-03 05:07:33413 size_t count = 0;
414 for (UpdateItems::iterator it = work_items_.begin();
[email protected]d0c8b8b42014-05-06 05:11:45415 it != work_items_.end();
416 ++it) {
[email protected]e8f96ff2011-08-03 05:07:33417 CrxUpdateItem* item = *it;
vasiliibc47245d82015-04-14 08:59:12418 if (item->status == from) {
[email protected]93e8e2c2014-01-04 12:29:23419 ChangeItemState(item, to);
420 ++count;
421 }
[email protected]e8f96ff2011-08-03 05:07:33422 }
423 return count;
424}
425
426// Adds a component to be checked for upgrades. If the component exists it
vasiliibc47245d82015-04-14 08:59:12427// it will be replaced and the return code is kReplaced.
[email protected]e8f96ff2011-08-03 05:07:33428ComponentUpdateService::Status CrxUpdateService::RegisterComponent(
429 const CrxComponent& component) {
[email protected]ed6fb982014-07-23 16:56:52430 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]d0c8b8b42014-05-06 05:11:45431 if (component.pk_hash.empty() || !component.version.IsValid() ||
[email protected]e8f96ff2011-08-03 05:07:33432 !component.installer)
vasiliibc47245d82015-04-14 08:59:12433 return kError;
[email protected]e8f96ff2011-08-03 05:07:33434
[email protected]055981f2014-01-17 20:22:32435 std::string id(GetCrxComponentID(component));
[email protected]93e8e2c2014-01-04 12:29:23436 CrxUpdateItem* uit = FindUpdateItemById(id);
[email protected]e8f96ff2011-08-03 05:07:33437 if (uit) {
438 uit->component = component;
bauerb1f6657e72015-02-09 00:00:27439 uit->unregistered = false;
vasiliibc47245d82015-04-14 08:59:12440 return kReplaced;
[email protected]e8f96ff2011-08-03 05:07:33441 }
442
443 uit = new CrxUpdateItem;
444 uit->id.swap(id);
445 uit->component = component;
[email protected]e3e696d32013-06-21 20:41:36446
[email protected]e8f96ff2011-08-03 05:07:33447 work_items_.push_back(uit);
[email protected]10bc0222014-06-11 13:44:38448
[email protected]e8f96ff2011-08-03 05:07:33449 // If this is the first component registered we call Start to
[email protected]10bc0222014-06-11 13:44:38450 // schedule the first timer. Otherwise, reset the timer to trigger another
451 // pass over the work items, if the component updater is sleeping, fact
452 // indicated by a running timer. If the timer is not running, it means that
453 // the service is busy updating something, and in that case, this component
454 // will be picked up at the next pass.
455 if (running_) {
456 if (work_items_.size() == 1) {
457 Start();
458 } else if (timer_.IsRunning()) {
459 timer_.Start(FROM_HERE,
460 base::TimeDelta::FromSeconds(config_->InitialDelay()),
461 this,
462 &CrxUpdateService::ProcessPendingItems);
463 }
464 }
[email protected]e8f96ff2011-08-03 05:07:33465
vasiliibc47245d82015-04-14 08:59:12466 return kOk;
[email protected]e8f96ff2011-08-03 05:07:33467}
468
bauerb1f6657e72015-02-09 00:00:27469ComponentUpdateService::Status CrxUpdateService::UnregisterComponent(
470 const std::string& crx_id) {
471 auto it = std::find_if(work_items_.begin(), work_items_.end(),
472 CrxUpdateItem::FindById(crx_id));
473 if (it == work_items_.end())
vasiliibc47245d82015-04-14 08:59:12474 return kError;
bauerb1f6657e72015-02-09 00:00:27475
476 (*it)->unregistered = true;
477
478 ScheduleNextRun(kStepDelayShort);
vasiliibc47245d82015-04-14 08:59:12479 return kOk;
bauerb1f6657e72015-02-09 00:00:27480}
481
[email protected]68bf09e2014-06-03 00:10:56482std::vector<std::string> CrxUpdateService::GetComponentIDs() const {
[email protected]ed6fb982014-07-23 16:56:52483 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]68bf09e2014-06-03 00:10:56484 std::vector<std::string> component_ids;
[email protected]2e919ddd2013-08-21 05:05:17485 for (UpdateItems::const_iterator it = work_items_.begin();
[email protected]d0c8b8b42014-05-06 05:11:45486 it != work_items_.end();
487 ++it) {
[email protected]2e919ddd2013-08-21 05:05:17488 const CrxUpdateItem* item = *it;
[email protected]68bf09e2014-06-03 00:10:56489 component_ids.push_back(item->id);
[email protected]2e919ddd2013-08-21 05:05:17490 }
[email protected]68bf09e2014-06-03 00:10:56491 return component_ids;
492}
493
[email protected]78efe2e92014-08-08 15:53:22494OnDemandUpdater& CrxUpdateService::GetOnDemandUpdater() {
495 return *this;
496}
497
498void CrxUpdateService::MaybeThrottle(const std::string& crx_id,
499 const base::Closure& callback) {
500 DCHECK(thread_checker_.CalledOnValidThread());
501 // Check if we can on-demand update, else unblock the request anyway.
502 CrxUpdateItem* item = FindUpdateItemById(crx_id);
503 Status status = OnDemandUpdateWithCooldown(item);
vasiliibc47245d82015-04-14 08:59:12504 if (status == kOk || status == kInProgress) {
[email protected]78efe2e92014-08-08 15:53:22505 item->ready_callbacks.push_back(callback);
506 return;
507 }
508 callback.Run();
509}
510
511scoped_refptr<base::SequencedTaskRunner>
512CrxUpdateService::GetSequencedTaskRunner() {
bauerb1f6657e72015-02-09 00:00:27513 return blocking_task_runner_;
[email protected]78efe2e92014-08-08 15:53:22514}
515
[email protected]f392e372014-06-12 07:25:57516bool CrxUpdateService::GetComponentDetails(const std::string& component_id,
517 CrxUpdateItem* item) const {
[email protected]ed6fb982014-07-23 16:56:52518 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]f392e372014-06-12 07:25:57519 const CrxUpdateItem* crx_update_item(FindUpdateItemById(component_id));
520 if (crx_update_item)
521 *item = *crx_update_item;
522 return crx_update_item != NULL;
[email protected]2e919ddd2013-08-21 05:05:17523}
524
[email protected]78efe2e92014-08-08 15:53:22525// Start the process of checking for an update, for a particular component
526// that was previously registered.
527// |component_id| is a value returned from GetCrxComponentID().
528ComponentUpdateService::Status CrxUpdateService::OnDemandUpdate(
529 const std::string& component_id) {
530 return OnDemandUpdateInternal(FindUpdateItemById(component_id));
[email protected]ed6fb982014-07-23 16:56:52531}
532
[email protected]93e8e2c2014-01-04 12:29:23533// This is the main loop of the component updater. It updates one component
534// at a time if updates are available. Otherwise, it does an update check or
535// takes a long sleep until the loop runs again.
[email protected]e8f96ff2011-08-03 05:07:33536void CrxUpdateService::ProcessPendingItems() {
[email protected]ed6fb982014-07-23 16:56:52537 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]93e8e2c2014-01-04 12:29:23538
[email protected]61aca4cd2013-10-26 10:50:59539 CrxUpdateItem* ready_upgrade = FindReadyComponent();
540 if (ready_upgrade) {
541 UpdateComponent(ready_upgrade);
[email protected]e8f96ff2011-08-03 05:07:33542 return;
543 }
[email protected]93e8e2c2014-01-04 12:29:23544
bauerb1f6657e72015-02-09 00:00:27545 UninstallUnregisteredItems();
546
[email protected]93e8e2c2014-01-04 12:29:23547 if (!CheckForUpdates())
548 ScheduleNextRun(kStepDelayLong);
[email protected]61aca4cd2013-10-26 10:50:59549}
550
bauerb1f6657e72015-02-09 00:00:27551void CrxUpdateService::UninstallUnregisteredItems() {
552 std::vector<CrxUpdateItem*> new_work_items;
553 for (CrxUpdateItem* item : work_items_) {
554 scoped_ptr<CrxUpdateItem> owned_item(item);
555 if (owned_item->unregistered) {
556 const bool success = owned_item->component.installer->Uninstall();
557 DCHECK(success);
558 } else {
559 new_work_items.push_back(owned_item.release());
560 }
561 }
562 new_work_items.swap(work_items_);
563}
564
[email protected]93e8e2c2014-01-04 12:29:23565CrxUpdateItem* CrxUpdateService::FindReadyComponent() const {
[email protected]c4b4cfa2014-03-10 18:55:00566 class Helper {
567 public:
568 static bool IsReadyOnDemand(CrxUpdateItem* item) {
569 return item->on_demand && IsReady(item);
[email protected]61aca4cd2013-10-26 10:50:59570 }
[email protected]c4b4cfa2014-03-10 18:55:00571 static bool IsReady(CrxUpdateItem* item) {
vasiliibc47245d82015-04-14 08:59:12572 return item->status == CrxUpdateItem::kCanUpdate;
[email protected]c4b4cfa2014-03-10 18:55:00573 }
574 };
[email protected]61aca4cd2013-10-26 10:50:59575
[email protected]c4b4cfa2014-03-10 18:55:00576 std::vector<CrxUpdateItem*>::const_iterator it = std::find_if(
577 work_items_.begin(), work_items_.end(), Helper::IsReadyOnDemand);
578 if (it != work_items_.end())
579 return *it;
580 it = std::find_if(work_items_.begin(), work_items_.end(), Helper::IsReady);
581 if (it != work_items_.end())
582 return *it;
[email protected]61aca4cd2013-10-26 10:50:59583 return NULL;
584}
585
[email protected]93e8e2c2014-01-04 12:29:23586// Prepares the components for an update check and initiates the request.
[email protected]21a9c9a2014-02-19 19:37:11587// On demand components are always included in the update check request.
588// Otherwise, only include components that have not been checked recently.
[email protected]93e8e2c2014-01-04 12:29:23589bool CrxUpdateService::CheckForUpdates() {
[email protected]21a9c9a2014-02-19 19:37:11590 const base::TimeDelta minimum_recheck_wait_time =
591 base::TimeDelta::FromSeconds(config_->MinimumReCheckWait());
592 const base::Time now(base::Time::Now());
593
[email protected]93e8e2c2014-01-04 12:29:23594 std::vector<CrxUpdateItem*> items_to_check;
595 for (size_t i = 0; i != work_items_.size(); ++i) {
596 CrxUpdateItem* item = work_items_[i];
vasiliibc47245d82015-04-14 08:59:12597 DCHECK(item->status == CrxUpdateItem::kNew ||
598 item->status == CrxUpdateItem::kNoUpdate ||
599 item->status == CrxUpdateItem::kUpToDate ||
600 item->status == CrxUpdateItem::kUpdated);
[email protected]93e8e2c2014-01-04 12:29:23601
[email protected]21a9c9a2014-02-19 19:37:11602 const base::TimeDelta time_since_last_checked(now - item->last_check);
603
604 if (!item->on_demand &&
605 time_since_last_checked < minimum_recheck_wait_time) {
[email protected]fb53e652014-04-30 11:27:19606 VLOG(1) << "Skipping check for component update: id=" << item->id
607 << ", time_since_last_checked="
608 << time_since_last_checked.InSeconds()
609 << " seconds: too soon to check for an update";
[email protected]21a9c9a2014-02-19 19:37:11610 continue;
611 }
612
[email protected]fb53e652014-04-30 11:27:19613 VLOG(1) << "Scheduling update check for component id=" << item->id
614 << ", time_since_last_checked="
[email protected]d0c8b8b42014-05-06 05:11:45615 << time_since_last_checked.InSeconds() << " seconds";
[email protected]fb53e652014-04-30 11:27:19616
[email protected]21a9c9a2014-02-19 19:37:11617 item->last_check = now;
[email protected]93e8e2c2014-01-04 12:29:23618 item->crx_urls.clear();
619 item->crx_diffurls.clear();
620 item->previous_version = item->component.version;
621 item->next_version = Version();
622 item->previous_fp = item->component.fingerprint;
623 item->next_fp.clear();
624 item->diff_update_failed = false;
625 item->error_category = 0;
626 item->error_code = 0;
627 item->extra_code1 = 0;
628 item->diff_error_category = 0;
629 item->diff_error_code = 0;
630 item->diff_extra_code1 = 0;
631 item->download_metrics.clear();
632
633 items_to_check.push_back(item);
[email protected]3a10c572014-07-04 06:57:47634
vasiliibc47245d82015-04-14 08:59:12635 ChangeItemState(item, CrxUpdateItem::kChecking);
[email protected]93e8e2c2014-01-04 12:29:23636 }
637
638 if (items_to_check.empty())
639 return false;
640
sorin395c2ac2014-09-16 21:31:07641 update_checker_ = UpdateChecker::Create(*config_).Pass();
642 return update_checker_->CheckForUpdates(
643 items_to_check,
644 config_->ExtraRequestParams(),
645 base::Bind(&CrxUpdateService::UpdateCheckComplete,
646 base::Unretained(this)));
[email protected]93e8e2c2014-01-04 12:29:23647}
648
[email protected]61aca4cd2013-10-26 10:50:59649void CrxUpdateService::UpdateComponent(CrxUpdateItem* workitem) {
[email protected]afa378f22013-12-02 03:37:54650 scoped_ptr<CRXContext> crx_context(new CRXContext);
651 crx_context->pk_hash = workitem->component.pk_hash;
652 crx_context->id = workitem->id;
653 crx_context->installer = workitem->component.installer;
654 crx_context->fingerprint = workitem->next_fp;
[email protected]da37c1d2013-12-19 01:04:38655 const std::vector<GURL>* urls = NULL;
[email protected]cfd13e52014-02-05 09:35:07656 bool allow_background_download = false;
[email protected]61aca4cd2013-10-26 10:50:59657 if (CanTryDiffUpdate(workitem, *config_)) {
[email protected]da37c1d2013-12-19 01:04:38658 urls = &workitem->crx_diffurls;
vasiliibc47245d82015-04-14 08:59:12659 ChangeItemState(workitem, CrxUpdateItem::kDownloadingDiff);
[email protected]61aca4cd2013-10-26 10:50:59660 } else {
[email protected]cfd13e52014-02-05 09:35:07661 // Background downloads are enabled only for selected components and
662 // only for full downloads (see issue 340448).
663 allow_background_download = workitem->component.allow_background_download;
[email protected]da37c1d2013-12-19 01:04:38664 urls = &workitem->crx_urls;
vasiliibc47245d82015-04-14 08:59:12665 ChangeItemState(workitem, CrxUpdateItem::kDownloading);
[email protected]61aca4cd2013-10-26 10:50:59666 }
[email protected]3cb2a4f2013-12-07 21:54:34667
668 // On demand component updates are always downloaded in foreground.
[email protected]d0c8b8b42014-05-06 05:11:45669 const bool is_background_download = !workitem->on_demand &&
670 allow_background_download &&
671 config_->UseBackgroundDownloader();
[email protected]3cb2a4f2013-12-07 21:54:34672
[email protected]ed6fb982014-07-23 16:56:52673 crx_downloader_.reset(
vasiliibc47245d82015-04-14 08:59:12674 CrxDownloader::Create(is_background_download,
675 config_->RequestContext(),
[email protected]ed6fb982014-07-23 16:56:52676 blocking_task_runner_,
vasiliibc47245d82015-04-14 08:59:12677 config_->GetSingleThreadTaskRunner()));
[email protected]8a5ebd432014-05-02 00:21:22678 crx_downloader_->set_progress_callback(
679 base::Bind(&CrxUpdateService::DownloadProgress,
680 base::Unretained(this),
681 crx_context->id));
[email protected]1b6587dc52014-04-26 00:38:55682 crx_downloader_->StartDownload(*urls,
683 base::Bind(&CrxUpdateService::DownloadComplete,
684 base::Unretained(this),
685 base::Passed(&crx_context)));
[email protected]61aca4cd2013-10-26 10:50:59686}
687
[email protected]93e8e2c2014-01-04 12:29:23688void CrxUpdateService::UpdateCheckComplete(
sorin395c2ac2014-09-16 21:31:07689 const GURL& original_url,
[email protected]93e8e2c2014-01-04 12:29:23690 int error,
691 const std::string& error_message,
[email protected]055981f2014-01-17 20:22:32692 const UpdateResponse::Results& results) {
[email protected]ed6fb982014-07-23 16:56:52693 DCHECK(thread_checker_.CalledOnValidThread());
sorin395c2ac2014-09-16 21:31:07694 VLOG(1) << "Update check completed from: " << original_url.spec();
[email protected]93e8e2c2014-01-04 12:29:23695 update_checker_.reset();
696 if (!error)
697 OnUpdateCheckSucceeded(results);
[email protected]652f4272013-11-13 09:23:41698 else
[email protected]93e8e2c2014-01-04 12:29:23699 OnUpdateCheckFailed(error, error_message);
[email protected]e8f96ff2011-08-03 05:07:33700}
701
[email protected]93e8e2c2014-01-04 12:29:23702// Handles a valid Omaha update check response by matching the results with
703// the registered components which were checked for updates.
704// If updates are found, prepare the components for the actual version upgrade.
705// One of these components will be drafted for the upgrade next time
706// ProcessPendingItems is called.
707void CrxUpdateService::OnUpdateCheckSucceeded(
[email protected]055981f2014-01-17 20:22:32708 const UpdateResponse::Results& results) {
[email protected]2cddef42013-11-22 08:23:22709 size_t num_updates_pending = 0;
[email protected]ed6fb982014-07-23 16:56:52710 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]fb53e652014-04-30 11:27:19711 VLOG(1) << "Update check succeeded.";
[email protected]055981f2014-01-17 20:22:32712 std::vector<UpdateResponse::Result>::const_iterator it;
[email protected]e8f96ff2011-08-03 05:07:33713 for (it = results.list.begin(); it != results.list.end(); ++it) {
714 CrxUpdateItem* crx = FindUpdateItemById(it->extension_id);
715 if (!crx)
716 continue;
717
vasiliibc47245d82015-04-14 08:59:12718 if (crx->status != CrxUpdateItem::kChecking) {
[email protected]2cddef42013-11-22 08:23:22719 NOTREACHED();
[email protected]e8f96ff2011-08-03 05:07:33720 continue; // Not updating this component now.
[email protected]2cddef42013-11-22 08:23:22721 }
[email protected]e8f96ff2011-08-03 05:07:33722
[email protected]2cddef42013-11-22 08:23:22723 if (it->manifest.version.empty()) {
[email protected]cf442612011-08-09 20:20:12724 // No version means no update available.
vasiliibc47245d82015-04-14 08:59:12725 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]fb53e652014-04-30 11:27:19726 VLOG(1) << "No update available for component: " << crx->id;
[email protected]cf442612011-08-09 20:20:12727 continue;
[email protected]e8f96ff2011-08-03 05:07:33728 }
[email protected]2cddef42013-11-22 08:23:22729
730 if (!IsVersionNewer(crx->component.version, it->manifest.version)) {
731 // The component is up to date.
vasiliibc47245d82015-04-14 08:59:12732 ChangeItemState(crx, CrxUpdateItem::kUpToDate);
[email protected]fb53e652014-04-30 11:27:19733 VLOG(1) << "Component already up-to-date: " << crx->id;
[email protected]cf442612011-08-09 20:20:12734 continue;
[email protected]e8f96ff2011-08-03 05:07:33735 }
[email protected]2cddef42013-11-22 08:23:22736
737 if (!it->manifest.browser_min_version.empty()) {
[email protected]6a8ab1d2014-07-10 22:47:39738 if (IsVersionNewer(config_->GetBrowserVersion(),
739 it->manifest.browser_min_version)) {
[email protected]2cddef42013-11-22 08:23:22740 // The component is not compatible with this Chrome version.
[email protected]fb53e652014-04-30 11:27:19741 VLOG(1) << "Ignoring incompatible component: " << crx->id;
vasiliibc47245d82015-04-14 08:59:12742 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]cf442612011-08-09 20:20:12743 continue;
744 }
[email protected]e8f96ff2011-08-03 05:07:33745 }
[email protected]2cddef42013-11-22 08:23:22746
747 if (it->manifest.packages.size() != 1) {
748 // Assume one and only one package per component.
[email protected]fb53e652014-04-30 11:27:19749 VLOG(1) << "Ignoring multiple packages for component: " << crx->id;
vasiliibc47245d82015-04-14 08:59:12750 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]2cddef42013-11-22 08:23:22751 continue;
752 }
753
754 // Parse the members of the result and queue an upgrade for this component.
[email protected]c5e4a2222014-01-03 16:06:13755 crx->next_version = Version(it->manifest.version);
[email protected]2cddef42013-11-22 08:23:22756
[email protected]fb53e652014-04-30 11:27:19757 VLOG(1) << "Update found for component: " << crx->id;
758
[email protected]055981f2014-01-17 20:22:32759 typedef UpdateResponse::Result::Manifest::Package Package;
[email protected]2cddef42013-11-22 08:23:22760 const Package& package(it->manifest.packages[0]);
761 crx->next_fp = package.fingerprint;
762
[email protected]da37c1d2013-12-19 01:04:38763 // Resolve the urls by combining the base urls with the package names.
764 for (size_t i = 0; i != it->crx_urls.size(); ++i) {
765 const GURL url(it->crx_urls[i].Resolve(package.name));
766 if (url.is_valid())
[email protected]d0c8b8b42014-05-06 05:11:45767 crx->crx_urls.push_back(url);
[email protected]da37c1d2013-12-19 01:04:38768 }
769 for (size_t i = 0; i != it->crx_diffurls.size(); ++i) {
770 const GURL url(it->crx_diffurls[i].Resolve(package.namediff));
771 if (url.is_valid())
[email protected]d0c8b8b42014-05-06 05:11:45772 crx->crx_diffurls.push_back(url);
[email protected]da37c1d2013-12-19 01:04:38773 }
[email protected]2cddef42013-11-22 08:23:22774
vasiliibc47245d82015-04-14 08:59:12775 ChangeItemState(crx, CrxUpdateItem::kCanUpdate);
[email protected]2cddef42013-11-22 08:23:22776 ++num_updates_pending;
[email protected]e8f96ff2011-08-03 05:07:33777 }
[email protected]cf442612011-08-09 20:20:12778
[email protected]2cddef42013-11-22 08:23:22779 // All components that are not included in the update response are
780 // considered up to date.
vasiliibc47245d82015-04-14 08:59:12781 ChangeItemStatus(CrxUpdateItem::kChecking, CrxUpdateItem::kUpToDate);
[email protected]cf442612011-08-09 20:20:12782
[email protected]32a6c8382013-08-20 00:29:20783 // If there are updates pending we do a short wait, otherwise we take
784 // a longer delay until we check the components again.
[email protected]21a9c9a2014-02-19 19:37:11785 ScheduleNextRun(num_updates_pending > 0 ? kStepDelayShort : kStepDelayLong);
[email protected]e8f96ff2011-08-03 05:07:33786}
787
[email protected]93e8e2c2014-01-04 12:29:23788void CrxUpdateService::OnUpdateCheckFailed(int error,
789 const std::string& error_message) {
[email protected]ed6fb982014-07-23 16:56:52790 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]93e8e2c2014-01-04 12:29:23791 DCHECK(error);
vasiliibc47245d82015-04-14 08:59:12792 size_t count =
793 ChangeItemStatus(CrxUpdateItem::kChecking, CrxUpdateItem::kNoUpdate);
[email protected]e8f96ff2011-08-03 05:07:33794 DCHECK_GT(count, 0ul);
[email protected]fb53e652014-04-30 11:27:19795 VLOG(1) << "Update check failed.";
[email protected]32a6c8382013-08-20 00:29:20796 ScheduleNextRun(kStepDelayLong);
[email protected]e8f96ff2011-08-03 05:07:33797}
798
[email protected]8a5ebd432014-05-02 00:21:22799// Called when progress is being made downloading a CRX. The progress may
800// not monotonically increase due to how the CRX downloader switches between
801// different downloaders and fallback urls.
802void CrxUpdateService::DownloadProgress(
803 const std::string& component_id,
804 const CrxDownloader::Result& download_result) {
[email protected]ed6fb982014-07-23 16:56:52805 DCHECK(thread_checker_.CalledOnValidThread());
vasiliibc47245d82015-04-14 08:59:12806 NotifyObservers(Observer::COMPONENT_UPDATE_DOWNLOADING, component_id);
[email protected]8a5ebd432014-05-02 00:21:22807}
808
[email protected]e8f96ff2011-08-03 05:07:33809// Called when the CRX package has been downloaded to a temporary location.
810// Here we fire the notifications and schedule the component-specific installer
811// to be called in the file thread.
[email protected]3cb2a4f2013-12-07 21:54:34812void CrxUpdateService::DownloadComplete(
813 scoped_ptr<CRXContext> crx_context,
[email protected]3a0092d2013-12-18 03:04:35814 const CrxDownloader::Result& download_result) {
[email protected]ed6fb982014-07-23 16:56:52815 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]64f39fa12013-09-03 21:49:37816
[email protected]64f39fa12013-09-03 21:49:37817 CrxUpdateItem* crx = FindUpdateItemById(crx_context->id);
bauerb1f6657e72015-02-09 00:00:27818
vasiliibc47245d82015-04-14 08:59:12819 DCHECK(crx->status == CrxUpdateItem::kDownloadingDiff ||
820 crx->status == CrxUpdateItem::kDownloading);
[email protected]e3e696d32013-06-21 20:41:36821
[email protected]3a0092d2013-12-18 03:04:35822 AppendDownloadMetrics(crx_downloader_->download_metrics(),
823 &crx->download_metrics);
824
[email protected]8a5ebd432014-05-02 00:21:22825 crx_downloader_.reset();
826
[email protected]3cb2a4f2013-12-07 21:54:34827 if (download_result.error) {
vasiliibc47245d82015-04-14 08:59:12828 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
[email protected]7b0529242013-07-20 05:45:46829 crx->diff_error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34830 crx->diff_error_code = download_result.error;
[email protected]e630ba62013-06-22 15:22:34831 crx->diff_update_failed = true;
vasiliibc47245d82015-04-14 08:59:12832 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
833 CrxUpdateItem::kCanUpdate);
[email protected]e3e696d32013-06-21 20:41:36834 DCHECK_EQ(count, 1ul);
[email protected]7b0529242013-07-20 05:45:46835
[email protected]32a6c8382013-08-20 00:29:20836 ScheduleNextRun(kStepDelayShort);
[email protected]e3e696d32013-06-21 20:41:36837 return;
838 }
[email protected]7b0529242013-07-20 05:45:46839 crx->error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34840 crx->error_code = download_result.error;
vasiliibc47245d82015-04-14 08:59:12841 size_t count =
842 ChangeItemStatus(CrxUpdateItem::kDownloading, CrxUpdateItem::kNoUpdate);
[email protected]e8f96ff2011-08-03 05:07:33843 DCHECK_EQ(count, 1ul);
[email protected]e3e696d32013-06-21 20:41:36844
[email protected]7b0529242013-07-20 05:45:46845 // At this point, since both the differential and the full downloads failed,
846 // the update for this component has finished with an error.
847 ping_manager_->OnUpdateComplete(crx);
848
[email protected]32a6c8382013-08-20 00:29:20849 // Move on to the next update, if there is one available.
850 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33851 } else {
[email protected]e3e696d32013-06-21 20:41:36852 size_t count = 0;
vasiliibc47245d82015-04-14 08:59:12853 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
854 count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
855 CrxUpdateItem::kUpdatingDiff);
[email protected]e3e696d32013-06-21 20:41:36856 } else {
vasiliibc47245d82015-04-14 08:59:12857 count = ChangeItemStatus(CrxUpdateItem::kDownloading,
858 CrxUpdateItem::kUpdating);
[email protected]e3e696d32013-06-21 20:41:36859 }
[email protected]e8f96ff2011-08-03 05:07:33860 DCHECK_EQ(count, 1ul);
[email protected]cf442612011-08-09 20:20:12861
[email protected]44da56e2011-11-21 19:59:14862 // Why unretained? See comment at top of file.
[email protected]8f5f2ea2013-10-31 09:39:10863 blocking_task_runner_->PostDelayedTask(
[email protected]73251e72012-03-04 02:10:33864 FROM_HERE,
[email protected]44da56e2011-11-21 19:59:14865 base::Bind(&CrxUpdateService::Install,
866 base::Unretained(this),
[email protected]afa378f22013-12-02 03:37:54867 base::Passed(&crx_context),
[email protected]3cb2a4f2013-12-07 21:54:34868 download_result.response),
[email protected]73251e72012-03-04 02:10:33869 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]e8f96ff2011-08-03 05:07:33870 }
[email protected]e8f96ff2011-08-03 05:07:33871}
872
873// Install consists of digital signature verification, unpacking and then
874// calling the component specific installer. All that is handled by the
[email protected]f5d27e32014-01-31 06:48:53875// |unpacker_|. If there is an error this function is in charge of deleting
[email protected]e8f96ff2011-08-03 05:07:33876// the files created.
[email protected]afa378f22013-12-02 03:37:54877void CrxUpdateService::Install(scoped_ptr<CRXContext> context,
[email protected]650b2d52013-02-10 03:41:45878 const base::FilePath& crx_path) {
[email protected]8f5f2ea2013-10-31 09:39:10879 // This function owns the file at |crx_path| and the |context| object.
[email protected]94a481b2014-03-28 19:41:55880 unpacker_ = new ComponentUnpacker(context->pk_hash,
881 crx_path,
882 context->fingerprint,
883 context->installer,
[email protected]e260af72014-08-05 07:52:39884 config_->CreateOutOfProcessPatcher(),
[email protected]94a481b2014-03-28 19:41:55885 blocking_task_runner_);
[email protected]f5d27e32014-01-31 06:48:53886 unpacker_->Unpack(base::Bind(&CrxUpdateService::EndUnpacking,
887 base::Unretained(this),
888 context->id,
889 crx_path));
890}
891
892void CrxUpdateService::EndUnpacking(const std::string& component_id,
893 const base::FilePath& crx_path,
894 ComponentUnpacker::Error error,
895 int extended_error) {
sorin52ac0882015-01-24 01:15:00896 if (!update_client::DeleteFileAndEmptyParentDirectory(crx_path))
[email protected]e8f96ff2011-08-03 05:07:33897 NOTREACHED() << crx_path.value();
[email protected]e93632052014-07-28 23:51:09898 main_task_runner_->PostDelayedTask(
[email protected]73251e72012-03-04 02:10:33899 FROM_HERE,
[email protected]d0c8b8b42014-05-06 05:11:45900 base::Bind(&CrxUpdateService::DoneInstalling,
901 base::Unretained(this),
902 component_id,
903 error,
904 extended_error),
[email protected]73251e72012-03-04 02:10:33905 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]94a481b2014-03-28 19:41:55906 // Reset the unpacker last, otherwise we free our own arguments.
907 unpacker_ = NULL;
[email protected]e8f96ff2011-08-03 05:07:33908}
909
vasiliibc47245d82015-04-14 08:59:12910// Installation has been completed. Adjust the component status and
[email protected]7b0529242013-07-20 05:45:46911// schedule the next check. Schedule a short delay before trying the full
912// update when the differential update failed.
[email protected]07f93af12011-08-17 20:57:22913void CrxUpdateService::DoneInstalling(const std::string& component_id,
[email protected]e3e696d32013-06-21 20:41:36914 ComponentUnpacker::Error error,
915 int extra_code) {
[email protected]ed6fb982014-07-23 16:56:52916 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]07f93af12011-08-17 20:57:22917
[email protected]7b0529242013-07-20 05:45:46918 ErrorCategory error_category = kErrorNone;
919 switch (error) {
920 case ComponentUnpacker::kNone:
921 break;
922 case ComponentUnpacker::kInstallerError:
923 error_category = kInstallError;
924 break;
925 default:
926 error_category = kUnpackError;
927 break;
928 }
929
930 const bool is_success = error == ComponentUnpacker::kNone;
931
[email protected]07f93af12011-08-17 20:57:22932 CrxUpdateItem* item = FindUpdateItemById(component_id);
bauerb1f6657e72015-02-09 00:00:27933
vasiliibc47245d82015-04-14 08:59:12934 if (item->status == CrxUpdateItem::kUpdatingDiff && !is_success) {
[email protected]7b0529242013-07-20 05:45:46935 item->diff_error_category = error_category;
936 item->diff_error_code = error;
937 item->diff_extra_code1 = extra_code;
[email protected]1ea21ad2013-09-02 04:20:39938 item->diff_update_failed = true;
vasiliibc47245d82015-04-14 08:59:12939 size_t count = ChangeItemStatus(CrxUpdateItem::kUpdatingDiff,
940 CrxUpdateItem::kCanUpdate);
[email protected]1ea21ad2013-09-02 04:20:39941 DCHECK_EQ(count, 1ul);
942 ScheduleNextRun(kStepDelayShort);
943 return;
[email protected]d0c8b8b42014-05-06 05:11:45944 }
[email protected]e3e696d32013-06-21 20:41:36945
[email protected]7b0529242013-07-20 05:45:46946 if (is_success) {
[email protected]07f93af12011-08-17 20:57:22947 item->component.version = item->next_version;
[email protected]e3e696d32013-06-21 20:41:36948 item->component.fingerprint = item->next_fp;
vasiliibc47245d82015-04-14 08:59:12949 ChangeItemState(item, CrxUpdateItem::kUpdated);
[email protected]7b0529242013-07-20 05:45:46950 } else {
[email protected]7b0529242013-07-20 05:45:46951 item->error_category = error_category;
952 item->error_code = error;
953 item->extra_code1 = extra_code;
vasiliibc47245d82015-04-14 08:59:12954 ChangeItemState(item, CrxUpdateItem::kNoUpdate);
[email protected]e3e696d32013-06-21 20:41:36955 }
[email protected]07f93af12011-08-17 20:57:22956
[email protected]7b0529242013-07-20 05:45:46957 ping_manager_->OnUpdateComplete(item);
[email protected]360b8bb2011-09-01 21:48:06958
[email protected]32a6c8382013-08-20 00:29:20959 // Move on to the next update, if there is one available.
960 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33961}
962
[email protected]d3268fe2014-04-25 02:14:23963void CrxUpdateService::NotifyObservers(Observer::Events event,
964 const std::string& id) {
[email protected]ed6fb982014-07-23 16:56:52965 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]d3268fe2014-04-25 02:14:23966 FOR_EACH_OBSERVER(Observer, observer_list_, OnEvent(event, id));
[email protected]85e61d52013-08-01 22:23:42967}
968
[email protected]5b53f932014-06-06 10:26:54969ComponentUpdateService::Status CrxUpdateService::OnDemandUpdateWithCooldown(
[email protected]504830a2014-05-20 21:53:54970 CrxUpdateItem* uit) {
971 if (!uit)
vasiliibc47245d82015-04-14 08:59:12972 return kError;
[email protected]504830a2014-05-20 21:53:54973
974 // Check if the request is too soon.
975 base::TimeDelta delta = base::Time::Now() - uit->last_check;
976 if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay()))
vasiliibc47245d82015-04-14 08:59:12977 return kError;
[email protected]504830a2014-05-20 21:53:54978
[email protected]5b53f932014-06-06 10:26:54979 return OnDemandUpdateInternal(uit);
980}
981
982ComponentUpdateService::Status CrxUpdateService::OnDemandUpdateInternal(
983 CrxUpdateItem* uit) {
984 if (!uit)
vasiliibc47245d82015-04-14 08:59:12985 return kError;
[email protected]5b53f932014-06-06 10:26:54986
[email protected]68bf09e2014-06-03 00:10:56987 uit->on_demand = true;
[email protected]504830a2014-05-20 21:53:54988
[email protected]3a10c572014-07-04 06:57:47989 // If there is an update available for this item, then continue processing
990 // the update. This is an artifact of how update checks are done: in addition
991 // to the on-demand item, the update check may include other items as well.
vasiliibc47245d82015-04-14 08:59:12992 if (uit->status != CrxUpdateItem::kCanUpdate) {
993 Status service_status = GetServiceStatus(uit->status);
[email protected]3a10c572014-07-04 06:57:47994 // If the item is already in the process of being updated, there is
vasiliibc47245d82015-04-14 08:59:12995 // no point in this call, so return kInProgress.
996 if (service_status == kInProgress)
[email protected]3a10c572014-07-04 06:57:47997 return service_status;
998
999 // Otherwise the item was already checked a while back (or it is new),
vasiliibc47245d82015-04-14 08:59:121000 // set its status to kNew to give it a slightly higher priority.
1001 ChangeItemState(uit, CrxUpdateItem::kNew);
[email protected]3a10c572014-07-04 06:57:471002 }
1003
[email protected]504830a2014-05-20 21:53:541004 // In case the current delay is long, set the timer to a shorter value
1005 // to get the ball rolling.
1006 if (timer_.IsRunning()) {
1007 timer_.Stop();
1008 timer_.Start(FROM_HERE,
1009 base::TimeDelta::FromSeconds(config_->StepDelay()),
1010 this,
1011 &CrxUpdateService::ProcessPendingItems);
1012 }
1013
vasiliibc47245d82015-04-14 08:59:121014 return kOk;
[email protected]504830a2014-05-20 21:53:541015}
1016
[email protected]68bf09e2014-06-03 00:10:561017ComponentUpdateService::Status CrxUpdateService::GetServiceStatus(
vasiliibc47245d82015-04-14 08:59:121018 CrxUpdateItem::Status status) {
1019 switch (status) {
1020 case CrxUpdateItem::kChecking:
1021 case CrxUpdateItem::kCanUpdate:
1022 case CrxUpdateItem::kDownloadingDiff:
1023 case CrxUpdateItem::kDownloading:
1024 case CrxUpdateItem::kUpdatingDiff:
1025 case CrxUpdateItem::kUpdating:
1026 return kInProgress;
1027 case CrxUpdateItem::kNew:
1028 case CrxUpdateItem::kUpdated:
1029 case CrxUpdateItem::kUpToDate:
1030 case CrxUpdateItem::kNoUpdate:
1031 return kOk;
1032 case CrxUpdateItem::kLastStatus:
1033 NOTREACHED() << status;
[email protected]68bf09e2014-06-03 00:10:561034 }
vasiliibc47245d82015-04-14 08:59:121035 return kError;
[email protected]68bf09e2014-06-03 00:10:561036}
1037
[email protected]00a77fa2013-11-02 04:18:461038///////////////////////////////////////////////////////////////////////////////
1039
[email protected]e8f96ff2011-08-03 05:07:331040// The component update factory. Using the component updater as a singleton
1041// is the job of the browser process.
vasiliibc47245d82015-04-14 08:59:121042ComponentUpdateService* ComponentUpdateServiceFactory(Configurator* config) {
[email protected]e8f96ff2011-08-03 05:07:331043 DCHECK(config);
vasiliibc47245d82015-04-14 08:59:121044 return new CrxUpdateService(config);
[email protected]e8f96ff2011-08-03 05:07:331045}
[email protected]2cddef42013-11-22 08:23:221046
[email protected]055981f2014-01-17 20:22:321047} // namespace component_updater