Roll src/third_party/skia 99c7c07:4af6580
[chromium-blink-merge.git] / components / update_client / update_client.cc
blobce857e13ba81fd59a24ceff3f9ab1fe98caff7ac
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/update_client/update_client.h"
7 #include <algorithm>
8 #include <queue>
9 #include <set>
10 #include <utility>
11 #include <vector>
13 #include "base/bind.h"
14 #include "base/bind_helpers.h"
15 #include "base/callback.h"
16 #include "base/compiler_specific.h"
17 #include "base/location.h"
18 #include "base/logging.h"
19 #include "base/macros.h"
20 #include "base/observer_list.h"
21 #include "base/sequenced_task_runner.h"
22 #include "base/single_thread_task_runner.h"
23 #include "base/thread_task_runner_handle.h"
24 #include "base/threading/sequenced_worker_pool.h"
25 #include "base/threading/thread_checker.h"
26 #include "components/update_client/configurator.h"
27 #include "components/update_client/crx_update_item.h"
28 #include "components/update_client/ping_manager.h"
29 #include "components/update_client/task_update.h"
30 #include "components/update_client/update_checker.h"
31 #include "components/update_client/update_client_internal.h"
32 #include "components/update_client/update_engine.h"
33 #include "components/update_client/update_response.h"
34 #include "components/update_client/utils.h"
35 #include "url/gurl.h"
37 namespace update_client {
39 CrxUpdateItem::CrxUpdateItem()
40 : state(State::kNew),
41 unregistered(false),
42 on_demand(false),
43 diff_update_failed(false),
44 error_category(0),
45 error_code(0),
46 extra_code1(0),
47 diff_error_category(0),
48 diff_error_code(0),
49 diff_extra_code1(0) {
52 CrxUpdateItem::~CrxUpdateItem() {
55 CrxComponent::CrxComponent() : allow_background_download(true) {
58 CrxComponent::~CrxComponent() {
61 UpdateClientImpl::UpdateClientImpl(
62 const scoped_refptr<Configurator>& config,
63 scoped_ptr<PingManager> ping_manager,
64 UpdateChecker::Factory update_checker_factory,
65 CrxDownloader::Factory crx_downloader_factory)
66 : config_(config),
67 ping_manager_(ping_manager.Pass()),
68 update_engine_(
69 new UpdateEngine(config,
70 update_checker_factory,
71 crx_downloader_factory,
72 ping_manager_.get(),
73 base::Bind(&UpdateClientImpl::NotifyObservers,
74 base::Unretained(this)))) {
77 UpdateClientImpl::~UpdateClientImpl() {
78 DCHECK(thread_checker_.CalledOnValidThread());
79 config_ = nullptr;
82 void UpdateClientImpl::Install(const std::string& id,
83 const CrxDataCallback& crx_data_callback,
84 const CompletionCallback& completion_callback) {
85 DCHECK(thread_checker_.CalledOnValidThread());
87 if (update_engine_->IsUpdating(id)) {
88 completion_callback.Run(Error::ERROR_UPDATE_IN_PROGRESS);
89 return;
92 std::vector<std::string> ids;
93 ids.push_back(id);
95 scoped_ptr<TaskUpdate> task(
96 new TaskUpdate(update_engine_.get(), ids, crx_data_callback));
98 auto it = tasks_.insert(task.release()).first;
99 RunTask(*it, completion_callback);
102 void UpdateClientImpl::Update(const std::vector<std::string>& ids,
103 const CrxDataCallback& crx_data_callback,
104 const CompletionCallback& completion_callback) {
105 DCHECK(thread_checker_.CalledOnValidThread());
107 scoped_ptr<TaskUpdate> task(
108 new TaskUpdate(update_engine_.get(), ids, crx_data_callback));
109 if (tasks_.empty()) {
110 auto it = tasks_.insert(task.release()).first;
111 RunTask(*it, completion_callback);
112 } else {
113 task_queue_.push(task.release());
117 void UpdateClientImpl::RunTask(Task* task,
118 const CompletionCallback& completion_callback) {
119 DCHECK(thread_checker_.CalledOnValidThread());
120 base::ThreadTaskRunnerHandle::Get()->PostTask(
121 FROM_HERE,
122 base::Bind(&Task::Run, base::Unretained(task),
123 base::Bind(&UpdateClientImpl::OnTaskComplete,
124 base::Unretained(this), completion_callback)));
127 void UpdateClientImpl::OnTaskComplete(
128 const CompletionCallback& completion_callback,
129 Task* task,
130 int error) {
131 DCHECK(thread_checker_.CalledOnValidThread());
132 DCHECK(task);
134 base::ThreadTaskRunnerHandle::Get()->PostTask(
135 FROM_HERE, base::Bind(completion_callback, error));
137 tasks_.erase(task);
138 delete task;
140 if (!task_queue_.empty()) {
141 RunTask(task_queue_.front(), completion_callback);
142 task_queue_.pop();
146 void UpdateClientImpl::AddObserver(Observer* observer) {
147 DCHECK(thread_checker_.CalledOnValidThread());
148 observer_list_.AddObserver(observer);
151 void UpdateClientImpl::RemoveObserver(Observer* observer) {
152 DCHECK(thread_checker_.CalledOnValidThread());
153 observer_list_.RemoveObserver(observer);
156 void UpdateClientImpl::NotifyObservers(Observer::Events event,
157 const std::string& id) {
158 DCHECK(thread_checker_.CalledOnValidThread());
159 FOR_EACH_OBSERVER(Observer, observer_list_, OnEvent(event, id));
162 bool UpdateClientImpl::GetCrxUpdateState(const std::string& id,
163 CrxUpdateItem* update_item) const {
164 return update_engine_->GetUpdateState(id, update_item);
167 bool UpdateClientImpl::IsUpdating(const std::string& id) const {
168 return update_engine_->IsUpdating(id);
171 scoped_ptr<UpdateClient> UpdateClientFactory(
172 const scoped_refptr<Configurator>& config) {
173 scoped_ptr<PingManager> ping_manager(new PingManager(*config));
174 return scoped_ptr<UpdateClient>(
175 new UpdateClientImpl(config, ping_manager.Pass(), &UpdateChecker::Create,
176 &CrxDownloader::Create));
179 } // namespace update_client