[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> |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 8 | #include <map> |
| 9 | #include <string> |
| 10 | #include <utility> |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
[email protected] | 44da56e | 2011-11-21 19:59:14 | [diff] [blame] | 13 | #include "base/bind.h" |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 14 | #include "base/bind_helpers.h" |
[email protected] | 78efe2e9 | 2014-08-08 15:53:22 | [diff] [blame] | 15 | #include "base/callback.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" |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 20 | #include "base/metrics/histogram_macros.h" |
waffles | 77255cc | 2016-08-02 17:25:12 | [diff] [blame] | 21 | #include "base/strings/utf_string_conversions.h" |
[email protected] | ed6fb98 | 2014-07-23 16:56:52 | [diff] [blame] | 22 | #include "base/threading/thread_checker.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 23 | #include "base/threading/thread_task_runner_handle.h" |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 24 | #include "base/time/time.h" |
[email protected] | 41a17c5 | 2013-06-28 00:27:53 | [diff] [blame] | 25 | #include "base/timer/timer.h" |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 26 | #include "components/component_updater/component_updater_service_internal.h" |
| 27 | #include "components/component_updater/timer.h" |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 28 | #include "components/update_client/configurator.h" |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 29 | #include "components/update_client/crx_update_item.h" |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 30 | #include "components/update_client/update_client.h" |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 31 | #include "components/update_client/update_client_errors.h" |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 32 | #include "components/update_client/utils.h" |
[email protected] | 761fa470 | 2013-07-02 15:25:15 | [diff] [blame] | 33 | #include "url/gurl.h" |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 34 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 35 | using CrxInstaller = update_client::CrxInstaller; |
| 36 | using UpdateClient = update_client::UpdateClient; |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 37 | |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 38 | namespace { |
| 39 | |
| 40 | enum UpdateType { |
| 41 | UPDATE_TYPE_MANUAL = 0, |
| 42 | UPDATE_TYPE_AUTOMATIC, |
| 43 | UPDATE_TYPE_COUNT, |
| 44 | }; |
| 45 | |
| 46 | } // namespace |
| 47 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 48 | namespace component_updater { |
[email protected] | 3a0092d | 2013-12-18 03:04:35 | [diff] [blame] | 49 | |
Joshua Pawlicki | 0499ac8 | 2017-08-17 18:29:07 | [diff] [blame] | 50 | ComponentInfo::ComponentInfo(const std::string& id, |
| 51 | const std::string& fingerprint, |
| 52 | const base::string16& name, |
waffles | e7759f7 | 2016-10-10 23:41:25 | [diff] [blame] | 53 | const base::Version& version) |
Joshua Pawlicki | 0499ac8 | 2017-08-17 18:29:07 | [diff] [blame] | 54 | : id(id), fingerprint(fingerprint), name(name), version(version) {} |
| 55 | ComponentInfo::ComponentInfo(const ComponentInfo& other) = default; |
| 56 | ComponentInfo::ComponentInfo(ComponentInfo&& other) = default; |
waffles | 77255cc | 2016-08-02 17:25:12 | [diff] [blame] | 57 | ComponentInfo::~ComponentInfo() {} |
| 58 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 59 | CrxUpdateService::CrxUpdateService( |
| 60 | const scoped_refptr<Configurator>& config, |
| 61 | const scoped_refptr<UpdateClient>& update_client) |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 62 | : config_(config), update_client_(update_client) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 63 | AddObserver(this); |
[email protected] | 1ea21ad | 2013-09-02 04:20:39 | [diff] [blame] | 64 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 65 | |
| 66 | CrxUpdateService::~CrxUpdateService() { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 67 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 68 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 69 | for (auto& item : ready_callbacks_) { |
| 70 | std::move(item.second).Run(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | RemoveObserver(this); |
| 74 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 75 | Stop(); |
[email protected] | 1ea21ad | 2013-09-02 04:20:39 | [diff] [blame] | 76 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 77 | |
[email protected] | d3268fe | 2014-04-25 02:14:23 | [diff] [blame] | 78 | void CrxUpdateService::AddObserver(Observer* observer) { |
[email protected] | ed6fb98 | 2014-07-23 16:56:52 | [diff] [blame] | 79 | DCHECK(thread_checker_.CalledOnValidThread()); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 80 | update_client_->AddObserver(observer); |
[email protected] | d3268fe | 2014-04-25 02:14:23 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void CrxUpdateService::RemoveObserver(Observer* observer) { |
[email protected] | ed6fb98 | 2014-07-23 16:56:52 | [diff] [blame] | 84 | DCHECK(thread_checker_.CalledOnValidThread()); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 85 | update_client_->RemoveObserver(observer); |
[email protected] | d3268fe | 2014-04-25 02:14:23 | [diff] [blame] | 86 | } |
| 87 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 88 | void CrxUpdateService::Start() { |
| 89 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 90 | VLOG(1) << "CrxUpdateService starting up. " |
| 91 | << "First update attempt will take place in " |
| 92 | << config_->InitialDelay() << " seconds. " |
| 93 | << "Next update attempt will take place in " |
| 94 | << config_->NextCheckDelay() << " seconds. "; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 95 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 96 | timer_.Start( |
| 97 | base::TimeDelta::FromSeconds(config_->InitialDelay()), |
| 98 | base::TimeDelta::FromSeconds(config_->NextCheckDelay()), |
| 99 | base::Bind(base::IgnoreResult(&CrxUpdateService::CheckForUpdates), |
| 100 | base::Unretained(this))); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 101 | } |
| 102 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 103 | // Stops the update loop. In flight operations will be completed. |
| 104 | void CrxUpdateService::Stop() { |
| 105 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | fb53e65 | 2014-04-30 11:27:19 | [diff] [blame] | 106 | VLOG(1) << "CrxUpdateService stopping"; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 107 | timer_.Stop(); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 108 | update_client_->Stop(); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // Adds a component to be checked for upgrades. If the component exists it |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 112 | // it will be replaced. |
| 113 | bool CrxUpdateService::RegisterComponent(const CrxComponent& component) { |
[email protected] | ed6fb98 | 2014-07-23 16:56:52 | [diff] [blame] | 114 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 115 | if (component.pk_hash.empty() || !component.version.IsValid() || |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 116 | !component.installer) { |
| 117 | return false; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 118 | } |
| 119 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 120 | // Update the registration data if the component has been registered before. |
| 121 | const std::string id(GetCrxComponentID(component)); |
| 122 | auto it = components_.find(id); |
| 123 | if (it != components_.end()) { |
| 124 | it->second = component; |
| 125 | return true; |
[email protected] | 10bc022 | 2014-06-11 13:44:38 | [diff] [blame] | 126 | } |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 127 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 128 | components_.insert(std::make_pair(id, component)); |
| 129 | components_order_.push_back(id); |
waffles | 77255cc | 2016-08-02 17:25:12 | [diff] [blame] | 130 | for (const auto& mime_type : component.handled_mime_types) |
| 131 | component_ids_by_mime_type_[mime_type] = id; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 132 | |
| 133 | // Create an initial state for this component. The state is mutated in |
| 134 | // response to events from the UpdateClient instance. |
| 135 | CrxUpdateItem item; |
| 136 | item.id = id; |
| 137 | item.component = component; |
| 138 | const auto inserted = component_states_.insert(std::make_pair(id, item)); |
| 139 | DCHECK(inserted.second); |
| 140 | |
| 141 | // Start the timer if this is the first component registered. The first timer |
| 142 | // event occurs after an interval defined by the component update |
| 143 | // configurator. The subsequent timer events are repeated with a period |
| 144 | // defined by the same configurator. |
| 145 | if (components_.size() == 1) |
| 146 | Start(); |
| 147 | |
| 148 | return true; |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 149 | } |
| 150 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 151 | bool CrxUpdateService::UnregisterComponent(const std::string& id) { |
| 152 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 153 | auto it = components_.find(id); |
| 154 | if (it == components_.end()) |
| 155 | return false; |
bauerb | 1f6657e7 | 2015-02-09 00:00:27 | [diff] [blame] | 156 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 157 | DCHECK_EQ(id, it->first); |
bauerb | 1f6657e7 | 2015-02-09 00:00:27 | [diff] [blame] | 158 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 159 | // Delay the uninstall of the component if the component is being updated. |
| 160 | if (update_client_->IsUpdating(id)) { |
| 161 | components_pending_unregistration_.push_back(id); |
| 162 | return true; |
| 163 | } |
| 164 | |
| 165 | return DoUnregisterComponent(it->second); |
| 166 | } |
| 167 | |
| 168 | bool CrxUpdateService::DoUnregisterComponent(const CrxComponent& component) { |
| 169 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 170 | |
| 171 | const auto id = GetCrxComponentID(component); |
| 172 | DCHECK(ready_callbacks_.find(id) == ready_callbacks_.end()); |
| 173 | |
| 174 | const bool result = component.installer->Uninstall(); |
| 175 | |
| 176 | const auto pos = |
| 177 | std::find(components_order_.begin(), components_order_.end(), id); |
| 178 | if (pos != components_order_.end()) |
| 179 | components_order_.erase(pos); |
| 180 | |
| 181 | components_.erase(id); |
| 182 | component_states_.erase(id); |
| 183 | |
| 184 | return result; |
bauerb | 1f6657e7 | 2015-02-09 00:00:27 | [diff] [blame] | 185 | } |
| 186 | |
[email protected] | 68bf09e | 2014-06-03 00:10:56 | [diff] [blame] | 187 | std::vector<std::string> CrxUpdateService::GetComponentIDs() const { |
[email protected] | ed6fb98 | 2014-07-23 16:56:52 | [diff] [blame] | 188 | DCHECK(thread_checker_.CalledOnValidThread()); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 189 | std::vector<std::string> ids; |
| 190 | for (const auto& it : components_) |
| 191 | ids.push_back(it.first); |
| 192 | return ids; |
[email protected] | 68bf09e | 2014-06-03 00:10:56 | [diff] [blame] | 193 | } |
| 194 | |
waffles | 77255cc | 2016-08-02 17:25:12 | [diff] [blame] | 195 | std::unique_ptr<ComponentInfo> CrxUpdateService::GetComponentForMimeType( |
| 196 | const std::string& mime_type) const { |
| 197 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 198 | const auto it = component_ids_by_mime_type_.find(mime_type); |
| 199 | if (it == component_ids_by_mime_type_.end()) |
| 200 | return nullptr; |
vmpstr | 6d9996c8 | 2017-02-23 00:43:25 | [diff] [blame] | 201 | auto* const component = GetComponent(it->second); |
waffles | 77255cc | 2016-08-02 17:25:12 | [diff] [blame] | 202 | if (!component) |
| 203 | return nullptr; |
Gyuyoung Kim | 6afb508 | 2018-01-19 13:35:57 | [diff] [blame] | 204 | return std::make_unique<ComponentInfo>( |
Joshua Pawlicki | 0499ac8 | 2017-08-17 18:29:07 | [diff] [blame] | 205 | GetCrxComponentID(*component), component->fingerprint, |
| 206 | base::UTF8ToUTF16(component->name), component->version); |
| 207 | } |
| 208 | |
| 209 | std::vector<ComponentInfo> CrxUpdateService::GetComponents() const { |
| 210 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 211 | std::vector<ComponentInfo> result; |
| 212 | for (const auto& it : components_) { |
| 213 | result.push_back(ComponentInfo(it.first, it.second.fingerprint, |
| 214 | base::UTF8ToUTF16(it.second.name), |
| 215 | it.second.version)); |
| 216 | } |
| 217 | return result; |
waffles | 77255cc | 2016-08-02 17:25:12 | [diff] [blame] | 218 | } |
| 219 | |
[email protected] | 78efe2e9 | 2014-08-08 15:53:22 | [diff] [blame] | 220 | OnDemandUpdater& CrxUpdateService::GetOnDemandUpdater() { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 221 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 78efe2e9 | 2014-08-08 15:53:22 | [diff] [blame] | 222 | return *this; |
| 223 | } |
| 224 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 225 | const CrxComponent* CrxUpdateService::GetComponent( |
| 226 | const std::string& id) const { |
| 227 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 228 | const auto it(components_.find(id)); |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 229 | return it != components_.end() ? &(it->second) : nullptr; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | const CrxUpdateItem* CrxUpdateService::GetComponentState( |
| 233 | const std::string& id) const { |
| 234 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 235 | const auto it(component_states_.find(id)); |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 236 | return it != component_states_.end() ? &it->second : nullptr; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void CrxUpdateService::MaybeThrottle(const std::string& id, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 240 | base::OnceClosure callback) { |
[email protected] | 78efe2e9 | 2014-08-08 15:53:22 | [diff] [blame] | 241 | DCHECK(thread_checker_.CalledOnValidThread()); |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 242 | const auto it = components_.find(id); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 243 | if (it != components_.end()) { |
| 244 | DCHECK_EQ(it->first, id); |
| 245 | if (OnDemandUpdateWithCooldown(id)) { |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 246 | ready_callbacks_.insert(std::make_pair(id, std::move(callback))); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 247 | return; |
| 248 | } |
[email protected] | 78efe2e9 | 2014-08-08 15:53:22 | [diff] [blame] | 249 | } |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 250 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 251 | // Unblock the request if the request can't be throttled. |
| 252 | std::move(callback).Run(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 253 | } |
| 254 | |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 255 | void CrxUpdateService::OnDemandUpdate(const std::string& id, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 256 | Callback callback) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 257 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 258 | |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 259 | if (!GetComponent(id)) { |
drbasic | 6d4d9ce | 2017-02-22 10:33:59 | [diff] [blame] | 260 | if (!callback.is_null()) { |
| 261 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 262 | FROM_HERE, base::BindOnce(std::move(callback), |
| 263 | update_client::Error::INVALID_ARGUMENT)); |
drbasic | 6d4d9ce | 2017-02-22 10:33:59 | [diff] [blame] | 264 | } |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 265 | return; |
| 266 | } |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 267 | |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 268 | OnDemandUpdateInternal(id, std::move(callback)); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | bool CrxUpdateService::OnDemandUpdateWithCooldown(const std::string& id) { |
| 272 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 273 | |
| 274 | DCHECK(GetComponent(id)); |
| 275 | |
| 276 | // Check if the request is too soon. |
vmpstr | 2de366b | 2016-07-20 21:35:48 | [diff] [blame] | 277 | const auto* component_state(GetComponentState(id)); |
Ivan Afanasyev | ec822ac | 2017-09-05 14:10:06 | [diff] [blame] | 278 | if (component_state && !component_state->last_check.is_null()) { |
sorin | 1827db1 | 2016-09-15 20:43:31 | [diff] [blame] | 279 | base::TimeDelta delta = |
| 280 | base::TimeTicks::Now() - component_state->last_check; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 281 | if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay())) |
| 282 | return false; |
| 283 | } |
| 284 | |
sorin | 842703b | 2016-11-02 23:59:23 | [diff] [blame] | 285 | OnDemandUpdateInternal(id, Callback()); |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 286 | return true; |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 287 | } |
| 288 | |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 289 | void CrxUpdateService::OnDemandUpdateInternal(const std::string& id, |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 290 | Callback callback) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 291 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 292 | |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 293 | UMA_HISTOGRAM_ENUMERATION("ComponentUpdater.Calls", UPDATE_TYPE_MANUAL, |
| 294 | UPDATE_TYPE_COUNT); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 295 | update_client_->Install( |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 296 | id, base::BindOnce(&CrxUpdateService::OnUpdate, base::Unretained(this)), |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 297 | base::BindOnce(&CrxUpdateService::OnUpdateComplete, |
| 298 | base::Unretained(this), std::move(callback), |
| 299 | base::TimeTicks::Now())); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | bool CrxUpdateService::CheckForUpdates() { |
| 303 | DCHECK(thread_checker_.CalledOnValidThread()); |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 304 | |
| 305 | UMA_HISTOGRAM_ENUMERATION("ComponentUpdater.Calls", UPDATE_TYPE_AUTOMATIC, |
| 306 | UPDATE_TYPE_COUNT); |
| 307 | |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 308 | std::vector<std::string> secure_ids; // Requires HTTPS for update checks. |
| 309 | std::vector<std::string> unsecure_ids; // Can fallback to HTTP. |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 310 | for (const auto id : components_order_) { |
| 311 | DCHECK(components_.find(id) != components_.end()); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 312 | |
vmpstr | 2de366b | 2016-07-20 21:35:48 | [diff] [blame] | 313 | auto* component(GetComponent(id)); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 314 | if (!component || component->requires_network_encryption) |
| 315 | secure_ids.push_back(id); |
| 316 | else |
| 317 | unsecure_ids.push_back(id); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 318 | } |
| 319 | |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 320 | if (!unsecure_ids.empty()) { |
| 321 | update_client_->Update( |
| 322 | unsecure_ids, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 323 | base::BindOnce(&CrxUpdateService::OnUpdate, base::Unretained(this)), |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 324 | base::BindOnce(&CrxUpdateService::OnUpdateComplete, |
| 325 | base::Unretained(this), Callback(), |
| 326 | base::TimeTicks::Now())); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | if (!secure_ids.empty()) { |
| 330 | update_client_->Update( |
| 331 | secure_ids, |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 332 | base::BindOnce(&CrxUpdateService::OnUpdate, base::Unretained(this)), |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 333 | base::BindOnce(&CrxUpdateService::OnUpdateComplete, |
| 334 | base::Unretained(this), Callback(), |
| 335 | base::TimeTicks::Now())); |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 336 | } |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 337 | |
| 338 | return true; |
[email protected] | 78efe2e9 | 2014-08-08 15:53:22 | [diff] [blame] | 339 | } |
| 340 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 341 | bool CrxUpdateService::GetComponentDetails(const std::string& id, |
[email protected] | f392e37 | 2014-06-12 07:25:57 | [diff] [blame] | 342 | CrxUpdateItem* item) const { |
[email protected] | ed6fb98 | 2014-07-23 16:56:52 | [diff] [blame] | 343 | DCHECK(thread_checker_.CalledOnValidThread()); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 344 | |
| 345 | // First, if this component is currently being updated, return its state from |
| 346 | // the update client. |
| 347 | if (update_client_->GetCrxUpdateState(id, item)) |
| 348 | return true; |
| 349 | |
| 350 | // Otherwise, return the last seen state of the component, if such a |
| 351 | // state exists. |
| 352 | const auto component_states_it = component_states_.find(id); |
| 353 | if (component_states_it != component_states_.end()) { |
| 354 | *item = component_states_it->second; |
| 355 | return true; |
| 356 | } |
| 357 | |
| 358 | return false; |
[email protected] | 2e919ddd | 2013-08-21 05:05:17 | [diff] [blame] | 359 | } |
| 360 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 361 | void CrxUpdateService::OnUpdate(const std::vector<std::string>& ids, |
| 362 | std::vector<CrxComponent>* components) { |
| 363 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 364 | DCHECK(components->empty()); |
| 365 | |
| 366 | for (const auto& id : ids) { |
sorin | eae115a | 2016-08-26 02:27:20 | [diff] [blame] | 367 | const update_client::CrxComponent* registered_component(GetComponent(id)); |
sorin | 97bd029 | 2016-11-14 19:46:53 | [diff] [blame] | 368 | if (registered_component) |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 369 | components->push_back(*registered_component); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 370 | } |
[email protected] | ed6fb98 | 2014-07-23 16:56:52 | [diff] [blame] | 371 | } |
| 372 | |
sorin | 842703b | 2016-11-02 23:59:23 | [diff] [blame] | 373 | void CrxUpdateService::OnUpdateComplete(Callback callback, |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 374 | const base::TimeTicks& start_time, |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 375 | update_client::Error error) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 376 | DCHECK(thread_checker_.CalledOnValidThread()); |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 377 | VLOG(1) << "Update completed with error " << static_cast<int>(error); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 378 | |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 379 | UMA_HISTOGRAM_BOOLEAN("ComponentUpdater.UpdateCompleteResult", |
| 380 | error != update_client::Error::NONE); |
sorin | 85953dc | 2016-03-10 00:32:48 | [diff] [blame] | 381 | UMA_HISTOGRAM_LONG_TIMES_100("ComponentUpdater.UpdateCompleteTime", |
| 382 | base::TimeTicks::Now() - start_time); |
| 383 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 384 | for (const auto id : components_pending_unregistration_) { |
| 385 | if (!update_client_->IsUpdating(id)) { |
vmpstr | 2de366b | 2016-07-20 21:35:48 | [diff] [blame] | 386 | const auto* component = GetComponent(id); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 387 | if (component) |
| 388 | DoUnregisterComponent(*component); |
| 389 | } |
| 390 | } |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 391 | |
| 392 | if (!callback.is_null()) { |
Sorin Jianu | ebd65246 | 2017-07-23 02:00:58 | [diff] [blame] | 393 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
Vladislav Kuzkokov | 12eca79 | 2017-10-20 12:45:38 | [diff] [blame] | 394 | FROM_HERE, base::BindOnce(std::move(callback), error)); |
sorin | 4c52018 | 2016-08-19 17:27:44 | [diff] [blame] | 395 | } |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | void CrxUpdateService::OnEvent(Events event, const std::string& id) { |
[email protected] | ed6fb98 | 2014-07-23 16:56:52 | [diff] [blame] | 399 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 400 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 401 | // Unblock all throttles for the component. |
| 402 | if (event == Observer::Events::COMPONENT_UPDATED || |
Sorin Jianu | cbb10e1 | 2018-01-23 18:01:44 | [diff] [blame^] | 403 | event == Observer::Events::COMPONENT_NOT_UPDATED || |
| 404 | event == Observer::Events::COMPONENT_UPDATE_ERROR) { |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 405 | auto callbacks = ready_callbacks_.equal_range(id); |
| 406 | for (auto it = callbacks.first; it != callbacks.second; ++it) { |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 407 | std::move(it->second).Run(); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 408 | } |
| 409 | ready_callbacks_.erase(id); |
| 410 | } |
| 411 | |
| 412 | CrxUpdateItem update_item; |
| 413 | if (!update_client_->GetCrxUpdateState(id, &update_item)) |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 414 | return; |
[email protected] | 93e8e2c | 2014-01-04 12:29:23 | [diff] [blame] | 415 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 416 | // Update the state of the item. |
| 417 | auto it = component_states_.find(id); |
| 418 | DCHECK(it != component_states_.end()); |
| 419 | it->second = update_item; |
bauerb | 1f6657e7 | 2015-02-09 00:00:27 | [diff] [blame] | 420 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 421 | // Update the component registration with the new version. |
| 422 | if (event == Observer::Events::COMPONENT_UPDATED) { |
vmpstr | 2de366b | 2016-07-20 21:35:48 | [diff] [blame] | 423 | auto* component(const_cast<CrxComponent*>(GetComponent(id))); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 424 | if (component) { |
| 425 | component->version = update_item.next_version; |
| 426 | component->fingerprint = update_item.next_fp; |
bauerb | 1f6657e7 | 2015-02-09 00:00:27 | [diff] [blame] | 427 | } |
| 428 | } |
[email protected] | 68bf09e | 2014-06-03 00:10:56 | [diff] [blame] | 429 | } |
| 430 | |
[email protected] | 00a77fa | 2013-11-02 04:18:46 | [diff] [blame] | 431 | /////////////////////////////////////////////////////////////////////////////// |
| 432 | |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 433 | // The component update factory. Using the component updater as a singleton |
| 434 | // is the job of the browser process. |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 435 | // TODO(sorin): consider making this a singleton. |
dcheng | a0ee5fb8 | 2016-04-26 02:46:55 | [diff] [blame] | 436 | std::unique_ptr<ComponentUpdateService> ComponentUpdateServiceFactory( |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 437 | const scoped_refptr<Configurator>& config) { |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 438 | DCHECK(config); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 439 | auto update_client = update_client::UpdateClientFactory(config); |
Gyuyoung Kim | 6afb508 | 2018-01-19 13:35:57 | [diff] [blame] | 440 | return std::make_unique<CrxUpdateService>(config, std::move(update_client)); |
[email protected] | e8f96ff | 2011-08-03 05:07:33 | [diff] [blame] | 441 | } |
[email protected] | 2cddef4 | 2013-11-22 08:23:22 | [diff] [blame] | 442 | |
[email protected] | 055981f | 2014-01-17 20:22:32 | [diff] [blame] | 443 | } // namespace component_updater |