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