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