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"
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"
37 namespace update_client
{
39 CrxUpdateItem::CrxUpdateItem()
43 diff_update_failed(false),
47 diff_error_category(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
)
67 ping_manager_(ping_manager
.Pass()),
69 new UpdateEngine(config
,
70 update_checker_factory
,
71 crx_downloader_factory
,
73 base::Bind(&UpdateClientImpl::NotifyObservers
,
74 base::Unretained(this)))) {
77 UpdateClientImpl::~UpdateClientImpl() {
78 DCHECK(thread_checker_
.CalledOnValidThread());
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
);
92 std::vector
<std::string
> ids
;
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
);
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(
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
,
131 DCHECK(thread_checker_
.CalledOnValidThread());
134 base::ThreadTaskRunnerHandle::Get()->PostTask(
135 FROM_HERE
, base::Bind(completion_callback
, error
));
140 if (!task_queue_
.empty()) {
141 RunTask(task_queue_
.front(), completion_callback
);
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