OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/update_client/update_client.h" | 5 #include "components/update_client/update_client.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <queue> | 8 #include <queue> |
9 #include <set> | 9 #include <set> |
10 #include <utility> | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
15 #include "base/callback.h" | 15 #include "base/callback.h" |
16 #include "base/location.h" | 16 #include "base/location.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/macros.h" | 18 #include "base/macros.h" |
| 19 #include "base/memory/ptr_util.h" |
19 #include "base/observer_list.h" | 20 #include "base/observer_list.h" |
20 #include "base/sequenced_task_runner.h" | 21 #include "base/sequenced_task_runner.h" |
21 #include "base/single_thread_task_runner.h" | 22 #include "base/single_thread_task_runner.h" |
22 #include "base/threading/sequenced_worker_pool.h" | 23 #include "base/threading/sequenced_worker_pool.h" |
23 #include "base/threading/thread_checker.h" | 24 #include "base/threading/thread_checker.h" |
24 #include "base/threading/thread_task_runner_handle.h" | 25 #include "base/threading/thread_task_runner_handle.h" |
25 #include "components/prefs/pref_registry_simple.h" | 26 #include "components/prefs/pref_registry_simple.h" |
26 #include "components/update_client/configurator.h" | 27 #include "components/update_client/configurator.h" |
27 #include "components/update_client/crx_update_item.h" | 28 #include "components/update_client/crx_update_item.h" |
28 #include "components/update_client/persisted_data.h" | 29 #include "components/update_client/persisted_data.h" |
29 #include "components/update_client/ping_manager.h" | 30 #include "components/update_client/ping_manager.h" |
| 31 #include "components/update_client/task_send_uninstall_ping.h" |
30 #include "components/update_client/task_update.h" | 32 #include "components/update_client/task_update.h" |
31 #include "components/update_client/update_checker.h" | 33 #include "components/update_client/update_checker.h" |
32 #include "components/update_client/update_client_errors.h" | 34 #include "components/update_client/update_client_errors.h" |
33 #include "components/update_client/update_client_internal.h" | 35 #include "components/update_client/update_client_internal.h" |
34 #include "components/update_client/update_engine.h" | 36 #include "components/update_client/update_engine.h" |
35 #include "components/update_client/update_response.h" | 37 #include "components/update_client/update_response.h" |
36 #include "components/update_client/utils.h" | 38 #include "components/update_client/utils.h" |
37 #include "url/gurl.h" | 39 #include "url/gurl.h" |
38 | 40 |
39 namespace update_client { | 41 namespace update_client { |
40 | 42 |
41 CrxUpdateItem::CrxUpdateItem() | 43 CrxUpdateItem::CrxUpdateItem() : state(ComponentState::kNew) {} |
42 : state(State::kNew), | 44 |
43 on_demand(false), | 45 CrxUpdateItem::~CrxUpdateItem() { |
44 diff_update_failed(false), | |
45 error_category(0), | |
46 error_code(0), | |
47 extra_code1(0), | |
48 diff_error_category(0), | |
49 diff_error_code(0), | |
50 diff_extra_code1(0) { | |
51 } | 46 } |
52 | 47 |
53 CrxUpdateItem::CrxUpdateItem(const CrxUpdateItem& other) = default; | 48 CrxUpdateItem::CrxUpdateItem(const CrxUpdateItem& other) = default; |
54 | 49 |
55 CrxUpdateItem::~CrxUpdateItem() { | |
56 } | |
57 | |
58 CrxComponent::CrxComponent() | 50 CrxComponent::CrxComponent() |
59 : allows_background_download(true), | 51 : allows_background_download(true), |
60 requires_network_encryption(true), | 52 requires_network_encryption(true), |
61 supports_group_policy_enable_component_updates(false) {} | 53 supports_group_policy_enable_component_updates(false) {} |
62 | 54 |
63 CrxComponent::CrxComponent(const CrxComponent& other) = default; | 55 CrxComponent::CrxComponent(const CrxComponent& other) = default; |
64 | 56 |
65 CrxComponent::~CrxComponent() { | 57 CrxComponent::~CrxComponent() { |
66 } | 58 } |
67 | 59 |
68 // It is important that an instance of the UpdateClient binds an unretained | 60 // It is important that an instance of the UpdateClient binds an unretained |
69 // pointer to itself. Otherwise, a life time circular dependency between this | 61 // pointer to itself. Otherwise, a life time circular dependency between this |
70 // instance and its inner members prevents the destruction of this instance. | 62 // instance and its inner members prevents the destruction of this instance. |
71 // Using unretained references is allowed in this case since the life time of | 63 // Using unretained references is allowed in this case since the life time of |
72 // the UpdateClient instance exceeds the life time of its inner members, | 64 // the UpdateClient instance exceeds the life time of its inner members, |
73 // including any thread objects that might execute callbacks bound to it. | 65 // including any thread objects that might execute callbacks bound to it. |
74 UpdateClientImpl::UpdateClientImpl( | 66 UpdateClientImpl::UpdateClientImpl( |
75 const scoped_refptr<Configurator>& config, | 67 const scoped_refptr<Configurator>& config, |
76 std::unique_ptr<PingManager> ping_manager, | 68 std::unique_ptr<PingManager> ping_manager, |
77 UpdateChecker::Factory update_checker_factory, | 69 UpdateChecker::Factory update_checker_factory, |
78 CrxDownloader::Factory crx_downloader_factory) | 70 CrxDownloader::Factory crx_downloader_factory) |
79 : is_stopped_(false), | 71 : is_stopped_(false), |
80 config_(config), | 72 config_(config), |
81 ping_manager_(std::move(ping_manager)), | 73 ping_manager_(std::move(ping_manager)), |
82 update_engine_( | 74 update_engine_(base::MakeUnique<UpdateEngine>( |
83 new UpdateEngine(config, | 75 config, |
84 update_checker_factory, | 76 update_checker_factory, |
85 crx_downloader_factory, | 77 crx_downloader_factory, |
86 ping_manager_.get(), | 78 ping_manager_.get(), |
87 base::Bind(&UpdateClientImpl::NotifyObservers, | 79 base::Bind(&UpdateClientImpl::NotifyObservers, |
88 base::Unretained(this)))) {} | 80 base::Unretained(this)))) {} |
89 | 81 |
90 UpdateClientImpl::~UpdateClientImpl() { | 82 UpdateClientImpl::~UpdateClientImpl() { |
91 DCHECK(thread_checker_.CalledOnValidThread()); | 83 DCHECK(thread_checker_.CalledOnValidThread()); |
92 | 84 |
93 DCHECK(task_queue_.empty()); | 85 DCHECK(task_queue_.empty()); |
94 DCHECK(tasks_.empty()); | 86 DCHECK(tasks_.empty()); |
95 | 87 |
96 config_ = nullptr; | 88 config_ = nullptr; |
97 } | 89 } |
98 | 90 |
99 void UpdateClientImpl::Install(const std::string& id, | 91 void UpdateClientImpl::Install(const std::string& id, |
100 const CrxDataCallback& crx_data_callback, | 92 const CrxDataCallback& crx_data_callback, |
101 const Callback& callback) { | 93 const Callback& callback) { |
102 DCHECK(thread_checker_.CalledOnValidThread()); | 94 DCHECK(thread_checker_.CalledOnValidThread()); |
103 | 95 |
104 if (IsUpdating(id)) { | 96 if (IsUpdating(id)) { |
105 callback.Run(Error::UPDATE_IN_PROGRESS); | 97 callback.Run(Error::UPDATE_IN_PROGRESS); |
106 return; | 98 return; |
107 } | 99 } |
108 | 100 |
109 std::vector<std::string> ids; | 101 std::vector<std::string> ids = {id}; |
110 ids.push_back(id); | |
111 | 102 |
112 // Partially applies |callback| to OnTaskComplete, so this argument is | 103 // Partially applies |callback| to OnTaskComplete, so this argument is |
113 // available when the task completes, along with the task itself. | 104 // available when the task completes, along with the task itself. |
114 std::unique_ptr<TaskUpdate> task(new TaskUpdate( | 105 std::unique_ptr<TaskUpdate> task = base::MakeUnique<TaskUpdate>( |
115 update_engine_.get(), true, ids, crx_data_callback, | 106 update_engine_.get(), true, ids, crx_data_callback, |
116 base::Bind(&UpdateClientImpl::OnTaskComplete, this, callback))); | 107 base::Bind(&UpdateClientImpl::OnTaskComplete, this, callback)); |
117 | 108 |
118 // Install tasks are run concurrently and never queued up. | 109 // Install tasks are run concurrently and never queued up. |
119 RunTask(std::move(task)); | 110 RunTask(std::move(task)); |
120 } | 111 } |
121 | 112 |
122 void UpdateClientImpl::Update(const std::vector<std::string>& ids, | 113 void UpdateClientImpl::Update(const std::vector<std::string>& ids, |
123 const CrxDataCallback& crx_data_callback, | 114 const CrxDataCallback& crx_data_callback, |
124 const Callback& callback) { | 115 const Callback& callback) { |
125 DCHECK(thread_checker_.CalledOnValidThread()); | 116 DCHECK(thread_checker_.CalledOnValidThread()); |
126 | 117 |
127 std::unique_ptr<TaskUpdate> task(new TaskUpdate( | 118 std::unique_ptr<TaskUpdate> task = base::MakeUnique<TaskUpdate>( |
128 update_engine_.get(), false, ids, crx_data_callback, | 119 update_engine_.get(), false, ids, crx_data_callback, |
129 base::Bind(&UpdateClientImpl::OnTaskComplete, this, callback))); | 120 base::Bind(&UpdateClientImpl::OnTaskComplete, this, callback)); |
130 | 121 |
131 // If no other tasks are running at the moment, run this update task. | 122 // If no other tasks are running at the moment, run this update task. |
132 // Otherwise, queue the task up. | 123 // Otherwise, queue the task up. |
133 if (tasks_.empty()) { | 124 if (tasks_.empty()) { |
134 RunTask(std::move(task)); | 125 RunTask(std::move(task)); |
135 } else { | 126 } else { |
136 task_queue_.push(task.release()); | 127 task_queue_.push(task.release()); |
137 } | 128 } |
138 } | 129 } |
139 | 130 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 task->Cancel(); | 222 task->Cancel(); |
232 } | 223 } |
233 } | 224 } |
234 | 225 |
235 void UpdateClientImpl::SendUninstallPing(const std::string& id, | 226 void UpdateClientImpl::SendUninstallPing(const std::string& id, |
236 const base::Version& version, | 227 const base::Version& version, |
237 int reason, | 228 int reason, |
238 const Callback& callback) { | 229 const Callback& callback) { |
239 DCHECK(thread_checker_.CalledOnValidThread()); | 230 DCHECK(thread_checker_.CalledOnValidThread()); |
240 | 231 |
241 // The implementation of PingManager::SendPing contains a self-deleting | 232 std::unique_ptr<TaskSendUninstallPing> task = |
242 // object responsible for sending the ping. | 233 base::MakeUnique<TaskSendUninstallPing>( |
243 CrxUpdateItem item; | 234 update_engine_.get(), id, version, reason, |
244 item.state = CrxUpdateItem::State::kUninstalled; | 235 base::Bind(&UpdateClientImpl::OnTaskComplete, base::Unretained(this), |
245 item.id = id; | 236 callback)); |
246 item.previous_version = version; | 237 RunTask(std::move(task)); |
247 item.next_version = base::Version("0"); | |
248 item.extra_code1 = reason; | |
249 | |
250 ping_manager_->SendPing(&item); | |
251 | |
252 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
253 FROM_HERE, base::Bind(callback, Error::NONE)); | |
254 } | 238 } |
255 | 239 |
256 scoped_refptr<UpdateClient> UpdateClientFactory( | 240 scoped_refptr<UpdateClient> UpdateClientFactory( |
257 const scoped_refptr<Configurator>& config) { | 241 const scoped_refptr<Configurator>& config) { |
258 std::unique_ptr<PingManager> ping_manager(new PingManager(config)); | 242 return base::MakeShared<UpdateClientImpl>( |
259 return new UpdateClientImpl(config, std::move(ping_manager), | 243 config, base::MakeUnique<PingManager>(config), &UpdateChecker::Create, |
260 &UpdateChecker::Create, &CrxDownloader::Create); | 244 &CrxDownloader::Create); |
261 } | 245 } |
262 | 246 |
263 void RegisterPrefs(PrefRegistrySimple* registry) { | 247 void RegisterPrefs(PrefRegistrySimple* registry) { |
264 PersistedData::RegisterPrefs(registry); | 248 PersistedData::RegisterPrefs(registry); |
265 } | 249 } |
266 | 250 |
267 } // namespace update_client | 251 } // namespace update_client |
OLD | NEW |