blob: 5d6e71d80c3076bba1877c79a862c13307fe1793 [file] [log] [blame]
[email protected]93e8e2c2014-01-04 12:29:231// Copyright 2012 The Chromium Authors. All rights reserved.
[email protected]e8f96ff2011-08-03 05:07:332// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/component_updater/component_updater_service.h"
6
7#include <algorithm>
[email protected]ccb4feef2013-02-14 06:16:478#include <set>
[email protected]e8f96ff2011-08-03 05:07:339#include <vector>
10
11#include "base/at_exit.h"
[email protected]44da56e2011-11-21 19:59:1412#include "base/bind.h"
[email protected]e3e696d32013-06-21 20:41:3613#include "base/compiler_specific.h"
[email protected]e8f96ff2011-08-03 05:07:3314#include "base/file_util.h"
[email protected]57999812013-02-24 05:40:5215#include "base/files/file_path.h"
[email protected]e8f96ff2011-08-03 05:07:3316#include "base/logging.h"
[email protected]7226b33c2011-08-18 08:44:2217#include "base/memory/scoped_ptr.h"
[email protected]00a77fa2013-11-02 04:18:4618#include "base/memory/weak_ptr.h"
[email protected]d3268fe2014-04-25 02:14:2319#include "base/observer_list.h"
[email protected]f5d27e32014-01-31 06:48:5320#include "base/sequenced_task_runner.h"
[email protected]e8f96ff2011-08-03 05:07:3321#include "base/stl_util.h"
[email protected]8f5f2ea2013-10-31 09:39:1022#include "base/threading/sequenced_worker_pool.h"
[email protected]41a17c52013-06-28 00:27:5323#include "base/timer/timer.h"
[email protected]e8f96ff2011-08-03 05:07:3324#include "chrome/browser/browser_process.h"
25#include "chrome/browser/component_updater/component_unpacker.h"
[email protected]7b0529242013-07-20 05:45:4626#include "chrome/browser/component_updater/component_updater_ping_manager.h"
[email protected]2cddef42013-11-22 08:23:2227#include "chrome/browser/component_updater/component_updater_utils.h"
[email protected]afa378f22013-12-02 03:37:5428#include "chrome/browser/component_updater/crx_downloader.h"
[email protected]7b0529242013-07-20 05:45:4629#include "chrome/browser/component_updater/crx_update_item.h"
[email protected]93e8e2c2014-01-04 12:29:2330#include "chrome/browser/component_updater/update_checker.h"
[email protected]6268d3a2013-11-27 01:28:0931#include "chrome/browser/component_updater/update_response.h"
[email protected]e8f96ff2011-08-03 05:07:3332#include "chrome/common/chrome_version_info.h"
[email protected]b7b63872013-01-03 02:41:1933#include "content/public/browser/browser_thread.h"
[email protected]00a77fa2013-11-02 04:18:4634#include "content/public/browser/resource_controller.h"
35#include "content/public/browser/resource_throttle.h"
[email protected]761fa4702013-07-02 15:25:1536#include "url/gurl.h"
[email protected]e8f96ff2011-08-03 05:07:3337
[email protected]631bb742011-11-02 11:29:3938using content::BrowserThread;
39
[email protected]055981f2014-01-17 20:22:3240namespace component_updater {
[email protected]3a0092d2013-12-18 03:04:3541
[email protected]44da56e2011-11-21 19:59:1442// The component updater is designed to live until process shutdown, so
43// base::Bind() calls are not refcounted.
44
[email protected]e8f96ff2011-08-03 05:07:3345namespace {
[email protected]e3e696d32013-06-21 20:41:3646
[email protected]2cddef42013-11-22 08:23:2247// Returns true if the |proposed| version is newer than |current| version.
[email protected]c5e4a2222014-01-03 16:06:1348bool IsVersionNewer(const Version& current, const std::string& proposed) {
49 Version proposed_ver(proposed);
[email protected]2cddef42013-11-22 08:23:2250 return proposed_ver.IsValid() && current.CompareTo(proposed_ver) < 0;
[email protected]e8f96ff2011-08-03 05:07:3351}
52
[email protected]e3e696d32013-06-21 20:41:3653// Returns true if a differential update is available, it has not failed yet,
54// and the configuration allows it.
55bool CanTryDiffUpdate(const CrxUpdateItem* update_item,
56 const ComponentUpdateService::Configurator& config) {
[email protected]055981f2014-01-17 20:22:3257 return HasDiffUpdate(update_item) &&
[email protected]e3e696d32013-06-21 20:41:3658 !update_item->diff_update_failed &&
59 config.DeltasEnabled();
60}
61
[email protected]3a0092d2013-12-18 03:04:3562void AppendDownloadMetrics(
63 const std::vector<CrxDownloader::DownloadMetrics>& source,
64 std::vector<CrxDownloader::DownloadMetrics>* destination) {
65 destination->insert(destination->end(), source.begin(), source.end());
66}
67
[email protected]7b0529242013-07-20 05:45:4668} // namespace
69
70CrxUpdateItem::CrxUpdateItem()
71 : status(kNew),
[email protected]61aca4cd2013-10-26 10:50:5972 on_demand(false),
[email protected]7b0529242013-07-20 05:45:4673 diff_update_failed(false),
74 error_category(0),
75 error_code(0),
76 extra_code1(0),
77 diff_error_category(0),
78 diff_error_code(0),
79 diff_extra_code1(0) {
80}
81
82CrxUpdateItem::~CrxUpdateItem() {
83}
[email protected]86550a42013-06-21 15:20:4984
[email protected]dc06f0b2013-01-23 20:03:1685CrxComponent::CrxComponent()
[email protected]85e61d52013-08-01 22:23:4286 : installer(NULL),
[email protected]cfd13e52014-02-05 09:35:0787 allow_background_download(true) {
[email protected]dc06f0b2013-01-23 20:03:1688}
89
90CrxComponent::~CrxComponent() {
91}
[email protected]e8f96ff2011-08-03 05:07:3392
[email protected]2e919ddd2013-08-21 05:05:1793CrxComponentInfo::CrxComponentInfo() {
94}
95
96CrxComponentInfo::~CrxComponentInfo() {
97}
98
[email protected]00a77fa2013-11-02 04:18:4699///////////////////////////////////////////////////////////////////////////////
100// In charge of blocking url requests until the |crx_id| component has been
101// updated. This class is touched solely from the IO thread. The UI thread
102// can post tasks to it via weak pointers. By default the request is blocked
103// unless the CrxUpdateService calls Unblock().
104// The lifetime is controlled by Chrome's resource loader so the component
105// updater cannot touch objects from this class except via weak pointers.
106class CUResourceThrottle
107 : public content::ResourceThrottle,
108 public base::SupportsWeakPtr<CUResourceThrottle> {
109 public:
110 explicit CUResourceThrottle(const net::URLRequest* request);
111 virtual ~CUResourceThrottle();
[email protected]2cddef42013-11-22 08:23:22112
[email protected]00a77fa2013-11-02 04:18:46113 // Overriden from ResourceThrottle.
114 virtual void WillStartRequest(bool* defer) OVERRIDE;
115 virtual void WillRedirectRequest(const GURL& new_url, bool* defer) OVERRIDE;
[email protected]f8fe5cf2013-12-04 20:11:53116 virtual const char* GetNameForLogging() const OVERRIDE;
[email protected]00a77fa2013-11-02 04:18:46117
118 // Component updater calls this function via PostTask to unblock the request.
119 void Unblock();
120
121 typedef std::vector<base::WeakPtr<CUResourceThrottle> > WeakPtrVector;
122
123 private:
[email protected]2cddef42013-11-22 08:23:22124 enum State {
125 NEW,
126 BLOCKED,
127 UNBLOCKED
128 };
[email protected]00a77fa2013-11-02 04:18:46129
[email protected]2cddef42013-11-22 08:23:22130 State state_;
[email protected]00a77fa2013-11-02 04:18:46131};
132
133void UnblockResourceThrottle(base::WeakPtr<CUResourceThrottle> rt) {
134 BrowserThread::PostTask(
135 BrowserThread::IO,
136 FROM_HERE,
137 base::Bind(&CUResourceThrottle::Unblock, rt));
138}
139
140void UnblockandReapAllThrottles(CUResourceThrottle::WeakPtrVector* throttles) {
141 CUResourceThrottle::WeakPtrVector::iterator it;
142 for (it = throttles->begin(); it != throttles->end(); ++it)
143 UnblockResourceThrottle(*it);
144 throttles->clear();
145}
146
[email protected]e8f96ff2011-08-03 05:07:33147//////////////////////////////////////////////////////////////////////////////
148// The one and only implementation of the ComponentUpdateService interface. In
149// charge of running the show. The main method is ProcessPendingItems() which
[email protected]50b01392012-09-01 03:33:13150// is called periodically to do the upgrades/installs or the update checks.
[email protected]e8f96ff2011-08-03 05:07:33151// An important consideration here is to be as "low impact" as we can to the
152// rest of the browser, so even if we have many components registered and
[email protected]f1050432012-02-15 01:35:46153// eligible for update, we only do one thing at a time with pauses in between
[email protected]e8f96ff2011-08-03 05:07:33154// the tasks. Also when we do network requests there is only one |url_fetcher_|
[email protected]e3e696d32013-06-21 20:41:36155// in flight at a time.
[email protected]e8f96ff2011-08-03 05:07:33156// There are no locks in this code, the main structure |work_items_| is mutated
[email protected]74be2642014-02-07 09:40:37157// only from the UI thread. The unpack and installation is done in a blocking
158// pool thread. The network requests are done in the IO thread or in the file
[email protected]e8f96ff2011-08-03 05:07:33159// thread.
160class CrxUpdateService : public ComponentUpdateService {
161 public:
162 explicit CrxUpdateService(ComponentUpdateService::Configurator* config);
[email protected]e8f96ff2011-08-03 05:07:33163 virtual ~CrxUpdateService();
164
165 // Overrides for ComponentUpdateService.
[email protected]d3268fe2014-04-25 02:14:23166 virtual void AddObserver(Observer* observer) OVERRIDE;
167 virtual void RemoveObserver(Observer* observer) OVERRIDE;
[email protected]e8f96ff2011-08-03 05:07:33168 virtual Status Start() OVERRIDE;
169 virtual Status Stop() OVERRIDE;
170 virtual Status RegisterComponent(const CrxComponent& component) OVERRIDE;
[email protected]61aca4cd2013-10-26 10:50:59171 virtual Status OnDemandUpdate(const std::string& component_id) OVERRIDE;
[email protected]2e919ddd2013-08-21 05:05:17172 virtual void GetComponents(
173 std::vector<CrxComponentInfo>* components) OVERRIDE;
[email protected]00a77fa2013-11-02 04:18:46174 virtual content::ResourceThrottle* GetOnDemandResourceThrottle(
175 net::URLRequest* request, const std::string& crx_id) OVERRIDE;
[email protected]e8f96ff2011-08-03 05:07:33176
[email protected]93e8e2c2014-01-04 12:29:23177 // Context for a crx download url request.
178 struct CRXContext {
179 ComponentInstaller* installer;
180 std::vector<uint8> pk_hash;
181 std::string id;
182 std::string fingerprint;
183 CRXContext() : installer(NULL) {}
184 };
[email protected]e8f96ff2011-08-03 05:07:33185
[email protected]e8f96ff2011-08-03 05:07:33186 private:
[email protected]7b0529242013-07-20 05:45:46187 enum ErrorCategory {
188 kErrorNone = 0,
189 kNetworkError,
190 kUnpackError,
191 kInstallError,
192 };
193
[email protected]32a6c8382013-08-20 00:29:20194 enum StepDelayInterval {
195 kStepDelayShort = 0,
196 kStepDelayMedium,
197 kStepDelayLong,
198 };
199
[email protected]055981f2014-01-17 20:22:32200 void UpdateCheckComplete(int error,
201 const std::string& error_message,
202 const UpdateResponse::Results& results);
203 void OnUpdateCheckSucceeded(const UpdateResponse::Results& results);
[email protected]93e8e2c2014-01-04 12:29:23204 void OnUpdateCheckFailed(int error, const std::string& error_message);
[email protected]e8f96ff2011-08-03 05:07:33205
[email protected]3cb2a4f2013-12-07 21:54:34206 void DownloadComplete(
207 scoped_ptr<CRXContext> crx_context,
[email protected]3a0092d2013-12-18 03:04:35208 const CrxDownloader::Result& download_result);
[email protected]afa378f22013-12-02 03:37:54209
[email protected]00a77fa2013-11-02 04:18:46210 Status OnDemandUpdateInternal(CrxUpdateItem* item);
211
[email protected]e8f96ff2011-08-03 05:07:33212 void ProcessPendingItems();
213
[email protected]93e8e2c2014-01-04 12:29:23214 // Find a component that is ready to update.
215 CrxUpdateItem* FindReadyComponent() const;
216
217 // Prepares the components for an update check and initiates the request.
218 // Returns true if an update check request has been made. Returns false if
219 // no update check was needed or an error occured.
220 bool CheckForUpdates();
[email protected]61aca4cd2013-10-26 10:50:59221
222 void UpdateComponent(CrxUpdateItem* workitem);
223
[email protected]32a6c8382013-08-20 00:29:20224 void ScheduleNextRun(StepDelayInterval step_delay);
[email protected]e8f96ff2011-08-03 05:07:33225
[email protected]6268d3a2013-11-27 01:28:09226 void ParseResponse(const std::string& xml);
[email protected]e8f96ff2011-08-03 05:07:33227
[email protected]afa378f22013-12-02 03:37:54228 void Install(scoped_ptr<CRXContext> context, const base::FilePath& crx_path);
[email protected]e8f96ff2011-08-03 05:07:33229
[email protected]f5d27e32014-01-31 06:48:53230 void EndUnpacking(const std::string& component_id,
231 const base::FilePath& crx_path,
232 ComponentUnpacker::Error error,
233 int extended_error);
234
[email protected]07f93af12011-08-17 20:57:22235 void DoneInstalling(const std::string& component_id,
[email protected]e3e696d32013-06-21 20:41:36236 ComponentUnpacker::Error error,
237 int extended_error);
[email protected]e8f96ff2011-08-03 05:07:33238
[email protected]61aca4cd2013-10-26 10:50:59239 void ChangeItemState(CrxUpdateItem* item, CrxUpdateItem::Status to);
240
[email protected]e8f96ff2011-08-03 05:07:33241 size_t ChangeItemStatus(CrxUpdateItem::Status from,
242 CrxUpdateItem::Status to);
243
244 CrxUpdateItem* FindUpdateItemById(const std::string& id);
245
[email protected]d3268fe2014-04-25 02:14:23246 void NotifyObservers(Observer::Events event, const std::string& id);
[email protected]85e61d52013-08-01 22:23:42247
[email protected]61aca4cd2013-10-26 10:50:59248 bool HasOnDemandItems() const;
249
[email protected]00a77fa2013-11-02 04:18:46250 void OnNewResourceThrottle(base::WeakPtr<CUResourceThrottle> rt,
251 const std::string& crx_id);
252
[email protected]e3e696d32013-06-21 20:41:36253 scoped_ptr<ComponentUpdateService::Configurator> config_;
254
[email protected]055981f2014-01-17 20:22:32255 scoped_ptr<UpdateChecker> update_checker_;
[email protected]e8f96ff2011-08-03 05:07:33256
[email protected]055981f2014-01-17 20:22:32257 scoped_ptr<PingManager> ping_manager_;
[email protected]7b0529242013-07-20 05:45:46258
[email protected]94a481b2014-03-28 19:41:55259 scoped_refptr<ComponentUnpacker> unpacker_;
[email protected]f5d27e32014-01-31 06:48:53260
[email protected]055981f2014-01-17 20:22:32261 scoped_ptr<CrxDownloader> crx_downloader_;
[email protected]afa378f22013-12-02 03:37:54262
[email protected]86550a42013-06-21 15:20:49263 // A collection of every work item.
[email protected]e3e696d32013-06-21 20:41:36264 typedef std::vector<CrxUpdateItem*> UpdateItems;
[email protected]e8f96ff2011-08-03 05:07:33265 UpdateItems work_items_;
266
267 base::OneShotTimer<CrxUpdateService> timer_;
268
[email protected]8f5f2ea2013-10-31 09:39:10269 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
270
[email protected]c5e4a2222014-01-03 16:06:13271 const Version chrome_version_;
[email protected]e8f96ff2011-08-03 05:07:33272
273 bool running_;
274
[email protected]d3268fe2014-04-25 02:14:23275 ObserverList<Observer> observer_list_;
276
[email protected]e8f96ff2011-08-03 05:07:33277 DISALLOW_COPY_AND_ASSIGN(CrxUpdateService);
278};
279
[email protected]e8f96ff2011-08-03 05:07:33280//////////////////////////////////////////////////////////////////////////////
281
[email protected]e3e696d32013-06-21 20:41:36282CrxUpdateService::CrxUpdateService(ComponentUpdateService::Configurator* config)
[email protected]e8f96ff2011-08-03 05:07:33283 : config_(config),
[email protected]055981f2014-01-17 20:22:32284 ping_manager_(new PingManager(config->PingUrl(),
285 config->RequestContext())),
[email protected]8f5f2ea2013-10-31 09:39:10286 blocking_task_runner_(BrowserThread::GetBlockingPool()->
287 GetSequencedTaskRunnerWithShutdownBehavior(
288 BrowserThread::GetBlockingPool()->GetSequenceToken(),
289 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)),
[email protected]e8f96ff2011-08-03 05:07:33290 chrome_version_(chrome::VersionInfo().Version()),
291 running_(false) {
[email protected]1ea21ad2013-09-02 04:20:39292}
[email protected]e8f96ff2011-08-03 05:07:33293
294CrxUpdateService::~CrxUpdateService() {
295 // Because we are a singleton, at this point only the UI thread should be
296 // alive, this simplifies the management of the work that could be in
297 // flight in other threads.
298 Stop();
299 STLDeleteElements(&work_items_);
[email protected]1ea21ad2013-09-02 04:20:39300}
[email protected]e8f96ff2011-08-03 05:07:33301
[email protected]d3268fe2014-04-25 02:14:23302void CrxUpdateService::AddObserver(Observer* observer) {
303 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
304 observer_list_.AddObserver(observer);
305}
306
307void CrxUpdateService::RemoveObserver(Observer* observer) {
308 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
309 observer_list_.RemoveObserver(observer);
310}
311
[email protected]e8f96ff2011-08-03 05:07:33312ComponentUpdateService::Status CrxUpdateService::Start() {
313 // Note that RegisterComponent will call Start() when the first
314 // component is registered, so it can be called twice. This way
315 // we avoid scheduling the timer if there is no work to do.
[email protected]fb53e652014-04-30 11:27:19316 VLOG(1) << "CrxUpdateService starting up";
[email protected]e8f96ff2011-08-03 05:07:33317 running_ = true;
318 if (work_items_.empty())
319 return kOk;
320
[email protected]d3268fe2014-04-25 02:14:23321 NotifyObservers(Observer::COMPONENT_UPDATER_STARTED, "");
[email protected]85e61d52013-08-01 22:23:42322
[email protected]fb53e652014-04-30 11:27:19323 VLOG(1) << "First update attempt will take place in "
324 << config_->InitialDelay() << " seconds";
[email protected]d323a172011-09-02 18:23:02325 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(config_->InitialDelay()),
[email protected]e8f96ff2011-08-03 05:07:33326 this, &CrxUpdateService::ProcessPendingItems);
327 return kOk;
328}
329
330// Stop the main check + update loop. In flight operations will be
331// completed.
332ComponentUpdateService::Status CrxUpdateService::Stop() {
[email protected]fb53e652014-04-30 11:27:19333 VLOG(1) << "CrxUpdateService stopping";
[email protected]e8f96ff2011-08-03 05:07:33334 running_ = false;
335 timer_.Stop();
336 return kOk;
337}
338
[email protected]61aca4cd2013-10-26 10:50:59339bool CrxUpdateService::HasOnDemandItems() const {
340 class Helper {
341 public:
342 static bool IsOnDemand(CrxUpdateItem* item) {
343 return item->on_demand;
344 }
345 };
346 return std::find_if(work_items_.begin(),
347 work_items_.end(),
348 Helper::IsOnDemand) != work_items_.end();
349}
350
[email protected]ccb4feef2013-02-14 06:16:47351// This function sets the timer which will call ProcessPendingItems() or
[email protected]61aca4cd2013-10-26 10:50:59352// ProcessRequestedItem() if there is an on_demand item. There
[email protected]32a6c8382013-08-20 00:29:20353// are three kinds of waits:
354// - a short delay, when there is immediate work to be done.
355// - a medium delay, when there are updates to be applied within the current
356// update cycle, or there are components that are still unchecked.
357// - a long delay when a full check/update cycle has completed for all
358// components.
359void CrxUpdateService::ScheduleNextRun(StepDelayInterval step_delay) {
[email protected]e8f96ff2011-08-03 05:07:33360 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23361 DCHECK(!update_checker_);
[email protected]e8f96ff2011-08-03 05:07:33362 CHECK(!timer_.IsRunning());
363 // It could be the case that Stop() had been called while a url request
364 // or unpacking was in flight, if so we arrive here but |running_| is
365 // false. In that case do not loop again.
366 if (!running_)
367 return;
368
[email protected]ccb4feef2013-02-14 06:16:47369 // Keep the delay short if in the middle of an update (step_delay),
370 // or there are new requested_work_items_ that have not been processed yet.
[email protected]32a6c8382013-08-20 00:29:20371 int64 delay_seconds = 0;
[email protected]61aca4cd2013-10-26 10:50:59372 if (!HasOnDemandItems()) {
[email protected]32a6c8382013-08-20 00:29:20373 switch (step_delay) {
374 case kStepDelayShort:
375 delay_seconds = config_->StepDelay();
376 break;
377 case kStepDelayMedium:
378 delay_seconds = config_->StepDelayMedium();
379 break;
380 case kStepDelayLong:
381 delay_seconds = config_->NextCheckDelay();
382 break;
383 }
384 } else {
385 delay_seconds = config_->StepDelay();
386 }
[email protected]cf442612011-08-09 20:20:12387
[email protected]32a6c8382013-08-20 00:29:20388 if (step_delay != kStepDelayShort) {
[email protected]d3268fe2014-04-25 02:14:23389 NotifyObservers(Observer::COMPONENT_UPDATER_SLEEPING, "");
[email protected]85e61d52013-08-01 22:23:42390
[email protected]e8f96ff2011-08-03 05:07:33391 // Zero is only used for unit tests.
[email protected]32a6c8382013-08-20 00:29:20392 if (0 == delay_seconds)
[email protected]e8f96ff2011-08-03 05:07:33393 return;
394 }
395
[email protected]fb53e652014-04-30 11:27:19396 VLOG(1) << "Scheduling next run to occur in " << delay_seconds << " seconds";
[email protected]32a6c8382013-08-20 00:29:20397 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(delay_seconds),
[email protected]e8f96ff2011-08-03 05:07:33398 this, &CrxUpdateService::ProcessPendingItems);
399}
400
401// Given a extension-like component id, find the associated component.
402CrxUpdateItem* CrxUpdateService::FindUpdateItemById(const std::string& id) {
403 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
404 CrxUpdateItem::FindById finder(id);
405 UpdateItems::iterator it = std::find_if(work_items_.begin(),
406 work_items_.end(),
407 finder);
[email protected]93e8e2c2014-01-04 12:29:23408 return it != work_items_.end() ? *it : NULL;
[email protected]e8f96ff2011-08-03 05:07:33409}
410
[email protected]61aca4cd2013-10-26 10:50:59411// Changes a component's status, clearing on_demand and firing notifications as
412// necessary. By convention, this is the only function that can change a
413// CrxUpdateItem's |status|.
414// TODO(waffles): Do we want to add DCHECKS for valid state transitions here?
415void CrxUpdateService::ChangeItemState(CrxUpdateItem* item,
416 CrxUpdateItem::Status to) {
417 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
418 if (to == CrxUpdateItem::kNoUpdate ||
419 to == CrxUpdateItem::kUpdated ||
420 to == CrxUpdateItem::kUpToDate) {
421 item->on_demand = false;
422 }
423
424 item->status = to;
425
[email protected]d3268fe2014-04-25 02:14:23426 switch (to) {
427 case CrxUpdateItem::kCanUpdate:
428 NotifyObservers(Observer::COMPONENT_UPDATE_FOUND, item->id);
429 break;
430 case CrxUpdateItem::kUpdatingDiff:
431 case CrxUpdateItem::kUpdating:
432 NotifyObservers(Observer::COMPONENT_UPDATE_READY, item->id);
433 break;
434 case CrxUpdateItem::kUpdated:
435 NotifyObservers(Observer::COMPONENT_UPDATED, item->id);
436 break;
437 case CrxUpdateItem::kUpToDate:
438 case CrxUpdateItem::kNoUpdate:
439 NotifyObservers(Observer::COMPONENT_NOT_UPDATED, item->id);
440 break;
441 case CrxUpdateItem::kNew:
442 case CrxUpdateItem::kChecking:
443 case CrxUpdateItem::kDownloading:
444 case CrxUpdateItem::kDownloadingDiff:
445 case CrxUpdateItem::kLastStatus:
446 // No notification for these states.
447 break;
[email protected]61aca4cd2013-10-26 10:50:59448 }
[email protected]00a77fa2013-11-02 04:18:46449
450 // Free possible pending network requests.
451 if ((to == CrxUpdateItem::kUpdated) ||
452 (to == CrxUpdateItem::kUpToDate) ||
453 (to == CrxUpdateItem::kNoUpdate)) {
454 UnblockandReapAllThrottles(&item->throttles);
455 }
[email protected]61aca4cd2013-10-26 10:50:59456}
457
[email protected]e8f96ff2011-08-03 05:07:33458// Changes all the components in |work_items_| that have |from| status to
[email protected]e3e696d32013-06-21 20:41:36459// |to| status and returns how many have been changed.
[email protected]e8f96ff2011-08-03 05:07:33460size_t CrxUpdateService::ChangeItemStatus(CrxUpdateItem::Status from,
461 CrxUpdateItem::Status to) {
462 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
463 size_t count = 0;
464 for (UpdateItems::iterator it = work_items_.begin();
465 it != work_items_.end(); ++it) {
466 CrxUpdateItem* item = *it;
[email protected]93e8e2c2014-01-04 12:29:23467 if (item->status == from) {
468 ChangeItemState(item, to);
469 ++count;
470 }
[email protected]e8f96ff2011-08-03 05:07:33471 }
472 return count;
473}
474
475// Adds a component to be checked for upgrades. If the component exists it
476// it will be replaced and the return code is kReplaced.
[email protected]e8f96ff2011-08-03 05:07:33477ComponentUpdateService::Status CrxUpdateService::RegisterComponent(
478 const CrxComponent& component) {
479 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
480 if (component.pk_hash.empty() ||
481 !component.version.IsValid() ||
482 !component.installer)
483 return kError;
484
[email protected]055981f2014-01-17 20:22:32485 std::string id(GetCrxComponentID(component));
[email protected]93e8e2c2014-01-04 12:29:23486 CrxUpdateItem* uit = FindUpdateItemById(id);
[email protected]e8f96ff2011-08-03 05:07:33487 if (uit) {
488 uit->component = component;
489 return kReplaced;
490 }
491
492 uit = new CrxUpdateItem;
493 uit->id.swap(id);
494 uit->component = component;
[email protected]e3e696d32013-06-21 20:41:36495
[email protected]e8f96ff2011-08-03 05:07:33496 work_items_.push_back(uit);
497 // If this is the first component registered we call Start to
498 // schedule the first timer.
499 if (running_ && (work_items_.size() == 1))
500 Start();
501
502 return kOk;
503}
504
[email protected]ccb4feef2013-02-14 06:16:47505// Start the process of checking for an update, for a particular component
506// that was previously registered.
[email protected]2e919ddd2013-08-21 05:05:17507// |component_id| is a value returned from GetCrxComponentID().
[email protected]61aca4cd2013-10-26 10:50:59508ComponentUpdateService::Status CrxUpdateService::OnDemandUpdate(
[email protected]2e919ddd2013-08-21 05:05:17509 const std::string& component_id) {
[email protected]00a77fa2013-11-02 04:18:46510 return OnDemandUpdateInternal(FindUpdateItemById(component_id));
511}
512
513ComponentUpdateService::Status CrxUpdateService::OnDemandUpdateInternal(
514 CrxUpdateItem* uit) {
[email protected]ccb4feef2013-02-14 06:16:47515 if (!uit)
516 return kError;
[email protected]2cddef42013-11-22 08:23:22517
[email protected]ccb4feef2013-02-14 06:16:47518 // Check if the request is too soon.
519 base::TimeDelta delta = base::Time::Now() - uit->last_check;
[email protected]e3e696d32013-06-21 20:41:36520 if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay()))
[email protected]ccb4feef2013-02-14 06:16:47521 return kError;
[email protected]ccb4feef2013-02-14 06:16:47522
523 switch (uit->status) {
524 // If the item is already in the process of being updated, there is
525 // no point in this call, so return kInProgress.
526 case CrxUpdateItem::kChecking:
527 case CrxUpdateItem::kCanUpdate:
[email protected]e3e696d32013-06-21 20:41:36528 case CrxUpdateItem::kDownloadingDiff:
[email protected]ccb4feef2013-02-14 06:16:47529 case CrxUpdateItem::kDownloading:
[email protected]e3e696d32013-06-21 20:41:36530 case CrxUpdateItem::kUpdatingDiff:
[email protected]ccb4feef2013-02-14 06:16:47531 case CrxUpdateItem::kUpdating:
532 return kInProgress;
533 // Otherwise the item was already checked a while back (or it is new),
534 // set its status to kNew to give it a slightly higher priority.
535 case CrxUpdateItem::kNew:
536 case CrxUpdateItem::kUpdated:
537 case CrxUpdateItem::kUpToDate:
538 case CrxUpdateItem::kNoUpdate:
[email protected]61aca4cd2013-10-26 10:50:59539 ChangeItemState(uit, CrxUpdateItem::kNew);
540 uit->on_demand = true;
[email protected]ccb4feef2013-02-14 06:16:47541 break;
542 case CrxUpdateItem::kLastStatus:
543 NOTREACHED() << uit->status;
544 }
545
546 // In case the current delay is long, set the timer to a shorter value
547 // to get the ball rolling.
548 if (timer_.IsRunning()) {
549 timer_.Stop();
550 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(config_->StepDelay()),
551 this, &CrxUpdateService::ProcessPendingItems);
552 }
553
554 return kOk;
555}
556
[email protected]2e919ddd2013-08-21 05:05:17557void CrxUpdateService::GetComponents(
558 std::vector<CrxComponentInfo>* components) {
559 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
560 for (UpdateItems::const_iterator it = work_items_.begin();
561 it != work_items_.end(); ++it) {
562 const CrxUpdateItem* item = *it;
563 CrxComponentInfo info;
[email protected]055981f2014-01-17 20:22:32564 info.id = GetCrxComponentID(item->component);
[email protected]2e919ddd2013-08-21 05:05:17565 info.version = item->component.version.GetString();
566 info.name = item->component.name;
567 components->push_back(info);
568 }
569}
570
[email protected]93e8e2c2014-01-04 12:29:23571// This is the main loop of the component updater. It updates one component
572// at a time if updates are available. Otherwise, it does an update check or
573// takes a long sleep until the loop runs again.
[email protected]e8f96ff2011-08-03 05:07:33574void CrxUpdateService::ProcessPendingItems() {
575 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23576
[email protected]61aca4cd2013-10-26 10:50:59577 CrxUpdateItem* ready_upgrade = FindReadyComponent();
578 if (ready_upgrade) {
579 UpdateComponent(ready_upgrade);
[email protected]e8f96ff2011-08-03 05:07:33580 return;
581 }
[email protected]93e8e2c2014-01-04 12:29:23582
583 if (!CheckForUpdates())
584 ScheduleNextRun(kStepDelayLong);
[email protected]61aca4cd2013-10-26 10:50:59585}
586
[email protected]93e8e2c2014-01-04 12:29:23587CrxUpdateItem* CrxUpdateService::FindReadyComponent() const {
[email protected]c4b4cfa2014-03-10 18:55:00588 class Helper {
589 public:
590 static bool IsReadyOnDemand(CrxUpdateItem* item) {
591 return item->on_demand && IsReady(item);
[email protected]61aca4cd2013-10-26 10:50:59592 }
[email protected]c4b4cfa2014-03-10 18:55:00593 static bool IsReady(CrxUpdateItem* item) {
594 return item->status == CrxUpdateItem::kCanUpdate;
595 }
596 };
[email protected]61aca4cd2013-10-26 10:50:59597
[email protected]c4b4cfa2014-03-10 18:55:00598 std::vector<CrxUpdateItem*>::const_iterator it = std::find_if(
599 work_items_.begin(), work_items_.end(), Helper::IsReadyOnDemand);
600 if (it != work_items_.end())
601 return *it;
602 it = std::find_if(work_items_.begin(), work_items_.end(), Helper::IsReady);
603 if (it != work_items_.end())
604 return *it;
[email protected]61aca4cd2013-10-26 10:50:59605 return NULL;
606}
607
[email protected]93e8e2c2014-01-04 12:29:23608// Prepares the components for an update check and initiates the request.
[email protected]21a9c9a2014-02-19 19:37:11609// On demand components are always included in the update check request.
610// Otherwise, only include components that have not been checked recently.
[email protected]93e8e2c2014-01-04 12:29:23611bool CrxUpdateService::CheckForUpdates() {
[email protected]21a9c9a2014-02-19 19:37:11612 const base::TimeDelta minimum_recheck_wait_time =
613 base::TimeDelta::FromSeconds(config_->MinimumReCheckWait());
614 const base::Time now(base::Time::Now());
615
[email protected]93e8e2c2014-01-04 12:29:23616 std::vector<CrxUpdateItem*> items_to_check;
617 for (size_t i = 0; i != work_items_.size(); ++i) {
618 CrxUpdateItem* item = work_items_[i];
619 DCHECK(item->status == CrxUpdateItem::kNew ||
620 item->status == CrxUpdateItem::kNoUpdate ||
621 item->status == CrxUpdateItem::kUpToDate ||
622 item->status == CrxUpdateItem::kUpdated);
623
[email protected]21a9c9a2014-02-19 19:37:11624 const base::TimeDelta time_since_last_checked(now - item->last_check);
625
626 if (!item->on_demand &&
627 time_since_last_checked < minimum_recheck_wait_time) {
[email protected]fb53e652014-04-30 11:27:19628 VLOG(1) << "Skipping check for component update: id=" << item->id
629 << ", time_since_last_checked="
630 << time_since_last_checked.InSeconds()
631 << " seconds: too soon to check for an update";
[email protected]21a9c9a2014-02-19 19:37:11632 continue;
633 }
634
[email protected]fb53e652014-04-30 11:27:19635 VLOG(1) << "Scheduling update check for component id=" << item->id
636 << ", time_since_last_checked="
637 << time_since_last_checked.InSeconds()
638 << " seconds";
639
[email protected]93e8e2c2014-01-04 12:29:23640 ChangeItemState(item, CrxUpdateItem::kChecking);
641
[email protected]21a9c9a2014-02-19 19:37:11642 item->last_check = now;
[email protected]93e8e2c2014-01-04 12:29:23643 item->crx_urls.clear();
644 item->crx_diffurls.clear();
645 item->previous_version = item->component.version;
646 item->next_version = Version();
647 item->previous_fp = item->component.fingerprint;
648 item->next_fp.clear();
649 item->diff_update_failed = false;
650 item->error_category = 0;
651 item->error_code = 0;
652 item->extra_code1 = 0;
653 item->diff_error_category = 0;
654 item->diff_error_code = 0;
655 item->diff_extra_code1 = 0;
656 item->download_metrics.clear();
657
658 items_to_check.push_back(item);
659 }
660
661 if (items_to_check.empty())
662 return false;
663
[email protected]055981f2014-01-17 20:22:32664 update_checker_ = UpdateChecker::Create(
[email protected]93e8e2c2014-01-04 12:29:23665 config_->UpdateUrl(),
666 config_->RequestContext(),
667 base::Bind(&CrxUpdateService::UpdateCheckComplete,
668 base::Unretained(this))).Pass();
669 return update_checker_->CheckForUpdates(items_to_check,
670 config_->ExtraRequestParams());
671}
672
[email protected]61aca4cd2013-10-26 10:50:59673void CrxUpdateService::UpdateComponent(CrxUpdateItem* workitem) {
[email protected]afa378f22013-12-02 03:37:54674 scoped_ptr<CRXContext> crx_context(new CRXContext);
675 crx_context->pk_hash = workitem->component.pk_hash;
676 crx_context->id = workitem->id;
677 crx_context->installer = workitem->component.installer;
678 crx_context->fingerprint = workitem->next_fp;
[email protected]da37c1d2013-12-19 01:04:38679 const std::vector<GURL>* urls = NULL;
[email protected]cfd13e52014-02-05 09:35:07680 bool allow_background_download = false;
[email protected]61aca4cd2013-10-26 10:50:59681 if (CanTryDiffUpdate(workitem, *config_)) {
[email protected]da37c1d2013-12-19 01:04:38682 urls = &workitem->crx_diffurls;
[email protected]61aca4cd2013-10-26 10:50:59683 ChangeItemState(workitem, CrxUpdateItem::kDownloadingDiff);
684 } else {
[email protected]cfd13e52014-02-05 09:35:07685 // Background downloads are enabled only for selected components and
686 // only for full downloads (see issue 340448).
687 allow_background_download = workitem->component.allow_background_download;
[email protected]da37c1d2013-12-19 01:04:38688 urls = &workitem->crx_urls;
[email protected]61aca4cd2013-10-26 10:50:59689 ChangeItemState(workitem, CrxUpdateItem::kDownloading);
690 }
[email protected]3cb2a4f2013-12-07 21:54:34691
692 // On demand component updates are always downloaded in foreground.
[email protected]cfd13e52014-02-05 09:35:07693 const bool is_background_download =
694 !workitem->on_demand && allow_background_download &&
695 config_->UseBackgroundDownloader();
[email protected]3cb2a4f2013-12-07 21:54:34696
[email protected]1b6587dc52014-04-26 00:38:55697 crx_downloader_.reset(CrxDownloader::Create(is_background_download,
698 config_->RequestContext(),
699 blocking_task_runner_));
700 crx_downloader_->StartDownload(*urls,
701 base::Bind(&CrxUpdateService::DownloadComplete,
702 base::Unretained(this),
703 base::Passed(&crx_context)));
[email protected]61aca4cd2013-10-26 10:50:59704}
705
[email protected]93e8e2c2014-01-04 12:29:23706void CrxUpdateService::UpdateCheckComplete(
707 int error,
708 const std::string& error_message,
[email protected]055981f2014-01-17 20:22:32709 const UpdateResponse::Results& results) {
[email protected]e8f96ff2011-08-03 05:07:33710 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23711 update_checker_.reset();
712 if (!error)
713 OnUpdateCheckSucceeded(results);
[email protected]652f4272013-11-13 09:23:41714 else
[email protected]93e8e2c2014-01-04 12:29:23715 OnUpdateCheckFailed(error, error_message);
[email protected]e8f96ff2011-08-03 05:07:33716}
717
[email protected]93e8e2c2014-01-04 12:29:23718// Handles a valid Omaha update check response by matching the results with
719// the registered components which were checked for updates.
720// If updates are found, prepare the components for the actual version upgrade.
721// One of these components will be drafted for the upgrade next time
722// ProcessPendingItems is called.
723void CrxUpdateService::OnUpdateCheckSucceeded(
[email protected]055981f2014-01-17 20:22:32724 const UpdateResponse::Results& results) {
[email protected]2cddef42013-11-22 08:23:22725 size_t num_updates_pending = 0;
[email protected]e8f96ff2011-08-03 05:07:33726 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]fb53e652014-04-30 11:27:19727 VLOG(1) << "Update check succeeded.";
[email protected]055981f2014-01-17 20:22:32728 std::vector<UpdateResponse::Result>::const_iterator it;
[email protected]e8f96ff2011-08-03 05:07:33729 for (it = results.list.begin(); it != results.list.end(); ++it) {
730 CrxUpdateItem* crx = FindUpdateItemById(it->extension_id);
731 if (!crx)
732 continue;
733
[email protected]2cddef42013-11-22 08:23:22734 if (crx->status != CrxUpdateItem::kChecking) {
735 NOTREACHED();
[email protected]e8f96ff2011-08-03 05:07:33736 continue; // Not updating this component now.
[email protected]2cddef42013-11-22 08:23:22737 }
[email protected]e8f96ff2011-08-03 05:07:33738
[email protected]2cddef42013-11-22 08:23:22739 if (it->manifest.version.empty()) {
[email protected]cf442612011-08-09 20:20:12740 // No version means no update available.
[email protected]61aca4cd2013-10-26 10:50:59741 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]fb53e652014-04-30 11:27:19742 VLOG(1) << "No update available for component: " << crx->id;
[email protected]cf442612011-08-09 20:20:12743 continue;
[email protected]e8f96ff2011-08-03 05:07:33744 }
[email protected]2cddef42013-11-22 08:23:22745
746 if (!IsVersionNewer(crx->component.version, it->manifest.version)) {
747 // The component is up to date.
[email protected]61aca4cd2013-10-26 10:50:59748 ChangeItemState(crx, CrxUpdateItem::kUpToDate);
[email protected]fb53e652014-04-30 11:27:19749 VLOG(1) << "Component already up-to-date: " << crx->id;
[email protected]cf442612011-08-09 20:20:12750 continue;
[email protected]e8f96ff2011-08-03 05:07:33751 }
[email protected]2cddef42013-11-22 08:23:22752
753 if (!it->manifest.browser_min_version.empty()) {
754 if (IsVersionNewer(chrome_version_, it->manifest.browser_min_version)) {
755 // The component is not compatible with this Chrome version.
[email protected]fb53e652014-04-30 11:27:19756 VLOG(1) << "Ignoring incompatible component: " << crx->id;
[email protected]61aca4cd2013-10-26 10:50:59757 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]cf442612011-08-09 20:20:12758 continue;
759 }
[email protected]e8f96ff2011-08-03 05:07:33760 }
[email protected]2cddef42013-11-22 08:23:22761
762 if (it->manifest.packages.size() != 1) {
763 // Assume one and only one package per component.
[email protected]fb53e652014-04-30 11:27:19764 VLOG(1) << "Ignoring multiple packages for component: " << crx->id;
[email protected]2cddef42013-11-22 08:23:22765 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
766 continue;
767 }
768
769 // Parse the members of the result and queue an upgrade for this component.
[email protected]c5e4a2222014-01-03 16:06:13770 crx->next_version = Version(it->manifest.version);
[email protected]2cddef42013-11-22 08:23:22771
[email protected]fb53e652014-04-30 11:27:19772 VLOG(1) << "Update found for component: " << crx->id;
773
[email protected]055981f2014-01-17 20:22:32774 typedef UpdateResponse::Result::Manifest::Package Package;
[email protected]2cddef42013-11-22 08:23:22775 const Package& package(it->manifest.packages[0]);
776 crx->next_fp = package.fingerprint;
777
[email protected]da37c1d2013-12-19 01:04:38778 // Resolve the urls by combining the base urls with the package names.
779 for (size_t i = 0; i != it->crx_urls.size(); ++i) {
780 const GURL url(it->crx_urls[i].Resolve(package.name));
781 if (url.is_valid())
782 crx->crx_urls.push_back(url);
783 }
784 for (size_t i = 0; i != it->crx_diffurls.size(); ++i) {
785 const GURL url(it->crx_diffurls[i].Resolve(package.namediff));
786 if (url.is_valid())
787 crx->crx_diffurls.push_back(url);
788 }
[email protected]2cddef42013-11-22 08:23:22789
[email protected]61aca4cd2013-10-26 10:50:59790 ChangeItemState(crx, CrxUpdateItem::kCanUpdate);
[email protected]2cddef42013-11-22 08:23:22791 ++num_updates_pending;
[email protected]e8f96ff2011-08-03 05:07:33792 }
[email protected]cf442612011-08-09 20:20:12793
[email protected]2cddef42013-11-22 08:23:22794 // All components that are not included in the update response are
795 // considered up to date.
[email protected]cf442612011-08-09 20:20:12796 ChangeItemStatus(CrxUpdateItem::kChecking, CrxUpdateItem::kUpToDate);
797
[email protected]32a6c8382013-08-20 00:29:20798 // If there are updates pending we do a short wait, otherwise we take
799 // a longer delay until we check the components again.
[email protected]21a9c9a2014-02-19 19:37:11800 ScheduleNextRun(num_updates_pending > 0 ? kStepDelayShort : kStepDelayLong);
[email protected]e8f96ff2011-08-03 05:07:33801}
802
[email protected]93e8e2c2014-01-04 12:29:23803// TODO: record UMA stats.
804void CrxUpdateService::OnUpdateCheckFailed(int error,
805 const std::string& error_message) {
[email protected]e8f96ff2011-08-03 05:07:33806 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23807 DCHECK(error);
[email protected]e8f96ff2011-08-03 05:07:33808 size_t count = ChangeItemStatus(CrxUpdateItem::kChecking,
809 CrxUpdateItem::kNoUpdate);
810 DCHECK_GT(count, 0ul);
[email protected]fb53e652014-04-30 11:27:19811 VLOG(1) << "Update check failed.";
[email protected]32a6c8382013-08-20 00:29:20812 ScheduleNextRun(kStepDelayLong);
[email protected]e8f96ff2011-08-03 05:07:33813}
814
815// Called when the CRX package has been downloaded to a temporary location.
816// Here we fire the notifications and schedule the component-specific installer
817// to be called in the file thread.
[email protected]3cb2a4f2013-12-07 21:54:34818void CrxUpdateService::DownloadComplete(
819 scoped_ptr<CRXContext> crx_context,
[email protected]3a0092d2013-12-18 03:04:35820 const CrxDownloader::Result& download_result) {
[email protected]e8f96ff2011-08-03 05:07:33821 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]64f39fa12013-09-03 21:49:37822
[email protected]64f39fa12013-09-03 21:49:37823 CrxUpdateItem* crx = FindUpdateItemById(crx_context->id);
[email protected]e3e696d32013-06-21 20:41:36824 DCHECK(crx->status == CrxUpdateItem::kDownloadingDiff ||
825 crx->status == CrxUpdateItem::kDownloading);
826
[email protected]3a0092d2013-12-18 03:04:35827 AppendDownloadMetrics(crx_downloader_->download_metrics(),
828 &crx->download_metrics);
829
[email protected]3cb2a4f2013-12-07 21:54:34830 if (download_result.error) {
[email protected]e3e696d32013-06-21 20:41:36831 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
[email protected]7b0529242013-07-20 05:45:46832 crx->diff_error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34833 crx->diff_error_code = download_result.error;
[email protected]e630ba62013-06-22 15:22:34834 crx->diff_update_failed = true;
[email protected]e3e696d32013-06-21 20:41:36835 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
836 CrxUpdateItem::kCanUpdate);
837 DCHECK_EQ(count, 1ul);
[email protected]afa378f22013-12-02 03:37:54838 crx_downloader_.reset();
[email protected]7b0529242013-07-20 05:45:46839
[email protected]32a6c8382013-08-20 00:29:20840 ScheduleNextRun(kStepDelayShort);
[email protected]e3e696d32013-06-21 20:41:36841 return;
842 }
[email protected]7b0529242013-07-20 05:45:46843 crx->error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34844 crx->error_code = download_result.error;
[email protected]e8f96ff2011-08-03 05:07:33845 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloading,
846 CrxUpdateItem::kNoUpdate);
847 DCHECK_EQ(count, 1ul);
[email protected]afa378f22013-12-02 03:37:54848 crx_downloader_.reset();
[email protected]e3e696d32013-06-21 20:41:36849
[email protected]7b0529242013-07-20 05:45:46850 // At this point, since both the differential and the full downloads failed,
851 // the update for this component has finished with an error.
852 ping_manager_->OnUpdateComplete(crx);
853
[email protected]32a6c8382013-08-20 00:29:20854 // Move on to the next update, if there is one available.
855 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33856 } else {
[email protected]e3e696d32013-06-21 20:41:36857 size_t count = 0;
858 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
859 count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
860 CrxUpdateItem::kUpdatingDiff);
861 } else {
862 count = ChangeItemStatus(CrxUpdateItem::kDownloading,
863 CrxUpdateItem::kUpdating);
864 }
[email protected]e8f96ff2011-08-03 05:07:33865 DCHECK_EQ(count, 1ul);
[email protected]afa378f22013-12-02 03:37:54866 crx_downloader_.reset();
[email protected]cf442612011-08-09 20:20:12867
[email protected]44da56e2011-11-21 19:59:14868 // Why unretained? See comment at top of file.
[email protected]8f5f2ea2013-10-31 09:39:10869 blocking_task_runner_->PostDelayedTask(
[email protected]73251e72012-03-04 02:10:33870 FROM_HERE,
[email protected]44da56e2011-11-21 19:59:14871 base::Bind(&CrxUpdateService::Install,
872 base::Unretained(this),
[email protected]afa378f22013-12-02 03:37:54873 base::Passed(&crx_context),
[email protected]3cb2a4f2013-12-07 21:54:34874 download_result.response),
[email protected]73251e72012-03-04 02:10:33875 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]e8f96ff2011-08-03 05:07:33876 }
[email protected]e8f96ff2011-08-03 05:07:33877}
878
879// Install consists of digital signature verification, unpacking and then
880// calling the component specific installer. All that is handled by the
[email protected]f5d27e32014-01-31 06:48:53881// |unpacker_|. If there is an error this function is in charge of deleting
[email protected]e8f96ff2011-08-03 05:07:33882// the files created.
[email protected]afa378f22013-12-02 03:37:54883void CrxUpdateService::Install(scoped_ptr<CRXContext> context,
[email protected]650b2d52013-02-10 03:41:45884 const base::FilePath& crx_path) {
[email protected]8f5f2ea2013-10-31 09:39:10885 // This function owns the file at |crx_path| and the |context| object.
[email protected]94a481b2014-03-28 19:41:55886 unpacker_ = new ComponentUnpacker(context->pk_hash,
887 crx_path,
888 context->fingerprint,
889 context->installer,
890 config_->InProcess(),
891 blocking_task_runner_);
[email protected]f5d27e32014-01-31 06:48:53892 unpacker_->Unpack(base::Bind(&CrxUpdateService::EndUnpacking,
893 base::Unretained(this),
894 context->id,
895 crx_path));
896}
897
898void CrxUpdateService::EndUnpacking(const std::string& component_id,
899 const base::FilePath& crx_path,
900 ComponentUnpacker::Error error,
901 int extended_error) {
[email protected]055981f2014-01-17 20:22:32902 if (!DeleteFileAndEmptyParentDirectory(crx_path))
[email protected]e8f96ff2011-08-03 05:07:33903 NOTREACHED() << crx_path.value();
[email protected]73251e72012-03-04 02:10:33904 BrowserThread::PostDelayedTask(
905 BrowserThread::UI,
906 FROM_HERE,
[email protected]44da56e2011-11-21 19:59:14907 base::Bind(&CrxUpdateService::DoneInstalling, base::Unretained(this),
[email protected]f5d27e32014-01-31 06:48:53908 component_id, error, extended_error),
[email protected]73251e72012-03-04 02:10:33909 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]94a481b2014-03-28 19:41:55910 // Reset the unpacker last, otherwise we free our own arguments.
911 unpacker_ = NULL;
[email protected]e8f96ff2011-08-03 05:07:33912}
913
914// Installation has been completed. Adjust the component status and
[email protected]7b0529242013-07-20 05:45:46915// schedule the next check. Schedule a short delay before trying the full
916// update when the differential update failed.
[email protected]07f93af12011-08-17 20:57:22917void CrxUpdateService::DoneInstalling(const std::string& component_id,
[email protected]e3e696d32013-06-21 20:41:36918 ComponentUnpacker::Error error,
919 int extra_code) {
[email protected]e8f96ff2011-08-03 05:07:33920 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]07f93af12011-08-17 20:57:22921
[email protected]7b0529242013-07-20 05:45:46922 ErrorCategory error_category = kErrorNone;
923 switch (error) {
924 case ComponentUnpacker::kNone:
925 break;
926 case ComponentUnpacker::kInstallerError:
927 error_category = kInstallError;
928 break;
929 default:
930 error_category = kUnpackError;
931 break;
932 }
933
934 const bool is_success = error == ComponentUnpacker::kNone;
935
[email protected]07f93af12011-08-17 20:57:22936 CrxUpdateItem* item = FindUpdateItemById(component_id);
[email protected]7b0529242013-07-20 05:45:46937 if (item->status == CrxUpdateItem::kUpdatingDiff && !is_success) {
938 item->diff_error_category = error_category;
939 item->diff_error_code = error;
940 item->diff_extra_code1 = extra_code;
[email protected]1ea21ad2013-09-02 04:20:39941 item->diff_update_failed = true;
942 size_t count = ChangeItemStatus(CrxUpdateItem::kUpdatingDiff,
943 CrxUpdateItem::kCanUpdate);
944 DCHECK_EQ(count, 1ul);
945 ScheduleNextRun(kStepDelayShort);
946 return;
[email protected]e3e696d32013-06-21 20:41:36947 }
[email protected]e3e696d32013-06-21 20:41:36948
[email protected]7b0529242013-07-20 05:45:46949 if (is_success) {
[email protected]61aca4cd2013-10-26 10:50:59950 ChangeItemState(item, CrxUpdateItem::kUpdated);
[email protected]07f93af12011-08-17 20:57:22951 item->component.version = item->next_version;
[email protected]e3e696d32013-06-21 20:41:36952 item->component.fingerprint = item->next_fp;
[email protected]7b0529242013-07-20 05:45:46953 } else {
[email protected]61aca4cd2013-10-26 10:50:59954 ChangeItemState(item, CrxUpdateItem::kNoUpdate);
[email protected]7b0529242013-07-20 05:45:46955 item->error_category = error_category;
956 item->error_code = error;
957 item->extra_code1 = extra_code;
[email protected]e3e696d32013-06-21 20:41:36958 }
[email protected]07f93af12011-08-17 20:57:22959
[email protected]7b0529242013-07-20 05:45:46960 ping_manager_->OnUpdateComplete(item);
[email protected]360b8bb2011-09-01 21:48:06961
[email protected]32a6c8382013-08-20 00:29:20962 // Move on to the next update, if there is one available.
963 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33964}
965
[email protected]d3268fe2014-04-25 02:14:23966void CrxUpdateService::NotifyObservers(Observer::Events event,
967 const std::string& id) {
968 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
969 FOR_EACH_OBSERVER(Observer, observer_list_, OnEvent(event, id));
[email protected]85e61d52013-08-01 22:23:42970}
971
[email protected]00a77fa2013-11-02 04:18:46972content::ResourceThrottle* CrxUpdateService::GetOnDemandResourceThrottle(
973 net::URLRequest* request, const std::string& crx_id) {
974 // We give the raw pointer to the caller, who will delete it at will
975 // and we keep for ourselves a weak pointer to it so we can post tasks
976 // from the UI thread without having to track lifetime directly.
977 CUResourceThrottle* rt = new CUResourceThrottle(request);
978 BrowserThread::PostTask(
979 BrowserThread::UI,
980 FROM_HERE,
981 base::Bind(&CrxUpdateService::OnNewResourceThrottle,
982 base::Unretained(this),
983 rt->AsWeakPtr(),
984 crx_id));
985 return rt;
986}
987
988void CrxUpdateService::OnNewResourceThrottle(
989 base::WeakPtr<CUResourceThrottle> rt, const std::string& crx_id) {
990 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
991 // Check if we can on-demand update, else unblock the request anyway.
992 CrxUpdateItem* item = FindUpdateItemById(crx_id);
993 Status status = OnDemandUpdateInternal(item);
994 if (status == kOk || status == kInProgress) {
995 item->throttles.push_back(rt);
996 return;
997 }
998 UnblockResourceThrottle(rt);
999}
1000
1001///////////////////////////////////////////////////////////////////////////////
1002
1003CUResourceThrottle::CUResourceThrottle(const net::URLRequest* request)
1004 : state_(NEW) {
1005 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1006}
1007
1008CUResourceThrottle::~CUResourceThrottle() {
1009 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1010}
1011
1012void CUResourceThrottle::WillStartRequest(bool* defer) {
1013 if (state_ != UNBLOCKED) {
1014 state_ = BLOCKED;
1015 *defer = true;
1016 } else {
1017 *defer = false;
1018 }
1019}
1020
1021void CUResourceThrottle::WillRedirectRequest(const GURL& new_url, bool* defer) {
1022 WillStartRequest(defer);
1023}
1024
[email protected]f8fe5cf2013-12-04 20:11:531025const char* CUResourceThrottle::GetNameForLogging() const {
1026 return "ComponentUpdateResourceThrottle";
1027}
1028
[email protected]00a77fa2013-11-02 04:18:461029void CUResourceThrottle::Unblock() {
1030 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1031 if (state_ == BLOCKED)
1032 controller()->Resume();
1033 state_ = UNBLOCKED;
1034}
1035
[email protected]e8f96ff2011-08-03 05:07:331036// The component update factory. Using the component updater as a singleton
1037// is the job of the browser process.
1038ComponentUpdateService* ComponentUpdateServiceFactory(
1039 ComponentUpdateService::Configurator* config) {
1040 DCHECK(config);
1041 return new CrxUpdateService(config);
1042}
[email protected]2cddef42013-11-22 08:23:221043
[email protected]055981f2014-01-17 20:22:321044} // namespace component_updater
1045