blob: 903674a15409ab26adcd249e7c6f06c886e10402 [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]f5d27e32014-01-31 06:48:5319#include "base/sequenced_task_runner.h"
[email protected]e8f96ff2011-08-03 05:07:3320#include "base/stl_util.h"
[email protected]8f5f2ea2013-10-31 09:39:1021#include "base/threading/sequenced_worker_pool.h"
[email protected]41a17c52013-06-28 00:27:5322#include "base/timer/timer.h"
[email protected]e8f96ff2011-08-03 05:07:3323#include "chrome/browser/browser_process.h"
[email protected]e8f96ff2011-08-03 05:07:3324#include "chrome/browser/component_updater/component_unpacker.h"
[email protected]7b0529242013-07-20 05:45:4625#include "chrome/browser/component_updater/component_updater_ping_manager.h"
[email protected]2cddef42013-11-22 08:23:2226#include "chrome/browser/component_updater/component_updater_utils.h"
[email protected]afa378f22013-12-02 03:37:5427#include "chrome/browser/component_updater/crx_downloader.h"
[email protected]7b0529242013-07-20 05:45:4628#include "chrome/browser/component_updater/crx_update_item.h"
[email protected]93e8e2c2014-01-04 12:29:2329#include "chrome/browser/component_updater/update_checker.h"
[email protected]6268d3a2013-11-27 01:28:0930#include "chrome/browser/component_updater/update_response.h"
[email protected]e8f96ff2011-08-03 05:07:3331#include "chrome/common/chrome_version_info.h"
[email protected]b7b63872013-01-03 02:41:1932#include "content/public/browser/browser_thread.h"
[email protected]00a77fa2013-11-02 04:18:4633#include "content/public/browser/resource_controller.h"
34#include "content/public/browser/resource_throttle.h"
[email protected]761fa4702013-07-02 15:25:1535#include "url/gurl.h"
[email protected]e8f96ff2011-08-03 05:07:3336
[email protected]631bb742011-11-02 11:29:3937using content::BrowserThread;
38
[email protected]055981f2014-01-17 20:22:3239namespace component_updater {
[email protected]3a0092d2013-12-18 03:04:3540
[email protected]44da56e2011-11-21 19:59:1441// The component updater is designed to live until process shutdown, so
42// base::Bind() calls are not refcounted.
43
[email protected]e8f96ff2011-08-03 05:07:3344namespace {
[email protected]e3e696d32013-06-21 20:41:3645
[email protected]2cddef42013-11-22 08:23:2246// Returns true if the |proposed| version is newer than |current| version.
[email protected]c5e4a2222014-01-03 16:06:1347bool IsVersionNewer(const Version& current, const std::string& proposed) {
48 Version proposed_ver(proposed);
[email protected]2cddef42013-11-22 08:23:2249 return proposed_ver.IsValid() && current.CompareTo(proposed_ver) < 0;
[email protected]e8f96ff2011-08-03 05:07:3350}
51
[email protected]e3e696d32013-06-21 20:41:3652// Returns true if a differential update is available, it has not failed yet,
53// and the configuration allows it.
54bool CanTryDiffUpdate(const CrxUpdateItem* update_item,
55 const ComponentUpdateService::Configurator& config) {
[email protected]055981f2014-01-17 20:22:3256 return HasDiffUpdate(update_item) &&
[email protected]e3e696d32013-06-21 20:41:3657 !update_item->diff_update_failed &&
58 config.DeltasEnabled();
59}
60
[email protected]3a0092d2013-12-18 03:04:3561void AppendDownloadMetrics(
62 const std::vector<CrxDownloader::DownloadMetrics>& source,
63 std::vector<CrxDownloader::DownloadMetrics>* destination) {
64 destination->insert(destination->end(), source.begin(), source.end());
65}
66
[email protected]7b0529242013-07-20 05:45:4667} // namespace
68
69CrxUpdateItem::CrxUpdateItem()
70 : status(kNew),
[email protected]61aca4cd2013-10-26 10:50:5971 on_demand(false),
[email protected]7b0529242013-07-20 05:45:4672 diff_update_failed(false),
73 error_category(0),
74 error_code(0),
75 extra_code1(0),
76 diff_error_category(0),
77 diff_error_code(0),
78 diff_extra_code1(0) {
79}
80
81CrxUpdateItem::~CrxUpdateItem() {
82}
[email protected]86550a42013-06-21 15:20:4983
[email protected]dc06f0b2013-01-23 20:03:1684CrxComponent::CrxComponent()
[email protected]85e61d52013-08-01 22:23:4285 : installer(NULL),
[email protected]cfd13e52014-02-05 09:35:0786 observer(NULL),
87 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.
166 virtual Status Start() OVERRIDE;
167 virtual Status Stop() OVERRIDE;
168 virtual Status RegisterComponent(const CrxComponent& component) OVERRIDE;
[email protected]61aca4cd2013-10-26 10:50:59169 virtual Status OnDemandUpdate(const std::string& component_id) OVERRIDE;
[email protected]2e919ddd2013-08-21 05:05:17170 virtual void GetComponents(
171 std::vector<CrxComponentInfo>* components) OVERRIDE;
[email protected]00a77fa2013-11-02 04:18:46172 virtual content::ResourceThrottle* GetOnDemandResourceThrottle(
173 net::URLRequest* request, const std::string& crx_id) OVERRIDE;
[email protected]e8f96ff2011-08-03 05:07:33174
[email protected]93e8e2c2014-01-04 12:29:23175 // Context for a crx download url request.
176 struct CRXContext {
177 ComponentInstaller* installer;
178 std::vector<uint8> pk_hash;
179 std::string id;
180 std::string fingerprint;
181 CRXContext() : installer(NULL) {}
182 };
[email protected]e8f96ff2011-08-03 05:07:33183
[email protected]e8f96ff2011-08-03 05:07:33184 private:
[email protected]7b0529242013-07-20 05:45:46185 enum ErrorCategory {
186 kErrorNone = 0,
187 kNetworkError,
188 kUnpackError,
189 kInstallError,
190 };
191
[email protected]32a6c8382013-08-20 00:29:20192 enum StepDelayInterval {
193 kStepDelayShort = 0,
194 kStepDelayMedium,
195 kStepDelayLong,
196 };
197
[email protected]055981f2014-01-17 20:22:32198 void UpdateCheckComplete(int error,
199 const std::string& error_message,
200 const UpdateResponse::Results& results);
201 void OnUpdateCheckSucceeded(const UpdateResponse::Results& results);
[email protected]93e8e2c2014-01-04 12:29:23202 void OnUpdateCheckFailed(int error, const std::string& error_message);
[email protected]e8f96ff2011-08-03 05:07:33203
[email protected]3cb2a4f2013-12-07 21:54:34204 void DownloadComplete(
205 scoped_ptr<CRXContext> crx_context,
[email protected]3a0092d2013-12-18 03:04:35206 const CrxDownloader::Result& download_result);
[email protected]afa378f22013-12-02 03:37:54207
[email protected]00a77fa2013-11-02 04:18:46208 Status OnDemandUpdateInternal(CrxUpdateItem* item);
209
[email protected]e8f96ff2011-08-03 05:07:33210 void ProcessPendingItems();
211
[email protected]93e8e2c2014-01-04 12:29:23212 // Find a component that is ready to update.
213 CrxUpdateItem* FindReadyComponent() const;
214
215 // Prepares the components for an update check and initiates the request.
216 // Returns true if an update check request has been made. Returns false if
217 // no update check was needed or an error occured.
218 bool CheckForUpdates();
[email protected]61aca4cd2013-10-26 10:50:59219
220 void UpdateComponent(CrxUpdateItem* workitem);
221
[email protected]32a6c8382013-08-20 00:29:20222 void ScheduleNextRun(StepDelayInterval step_delay);
[email protected]e8f96ff2011-08-03 05:07:33223
[email protected]6268d3a2013-11-27 01:28:09224 void ParseResponse(const std::string& xml);
[email protected]e8f96ff2011-08-03 05:07:33225
[email protected]afa378f22013-12-02 03:37:54226 void Install(scoped_ptr<CRXContext> context, const base::FilePath& crx_path);
[email protected]e8f96ff2011-08-03 05:07:33227
[email protected]f5d27e32014-01-31 06:48:53228 void EndUnpacking(const std::string& component_id,
229 const base::FilePath& crx_path,
230 ComponentUnpacker::Error error,
231 int extended_error);
232
[email protected]07f93af12011-08-17 20:57:22233 void DoneInstalling(const std::string& component_id,
[email protected]e3e696d32013-06-21 20:41:36234 ComponentUnpacker::Error error,
235 int extended_error);
[email protected]e8f96ff2011-08-03 05:07:33236
[email protected]61aca4cd2013-10-26 10:50:59237 void ChangeItemState(CrxUpdateItem* item, CrxUpdateItem::Status to);
238
[email protected]e8f96ff2011-08-03 05:07:33239 size_t ChangeItemStatus(CrxUpdateItem::Status from,
240 CrxUpdateItem::Status to);
241
242 CrxUpdateItem* FindUpdateItemById(const std::string& id);
243
[email protected]85e61d52013-08-01 22:23:42244 void NotifyComponentObservers(ComponentObserver::Events event,
245 int extra) const;
246
[email protected]61aca4cd2013-10-26 10:50:59247 bool HasOnDemandItems() const;
248
[email protected]00a77fa2013-11-02 04:18:46249 void OnNewResourceThrottle(base::WeakPtr<CUResourceThrottle> rt,
250 const std::string& crx_id);
251
[email protected]e3e696d32013-06-21 20:41:36252 scoped_ptr<ComponentUpdateService::Configurator> config_;
253
[email protected]055981f2014-01-17 20:22:32254 scoped_ptr<UpdateChecker> update_checker_;
[email protected]e8f96ff2011-08-03 05:07:33255
[email protected]055981f2014-01-17 20:22:32256 scoped_ptr<PingManager> ping_manager_;
[email protected]7b0529242013-07-20 05:45:46257
[email protected]94a481b2014-03-28 19:41:55258 scoped_refptr<ComponentUnpacker> unpacker_;
[email protected]f5d27e32014-01-31 06:48:53259
[email protected]055981f2014-01-17 20:22:32260 scoped_ptr<CrxDownloader> crx_downloader_;
[email protected]afa378f22013-12-02 03:37:54261
[email protected]86550a42013-06-21 15:20:49262 // A collection of every work item.
[email protected]e3e696d32013-06-21 20:41:36263 typedef std::vector<CrxUpdateItem*> UpdateItems;
[email protected]e8f96ff2011-08-03 05:07:33264 UpdateItems work_items_;
265
266 base::OneShotTimer<CrxUpdateService> timer_;
267
[email protected]8f5f2ea2013-10-31 09:39:10268 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
269
[email protected]c5e4a2222014-01-03 16:06:13270 const Version chrome_version_;
[email protected]e8f96ff2011-08-03 05:07:33271
272 bool running_;
273
274 DISALLOW_COPY_AND_ASSIGN(CrxUpdateService);
275};
276
[email protected]e8f96ff2011-08-03 05:07:33277//////////////////////////////////////////////////////////////////////////////
278
[email protected]e3e696d32013-06-21 20:41:36279CrxUpdateService::CrxUpdateService(ComponentUpdateService::Configurator* config)
[email protected]e8f96ff2011-08-03 05:07:33280 : config_(config),
[email protected]055981f2014-01-17 20:22:32281 ping_manager_(new PingManager(config->PingUrl(),
282 config->RequestContext())),
[email protected]8f5f2ea2013-10-31 09:39:10283 blocking_task_runner_(BrowserThread::GetBlockingPool()->
284 GetSequencedTaskRunnerWithShutdownBehavior(
285 BrowserThread::GetBlockingPool()->GetSequenceToken(),
286 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)),
[email protected]e8f96ff2011-08-03 05:07:33287 chrome_version_(chrome::VersionInfo().Version()),
288 running_(false) {
[email protected]1ea21ad2013-09-02 04:20:39289}
[email protected]e8f96ff2011-08-03 05:07:33290
291CrxUpdateService::~CrxUpdateService() {
292 // Because we are a singleton, at this point only the UI thread should be
293 // alive, this simplifies the management of the work that could be in
294 // flight in other threads.
295 Stop();
296 STLDeleteElements(&work_items_);
[email protected]1ea21ad2013-09-02 04:20:39297}
[email protected]e8f96ff2011-08-03 05:07:33298
299ComponentUpdateService::Status CrxUpdateService::Start() {
300 // Note that RegisterComponent will call Start() when the first
301 // component is registered, so it can be called twice. This way
302 // we avoid scheduling the timer if there is no work to do.
303 running_ = true;
304 if (work_items_.empty())
305 return kOk;
306
[email protected]85e61d52013-08-01 22:23:42307 NotifyComponentObservers(ComponentObserver::COMPONENT_UPDATER_STARTED, 0);
308
[email protected]d323a172011-09-02 18:23:02309 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(config_->InitialDelay()),
[email protected]e8f96ff2011-08-03 05:07:33310 this, &CrxUpdateService::ProcessPendingItems);
311 return kOk;
312}
313
314// Stop the main check + update loop. In flight operations will be
315// completed.
316ComponentUpdateService::Status CrxUpdateService::Stop() {
317 running_ = false;
318 timer_.Stop();
319 return kOk;
320}
321
[email protected]61aca4cd2013-10-26 10:50:59322bool CrxUpdateService::HasOnDemandItems() const {
323 class Helper {
324 public:
325 static bool IsOnDemand(CrxUpdateItem* item) {
326 return item->on_demand;
327 }
328 };
329 return std::find_if(work_items_.begin(),
330 work_items_.end(),
331 Helper::IsOnDemand) != work_items_.end();
332}
333
[email protected]ccb4feef2013-02-14 06:16:47334// This function sets the timer which will call ProcessPendingItems() or
[email protected]61aca4cd2013-10-26 10:50:59335// ProcessRequestedItem() if there is an on_demand item. There
[email protected]32a6c8382013-08-20 00:29:20336// are three kinds of waits:
337// - a short delay, when there is immediate work to be done.
338// - a medium delay, when there are updates to be applied within the current
339// update cycle, or there are components that are still unchecked.
340// - a long delay when a full check/update cycle has completed for all
341// components.
342void CrxUpdateService::ScheduleNextRun(StepDelayInterval step_delay) {
[email protected]e8f96ff2011-08-03 05:07:33343 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23344 DCHECK(!update_checker_);
[email protected]e8f96ff2011-08-03 05:07:33345 CHECK(!timer_.IsRunning());
346 // It could be the case that Stop() had been called while a url request
347 // or unpacking was in flight, if so we arrive here but |running_| is
348 // false. In that case do not loop again.
349 if (!running_)
350 return;
351
[email protected]ccb4feef2013-02-14 06:16:47352 // Keep the delay short if in the middle of an update (step_delay),
353 // or there are new requested_work_items_ that have not been processed yet.
[email protected]32a6c8382013-08-20 00:29:20354 int64 delay_seconds = 0;
[email protected]61aca4cd2013-10-26 10:50:59355 if (!HasOnDemandItems()) {
[email protected]32a6c8382013-08-20 00:29:20356 switch (step_delay) {
357 case kStepDelayShort:
358 delay_seconds = config_->StepDelay();
359 break;
360 case kStepDelayMedium:
361 delay_seconds = config_->StepDelayMedium();
362 break;
363 case kStepDelayLong:
364 delay_seconds = config_->NextCheckDelay();
365 break;
366 }
367 } else {
368 delay_seconds = config_->StepDelay();
369 }
[email protected]cf442612011-08-09 20:20:12370
[email protected]32a6c8382013-08-20 00:29:20371 if (step_delay != kStepDelayShort) {
[email protected]85e61d52013-08-01 22:23:42372 NotifyComponentObservers(ComponentObserver::COMPONENT_UPDATER_SLEEPING, 0);
373
[email protected]e8f96ff2011-08-03 05:07:33374 // Zero is only used for unit tests.
[email protected]32a6c8382013-08-20 00:29:20375 if (0 == delay_seconds)
[email protected]e8f96ff2011-08-03 05:07:33376 return;
377 }
378
[email protected]32a6c8382013-08-20 00:29:20379 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(delay_seconds),
[email protected]e8f96ff2011-08-03 05:07:33380 this, &CrxUpdateService::ProcessPendingItems);
381}
382
383// Given a extension-like component id, find the associated component.
384CrxUpdateItem* CrxUpdateService::FindUpdateItemById(const std::string& id) {
385 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
386 CrxUpdateItem::FindById finder(id);
387 UpdateItems::iterator it = std::find_if(work_items_.begin(),
388 work_items_.end(),
389 finder);
[email protected]93e8e2c2014-01-04 12:29:23390 return it != work_items_.end() ? *it : NULL;
[email protected]e8f96ff2011-08-03 05:07:33391}
392
[email protected]61aca4cd2013-10-26 10:50:59393// Changes a component's status, clearing on_demand and firing notifications as
394// necessary. By convention, this is the only function that can change a
395// CrxUpdateItem's |status|.
396// TODO(waffles): Do we want to add DCHECKS for valid state transitions here?
397void CrxUpdateService::ChangeItemState(CrxUpdateItem* item,
398 CrxUpdateItem::Status to) {
399 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
400 if (to == CrxUpdateItem::kNoUpdate ||
401 to == CrxUpdateItem::kUpdated ||
402 to == CrxUpdateItem::kUpToDate) {
403 item->on_demand = false;
404 }
405
406 item->status = to;
407
408 ComponentObserver* observer = item->component.observer;
409 if (observer) {
410 switch (to) {
411 case CrxUpdateItem::kCanUpdate:
412 observer->OnEvent(ComponentObserver::COMPONENT_UPDATE_FOUND, 0);
413 break;
414 case CrxUpdateItem::kUpdatingDiff:
415 case CrxUpdateItem::kUpdating:
416 observer->OnEvent(ComponentObserver::COMPONENT_UPDATE_READY, 0);
417 break;
418 case CrxUpdateItem::kUpdated:
419 observer->OnEvent(ComponentObserver::COMPONENT_UPDATED, 0);
420 break;
421 case CrxUpdateItem::kUpToDate:
422 case CrxUpdateItem::kNoUpdate:
423 observer->OnEvent(ComponentObserver::COMPONENT_NOT_UPDATED, 0);
424 break;
425 case CrxUpdateItem::kNew:
426 case CrxUpdateItem::kChecking:
427 case CrxUpdateItem::kDownloading:
428 case CrxUpdateItem::kDownloadingDiff:
429 case CrxUpdateItem::kLastStatus:
430 // No notification for these states.
431 break;
432 }
433 }
[email protected]00a77fa2013-11-02 04:18:46434
435 // Free possible pending network requests.
436 if ((to == CrxUpdateItem::kUpdated) ||
437 (to == CrxUpdateItem::kUpToDate) ||
438 (to == CrxUpdateItem::kNoUpdate)) {
439 UnblockandReapAllThrottles(&item->throttles);
440 }
[email protected]61aca4cd2013-10-26 10:50:59441}
442
[email protected]e8f96ff2011-08-03 05:07:33443// Changes all the components in |work_items_| that have |from| status to
[email protected]e3e696d32013-06-21 20:41:36444// |to| status and returns how many have been changed.
[email protected]e8f96ff2011-08-03 05:07:33445size_t CrxUpdateService::ChangeItemStatus(CrxUpdateItem::Status from,
446 CrxUpdateItem::Status to) {
447 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
448 size_t count = 0;
449 for (UpdateItems::iterator it = work_items_.begin();
450 it != work_items_.end(); ++it) {
451 CrxUpdateItem* item = *it;
[email protected]93e8e2c2014-01-04 12:29:23452 if (item->status == from) {
453 ChangeItemState(item, to);
454 ++count;
455 }
[email protected]e8f96ff2011-08-03 05:07:33456 }
457 return count;
458}
459
460// Adds a component to be checked for upgrades. If the component exists it
461// it will be replaced and the return code is kReplaced.
[email protected]e8f96ff2011-08-03 05:07:33462ComponentUpdateService::Status CrxUpdateService::RegisterComponent(
463 const CrxComponent& component) {
464 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
465 if (component.pk_hash.empty() ||
466 !component.version.IsValid() ||
467 !component.installer)
468 return kError;
469
[email protected]055981f2014-01-17 20:22:32470 std::string id(GetCrxComponentID(component));
[email protected]93e8e2c2014-01-04 12:29:23471 CrxUpdateItem* uit = FindUpdateItemById(id);
[email protected]e8f96ff2011-08-03 05:07:33472 if (uit) {
473 uit->component = component;
474 return kReplaced;
475 }
476
477 uit = new CrxUpdateItem;
478 uit->id.swap(id);
479 uit->component = component;
[email protected]e3e696d32013-06-21 20:41:36480
[email protected]e8f96ff2011-08-03 05:07:33481 work_items_.push_back(uit);
482 // If this is the first component registered we call Start to
483 // schedule the first timer.
484 if (running_ && (work_items_.size() == 1))
485 Start();
486
487 return kOk;
488}
489
[email protected]ccb4feef2013-02-14 06:16:47490// Start the process of checking for an update, for a particular component
491// that was previously registered.
[email protected]2e919ddd2013-08-21 05:05:17492// |component_id| is a value returned from GetCrxComponentID().
[email protected]61aca4cd2013-10-26 10:50:59493ComponentUpdateService::Status CrxUpdateService::OnDemandUpdate(
[email protected]2e919ddd2013-08-21 05:05:17494 const std::string& component_id) {
[email protected]00a77fa2013-11-02 04:18:46495 return OnDemandUpdateInternal(FindUpdateItemById(component_id));
496}
497
498ComponentUpdateService::Status CrxUpdateService::OnDemandUpdateInternal(
499 CrxUpdateItem* uit) {
[email protected]ccb4feef2013-02-14 06:16:47500 if (!uit)
501 return kError;
[email protected]2cddef42013-11-22 08:23:22502
[email protected]ccb4feef2013-02-14 06:16:47503 // Check if the request is too soon.
504 base::TimeDelta delta = base::Time::Now() - uit->last_check;
[email protected]e3e696d32013-06-21 20:41:36505 if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay()))
[email protected]ccb4feef2013-02-14 06:16:47506 return kError;
[email protected]ccb4feef2013-02-14 06:16:47507
508 switch (uit->status) {
509 // If the item is already in the process of being updated, there is
510 // no point in this call, so return kInProgress.
511 case CrxUpdateItem::kChecking:
512 case CrxUpdateItem::kCanUpdate:
[email protected]e3e696d32013-06-21 20:41:36513 case CrxUpdateItem::kDownloadingDiff:
[email protected]ccb4feef2013-02-14 06:16:47514 case CrxUpdateItem::kDownloading:
[email protected]e3e696d32013-06-21 20:41:36515 case CrxUpdateItem::kUpdatingDiff:
[email protected]ccb4feef2013-02-14 06:16:47516 case CrxUpdateItem::kUpdating:
517 return kInProgress;
518 // Otherwise the item was already checked a while back (or it is new),
519 // set its status to kNew to give it a slightly higher priority.
520 case CrxUpdateItem::kNew:
521 case CrxUpdateItem::kUpdated:
522 case CrxUpdateItem::kUpToDate:
523 case CrxUpdateItem::kNoUpdate:
[email protected]61aca4cd2013-10-26 10:50:59524 ChangeItemState(uit, CrxUpdateItem::kNew);
525 uit->on_demand = true;
[email protected]ccb4feef2013-02-14 06:16:47526 break;
527 case CrxUpdateItem::kLastStatus:
528 NOTREACHED() << uit->status;
529 }
530
531 // In case the current delay is long, set the timer to a shorter value
532 // to get the ball rolling.
533 if (timer_.IsRunning()) {
534 timer_.Stop();
535 timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(config_->StepDelay()),
536 this, &CrxUpdateService::ProcessPendingItems);
537 }
538
539 return kOk;
540}
541
[email protected]2e919ddd2013-08-21 05:05:17542void CrxUpdateService::GetComponents(
543 std::vector<CrxComponentInfo>* components) {
544 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
545 for (UpdateItems::const_iterator it = work_items_.begin();
546 it != work_items_.end(); ++it) {
547 const CrxUpdateItem* item = *it;
548 CrxComponentInfo info;
[email protected]055981f2014-01-17 20:22:32549 info.id = GetCrxComponentID(item->component);
[email protected]2e919ddd2013-08-21 05:05:17550 info.version = item->component.version.GetString();
551 info.name = item->component.name;
552 components->push_back(info);
553 }
554}
555
[email protected]93e8e2c2014-01-04 12:29:23556// This is the main loop of the component updater. It updates one component
557// at a time if updates are available. Otherwise, it does an update check or
558// takes a long sleep until the loop runs again.
[email protected]e8f96ff2011-08-03 05:07:33559void CrxUpdateService::ProcessPendingItems() {
560 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23561
[email protected]61aca4cd2013-10-26 10:50:59562 CrxUpdateItem* ready_upgrade = FindReadyComponent();
563 if (ready_upgrade) {
564 UpdateComponent(ready_upgrade);
[email protected]e8f96ff2011-08-03 05:07:33565 return;
566 }
[email protected]93e8e2c2014-01-04 12:29:23567
568 if (!CheckForUpdates())
569 ScheduleNextRun(kStepDelayLong);
[email protected]61aca4cd2013-10-26 10:50:59570}
571
[email protected]93e8e2c2014-01-04 12:29:23572CrxUpdateItem* CrxUpdateService::FindReadyComponent() const {
[email protected]c4b4cfa2014-03-10 18:55:00573 class Helper {
574 public:
575 static bool IsReadyOnDemand(CrxUpdateItem* item) {
576 return item->on_demand && IsReady(item);
[email protected]61aca4cd2013-10-26 10:50:59577 }
[email protected]c4b4cfa2014-03-10 18:55:00578 static bool IsReady(CrxUpdateItem* item) {
579 return item->status == CrxUpdateItem::kCanUpdate;
580 }
581 };
[email protected]61aca4cd2013-10-26 10:50:59582
[email protected]c4b4cfa2014-03-10 18:55:00583 std::vector<CrxUpdateItem*>::const_iterator it = std::find_if(
584 work_items_.begin(), work_items_.end(), Helper::IsReadyOnDemand);
585 if (it != work_items_.end())
586 return *it;
587 it = std::find_if(work_items_.begin(), work_items_.end(), Helper::IsReady);
588 if (it != work_items_.end())
589 return *it;
[email protected]61aca4cd2013-10-26 10:50:59590 return NULL;
591}
592
[email protected]93e8e2c2014-01-04 12:29:23593// Prepares the components for an update check and initiates the request.
[email protected]21a9c9a2014-02-19 19:37:11594// On demand components are always included in the update check request.
595// Otherwise, only include components that have not been checked recently.
[email protected]93e8e2c2014-01-04 12:29:23596bool CrxUpdateService::CheckForUpdates() {
[email protected]21a9c9a2014-02-19 19:37:11597 const base::TimeDelta minimum_recheck_wait_time =
598 base::TimeDelta::FromSeconds(config_->MinimumReCheckWait());
599 const base::Time now(base::Time::Now());
600
[email protected]93e8e2c2014-01-04 12:29:23601 std::vector<CrxUpdateItem*> items_to_check;
602 for (size_t i = 0; i != work_items_.size(); ++i) {
603 CrxUpdateItem* item = work_items_[i];
604 DCHECK(item->status == CrxUpdateItem::kNew ||
605 item->status == CrxUpdateItem::kNoUpdate ||
606 item->status == CrxUpdateItem::kUpToDate ||
607 item->status == CrxUpdateItem::kUpdated);
608
[email protected]21a9c9a2014-02-19 19:37:11609 const base::TimeDelta time_since_last_checked(now - item->last_check);
610
611 if (!item->on_demand &&
612 time_since_last_checked < minimum_recheck_wait_time) {
613 continue;
614 }
615
[email protected]93e8e2c2014-01-04 12:29:23616 ChangeItemState(item, CrxUpdateItem::kChecking);
617
[email protected]21a9c9a2014-02-19 19:37:11618 item->last_check = now;
[email protected]93e8e2c2014-01-04 12:29:23619 item->crx_urls.clear();
620 item->crx_diffurls.clear();
621 item->previous_version = item->component.version;
622 item->next_version = Version();
623 item->previous_fp = item->component.fingerprint;
624 item->next_fp.clear();
625 item->diff_update_failed = false;
626 item->error_category = 0;
627 item->error_code = 0;
628 item->extra_code1 = 0;
629 item->diff_error_category = 0;
630 item->diff_error_code = 0;
631 item->diff_extra_code1 = 0;
632 item->download_metrics.clear();
633
634 items_to_check.push_back(item);
635 }
636
637 if (items_to_check.empty())
638 return false;
639
[email protected]055981f2014-01-17 20:22:32640 update_checker_ = UpdateChecker::Create(
[email protected]93e8e2c2014-01-04 12:29:23641 config_->UpdateUrl(),
642 config_->RequestContext(),
643 base::Bind(&CrxUpdateService::UpdateCheckComplete,
644 base::Unretained(this))).Pass();
645 return update_checker_->CheckForUpdates(items_to_check,
646 config_->ExtraRequestParams());
647}
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;
[email protected]61aca4cd2013-10-26 10:50:59659 ChangeItemState(workitem, CrxUpdateItem::kDownloadingDiff);
660 } 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;
[email protected]61aca4cd2013-10-26 10:50:59665 ChangeItemState(workitem, CrxUpdateItem::kDownloading);
666 }
[email protected]3cb2a4f2013-12-07 21:54:34667
668 // On demand component updates are always downloaded in foreground.
[email protected]cfd13e52014-02-05 09:35:07669 const bool is_background_download =
670 !workitem->on_demand && allow_background_download &&
671 config_->UseBackgroundDownloader();
[email protected]3cb2a4f2013-12-07 21:54:34672
[email protected]3a0092d2013-12-18 03:04:35673 crx_downloader_.reset(CrxDownloader::Create(
[email protected]3cb2a4f2013-12-07 21:54:34674 is_background_download,
[email protected]afa378f22013-12-02 03:37:54675 config_->RequestContext(),
676 blocking_task_runner_,
677 base::Bind(&CrxUpdateService::DownloadComplete,
678 base::Unretained(this),
679 base::Passed(&crx_context))));
[email protected]da37c1d2013-12-19 01:04:38680 crx_downloader_->StartDownload(*urls);
[email protected]61aca4cd2013-10-26 10:50:59681}
682
[email protected]93e8e2c2014-01-04 12:29:23683void CrxUpdateService::UpdateCheckComplete(
684 int error,
685 const std::string& error_message,
[email protected]055981f2014-01-17 20:22:32686 const UpdateResponse::Results& results) {
[email protected]e8f96ff2011-08-03 05:07:33687 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23688 update_checker_.reset();
689 if (!error)
690 OnUpdateCheckSucceeded(results);
[email protected]652f4272013-11-13 09:23:41691 else
[email protected]93e8e2c2014-01-04 12:29:23692 OnUpdateCheckFailed(error, error_message);
[email protected]e8f96ff2011-08-03 05:07:33693}
694
[email protected]93e8e2c2014-01-04 12:29:23695// Handles a valid Omaha update check response by matching the results with
696// the registered components which were checked for updates.
697// If updates are found, prepare the components for the actual version upgrade.
698// One of these components will be drafted for the upgrade next time
699// ProcessPendingItems is called.
700void CrxUpdateService::OnUpdateCheckSucceeded(
[email protected]055981f2014-01-17 20:22:32701 const UpdateResponse::Results& results) {
[email protected]2cddef42013-11-22 08:23:22702 size_t num_updates_pending = 0;
[email protected]e8f96ff2011-08-03 05:07:33703 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]055981f2014-01-17 20:22:32704 std::vector<UpdateResponse::Result>::const_iterator it;
[email protected]e8f96ff2011-08-03 05:07:33705 for (it = results.list.begin(); it != results.list.end(); ++it) {
706 CrxUpdateItem* crx = FindUpdateItemById(it->extension_id);
707 if (!crx)
708 continue;
709
[email protected]2cddef42013-11-22 08:23:22710 if (crx->status != CrxUpdateItem::kChecking) {
711 NOTREACHED();
[email protected]e8f96ff2011-08-03 05:07:33712 continue; // Not updating this component now.
[email protected]2cddef42013-11-22 08:23:22713 }
[email protected]e8f96ff2011-08-03 05:07:33714
[email protected]2cddef42013-11-22 08:23:22715 if (it->manifest.version.empty()) {
[email protected]cf442612011-08-09 20:20:12716 // No version means no update available.
[email protected]61aca4cd2013-10-26 10:50:59717 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]cf442612011-08-09 20:20:12718 continue;
[email protected]e8f96ff2011-08-03 05:07:33719 }
[email protected]2cddef42013-11-22 08:23:22720
721 if (!IsVersionNewer(crx->component.version, it->manifest.version)) {
722 // The component is up to date.
[email protected]61aca4cd2013-10-26 10:50:59723 ChangeItemState(crx, CrxUpdateItem::kUpToDate);
[email protected]cf442612011-08-09 20:20:12724 continue;
[email protected]e8f96ff2011-08-03 05:07:33725 }
[email protected]2cddef42013-11-22 08:23:22726
727 if (!it->manifest.browser_min_version.empty()) {
728 if (IsVersionNewer(chrome_version_, it->manifest.browser_min_version)) {
729 // The component is not compatible with this Chrome version.
[email protected]61aca4cd2013-10-26 10:50:59730 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
[email protected]cf442612011-08-09 20:20:12731 continue;
732 }
[email protected]e8f96ff2011-08-03 05:07:33733 }
[email protected]2cddef42013-11-22 08:23:22734
735 if (it->manifest.packages.size() != 1) {
736 // Assume one and only one package per component.
737 ChangeItemState(crx, CrxUpdateItem::kNoUpdate);
738 continue;
739 }
740
741 // Parse the members of the result and queue an upgrade for this component.
[email protected]c5e4a2222014-01-03 16:06:13742 crx->next_version = Version(it->manifest.version);
[email protected]2cddef42013-11-22 08:23:22743
[email protected]055981f2014-01-17 20:22:32744 typedef UpdateResponse::Result::Manifest::Package Package;
[email protected]2cddef42013-11-22 08:23:22745 const Package& package(it->manifest.packages[0]);
746 crx->next_fp = package.fingerprint;
747
[email protected]da37c1d2013-12-19 01:04:38748 // Resolve the urls by combining the base urls with the package names.
749 for (size_t i = 0; i != it->crx_urls.size(); ++i) {
750 const GURL url(it->crx_urls[i].Resolve(package.name));
751 if (url.is_valid())
752 crx->crx_urls.push_back(url);
753 }
754 for (size_t i = 0; i != it->crx_diffurls.size(); ++i) {
755 const GURL url(it->crx_diffurls[i].Resolve(package.namediff));
756 if (url.is_valid())
757 crx->crx_diffurls.push_back(url);
758 }
[email protected]2cddef42013-11-22 08:23:22759
[email protected]61aca4cd2013-10-26 10:50:59760 ChangeItemState(crx, CrxUpdateItem::kCanUpdate);
[email protected]2cddef42013-11-22 08:23:22761 ++num_updates_pending;
[email protected]e8f96ff2011-08-03 05:07:33762 }
[email protected]cf442612011-08-09 20:20:12763
[email protected]2cddef42013-11-22 08:23:22764 // All components that are not included in the update response are
765 // considered up to date.
[email protected]cf442612011-08-09 20:20:12766 ChangeItemStatus(CrxUpdateItem::kChecking, CrxUpdateItem::kUpToDate);
767
[email protected]32a6c8382013-08-20 00:29:20768 // If there are updates pending we do a short wait, otherwise we take
769 // a longer delay until we check the components again.
[email protected]21a9c9a2014-02-19 19:37:11770 ScheduleNextRun(num_updates_pending > 0 ? kStepDelayShort : kStepDelayLong);
[email protected]e8f96ff2011-08-03 05:07:33771}
772
[email protected]93e8e2c2014-01-04 12:29:23773// TODO: record UMA stats.
774void CrxUpdateService::OnUpdateCheckFailed(int error,
775 const std::string& error_message) {
[email protected]e8f96ff2011-08-03 05:07:33776 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]93e8e2c2014-01-04 12:29:23777 DCHECK(error);
[email protected]e8f96ff2011-08-03 05:07:33778 size_t count = ChangeItemStatus(CrxUpdateItem::kChecking,
779 CrxUpdateItem::kNoUpdate);
780 DCHECK_GT(count, 0ul);
[email protected]32a6c8382013-08-20 00:29:20781 ScheduleNextRun(kStepDelayLong);
[email protected]e8f96ff2011-08-03 05:07:33782}
783
784// Called when the CRX package has been downloaded to a temporary location.
785// Here we fire the notifications and schedule the component-specific installer
786// to be called in the file thread.
[email protected]3cb2a4f2013-12-07 21:54:34787void CrxUpdateService::DownloadComplete(
788 scoped_ptr<CRXContext> crx_context,
[email protected]3a0092d2013-12-18 03:04:35789 const CrxDownloader::Result& download_result) {
[email protected]e8f96ff2011-08-03 05:07:33790 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]64f39fa12013-09-03 21:49:37791
[email protected]64f39fa12013-09-03 21:49:37792 CrxUpdateItem* crx = FindUpdateItemById(crx_context->id);
[email protected]e3e696d32013-06-21 20:41:36793 DCHECK(crx->status == CrxUpdateItem::kDownloadingDiff ||
794 crx->status == CrxUpdateItem::kDownloading);
795
[email protected]3a0092d2013-12-18 03:04:35796 AppendDownloadMetrics(crx_downloader_->download_metrics(),
797 &crx->download_metrics);
798
[email protected]3cb2a4f2013-12-07 21:54:34799 if (download_result.error) {
[email protected]e3e696d32013-06-21 20:41:36800 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
[email protected]7b0529242013-07-20 05:45:46801 crx->diff_error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34802 crx->diff_error_code = download_result.error;
[email protected]e630ba62013-06-22 15:22:34803 crx->diff_update_failed = true;
[email protected]e3e696d32013-06-21 20:41:36804 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
805 CrxUpdateItem::kCanUpdate);
806 DCHECK_EQ(count, 1ul);
[email protected]afa378f22013-12-02 03:37:54807 crx_downloader_.reset();
[email protected]7b0529242013-07-20 05:45:46808
[email protected]32a6c8382013-08-20 00:29:20809 ScheduleNextRun(kStepDelayShort);
[email protected]e3e696d32013-06-21 20:41:36810 return;
811 }
[email protected]7b0529242013-07-20 05:45:46812 crx->error_category = kNetworkError;
[email protected]3cb2a4f2013-12-07 21:54:34813 crx->error_code = download_result.error;
[email protected]e8f96ff2011-08-03 05:07:33814 size_t count = ChangeItemStatus(CrxUpdateItem::kDownloading,
815 CrxUpdateItem::kNoUpdate);
816 DCHECK_EQ(count, 1ul);
[email protected]afa378f22013-12-02 03:37:54817 crx_downloader_.reset();
[email protected]e3e696d32013-06-21 20:41:36818
[email protected]7b0529242013-07-20 05:45:46819 // At this point, since both the differential and the full downloads failed,
820 // the update for this component has finished with an error.
821 ping_manager_->OnUpdateComplete(crx);
822
[email protected]32a6c8382013-08-20 00:29:20823 // Move on to the next update, if there is one available.
824 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33825 } else {
[email protected]e3e696d32013-06-21 20:41:36826 size_t count = 0;
827 if (crx->status == CrxUpdateItem::kDownloadingDiff) {
828 count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff,
829 CrxUpdateItem::kUpdatingDiff);
830 } else {
831 count = ChangeItemStatus(CrxUpdateItem::kDownloading,
832 CrxUpdateItem::kUpdating);
833 }
[email protected]e8f96ff2011-08-03 05:07:33834 DCHECK_EQ(count, 1ul);
[email protected]afa378f22013-12-02 03:37:54835 crx_downloader_.reset();
[email protected]cf442612011-08-09 20:20:12836
[email protected]44da56e2011-11-21 19:59:14837 // Why unretained? See comment at top of file.
[email protected]8f5f2ea2013-10-31 09:39:10838 blocking_task_runner_->PostDelayedTask(
[email protected]73251e72012-03-04 02:10:33839 FROM_HERE,
[email protected]44da56e2011-11-21 19:59:14840 base::Bind(&CrxUpdateService::Install,
841 base::Unretained(this),
[email protected]afa378f22013-12-02 03:37:54842 base::Passed(&crx_context),
[email protected]3cb2a4f2013-12-07 21:54:34843 download_result.response),
[email protected]73251e72012-03-04 02:10:33844 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]e8f96ff2011-08-03 05:07:33845 }
[email protected]e8f96ff2011-08-03 05:07:33846}
847
848// Install consists of digital signature verification, unpacking and then
849// calling the component specific installer. All that is handled by the
[email protected]f5d27e32014-01-31 06:48:53850// |unpacker_|. If there is an error this function is in charge of deleting
[email protected]e8f96ff2011-08-03 05:07:33851// the files created.
[email protected]afa378f22013-12-02 03:37:54852void CrxUpdateService::Install(scoped_ptr<CRXContext> context,
[email protected]650b2d52013-02-10 03:41:45853 const base::FilePath& crx_path) {
[email protected]8f5f2ea2013-10-31 09:39:10854 // This function owns the file at |crx_path| and the |context| object.
[email protected]94a481b2014-03-28 19:41:55855 unpacker_ = new ComponentUnpacker(context->pk_hash,
856 crx_path,
857 context->fingerprint,
858 context->installer,
859 config_->InProcess(),
860 blocking_task_runner_);
[email protected]f5d27e32014-01-31 06:48:53861 unpacker_->Unpack(base::Bind(&CrxUpdateService::EndUnpacking,
862 base::Unretained(this),
863 context->id,
864 crx_path));
865}
866
867void CrxUpdateService::EndUnpacking(const std::string& component_id,
868 const base::FilePath& crx_path,
869 ComponentUnpacker::Error error,
870 int extended_error) {
[email protected]055981f2014-01-17 20:22:32871 if (!DeleteFileAndEmptyParentDirectory(crx_path))
[email protected]e8f96ff2011-08-03 05:07:33872 NOTREACHED() << crx_path.value();
[email protected]73251e72012-03-04 02:10:33873 BrowserThread::PostDelayedTask(
874 BrowserThread::UI,
875 FROM_HERE,
[email protected]44da56e2011-11-21 19:59:14876 base::Bind(&CrxUpdateService::DoneInstalling, base::Unretained(this),
[email protected]f5d27e32014-01-31 06:48:53877 component_id, error, extended_error),
[email protected]73251e72012-03-04 02:10:33878 base::TimeDelta::FromMilliseconds(config_->StepDelay()));
[email protected]94a481b2014-03-28 19:41:55879 // Reset the unpacker last, otherwise we free our own arguments.
880 unpacker_ = NULL;
[email protected]e8f96ff2011-08-03 05:07:33881}
882
883// Installation has been completed. Adjust the component status and
[email protected]7b0529242013-07-20 05:45:46884// schedule the next check. Schedule a short delay before trying the full
885// update when the differential update failed.
[email protected]07f93af12011-08-17 20:57:22886void CrxUpdateService::DoneInstalling(const std::string& component_id,
[email protected]e3e696d32013-06-21 20:41:36887 ComponentUnpacker::Error error,
888 int extra_code) {
[email protected]e8f96ff2011-08-03 05:07:33889 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
[email protected]07f93af12011-08-17 20:57:22890
[email protected]7b0529242013-07-20 05:45:46891 ErrorCategory error_category = kErrorNone;
892 switch (error) {
893 case ComponentUnpacker::kNone:
894 break;
895 case ComponentUnpacker::kInstallerError:
896 error_category = kInstallError;
897 break;
898 default:
899 error_category = kUnpackError;
900 break;
901 }
902
903 const bool is_success = error == ComponentUnpacker::kNone;
904
[email protected]07f93af12011-08-17 20:57:22905 CrxUpdateItem* item = FindUpdateItemById(component_id);
[email protected]7b0529242013-07-20 05:45:46906 if (item->status == CrxUpdateItem::kUpdatingDiff && !is_success) {
907 item->diff_error_category = error_category;
908 item->diff_error_code = error;
909 item->diff_extra_code1 = extra_code;
[email protected]1ea21ad2013-09-02 04:20:39910 item->diff_update_failed = true;
911 size_t count = ChangeItemStatus(CrxUpdateItem::kUpdatingDiff,
912 CrxUpdateItem::kCanUpdate);
913 DCHECK_EQ(count, 1ul);
914 ScheduleNextRun(kStepDelayShort);
915 return;
[email protected]e3e696d32013-06-21 20:41:36916 }
[email protected]e3e696d32013-06-21 20:41:36917
[email protected]7b0529242013-07-20 05:45:46918 if (is_success) {
[email protected]61aca4cd2013-10-26 10:50:59919 ChangeItemState(item, CrxUpdateItem::kUpdated);
[email protected]07f93af12011-08-17 20:57:22920 item->component.version = item->next_version;
[email protected]e3e696d32013-06-21 20:41:36921 item->component.fingerprint = item->next_fp;
[email protected]7b0529242013-07-20 05:45:46922 } else {
[email protected]61aca4cd2013-10-26 10:50:59923 ChangeItemState(item, CrxUpdateItem::kNoUpdate);
[email protected]7b0529242013-07-20 05:45:46924 item->error_category = error_category;
925 item->error_code = error;
926 item->extra_code1 = extra_code;
[email protected]e3e696d32013-06-21 20:41:36927 }
[email protected]07f93af12011-08-17 20:57:22928
[email protected]7b0529242013-07-20 05:45:46929 ping_manager_->OnUpdateComplete(item);
[email protected]360b8bb2011-09-01 21:48:06930
[email protected]32a6c8382013-08-20 00:29:20931 // Move on to the next update, if there is one available.
932 ScheduleNextRun(kStepDelayMedium);
[email protected]e8f96ff2011-08-03 05:07:33933}
934
[email protected]85e61d52013-08-01 22:23:42935void CrxUpdateService::NotifyComponentObservers(
936 ComponentObserver::Events event, int extra) const {
937 for (UpdateItems::const_iterator it = work_items_.begin();
938 it != work_items_.end(); ++it) {
939 ComponentObserver* observer = (*it)->component.observer;
940 if (observer)
941 observer->OnEvent(event, 0);
942 }
943}
944
[email protected]00a77fa2013-11-02 04:18:46945content::ResourceThrottle* CrxUpdateService::GetOnDemandResourceThrottle(
946 net::URLRequest* request, const std::string& crx_id) {
947 // We give the raw pointer to the caller, who will delete it at will
948 // and we keep for ourselves a weak pointer to it so we can post tasks
949 // from the UI thread without having to track lifetime directly.
950 CUResourceThrottle* rt = new CUResourceThrottle(request);
951 BrowserThread::PostTask(
952 BrowserThread::UI,
953 FROM_HERE,
954 base::Bind(&CrxUpdateService::OnNewResourceThrottle,
955 base::Unretained(this),
956 rt->AsWeakPtr(),
957 crx_id));
958 return rt;
959}
960
961void CrxUpdateService::OnNewResourceThrottle(
962 base::WeakPtr<CUResourceThrottle> rt, const std::string& crx_id) {
963 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
964 // Check if we can on-demand update, else unblock the request anyway.
965 CrxUpdateItem* item = FindUpdateItemById(crx_id);
966 Status status = OnDemandUpdateInternal(item);
967 if (status == kOk || status == kInProgress) {
968 item->throttles.push_back(rt);
969 return;
970 }
971 UnblockResourceThrottle(rt);
972}
973
974///////////////////////////////////////////////////////////////////////////////
975
976CUResourceThrottle::CUResourceThrottle(const net::URLRequest* request)
977 : state_(NEW) {
978 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
979}
980
981CUResourceThrottle::~CUResourceThrottle() {
982 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
983}
984
985void CUResourceThrottle::WillStartRequest(bool* defer) {
986 if (state_ != UNBLOCKED) {
987 state_ = BLOCKED;
988 *defer = true;
989 } else {
990 *defer = false;
991 }
992}
993
994void CUResourceThrottle::WillRedirectRequest(const GURL& new_url, bool* defer) {
995 WillStartRequest(defer);
996}
997
[email protected]f8fe5cf2013-12-04 20:11:53998const char* CUResourceThrottle::GetNameForLogging() const {
999 return "ComponentUpdateResourceThrottle";
1000}
1001
[email protected]00a77fa2013-11-02 04:18:461002void CUResourceThrottle::Unblock() {
1003 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
1004 if (state_ == BLOCKED)
1005 controller()->Resume();
1006 state_ = UNBLOCKED;
1007}
1008
[email protected]e8f96ff2011-08-03 05:07:331009// The component update factory. Using the component updater as a singleton
1010// is the job of the browser process.
1011ComponentUpdateService* ComponentUpdateServiceFactory(
1012 ComponentUpdateService::Configurator* config) {
1013 DCHECK(config);
1014 return new CrxUpdateService(config);
1015}
[email protected]2cddef42013-11-22 08:23:221016
[email protected]055981f2014-01-17 20:22:321017} // namespace component_updater
1018