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