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