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