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