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