blob: 29b120ea45bf5c81581f424aa4138a2806b9703a [file] [log] [blame]
[email protected]de0fdca22014-08-19 05:26:091// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]e8f96ff2011-08-03 05:07:332// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]de0fdca22014-08-19 05:26:095#include "components/component_updater/component_updater_service.h"
[email protected]e8f96ff2011-08-03 05:07:336
7#include <algorithm>
sorin7c717622015-05-26 19:59:098#include <map>
9#include <string>
10#include <utility>
[email protected]e8f96ff2011-08-03 05:07:3311#include <vector>
12
[email protected]44da56e2011-11-21 19:59:1413#include "base/bind.h"
sorin395c2ac2014-09-16 21:31:0714#include "base/bind_helpers.h"
[email protected]78efe2e92014-08-08 15:53:2215#include "base/callback.h"
[email protected]e3e696d32013-06-21 20:41:3616#include "base/compiler_specific.h"
[email protected]57999812013-02-24 05:40:5217#include "base/files/file_path.h"
thestig819adcc82014-09-10 22:24:5318#include "base/files/file_util.h"
[email protected]e8f96ff2011-08-03 05:07:3319#include "base/logging.h"
sorin5cb1f5492014-09-23 04:07:4420#include "base/macros.h"
[email protected]7226b33c2011-08-18 08:44:2221#include "base/memory/scoped_ptr.h"
[email protected]f5d27e32014-01-31 06:48:5322#include "base/sequenced_task_runner.h"
sorin7c717622015-05-26 19:59:0923#include "base/single_thread_task_runner.h"
24#include "base/thread_task_runner_handle.h"
[email protected]8f5f2ea2013-10-31 09:39:1025#include "base/threading/sequenced_worker_pool.h"
[email protected]ed6fb982014-07-23 16:56:5226#include "base/threading/thread_checker.h"
[email protected]41a17c52013-06-28 00:27:5327#include "base/timer/timer.h"
sorin7c717622015-05-26 19:59:0928#include "components/component_updater/component_updater_service_internal.h"
29#include "components/component_updater/timer.h"
sorin52ac0882015-01-24 01:15:0030#include "components/update_client/configurator.h"
sorin52ac0882015-01-24 01:15:0031#include "components/update_client/crx_update_item.h"
sorin52ac0882015-01-24 01:15:0032#include "components/update_client/update_client.h"
sorin52ac0882015-01-24 01:15:0033#include "components/update_client/utils.h"
[email protected]761fa4702013-07-02 15:25:1534#include "url/gurl.h"
[email protected]e8f96ff2011-08-03 05:07:3335
sorin7c717622015-05-26 19:59:0936using CrxInstaller = update_client::CrxInstaller;
37using UpdateClient = update_client::UpdateClient;
sorin52ac0882015-01-24 01:15:0038
[email protected]055981f2014-01-17 20:22:3239namespace component_updater {
[email protected]3a0092d2013-12-18 03:04:3540
sorin7c717622015-05-26 19:59:0941CrxUpdateService::CrxUpdateService(
42 const scoped_refptr<Configurator>& config,
43 const scoped_refptr<UpdateClient>& update_client)
[email protected]e8f96ff2011-08-03 05:07:3344 : config_(config),
sorin7c717622015-05-26 19:59:0945 update_client_(update_client),
46 blocking_task_runner_(config->GetSequencedTaskRunner()) {
47 AddObserver(this);
[email protected]1ea21ad2013-09-02 04:20:3948}
[email protected]e8f96ff2011-08-03 05:07:3349
50CrxUpdateService::~CrxUpdateService() {
sorin7c717622015-05-26 19:59:0951 DCHECK(thread_checker_.CalledOnValidThread());
52
53 for (const auto item : ready_callbacks_) {
54 item.second.Run();
55 }
56
57 RemoveObserver(this);
58
[email protected]e8f96ff2011-08-03 05:07:3359 Stop();
[email protected]1ea21ad2013-09-02 04:20:3960}
[email protected]e8f96ff2011-08-03 05:07:3361
[email protected]d3268fe2014-04-25 02:14:2362void CrxUpdateService::AddObserver(Observer* observer) {
[email protected]ed6fb982014-07-23 16:56:5263 DCHECK(thread_checker_.CalledOnValidThread());
sorin7c717622015-05-26 19:59:0964 update_client_->AddObserver(observer);
[email protected]d3268fe2014-04-25 02:14:2365}
66
67void CrxUpdateService::RemoveObserver(Observer* observer) {
[email protected]ed6fb982014-07-23 16:56:5268 DCHECK(thread_checker_.CalledOnValidThread());
sorin7c717622015-05-26 19:59:0969 update_client_->RemoveObserver(observer);
[email protected]d3268fe2014-04-25 02:14:2370}
71
sorin7c717622015-05-26 19:59:0972void CrxUpdateService::Start() {
73 DCHECK(thread_checker_.CalledOnValidThread());
74 VLOG(1) << "CrxUpdateService starting up. "
75 << "First update attempt will take place in "
76 << config_->InitialDelay() << " seconds. "
77 << "Next update attempt will take place in "
78 << config_->NextCheckDelay() << " seconds. ";
[email protected]e8f96ff2011-08-03 05:07:3379
sorin7c717622015-05-26 19:59:0980 timer_.Start(
81 base::TimeDelta::FromSeconds(config_->InitialDelay()),
82 base::TimeDelta::FromSeconds(config_->NextCheckDelay()),
83 base::Bind(base::IgnoreResult(&CrxUpdateService::CheckForUpdates),
84 base::Unretained(this)));
[email protected]e8f96ff2011-08-03 05:07:3385}
86
sorin7c717622015-05-26 19:59:0987// Stops the update loop. In flight operations will be completed.
88void CrxUpdateService::Stop() {
89 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]fb53e652014-04-30 11:27:1990 VLOG(1) << "CrxUpdateService stopping";
[email protected]e8f96ff2011-08-03 05:07:3391 timer_.Stop();
[email protected]e8f96ff2011-08-03 05:07:3392}
93
94// Adds a component to be checked for upgrades. If the component exists it
sorin7c717622015-05-26 19:59:0995// it will be replaced.
96bool CrxUpdateService::RegisterComponent(const CrxComponent& component) {
[email protected]ed6fb982014-07-23 16:56:5297 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]d0c8b8b42014-05-06 05:11:4598 if (component.pk_hash.empty() || !component.version.IsValid() ||
sorin7c717622015-05-26 19:59:0999 !component.installer) {
100 return false;
[email protected]e8f96ff2011-08-03 05:07:33101 }
102
sorin7c717622015-05-26 19:59:09103 // Update the registration data if the component has been registered before.
104 const std::string id(GetCrxComponentID(component));
105 auto it = components_.find(id);
106 if (it != components_.end()) {
107 it->second = component;
108 return true;
[email protected]10bc0222014-06-11 13:44:38109 }
[email protected]e8f96ff2011-08-03 05:07:33110
sorin7c717622015-05-26 19:59:09111 components_.insert(std::make_pair(id, component));
112 components_order_.push_back(id);
113
114 // Create an initial state for this component. The state is mutated in
115 // response to events from the UpdateClient instance.
116 CrxUpdateItem item;
117 item.id = id;
118 item.component = component;
119 const auto inserted = component_states_.insert(std::make_pair(id, item));
120 DCHECK(inserted.second);
121
122 // Start the timer if this is the first component registered. The first timer
123 // event occurs after an interval defined by the component update
124 // configurator. The subsequent timer events are repeated with a period
125 // defined by the same configurator.
126 if (components_.size() == 1)
127 Start();
128
129 return true;
[email protected]e8f96ff2011-08-03 05:07:33130}
131
sorin7c717622015-05-26 19:59:09132bool CrxUpdateService::UnregisterComponent(const std::string& id) {
133 DCHECK(thread_checker_.CalledOnValidThread());
134 auto it = components_.find(id);
135 if (it == components_.end())
136 return false;
bauerb1f6657e72015-02-09 00:00:27137
sorin7c717622015-05-26 19:59:09138 DCHECK_EQ(id, it->first);
bauerb1f6657e72015-02-09 00:00:27139
sorin7c717622015-05-26 19:59:09140 // Delay the uninstall of the component if the component is being updated.
141 if (update_client_->IsUpdating(id)) {
142 components_pending_unregistration_.push_back(id);
143 return true;
144 }
145
146 return DoUnregisterComponent(it->second);
147}
148
149bool CrxUpdateService::DoUnregisterComponent(const CrxComponent& component) {
150 DCHECK(thread_checker_.CalledOnValidThread());
151
152 const auto id = GetCrxComponentID(component);
153 DCHECK(ready_callbacks_.find(id) == ready_callbacks_.end());
154
155 const bool result = component.installer->Uninstall();
156
157 const auto pos =
158 std::find(components_order_.begin(), components_order_.end(), id);
159 if (pos != components_order_.end())
160 components_order_.erase(pos);
161
162 components_.erase(id);
163 component_states_.erase(id);
164
165 return result;
bauerb1f6657e72015-02-09 00:00:27166}
167
[email protected]68bf09e2014-06-03 00:10:56168std::vector<std::string> CrxUpdateService::GetComponentIDs() const {
[email protected]ed6fb982014-07-23 16:56:52169 DCHECK(thread_checker_.CalledOnValidThread());
sorin7c717622015-05-26 19:59:09170 std::vector<std::string> ids;
171 for (const auto& it : components_)
172 ids.push_back(it.first);
173 return ids;
[email protected]68bf09e2014-06-03 00:10:56174}
175
[email protected]78efe2e92014-08-08 15:53:22176OnDemandUpdater& CrxUpdateService::GetOnDemandUpdater() {
sorin7c717622015-05-26 19:59:09177 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]78efe2e92014-08-08 15:53:22178 return *this;
179}
180
sorin7c717622015-05-26 19:59:09181const CrxComponent* CrxUpdateService::GetComponent(
182 const std::string& id) const {
183 DCHECK(thread_checker_.CalledOnValidThread());
184 const auto it(components_.find(id));
185 return it != components_.end() ? &(it->second) : NULL;
186}
187
188const CrxUpdateItem* CrxUpdateService::GetComponentState(
189 const std::string& id) const {
190 DCHECK(thread_checker_.CalledOnValidThread());
191 const auto it(component_states_.find(id));
192 return it != component_states_.end() ? &it->second : NULL;
193}
194
195void CrxUpdateService::MaybeThrottle(const std::string& id,
[email protected]78efe2e92014-08-08 15:53:22196 const base::Closure& callback) {
197 DCHECK(thread_checker_.CalledOnValidThread());
sorin7c717622015-05-26 19:59:09198 auto it = components_.find(id);
199 if (it != components_.end()) {
200 DCHECK_EQ(it->first, id);
201 if (OnDemandUpdateWithCooldown(id)) {
202 ready_callbacks_.insert(std::make_pair(id, callback));
203 return;
204 }
[email protected]78efe2e92014-08-08 15:53:22205 }
sorin7c717622015-05-26 19:59:09206
207 callback.Run(); // Unblock the request if the request can't be throttled.
208}
209
210bool CrxUpdateService::OnDemandUpdate(const std::string& id) {
211 DCHECK(thread_checker_.CalledOnValidThread());
212
213 if (!GetComponent(id))
214 return false;
215
216 return OnDemandUpdateInternal(id);
217}
218
219bool CrxUpdateService::OnDemandUpdateWithCooldown(const std::string& id) {
220 DCHECK(thread_checker_.CalledOnValidThread());
221
222 DCHECK(GetComponent(id));
223
224 // Check if the request is too soon.
225 const auto component_state(GetComponentState(id));
226 if (component_state) {
227 base::TimeDelta delta = base::Time::Now() - component_state->last_check;
228 if (delta < base::TimeDelta::FromSeconds(config_->OnDemandDelay()))
229 return false;
230 }
231
232 return OnDemandUpdateInternal(id);
233}
234
235bool CrxUpdateService::OnDemandUpdateInternal(const std::string& id) {
236 DCHECK(thread_checker_.CalledOnValidThread());
237
238 update_client_->Install(
239 id, base::Bind(&CrxUpdateService::OnUpdate, base::Unretained(this)),
240 base::Bind(&CrxUpdateService::OnUpdateComplete, base::Unretained(this)));
241
242 return true;
243}
244
245bool CrxUpdateService::CheckForUpdates() {
246 DCHECK(thread_checker_.CalledOnValidThread());
247 std::vector<std::string> ids;
248 for (const auto id : components_order_) {
249 DCHECK(components_.find(id) != components_.end());
250 ids.push_back(id);
251 }
252
253 update_client_->Update(
254 ids, base::Bind(&CrxUpdateService::OnUpdate, base::Unretained(this)),
255 base::Bind(&CrxUpdateService::OnUpdateComplete, base::Unretained(this)));
256
257 return true;
[email protected]78efe2e92014-08-08 15:53:22258}
259
260scoped_refptr<base::SequencedTaskRunner>
261CrxUpdateService::GetSequencedTaskRunner() {
sorin7c717622015-05-26 19:59:09262 DCHECK(thread_checker_.CalledOnValidThread());
bauerb1f6657e72015-02-09 00:00:27263 return blocking_task_runner_;
[email protected]78efe2e92014-08-08 15:53:22264}
265
sorin7c717622015-05-26 19:59:09266bool CrxUpdateService::GetComponentDetails(const std::string& id,
[email protected]f392e372014-06-12 07:25:57267 CrxUpdateItem* item) const {
[email protected]ed6fb982014-07-23 16:56:52268 DCHECK(thread_checker_.CalledOnValidThread());
sorin7c717622015-05-26 19:59:09269
270 // First, if this component is currently being updated, return its state from
271 // the update client.
272 if (update_client_->GetCrxUpdateState(id, item))
273 return true;
274
275 // Otherwise, return the last seen state of the component, if such a
276 // state exists.
277 const auto component_states_it = component_states_.find(id);
278 if (component_states_it != component_states_.end()) {
279 *item = component_states_it->second;
280 return true;
281 }
282
283 return false;
[email protected]2e919ddd2013-08-21 05:05:17284}
285
sorin7c717622015-05-26 19:59:09286void CrxUpdateService::OnUpdate(const std::vector<std::string>& ids,
287 std::vector<CrxComponent>* components) {
288 DCHECK(thread_checker_.CalledOnValidThread());
289 DCHECK(components->empty());
290
291 for (const auto& id : ids) {
292 const auto registered_component(GetComponent(id));
293 if (registered_component) {
294 components->push_back(*registered_component);
295 }
296 }
[email protected]ed6fb982014-07-23 16:56:52297}
298
sorin7c717622015-05-26 19:59:09299void CrxUpdateService::OnUpdateComplete(int error) {
300 DCHECK(thread_checker_.CalledOnValidThread());
301 VLOG(1) << "Update completed with error " << error;
302
303 for (const auto id : components_pending_unregistration_) {
304 if (!update_client_->IsUpdating(id)) {
305 const auto component = GetComponent(id);
306 if (component)
307 DoUnregisterComponent(*component);
308 }
309 }
310}
311
312void CrxUpdateService::OnEvent(Events event, const std::string& id) {
[email protected]ed6fb982014-07-23 16:56:52313 DCHECK(thread_checker_.CalledOnValidThread());
[email protected]93e8e2c2014-01-04 12:29:23314
sorin7c717622015-05-26 19:59:09315 // Unblock all throttles for the component.
316 if (event == Observer::Events::COMPONENT_UPDATED ||
317 event == Observer::Events::COMPONENT_NOT_UPDATED) {
318 auto callbacks = ready_callbacks_.equal_range(id);
319 for (auto it = callbacks.first; it != callbacks.second; ++it) {
320 it->second.Run();
321 }
322 ready_callbacks_.erase(id);
323 }
324
325 CrxUpdateItem update_item;
326 if (!update_client_->GetCrxUpdateState(id, &update_item))
[email protected]e8f96ff2011-08-03 05:07:33327 return;
[email protected]93e8e2c2014-01-04 12:29:23328
sorin7c717622015-05-26 19:59:09329 // Update the state of the item.
330 auto it = component_states_.find(id);
331 DCHECK(it != component_states_.end());
332 it->second = update_item;
bauerb1f6657e72015-02-09 00:00:27333
sorin7c717622015-05-26 19:59:09334 // Update the component registration with the new version.
335 if (event == Observer::Events::COMPONENT_UPDATED) {
336 auto component(const_cast<CrxComponent*>(GetComponent(id)));
337 if (component) {
338 component->version = update_item.next_version;
339 component->fingerprint = update_item.next_fp;
bauerb1f6657e72015-02-09 00:00:27340 }
341 }
[email protected]68bf09e2014-06-03 00:10:56342}
343
[email protected]00a77fa2013-11-02 04:18:46344///////////////////////////////////////////////////////////////////////////////
345
[email protected]e8f96ff2011-08-03 05:07:33346// The component update factory. Using the component updater as a singleton
347// is the job of the browser process.
sorin7c717622015-05-26 19:59:09348// TODO(sorin): consider making this a singleton.
sorin9797aba2015-04-17 17:15:03349scoped_ptr<ComponentUpdateService> ComponentUpdateServiceFactory(
350 const scoped_refptr<Configurator>& config) {
[email protected]e8f96ff2011-08-03 05:07:33351 DCHECK(config);
sorin7c717622015-05-26 19:59:09352 auto update_client = update_client::UpdateClientFactory(config);
353 return scoped_ptr<ComponentUpdateService>(
354 new CrxUpdateService(config, update_client.Pass()));
[email protected]e8f96ff2011-08-03 05:07:33355}
[email protected]2cddef42013-11-22 08:23:22356
[email protected]055981f2014-01-17 20:22:32357} // namespace component_updater