[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors. All rights reserved. |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 2 | // 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] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 8 | #include <set> |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
| 11 | #include "base/at_exit.h" |
[email protected] | 44da56e | 2011-11-21 19:59:14 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 14 | #include "base/file_util.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 15 | #include "base/files/file_path.h" |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 16 | #include "base/logging.h" |
[email protected] | 7226b33c | 2011-08-18 08:44:22 | [diff] [blame] | 17 | #include "base/memory/scoped_ptr.h" |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 18 | #include "base/memory/weak_ptr.h" |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 19 | #include "base/sequenced_task_runner.h" |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 20 | #include "base/stl_util.h" |
[email protected] | 8f5f2ea | 2013-10-31 09:39:10 | [diff] [blame] | 21 | #include "base/threading/sequenced_worker_pool.h" |
[email protected] | 41a17c5 | 2013-06-28 00:27:53 | [diff] [blame] | 22 | #include "base/timer/timer.h" |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 23 | #include "chrome/browser/browser_process.h" |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 24 | #include "chrome/browser/component_updater/component_patcher.h" |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 25 | #include "chrome/browser/component_updater/component_unpacker.h" |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 26 | #include "chrome/browser/component_updater/component_updater_ping_manager.h" |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 27 | #include "chrome/browser/component_updater/component_updater_utils.h" |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 28 | #include "chrome/browser/component_updater/crx_downloader.h" |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 29 | #include "chrome/browser/component_updater/crx_update_item.h" |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 30 | #include "chrome/browser/component_updater/update_checker.h" |
[email protected] | 6268d3a | 2013-11-27 01:28:09 | [diff] [blame] | 31 | #include "chrome/browser/component_updater/update_response.h" |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 32 | #include "chrome/common/chrome_version_info.h" |
[email protected] | b7b6387 | 2013-01-03 02:41:19 | [diff] [blame] | 33 | #include "content/public/browser/browser_thread.h" |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 34 | #include "content/public/browser/resource_controller.h" |
| 35 | #include "content/public/browser/resource_throttle.h" |
[email protected] | 761fa470 | 2013-07-02 15:25:15 | [diff] [blame] | 36 | #include "url/gurl.h" |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 37 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 38 | using content::BrowserThread; |
| 39 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 40 | namespace component_updater { |
[email protected] | 3a0092d | 2013-12-18 03:04:35 | [diff] [blame] | 41 | |
[email protected] | 44da56e | 2011-11-21 19:59:14 | [diff] [blame] | 42 | // The component updater is designed to live until process shutdown, so |
| 43 | // base::Bind() calls are not refcounted. |
| 44 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 45 | namespace { |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 46 | |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 47 | // Returns true if the |proposed| version is newer than |current| version. |
[email protected] | c5e4a222 | 2014-01-03 16:06:13 | [diff] [blame] | 48 | bool IsVersionNewer(const Version& current, const std::string& proposed) { |
| 49 | Version proposed_ver(proposed); |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 50 | return proposed_ver.IsValid() && current.CompareTo(proposed_ver) < 0; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 51 | } |
| 52 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 53 | // Returns true if a differential update is available, it has not failed yet, |
| 54 | // and the configuration allows it. |
| 55 | bool CanTryDiffUpdate(const CrxUpdateItem* update_item, |
| 56 | const ComponentUpdateService::Configurator& config) { |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 57 | return HasDiffUpdate(update_item) && |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 58 | !update_item->diff_update_failed && |
| 59 | config.DeltasEnabled(); |
| 60 | } |
| 61 | |
[email protected] | 3a0092d | 2013-12-18 03:04:35 | [diff] [blame] | 62 | void 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] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 68 | } // namespace |
| 69 | |
| 70 | CrxUpdateItem::CrxUpdateItem() |
| 71 | : status(kNew), |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 72 | on_demand(false), |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 73 | 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 | |
| 82 | CrxUpdateItem::~CrxUpdateItem() { |
| 83 | } |
[email protected] | 86550a4 | 2013-06-21 15:20:49 | [diff] [blame] | 84 | |
[email protected] | dc06f0b | 2013-01-23 20:03:16 | [diff] [blame] | 85 | CrxComponent::CrxComponent() |
[email protected] | 85e61d5 | 2013-08-01 22:23:42 | [diff] [blame] | 86 | : installer(NULL), |
[email protected] | cfd13e5 | 2014-02-05 09:35:07 | [diff] [blame] | 87 | observer(NULL), |
| 88 | allow_background_download(true) { |
[email protected] | dc06f0b | 2013-01-23 20:03:16 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | CrxComponent::~CrxComponent() { |
| 92 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 93 | |
[email protected] | 2e919ddd | 2013-08-21 05:05:17 | [diff] [blame] | 94 | CrxComponentInfo::CrxComponentInfo() { |
| 95 | } |
| 96 | |
| 97 | CrxComponentInfo::~CrxComponentInfo() { |
| 98 | } |
| 99 | |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 100 | /////////////////////////////////////////////////////////////////////////////// |
| 101 | // In charge of blocking url requests until the |crx_id| component has been |
| 102 | // updated. This class is touched solely from the IO thread. The UI thread |
| 103 | // can post tasks to it via weak pointers. By default the request is blocked |
| 104 | // unless the CrxUpdateService calls Unblock(). |
| 105 | // The lifetime is controlled by Chrome's resource loader so the component |
| 106 | // updater cannot touch objects from this class except via weak pointers. |
| 107 | class CUResourceThrottle |
| 108 | : public content::ResourceThrottle, |
| 109 | public base::SupportsWeakPtr<CUResourceThrottle> { |
| 110 | public: |
| 111 | explicit CUResourceThrottle(const net::URLRequest* request); |
| 112 | virtual ~CUResourceThrottle(); |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 113 | |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 114 | // Overriden from ResourceThrottle. |
| 115 | virtual void WillStartRequest(bool* defer) OVERRIDE; |
| 116 | virtual void WillRedirectRequest(const GURL& new_url, bool* defer) OVERRIDE; |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 117 | virtual const char* GetNameForLogging() const OVERRIDE; |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 118 | |
| 119 | // Component updater calls this function via PostTask to unblock the request. |
| 120 | void Unblock(); |
| 121 | |
| 122 | typedef std::vector<base::WeakPtr<CUResourceThrottle> > WeakPtrVector; |
| 123 | |
| 124 | private: |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 125 | enum State { |
| 126 | NEW, |
| 127 | BLOCKED, |
| 128 | UNBLOCKED |
| 129 | }; |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 130 | |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 131 | State state_; |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | void UnblockResourceThrottle(base::WeakPtr<CUResourceThrottle> rt) { |
| 135 | BrowserThread::PostTask( |
| 136 | BrowserThread::IO, |
| 137 | FROM_HERE, |
| 138 | base::Bind(&CUResourceThrottle::Unblock, rt)); |
| 139 | } |
| 140 | |
| 141 | void UnblockandReapAllThrottles(CUResourceThrottle::WeakPtrVector* throttles) { |
| 142 | CUResourceThrottle::WeakPtrVector::iterator it; |
| 143 | for (it = throttles->begin(); it != throttles->end(); ++it) |
| 144 | UnblockResourceThrottle(*it); |
| 145 | throttles->clear(); |
| 146 | } |
| 147 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 148 | ////////////////////////////////////////////////////////////////////////////// |
| 149 | // The one and only implementation of the ComponentUpdateService interface. In |
| 150 | // charge of running the show. The main method is ProcessPendingItems() which |
[email protected] | 50b0139 | 2012-09-01 03:33:13 | [diff] [blame] | 151 | // is called periodically to do the upgrades/installs or the update checks. |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 152 | // An important consideration here is to be as "low impact" as we can to the |
| 153 | // rest of the browser, so even if we have many components registered and |
[email protected] | f105043 | 2012-02-15 01:35:46 | [diff] [blame] | 154 | // eligible for update, we only do one thing at a time with pauses in between |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 155 | // the tasks. Also when we do network requests there is only one |url_fetcher_| |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 156 | // in flight at a time. |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 157 | // There are no locks in this code, the main structure |work_items_| is mutated |
[email protected] | 74be264 | 2014-02-07 09:40:37 | [diff] [blame] | 158 | // only from the UI thread. The unpack and installation is done in a blocking |
| 159 | // pool thread. The network requests are done in the IO thread or in the file |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 160 | // thread. |
| 161 | class CrxUpdateService : public ComponentUpdateService { |
| 162 | public: |
| 163 | explicit CrxUpdateService(ComponentUpdateService::Configurator* config); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 164 | virtual ~CrxUpdateService(); |
| 165 | |
| 166 | // Overrides for ComponentUpdateService. |
| 167 | virtual Status Start() OVERRIDE; |
| 168 | virtual Status Stop() OVERRIDE; |
| 169 | virtual Status RegisterComponent(const CrxComponent& component) OVERRIDE; |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 170 | virtual Status OnDemandUpdate(const std::string& component_id) OVERRIDE; |
[email protected] | 2e919ddd | 2013-08-21 05:05:17 | [diff] [blame] | 171 | virtual void GetComponents( |
| 172 | std::vector<CrxComponentInfo>* components) OVERRIDE; |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 173 | virtual content::ResourceThrottle* GetOnDemandResourceThrottle( |
| 174 | net::URLRequest* request, const std::string& crx_id) OVERRIDE; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 175 | |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 176 | // Context for a crx download url request. |
| 177 | struct CRXContext { |
| 178 | ComponentInstaller* installer; |
| 179 | std::vector<uint8> pk_hash; |
| 180 | std::string id; |
| 181 | std::string fingerprint; |
| 182 | CRXContext() : installer(NULL) {} |
| 183 | }; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 184 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 185 | private: |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 186 | enum ErrorCategory { |
| 187 | kErrorNone = 0, |
| 188 | kNetworkError, |
| 189 | kUnpackError, |
| 190 | kInstallError, |
| 191 | }; |
| 192 | |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 193 | enum StepDelayInterval { |
| 194 | kStepDelayShort = 0, |
| 195 | kStepDelayMedium, |
| 196 | kStepDelayLong, |
| 197 | }; |
| 198 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 199 | void UpdateCheckComplete(int error, |
| 200 | const std::string& error_message, |
| 201 | const UpdateResponse::Results& results); |
| 202 | void OnUpdateCheckSucceeded(const UpdateResponse::Results& results); |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 203 | void OnUpdateCheckFailed(int error, const std::string& error_message); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 204 | |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 205 | void DownloadComplete( |
| 206 | scoped_ptr<CRXContext> crx_context, |
[email protected] | 3a0092d | 2013-12-18 03:04:35 | [diff] [blame] | 207 | const CrxDownloader::Result& download_result); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 208 | |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 209 | Status OnDemandUpdateInternal(CrxUpdateItem* item); |
| 210 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 211 | void ProcessPendingItems(); |
| 212 | |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 213 | // Find a component that is ready to update. |
| 214 | CrxUpdateItem* FindReadyComponent() const; |
| 215 | |
| 216 | // Prepares the components for an update check and initiates the request. |
| 217 | // Returns true if an update check request has been made. Returns false if |
| 218 | // no update check was needed or an error occured. |
| 219 | bool CheckForUpdates(); |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 220 | |
| 221 | void UpdateComponent(CrxUpdateItem* workitem); |
| 222 | |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 223 | void ScheduleNextRun(StepDelayInterval step_delay); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 224 | |
[email protected] | 6268d3a | 2013-11-27 01:28:09 | [diff] [blame] | 225 | void ParseResponse(const std::string& xml); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 226 | |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 227 | void Install(scoped_ptr<CRXContext> context, const base::FilePath& crx_path); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 228 | |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 229 | void EndUnpacking(const std::string& component_id, |
| 230 | const base::FilePath& crx_path, |
| 231 | ComponentUnpacker::Error error, |
| 232 | int extended_error); |
| 233 | |
[email protected] | 07f93af1 | 2011-08-17 20:57:22 | [diff] [blame] | 234 | void DoneInstalling(const std::string& component_id, |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 235 | ComponentUnpacker::Error error, |
| 236 | int extended_error); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 237 | |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 238 | void ChangeItemState(CrxUpdateItem* item, CrxUpdateItem::Status to); |
| 239 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 240 | size_t ChangeItemStatus(CrxUpdateItem::Status from, |
| 241 | CrxUpdateItem::Status to); |
| 242 | |
| 243 | CrxUpdateItem* FindUpdateItemById(const std::string& id); |
| 244 | |
[email protected] | 85e61d5 | 2013-08-01 22:23:42 | [diff] [blame] | 245 | void NotifyComponentObservers(ComponentObserver::Events event, |
| 246 | int extra) const; |
| 247 | |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 248 | bool HasOnDemandItems() const; |
| 249 | |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 250 | void OnNewResourceThrottle(base::WeakPtr<CUResourceThrottle> rt, |
| 251 | const std::string& crx_id); |
| 252 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 253 | scoped_ptr<ComponentUpdateService::Configurator> config_; |
| 254 | |
| 255 | scoped_ptr<ComponentPatcher> component_patcher_; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 256 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 257 | scoped_ptr<UpdateChecker> update_checker_; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 258 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 259 | scoped_ptr<PingManager> ping_manager_; |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 260 | |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 261 | scoped_ptr<ComponentUnpacker> unpacker_; |
| 262 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 263 | scoped_ptr<CrxDownloader> crx_downloader_; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 264 | |
[email protected] | 86550a4 | 2013-06-21 15:20:49 | [diff] [blame] | 265 | // A collection of every work item. |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 266 | typedef std::vector<CrxUpdateItem*> UpdateItems; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 267 | UpdateItems work_items_; |
| 268 | |
| 269 | base::OneShotTimer<CrxUpdateService> timer_; |
| 270 | |
[email protected] | 8f5f2ea | 2013-10-31 09:39:10 | [diff] [blame] | 271 | scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
| 272 | |
[email protected] | c5e4a222 | 2014-01-03 16:06:13 | [diff] [blame] | 273 | const Version chrome_version_; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 274 | |
| 275 | bool running_; |
| 276 | |
| 277 | DISALLOW_COPY_AND_ASSIGN(CrxUpdateService); |
| 278 | }; |
| 279 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 280 | ////////////////////////////////////////////////////////////////////////////// |
| 281 | |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 282 | CrxUpdateService::CrxUpdateService(ComponentUpdateService::Configurator* config) |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 283 | : config_(config), |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 284 | component_patcher_(config->CreateComponentPatcher()), |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 285 | ping_manager_(new PingManager(config->PingUrl(), |
| 286 | config->RequestContext())), |
[email protected] | 8f5f2ea | 2013-10-31 09:39:10 | [diff] [blame] | 287 | blocking_task_runner_(BrowserThread::GetBlockingPool()-> |
| 288 | GetSequencedTaskRunnerWithShutdownBehavior( |
| 289 | BrowserThread::GetBlockingPool()->GetSequenceToken(), |
| 290 | base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 291 | chrome_version_(chrome::VersionInfo().Version()), |
| 292 | running_(false) { |
[email protected] | 1ea21ad | 2013-09-02 04:20:39 | [diff] [blame] | 293 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 294 | |
| 295 | CrxUpdateService::~CrxUpdateService() { |
| 296 | // Because we are a singleton, at this point only the UI thread should be |
| 297 | // alive, this simplifies the management of the work that could be in |
| 298 | // flight in other threads. |
| 299 | Stop(); |
| 300 | STLDeleteElements(&work_items_); |
[email protected] | 1ea21ad | 2013-09-02 04:20:39 | [diff] [blame] | 301 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 302 | |
| 303 | ComponentUpdateService::Status CrxUpdateService::Start() { |
| 304 | // Note that RegisterComponent will call Start() when the first |
| 305 | // component is registered, so it can be called twice. This way |
| 306 | // we avoid scheduling the timer if there is no work to do. |
| 307 | running_ = true; |
| 308 | if (work_items_.empty()) |
| 309 | return kOk; |
| 310 | |
[email protected] | 85e61d5 | 2013-08-01 22:23:42 | [diff] [blame] | 311 | NotifyComponentObservers(ComponentObserver::COMPONENT_UPDATER_STARTED, 0); |
| 312 | |
[email protected] | d323a17 | 2011-09-02 18:23:02 | [diff] [blame] | 313 | timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(config_->InitialDelay()), |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 314 | this, &CrxUpdateService::ProcessPendingItems); |
| 315 | return kOk; |
| 316 | } |
| 317 | |
| 318 | // Stop the main check + update loop. In flight operations will be |
| 319 | // completed. |
| 320 | ComponentUpdateService::Status CrxUpdateService::Stop() { |
| 321 | running_ = false; |
| 322 | timer_.Stop(); |
| 323 | return kOk; |
| 324 | } |
| 325 | |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 326 | bool CrxUpdateService::HasOnDemandItems() const { |
| 327 | class Helper { |
| 328 | public: |
| 329 | static bool IsOnDemand(CrxUpdateItem* item) { |
| 330 | return item->on_demand; |
| 331 | } |
| 332 | }; |
| 333 | return std::find_if(work_items_.begin(), |
| 334 | work_items_.end(), |
| 335 | Helper::IsOnDemand) != work_items_.end(); |
| 336 | } |
| 337 | |
[email protected] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 338 | // This function sets the timer which will call ProcessPendingItems() or |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 339 | // ProcessRequestedItem() if there is an on_demand item. There |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 340 | // are three kinds of waits: |
| 341 | // - a short delay, when there is immediate work to be done. |
| 342 | // - a medium delay, when there are updates to be applied within the current |
| 343 | // update cycle, or there are components that are still unchecked. |
| 344 | // - a long delay when a full check/update cycle has completed for all |
| 345 | // components. |
| 346 | void CrxUpdateService::ScheduleNextRun(StepDelayInterval step_delay) { |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 347 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 348 | DCHECK(!update_checker_); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 349 | CHECK(!timer_.IsRunning()); |
| 350 | // It could be the case that Stop() had been called while a url request |
| 351 | // or unpacking was in flight, if so we arrive here but |running_| is |
| 352 | // false. In that case do not loop again. |
| 353 | if (!running_) |
| 354 | return; |
| 355 | |
[email protected] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 356 | // Keep the delay short if in the middle of an update (step_delay), |
| 357 | // or there are new requested_work_items_ that have not been processed yet. |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 358 | int64 delay_seconds = 0; |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 359 | if (!HasOnDemandItems()) { |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 360 | switch (step_delay) { |
| 361 | case kStepDelayShort: |
| 362 | delay_seconds = config_->StepDelay(); |
| 363 | break; |
| 364 | case kStepDelayMedium: |
| 365 | delay_seconds = config_->StepDelayMedium(); |
| 366 | break; |
| 367 | case kStepDelayLong: |
| 368 | delay_seconds = config_->NextCheckDelay(); |
| 369 | break; |
| 370 | } |
| 371 | } else { |
| 372 | delay_seconds = config_->StepDelay(); |
| 373 | } |
[email protected] | cf44261 | 2011-08-09 20:20:12 | [diff] [blame] | 374 | |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 375 | if (step_delay != kStepDelayShort) { |
[email protected] | 85e61d5 | 2013-08-01 22:23:42 | [diff] [blame] | 376 | NotifyComponentObservers(ComponentObserver::COMPONENT_UPDATER_SLEEPING, 0); |
| 377 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 378 | // Zero is only used for unit tests. |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 379 | if (0 == delay_seconds) |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 380 | return; |
| 381 | } |
| 382 | |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 383 | timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(delay_seconds), |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 384 | this, &CrxUpdateService::ProcessPendingItems); |
| 385 | } |
| 386 | |
| 387 | // Given a extension-like component id, find the associated component. |
| 388 | CrxUpdateItem* CrxUpdateService::FindUpdateItemById(const std::string& id) { |
| 389 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 390 | CrxUpdateItem::FindById finder(id); |
| 391 | UpdateItems::iterator it = std::find_if(work_items_.begin(), |
| 392 | work_items_.end(), |
| 393 | finder); |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 394 | return it != work_items_.end() ? *it : NULL; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 395 | } |
| 396 | |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 397 | // Changes a component's status, clearing on_demand and firing notifications as |
| 398 | // necessary. By convention, this is the only function that can change a |
| 399 | // CrxUpdateItem's |status|. |
| 400 | // TODO(waffles): Do we want to add DCHECKS for valid state transitions here? |
| 401 | void CrxUpdateService::ChangeItemState(CrxUpdateItem* item, |
| 402 | CrxUpdateItem::Status to) { |
| 403 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 404 | if (to == CrxUpdateItem::kNoUpdate || |
| 405 | to == CrxUpdateItem::kUpdated || |
| 406 | to == CrxUpdateItem::kUpToDate) { |
| 407 | item->on_demand = false; |
| 408 | } |
| 409 | |
| 410 | item->status = to; |
| 411 | |
| 412 | ComponentObserver* observer = item->component.observer; |
| 413 | if (observer) { |
| 414 | switch (to) { |
| 415 | case CrxUpdateItem::kCanUpdate: |
| 416 | observer->OnEvent(ComponentObserver::COMPONENT_UPDATE_FOUND, 0); |
| 417 | break; |
| 418 | case CrxUpdateItem::kUpdatingDiff: |
| 419 | case CrxUpdateItem::kUpdating: |
| 420 | observer->OnEvent(ComponentObserver::COMPONENT_UPDATE_READY, 0); |
| 421 | break; |
| 422 | case CrxUpdateItem::kUpdated: |
| 423 | observer->OnEvent(ComponentObserver::COMPONENT_UPDATED, 0); |
| 424 | break; |
| 425 | case CrxUpdateItem::kUpToDate: |
| 426 | case CrxUpdateItem::kNoUpdate: |
| 427 | observer->OnEvent(ComponentObserver::COMPONENT_NOT_UPDATED, 0); |
| 428 | break; |
| 429 | case CrxUpdateItem::kNew: |
| 430 | case CrxUpdateItem::kChecking: |
| 431 | case CrxUpdateItem::kDownloading: |
| 432 | case CrxUpdateItem::kDownloadingDiff: |
| 433 | case CrxUpdateItem::kLastStatus: |
| 434 | // No notification for these states. |
| 435 | break; |
| 436 | } |
| 437 | } |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 438 | |
| 439 | // Free possible pending network requests. |
| 440 | if ((to == CrxUpdateItem::kUpdated) || |
| 441 | (to == CrxUpdateItem::kUpToDate) || |
| 442 | (to == CrxUpdateItem::kNoUpdate)) { |
| 443 | UnblockandReapAllThrottles(&item->throttles); |
| 444 | } |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 445 | } |
| 446 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 447 | // Changes all the components in |work_items_| that have |from| status to |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 448 | // |to| status and returns how many have been changed. |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 449 | size_t CrxUpdateService::ChangeItemStatus(CrxUpdateItem::Status from, |
| 450 | CrxUpdateItem::Status to) { |
| 451 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 452 | size_t count = 0; |
| 453 | for (UpdateItems::iterator it = work_items_.begin(); |
| 454 | it != work_items_.end(); ++it) { |
| 455 | CrxUpdateItem* item = *it; |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 456 | if (item->status == from) { |
| 457 | ChangeItemState(item, to); |
| 458 | ++count; |
| 459 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 460 | } |
| 461 | return count; |
| 462 | } |
| 463 | |
| 464 | // Adds a component to be checked for upgrades. If the component exists it |
| 465 | // it will be replaced and the return code is kReplaced. |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 466 | ComponentUpdateService::Status CrxUpdateService::RegisterComponent( |
| 467 | const CrxComponent& component) { |
| 468 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 469 | if (component.pk_hash.empty() || |
| 470 | !component.version.IsValid() || |
| 471 | !component.installer) |
| 472 | return kError; |
| 473 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 474 | std::string id(GetCrxComponentID(component)); |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 475 | CrxUpdateItem* uit = FindUpdateItemById(id); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 476 | if (uit) { |
| 477 | uit->component = component; |
| 478 | return kReplaced; |
| 479 | } |
| 480 | |
| 481 | uit = new CrxUpdateItem; |
| 482 | uit->id.swap(id); |
| 483 | uit->component = component; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 484 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 485 | work_items_.push_back(uit); |
| 486 | // If this is the first component registered we call Start to |
| 487 | // schedule the first timer. |
| 488 | if (running_ && (work_items_.size() == 1)) |
| 489 | Start(); |
| 490 | |
| 491 | return kOk; |
| 492 | } |
| 493 | |
[email protected] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 494 | // Start the process of checking for an update, for a particular component |
| 495 | // that was previously registered. |
[email protected] | 2e919ddd | 2013-08-21 05:05:17 | [diff] [blame] | 496 | // |component_id| is a value returned from GetCrxComponentID(). |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 497 | ComponentUpdateService::Status CrxUpdateService::OnDemandUpdate( |
[email protected] | 2e919ddd | 2013-08-21 05:05:17 | [diff] [blame] | 498 | const std::string& component_id) { |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 499 | return OnDemandUpdateInternal(FindUpdateItemById(component_id)); |
| 500 | } |
| 501 | |
| 502 | ComponentUpdateService::Status CrxUpdateService::OnDemandUpdateInternal( |
| 503 | CrxUpdateItem* uit) { |
[email protected] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 504 | if (!uit) |
| 505 | return kError; |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 506 | |
[email protected] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 507 | // Check if the request is too soon. |
| 508 | base::TimeDelta delta = base::Time::Now() - uit->last_check; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 509 | if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay())) |
[email protected] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 510 | return kError; |
[email protected] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 511 | |
| 512 | switch (uit->status) { |
| 513 | // If the item is already in the process of being updated, there is |
| 514 | // no point in this call, so return kInProgress. |
| 515 | case CrxUpdateItem::kChecking: |
| 516 | case CrxUpdateItem::kCanUpdate: |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 517 | case CrxUpdateItem::kDownloadingDiff: |
[email protected] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 518 | case CrxUpdateItem::kDownloading: |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 519 | case CrxUpdateItem::kUpdatingDiff: |
[email protected] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 520 | case CrxUpdateItem::kUpdating: |
| 521 | return kInProgress; |
| 522 | // Otherwise the item was already checked a while back (or it is new), |
| 523 | // set its status to kNew to give it a slightly higher priority. |
| 524 | case CrxUpdateItem::kNew: |
| 525 | case CrxUpdateItem::kUpdated: |
| 526 | case CrxUpdateItem::kUpToDate: |
| 527 | case CrxUpdateItem::kNoUpdate: |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 528 | ChangeItemState(uit, CrxUpdateItem::kNew); |
| 529 | uit->on_demand = true; |
[email protected] | ccb4feef | 2013-02-14 06:16:47 | [diff] [blame] | 530 | break; |
| 531 | case CrxUpdateItem::kLastStatus: |
| 532 | NOTREACHED() << uit->status; |
| 533 | } |
| 534 | |
| 535 | // In case the current delay is long, set the timer to a shorter value |
| 536 | // to get the ball rolling. |
| 537 | if (timer_.IsRunning()) { |
| 538 | timer_.Stop(); |
| 539 | timer_.Start(FROM_HERE, base::TimeDelta::FromSeconds(config_->StepDelay()), |
| 540 | this, &CrxUpdateService::ProcessPendingItems); |
| 541 | } |
| 542 | |
| 543 | return kOk; |
| 544 | } |
| 545 | |
[email protected] | 2e919ddd | 2013-08-21 05:05:17 | [diff] [blame] | 546 | void CrxUpdateService::GetComponents( |
| 547 | std::vector<CrxComponentInfo>* components) { |
| 548 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 549 | for (UpdateItems::const_iterator it = work_items_.begin(); |
| 550 | it != work_items_.end(); ++it) { |
| 551 | const CrxUpdateItem* item = *it; |
| 552 | CrxComponentInfo info; |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 553 | info.id = GetCrxComponentID(item->component); |
[email protected] | 2e919ddd | 2013-08-21 05:05:17 | [diff] [blame] | 554 | info.version = item->component.version.GetString(); |
| 555 | info.name = item->component.name; |
| 556 | components->push_back(info); |
| 557 | } |
| 558 | } |
| 559 | |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 560 | // This is the main loop of the component updater. It updates one component |
| 561 | // at a time if updates are available. Otherwise, it does an update check or |
| 562 | // takes a long sleep until the loop runs again. |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 563 | void CrxUpdateService::ProcessPendingItems() { |
| 564 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 565 | |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 566 | CrxUpdateItem* ready_upgrade = FindReadyComponent(); |
| 567 | if (ready_upgrade) { |
| 568 | UpdateComponent(ready_upgrade); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 569 | return; |
| 570 | } |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 571 | |
| 572 | if (!CheckForUpdates()) |
| 573 | ScheduleNextRun(kStepDelayLong); |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 574 | } |
| 575 | |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 576 | CrxUpdateItem* CrxUpdateService::FindReadyComponent() const { |
[email protected] | c4b4cfa | 2014-03-10 18:55:00 | [diff] [blame^] | 577 | class Helper { |
| 578 | public: |
| 579 | static bool IsReadyOnDemand(CrxUpdateItem* item) { |
| 580 | return item->on_demand && IsReady(item); |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 581 | } |
[email protected] | c4b4cfa | 2014-03-10 18:55:00 | [diff] [blame^] | 582 | static bool IsReady(CrxUpdateItem* item) { |
| 583 | return item->status == CrxUpdateItem::kCanUpdate; |
| 584 | } |
| 585 | }; |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 586 | |
[email protected] | c4b4cfa | 2014-03-10 18:55:00 | [diff] [blame^] | 587 | std::vector<CrxUpdateItem*>::const_iterator it = std::find_if( |
| 588 | work_items_.begin(), work_items_.end(), Helper::IsReadyOnDemand); |
| 589 | if (it != work_items_.end()) |
| 590 | return *it; |
| 591 | it = std::find_if(work_items_.begin(), work_items_.end(), Helper::IsReady); |
| 592 | if (it != work_items_.end()) |
| 593 | return *it; |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 594 | return NULL; |
| 595 | } |
| 596 | |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 597 | // Prepares the components for an update check and initiates the request. |
[email protected] | 21a9c9a | 2014-02-19 19:37:11 | [diff] [blame] | 598 | // On demand components are always included in the update check request. |
| 599 | // Otherwise, only include components that have not been checked recently. |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 600 | bool CrxUpdateService::CheckForUpdates() { |
[email protected] | 21a9c9a | 2014-02-19 19:37:11 | [diff] [blame] | 601 | const base::TimeDelta minimum_recheck_wait_time = |
| 602 | base::TimeDelta::FromSeconds(config_->MinimumReCheckWait()); |
| 603 | const base::Time now(base::Time::Now()); |
| 604 | |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 605 | std::vector<CrxUpdateItem*> items_to_check; |
| 606 | for (size_t i = 0; i != work_items_.size(); ++i) { |
| 607 | CrxUpdateItem* item = work_items_[i]; |
| 608 | DCHECK(item->status == CrxUpdateItem::kNew || |
| 609 | item->status == CrxUpdateItem::kNoUpdate || |
| 610 | item->status == CrxUpdateItem::kUpToDate || |
| 611 | item->status == CrxUpdateItem::kUpdated); |
| 612 | |
[email protected] | 21a9c9a | 2014-02-19 19:37:11 | [diff] [blame] | 613 | const base::TimeDelta time_since_last_checked(now - item->last_check); |
| 614 | |
| 615 | if (!item->on_demand && |
| 616 | time_since_last_checked < minimum_recheck_wait_time) { |
| 617 | continue; |
| 618 | } |
| 619 | |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 620 | ChangeItemState(item, CrxUpdateItem::kChecking); |
| 621 | |
[email protected] | 21a9c9a | 2014-02-19 19:37:11 | [diff] [blame] | 622 | item->last_check = now; |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 623 | item->crx_urls.clear(); |
| 624 | item->crx_diffurls.clear(); |
| 625 | item->previous_version = item->component.version; |
| 626 | item->next_version = Version(); |
| 627 | item->previous_fp = item->component.fingerprint; |
| 628 | item->next_fp.clear(); |
| 629 | item->diff_update_failed = false; |
| 630 | item->error_category = 0; |
| 631 | item->error_code = 0; |
| 632 | item->extra_code1 = 0; |
| 633 | item->diff_error_category = 0; |
| 634 | item->diff_error_code = 0; |
| 635 | item->diff_extra_code1 = 0; |
| 636 | item->download_metrics.clear(); |
| 637 | |
| 638 | items_to_check.push_back(item); |
| 639 | } |
| 640 | |
| 641 | if (items_to_check.empty()) |
| 642 | return false; |
| 643 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 644 | update_checker_ = UpdateChecker::Create( |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 645 | config_->UpdateUrl(), |
| 646 | config_->RequestContext(), |
| 647 | base::Bind(&CrxUpdateService::UpdateCheckComplete, |
| 648 | base::Unretained(this))).Pass(); |
| 649 | return update_checker_->CheckForUpdates(items_to_check, |
| 650 | config_->ExtraRequestParams()); |
| 651 | } |
| 652 | |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 653 | void CrxUpdateService::UpdateComponent(CrxUpdateItem* workitem) { |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 654 | scoped_ptr<CRXContext> crx_context(new CRXContext); |
| 655 | crx_context->pk_hash = workitem->component.pk_hash; |
| 656 | crx_context->id = workitem->id; |
| 657 | crx_context->installer = workitem->component.installer; |
| 658 | crx_context->fingerprint = workitem->next_fp; |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 659 | const std::vector<GURL>* urls = NULL; |
[email protected] | cfd13e5 | 2014-02-05 09:35:07 | [diff] [blame] | 660 | bool allow_background_download = false; |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 661 | if (CanTryDiffUpdate(workitem, *config_)) { |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 662 | urls = &workitem->crx_diffurls; |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 663 | ChangeItemState(workitem, CrxUpdateItem::kDownloadingDiff); |
| 664 | } else { |
[email protected] | cfd13e5 | 2014-02-05 09:35:07 | [diff] [blame] | 665 | // Background downloads are enabled only for selected components and |
| 666 | // only for full downloads (see issue 340448). |
| 667 | allow_background_download = workitem->component.allow_background_download; |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 668 | urls = &workitem->crx_urls; |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 669 | ChangeItemState(workitem, CrxUpdateItem::kDownloading); |
| 670 | } |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 671 | |
| 672 | // On demand component updates are always downloaded in foreground. |
[email protected] | cfd13e5 | 2014-02-05 09:35:07 | [diff] [blame] | 673 | const bool is_background_download = |
| 674 | !workitem->on_demand && allow_background_download && |
| 675 | config_->UseBackgroundDownloader(); |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 676 | |
[email protected] | 3a0092d | 2013-12-18 03:04:35 | [diff] [blame] | 677 | crx_downloader_.reset(CrxDownloader::Create( |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 678 | is_background_download, |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 679 | config_->RequestContext(), |
| 680 | blocking_task_runner_, |
| 681 | base::Bind(&CrxUpdateService::DownloadComplete, |
| 682 | base::Unretained(this), |
| 683 | base::Passed(&crx_context)))); |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 684 | crx_downloader_->StartDownload(*urls); |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 685 | } |
| 686 | |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 687 | void CrxUpdateService::UpdateCheckComplete( |
| 688 | int error, |
| 689 | const std::string& error_message, |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 690 | const UpdateResponse::Results& results) { |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 691 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 692 | update_checker_.reset(); |
| 693 | if (!error) |
| 694 | OnUpdateCheckSucceeded(results); |
[email protected] | 652f427 | 2013-11-13 09:23:41 | [diff] [blame] | 695 | else |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 696 | OnUpdateCheckFailed(error, error_message); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 697 | } |
| 698 | |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 699 | // Handles a valid Omaha update check response by matching the results with |
| 700 | // the registered components which were checked for updates. |
| 701 | // If updates are found, prepare the components for the actual version upgrade. |
| 702 | // One of these components will be drafted for the upgrade next time |
| 703 | // ProcessPendingItems is called. |
| 704 | void CrxUpdateService::OnUpdateCheckSucceeded( |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 705 | const UpdateResponse::Results& results) { |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 706 | size_t num_updates_pending = 0; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 707 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 708 | std::vector<UpdateResponse::Result>::const_iterator it; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 709 | for (it = results.list.begin(); it != results.list.end(); ++it) { |
| 710 | CrxUpdateItem* crx = FindUpdateItemById(it->extension_id); |
| 711 | if (!crx) |
| 712 | continue; |
| 713 | |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 714 | if (crx->status != CrxUpdateItem::kChecking) { |
| 715 | NOTREACHED(); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 716 | continue; // Not updating this component now. |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 717 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 718 | |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 719 | if (it->manifest.version.empty()) { |
[email protected] | cf44261 | 2011-08-09 20:20:12 | [diff] [blame] | 720 | // No version means no update available. |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 721 | ChangeItemState(crx, CrxUpdateItem::kNoUpdate); |
[email protected] | cf44261 | 2011-08-09 20:20:12 | [diff] [blame] | 722 | continue; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 723 | } |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 724 | |
| 725 | if (!IsVersionNewer(crx->component.version, it->manifest.version)) { |
| 726 | // The component is up to date. |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 727 | ChangeItemState(crx, CrxUpdateItem::kUpToDate); |
[email protected] | cf44261 | 2011-08-09 20:20:12 | [diff] [blame] | 728 | continue; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 729 | } |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 730 | |
| 731 | if (!it->manifest.browser_min_version.empty()) { |
| 732 | if (IsVersionNewer(chrome_version_, it->manifest.browser_min_version)) { |
| 733 | // The component is not compatible with this Chrome version. |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 734 | ChangeItemState(crx, CrxUpdateItem::kNoUpdate); |
[email protected] | cf44261 | 2011-08-09 20:20:12 | [diff] [blame] | 735 | continue; |
| 736 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 737 | } |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 738 | |
| 739 | if (it->manifest.packages.size() != 1) { |
| 740 | // Assume one and only one package per component. |
| 741 | ChangeItemState(crx, CrxUpdateItem::kNoUpdate); |
| 742 | continue; |
| 743 | } |
| 744 | |
| 745 | // Parse the members of the result and queue an upgrade for this component. |
[email protected] | c5e4a222 | 2014-01-03 16:06:13 | [diff] [blame] | 746 | crx->next_version = Version(it->manifest.version); |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 747 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 748 | typedef UpdateResponse::Result::Manifest::Package Package; |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 749 | const Package& package(it->manifest.packages[0]); |
| 750 | crx->next_fp = package.fingerprint; |
| 751 | |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 752 | // Resolve the urls by combining the base urls with the package names. |
| 753 | for (size_t i = 0; i != it->crx_urls.size(); ++i) { |
| 754 | const GURL url(it->crx_urls[i].Resolve(package.name)); |
| 755 | if (url.is_valid()) |
| 756 | crx->crx_urls.push_back(url); |
| 757 | } |
| 758 | for (size_t i = 0; i != it->crx_diffurls.size(); ++i) { |
| 759 | const GURL url(it->crx_diffurls[i].Resolve(package.namediff)); |
| 760 | if (url.is_valid()) |
| 761 | crx->crx_diffurls.push_back(url); |
| 762 | } |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 763 | |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 764 | ChangeItemState(crx, CrxUpdateItem::kCanUpdate); |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 765 | ++num_updates_pending; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 766 | } |
[email protected] | cf44261 | 2011-08-09 20:20:12 | [diff] [blame] | 767 | |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 768 | // All components that are not included in the update response are |
| 769 | // considered up to date. |
[email protected] | cf44261 | 2011-08-09 20:20:12 | [diff] [blame] | 770 | ChangeItemStatus(CrxUpdateItem::kChecking, CrxUpdateItem::kUpToDate); |
| 771 | |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 772 | // If there are updates pending we do a short wait, otherwise we take |
| 773 | // a longer delay until we check the components again. |
[email protected] | 21a9c9a | 2014-02-19 19:37:11 | [diff] [blame] | 774 | ScheduleNextRun(num_updates_pending > 0 ? kStepDelayShort : kStepDelayLong); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 775 | } |
| 776 | |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 777 | // TODO: record UMA stats. |
| 778 | void CrxUpdateService::OnUpdateCheckFailed(int error, |
| 779 | const std::string& error_message) { |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 780 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 781 | DCHECK(error); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 782 | size_t count = ChangeItemStatus(CrxUpdateItem::kChecking, |
| 783 | CrxUpdateItem::kNoUpdate); |
| 784 | DCHECK_GT(count, 0ul); |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 785 | ScheduleNextRun(kStepDelayLong); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | // Called when the CRX package has been downloaded to a temporary location. |
| 789 | // Here we fire the notifications and schedule the component-specific installer |
| 790 | // to be called in the file thread. |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 791 | void CrxUpdateService::DownloadComplete( |
| 792 | scoped_ptr<CRXContext> crx_context, |
[email protected] | 3a0092d | 2013-12-18 03:04:35 | [diff] [blame] | 793 | const CrxDownloader::Result& download_result) { |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 794 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 64f39fa1 | 2013-09-03 21:49:37 | [diff] [blame] | 795 | |
[email protected] | 64f39fa1 | 2013-09-03 21:49:37 | [diff] [blame] | 796 | CrxUpdateItem* crx = FindUpdateItemById(crx_context->id); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 797 | DCHECK(crx->status == CrxUpdateItem::kDownloadingDiff || |
| 798 | crx->status == CrxUpdateItem::kDownloading); |
| 799 | |
[email protected] | 3a0092d | 2013-12-18 03:04:35 | [diff] [blame] | 800 | AppendDownloadMetrics(crx_downloader_->download_metrics(), |
| 801 | &crx->download_metrics); |
| 802 | |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 803 | if (download_result.error) { |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 804 | if (crx->status == CrxUpdateItem::kDownloadingDiff) { |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 805 | crx->diff_error_category = kNetworkError; |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 806 | crx->diff_error_code = download_result.error; |
[email protected] | e630ba6 | 2013-06-22 15:22:34 | [diff] [blame] | 807 | crx->diff_update_failed = true; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 808 | size_t count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff, |
| 809 | CrxUpdateItem::kCanUpdate); |
| 810 | DCHECK_EQ(count, 1ul); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 811 | crx_downloader_.reset(); |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 812 | |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 813 | ScheduleNextRun(kStepDelayShort); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 814 | return; |
| 815 | } |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 816 | crx->error_category = kNetworkError; |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 817 | crx->error_code = download_result.error; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 818 | size_t count = ChangeItemStatus(CrxUpdateItem::kDownloading, |
| 819 | CrxUpdateItem::kNoUpdate); |
| 820 | DCHECK_EQ(count, 1ul); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 821 | crx_downloader_.reset(); |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 822 | |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 823 | // At this point, since both the differential and the full downloads failed, |
| 824 | // the update for this component has finished with an error. |
| 825 | ping_manager_->OnUpdateComplete(crx); |
| 826 | |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 827 | // Move on to the next update, if there is one available. |
| 828 | ScheduleNextRun(kStepDelayMedium); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 829 | } else { |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 830 | size_t count = 0; |
| 831 | if (crx->status == CrxUpdateItem::kDownloadingDiff) { |
| 832 | count = ChangeItemStatus(CrxUpdateItem::kDownloadingDiff, |
| 833 | CrxUpdateItem::kUpdatingDiff); |
| 834 | } else { |
| 835 | count = ChangeItemStatus(CrxUpdateItem::kDownloading, |
| 836 | CrxUpdateItem::kUpdating); |
| 837 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 838 | DCHECK_EQ(count, 1ul); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 839 | crx_downloader_.reset(); |
[email protected] | cf44261 | 2011-08-09 20:20:12 | [diff] [blame] | 840 | |
[email protected] | 44da56e | 2011-11-21 19:59:14 | [diff] [blame] | 841 | // Why unretained? See comment at top of file. |
[email protected] | 8f5f2ea | 2013-10-31 09:39:10 | [diff] [blame] | 842 | blocking_task_runner_->PostDelayedTask( |
[email protected] | 73251e7 | 2012-03-04 02:10:33 | [diff] [blame] | 843 | FROM_HERE, |
[email protected] | 44da56e | 2011-11-21 19:59:14 | [diff] [blame] | 844 | base::Bind(&CrxUpdateService::Install, |
| 845 | base::Unretained(this), |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 846 | base::Passed(&crx_context), |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 847 | download_result.response), |
[email protected] | 73251e7 | 2012-03-04 02:10:33 | [diff] [blame] | 848 | base::TimeDelta::FromMilliseconds(config_->StepDelay())); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 849 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | // Install consists of digital signature verification, unpacking and then |
| 853 | // calling the component specific installer. All that is handled by the |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 854 | // |unpacker_|. If there is an error this function is in charge of deleting |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 855 | // the files created. |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 856 | void CrxUpdateService::Install(scoped_ptr<CRXContext> context, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 857 | const base::FilePath& crx_path) { |
[email protected] | 8f5f2ea | 2013-10-31 09:39:10 | [diff] [blame] | 858 | // This function owns the file at |crx_path| and the |context| object. |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 859 | unpacker_.reset(new ComponentUnpacker(context->pk_hash, |
| 860 | crx_path, |
| 861 | context->fingerprint, |
| 862 | component_patcher_.get(), |
| 863 | context->installer, |
| 864 | blocking_task_runner_)); |
| 865 | unpacker_->Unpack(base::Bind(&CrxUpdateService::EndUnpacking, |
| 866 | base::Unretained(this), |
| 867 | context->id, |
| 868 | crx_path)); |
| 869 | } |
| 870 | |
| 871 | void CrxUpdateService::EndUnpacking(const std::string& component_id, |
| 872 | const base::FilePath& crx_path, |
| 873 | ComponentUnpacker::Error error, |
| 874 | int extended_error) { |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 875 | if (!DeleteFileAndEmptyParentDirectory(crx_path)) |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 876 | NOTREACHED() << crx_path.value(); |
[email protected] | 73251e7 | 2012-03-04 02:10:33 | [diff] [blame] | 877 | BrowserThread::PostDelayedTask( |
| 878 | BrowserThread::UI, |
| 879 | FROM_HERE, |
[email protected] | 44da56e | 2011-11-21 19:59:14 | [diff] [blame] | 880 | base::Bind(&CrxUpdateService::DoneInstalling, base::Unretained(this), |
[email protected] | f5d27e3 | 2014-01-31 06:48:53 | [diff] [blame] | 881 | component_id, error, extended_error), |
[email protected] | 73251e7 | 2012-03-04 02:10:33 | [diff] [blame] | 882 | base::TimeDelta::FromMilliseconds(config_->StepDelay())); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | // Installation has been completed. Adjust the component status and |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 886 | // schedule the next check. Schedule a short delay before trying the full |
| 887 | // update when the differential update failed. |
[email protected] | 07f93af1 | 2011-08-17 20:57:22 | [diff] [blame] | 888 | void CrxUpdateService::DoneInstalling(const std::string& component_id, |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 889 | ComponentUnpacker::Error error, |
| 890 | int extra_code) { |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 891 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
[email protected] | 07f93af1 | 2011-08-17 20:57:22 | [diff] [blame] | 892 | |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 893 | ErrorCategory error_category = kErrorNone; |
| 894 | switch (error) { |
| 895 | case ComponentUnpacker::kNone: |
| 896 | break; |
| 897 | case ComponentUnpacker::kInstallerError: |
| 898 | error_category = kInstallError; |
| 899 | break; |
| 900 | default: |
| 901 | error_category = kUnpackError; |
| 902 | break; |
| 903 | } |
| 904 | |
| 905 | const bool is_success = error == ComponentUnpacker::kNone; |
| 906 | |
[email protected] | 07f93af1 | 2011-08-17 20:57:22 | [diff] [blame] | 907 | CrxUpdateItem* item = FindUpdateItemById(component_id); |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 908 | if (item->status == CrxUpdateItem::kUpdatingDiff && !is_success) { |
| 909 | item->diff_error_category = error_category; |
| 910 | item->diff_error_code = error; |
| 911 | item->diff_extra_code1 = extra_code; |
[email protected] | 1ea21ad | 2013-09-02 04:20:39 | [diff] [blame] | 912 | item->diff_update_failed = true; |
| 913 | size_t count = ChangeItemStatus(CrxUpdateItem::kUpdatingDiff, |
| 914 | CrxUpdateItem::kCanUpdate); |
| 915 | DCHECK_EQ(count, 1ul); |
| 916 | ScheduleNextRun(kStepDelayShort); |
| 917 | return; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 918 | } |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 919 | |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 920 | if (is_success) { |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 921 | ChangeItemState(item, CrxUpdateItem::kUpdated); |
[email protected] | 07f93af1 | 2011-08-17 20:57:22 | [diff] [blame] | 922 | item->component.version = item->next_version; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 923 | item->component.fingerprint = item->next_fp; |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 924 | } else { |
[email protected] | 61aca4cd | 2013-10-26 10:50:59 | [diff] [blame] | 925 | ChangeItemState(item, CrxUpdateItem::kNoUpdate); |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 926 | item->error_category = error_category; |
| 927 | item->error_code = error; |
| 928 | item->extra_code1 = extra_code; |
[email protected] | e3e696d3 | 2013-06-21 20:41:36 | [diff] [blame] | 929 | } |
[email protected] | 07f93af1 | 2011-08-17 20:57:22 | [diff] [blame] | 930 | |
[email protected] | 7b052924 | 2013-07-20 05:45:46 | [diff] [blame] | 931 | ping_manager_->OnUpdateComplete(item); |
[email protected] | 360b8bb | 2011-09-01 21:48:06 | [diff] [blame] | 932 | |
[email protected] | 32a6c838 | 2013-08-20 00:29:20 | [diff] [blame] | 933 | // Move on to the next update, if there is one available. |
| 934 | ScheduleNextRun(kStepDelayMedium); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 935 | } |
| 936 | |
[email protected] | 85e61d5 | 2013-08-01 22:23:42 | [diff] [blame] | 937 | void CrxUpdateService::NotifyComponentObservers( |
| 938 | ComponentObserver::Events event, int extra) const { |
| 939 | for (UpdateItems::const_iterator it = work_items_.begin(); |
| 940 | it != work_items_.end(); ++it) { |
| 941 | ComponentObserver* observer = (*it)->component.observer; |
| 942 | if (observer) |
| 943 | observer->OnEvent(event, 0); |
| 944 | } |
| 945 | } |
| 946 | |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 947 | content::ResourceThrottle* CrxUpdateService::GetOnDemandResourceThrottle( |
| 948 | net::URLRequest* request, const std::string& crx_id) { |
| 949 | // We give the raw pointer to the caller, who will delete it at will |
| 950 | // and we keep for ourselves a weak pointer to it so we can post tasks |
| 951 | // from the UI thread without having to track lifetime directly. |
| 952 | CUResourceThrottle* rt = new CUResourceThrottle(request); |
| 953 | BrowserThread::PostTask( |
| 954 | BrowserThread::UI, |
| 955 | FROM_HERE, |
| 956 | base::Bind(&CrxUpdateService::OnNewResourceThrottle, |
| 957 | base::Unretained(this), |
| 958 | rt->AsWeakPtr(), |
| 959 | crx_id)); |
| 960 | return rt; |
| 961 | } |
| 962 | |
| 963 | void CrxUpdateService::OnNewResourceThrottle( |
| 964 | base::WeakPtr<CUResourceThrottle> rt, const std::string& crx_id) { |
| 965 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 966 | // Check if we can on-demand update, else unblock the request anyway. |
| 967 | CrxUpdateItem* item = FindUpdateItemById(crx_id); |
| 968 | Status status = OnDemandUpdateInternal(item); |
| 969 | if (status == kOk || status == kInProgress) { |
| 970 | item->throttles.push_back(rt); |
| 971 | return; |
| 972 | } |
| 973 | UnblockResourceThrottle(rt); |
| 974 | } |
| 975 | |
| 976 | /////////////////////////////////////////////////////////////////////////////// |
| 977 | |
| 978 | CUResourceThrottle::CUResourceThrottle(const net::URLRequest* request) |
| 979 | : state_(NEW) { |
| 980 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 981 | } |
| 982 | |
| 983 | CUResourceThrottle::~CUResourceThrottle() { |
| 984 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 985 | } |
| 986 | |
| 987 | void CUResourceThrottle::WillStartRequest(bool* defer) { |
| 988 | if (state_ != UNBLOCKED) { |
| 989 | state_ = BLOCKED; |
| 990 | *defer = true; |
| 991 | } else { |
| 992 | *defer = false; |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | void CUResourceThrottle::WillRedirectRequest(const GURL& new_url, bool* defer) { |
| 997 | WillStartRequest(defer); |
| 998 | } |
| 999 | |
[email protected] | f8fe5cf | 2013-12-04 20:11:53 | [diff] [blame] | 1000 | const char* CUResourceThrottle::GetNameForLogging() const { |
| 1001 | return "ComponentUpdateResourceThrottle"; |
| 1002 | } |
| 1003 | |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 1004 | void CUResourceThrottle::Unblock() { |
| 1005 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 1006 | if (state_ == BLOCKED) |
| 1007 | controller()->Resume(); |
| 1008 | state_ = UNBLOCKED; |
| 1009 | } |
| 1010 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 1011 | // The component update factory. Using the component updater as a singleton |
| 1012 | // is the job of the browser process. |
| 1013 | ComponentUpdateService* ComponentUpdateServiceFactory( |
| 1014 | ComponentUpdateService::Configurator* config) { |
| 1015 | DCHECK(config); |
| 1016 | return new CrxUpdateService(config); |
| 1017 | } |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 1018 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 1019 | } // namespace component_updater |
| 1020 | |