sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "components/update_client/update_client.h" |
| 6 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | #include <queue> |
| 9 | #include <set> |
| 10 | #include <utility> |
| 11 | #include <vector> |
| 12 | |
| 13 | #include "base/bind.h" |
| 14 | #include "base/bind_helpers.h" |
| 15 | #include "base/callback.h" |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 16 | #include "base/location.h" |
| 17 | #include "base/logging.h" |
| 18 | #include "base/macros.h" |
| 19 | #include "base/observer_list.h" |
| 20 | #include "base/sequenced_task_runner.h" |
| 21 | #include "base/single_thread_task_runner.h" |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 22 | #include "base/threading/sequenced_worker_pool.h" |
| 23 | #include "base/threading/thread_checker.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 24 | #include "base/threading/thread_task_runner_handle.h" |
waffles | d2d9a33 | 2016-04-09 01:59:57 | [diff] [blame] | 25 | #include "components/prefs/pref_registry_simple.h" |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 26 | #include "components/update_client/configurator.h" |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 27 | #include "components/update_client/crx_update_item.h" |
waffles | d2d9a33 | 2016-04-09 01:59:57 | [diff] [blame] | 28 | #include "components/update_client/persisted_data.h" |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 29 | #include "components/update_client/ping_manager.h" |
| 30 | #include "components/update_client/task_update.h" |
| 31 | #include "components/update_client/update_checker.h" |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 32 | #include "components/update_client/update_client_errors.h" |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 33 | #include "components/update_client/update_client_internal.h" |
| 34 | #include "components/update_client/update_engine.h" |
| 35 | #include "components/update_client/update_response.h" |
| 36 | #include "components/update_client/utils.h" |
| 37 | #include "url/gurl.h" |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 38 | |
| 39 | namespace update_client { |
| 40 | |
| 41 | CrxUpdateItem::CrxUpdateItem() |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 42 | : state(State::kNew), |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 43 | on_demand(false), |
| 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 | } |
| 52 | |
vmpstr | b6449d51 | 2016-02-25 23:55:40 | [diff] [blame] | 53 | CrxUpdateItem::CrxUpdateItem(const CrxUpdateItem& other) = default; |
| 54 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 55 | CrxUpdateItem::~CrxUpdateItem() { |
| 56 | } |
| 57 | |
sorin | fccbf2d | 2016-04-04 20:34:34 | [diff] [blame] | 58 | CrxComponent::CrxComponent() |
sorin | cb4e5e9 | 2016-08-02 21:48:40 | [diff] [blame] | 59 | : allows_background_download(true), |
| 60 | requires_network_encryption(true), |
| 61 | supports_group_policy_enable_component_updates(false) {} |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 62 | |
vmpstr | b6449d51 | 2016-02-25 23:55:40 | [diff] [blame] | 63 | CrxComponent::CrxComponent(const CrxComponent& other) = default; |
| 64 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 65 | CrxComponent::~CrxComponent() { |
| 66 | } |
| 67 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 68 | // It is important that an instance of the UpdateClient binds an unretained |
| 69 | // pointer to itself. Otherwise, a life time circular dependency between this |
| 70 | // 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 |
| 72 | // the UpdateClient instance exceeds the life time of its inner members, |
| 73 | // including any thread objects that might execute callbacks bound to it. |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 74 | UpdateClientImpl::UpdateClientImpl( |
| 75 | const scoped_refptr<Configurator>& config, |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 76 | std::unique_ptr<PingManager> ping_manager, |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 77 | UpdateChecker::Factory update_checker_factory, |
| 78 | CrxDownloader::Factory crx_downloader_factory) |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 79 | : is_stopped_(false), |
| 80 | config_(config), |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 81 | ping_manager_(std::move(ping_manager)), |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 82 | update_engine_( |
| 83 | new UpdateEngine(config, |
| 84 | update_checker_factory, |
| 85 | crx_downloader_factory, |
| 86 | ping_manager_.get(), |
| 87 | base::Bind(&UpdateClientImpl::NotifyObservers, |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 88 | base::Unretained(this)))) {} |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 89 | |
| 90 | UpdateClientImpl::~UpdateClientImpl() { |
| 91 | DCHECK(thread_checker_.CalledOnValidThread()); |
sorin | b318cc84a | 2015-10-30 23:19:51 | [diff] [blame] | 92 | |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 93 | DCHECK(task_queue_.empty()); |
| 94 | DCHECK(tasks_.empty()); |
sorin | b318cc84a | 2015-10-30 23:19:51 | [diff] [blame] | 95 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 96 | config_ = nullptr; |
| 97 | } |
| 98 | |
| 99 | void UpdateClientImpl::Install(const std::string& id, |
| 100 | const CrxDataCallback& crx_data_callback, |
sorin | 842703b | 2016-11-02 23:59:23 | [diff] [blame] | 101 | const Callback& callback) { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 102 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 103 | |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 104 | if (IsUpdating(id)) { |
sorin | 842703b | 2016-11-02 23:59:23 | [diff] [blame] | 105 | callback.Run(Error::UPDATE_IN_PROGRESS); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 106 | return; |
| 107 | } |
| 108 | |
| 109 | std::vector<std::string> ids; |
| 110 | ids.push_back(id); |
| 111 | |
sorin | 842703b | 2016-11-02 23:59:23 | [diff] [blame] | 112 | // Partially applies |callback| to OnTaskComplete, so this argument is |
| 113 | // available when the task completes, along with the task itself. |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 114 | std::unique_ptr<TaskUpdate> task(new TaskUpdate( |
sorin | 842703b | 2016-11-02 23:59:23 | [diff] [blame] | 115 | update_engine_.get(), true, ids, crx_data_callback, |
| 116 | base::Bind(&UpdateClientImpl::OnTaskComplete, this, callback))); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 117 | |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 118 | // Install tasks are run concurrently and never queued up. |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 119 | RunTask(std::move(task)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void UpdateClientImpl::Update(const std::vector<std::string>& ids, |
| 123 | const CrxDataCallback& crx_data_callback, |
sorin | 842703b | 2016-11-02 23:59:23 | [diff] [blame] | 124 | const Callback& callback) { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 125 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 126 | |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 127 | std::unique_ptr<TaskUpdate> task(new TaskUpdate( |
sorin | 842703b | 2016-11-02 23:59:23 | [diff] [blame] | 128 | update_engine_.get(), false, ids, crx_data_callback, |
| 129 | base::Bind(&UpdateClientImpl::OnTaskComplete, this, callback))); |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 130 | |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 131 | // If no other tasks are running at the moment, run this update task. |
| 132 | // Otherwise, queue the task up. |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 133 | if (tasks_.empty()) { |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 134 | RunTask(std::move(task)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 135 | } else { |
| 136 | task_queue_.push(task.release()); |
| 137 | } |
| 138 | } |
| 139 | |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 140 | void UpdateClientImpl::RunTask(std::unique_ptr<Task> task) { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 141 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 142 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
sorin | b318cc84a | 2015-10-30 23:19:51 | [diff] [blame] | 143 | FROM_HERE, base::Bind(&Task::Run, base::Unretained(task.get()))); |
| 144 | tasks_.insert(task.release()); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 145 | } |
| 146 | |
sorin | 842703b | 2016-11-02 23:59:23 | [diff] [blame] | 147 | void UpdateClientImpl::OnTaskComplete(const Callback& callback, |
| 148 | Task* task, |
| 149 | Error error) { |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 150 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 151 | DCHECK(task); |
| 152 | |
sorin | 842703b | 2016-11-02 23:59:23 | [diff] [blame] | 153 | base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 154 | base::Bind(callback, error)); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 155 | |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 156 | // Remove the task from the set of the running tasks. Only tasks handled by |
| 157 | // the update engine can be in this data structure. |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 158 | tasks_.erase(task); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 159 | |
| 160 | // Delete the completed task. A task can be completed because the update |
| 161 | // engine has run it or because it has been canceled but never run. |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 162 | delete task; |
| 163 | |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 164 | if (is_stopped_) |
| 165 | return; |
| 166 | |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 167 | // Pick up a task from the queue if the queue has pending tasks and no other |
| 168 | // task is running. |
| 169 | if (tasks_.empty() && !task_queue_.empty()) { |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 170 | RunTask(std::unique_ptr<Task>(task_queue_.front())); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 171 | task_queue_.pop(); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void UpdateClientImpl::AddObserver(Observer* observer) { |
| 176 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 177 | observer_list_.AddObserver(observer); |
| 178 | } |
| 179 | |
| 180 | void UpdateClientImpl::RemoveObserver(Observer* observer) { |
| 181 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 182 | observer_list_.RemoveObserver(observer); |
| 183 | } |
| 184 | |
| 185 | void UpdateClientImpl::NotifyObservers(Observer::Events event, |
| 186 | const std::string& id) { |
| 187 | DCHECK(thread_checker_.CalledOnValidThread()); |
ericwilligers | ff2af33 | 2016-10-19 00:16:50 | [diff] [blame] | 188 | for (auto& observer : observer_list_) |
| 189 | observer.OnEvent(event, id); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | bool UpdateClientImpl::GetCrxUpdateState(const std::string& id, |
| 193 | CrxUpdateItem* update_item) const { |
| 194 | return update_engine_->GetUpdateState(id, update_item); |
| 195 | } |
| 196 | |
| 197 | bool UpdateClientImpl::IsUpdating(const std::string& id) const { |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 198 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 199 | |
vmpstr | 2de366b | 2016-07-20 21:35:48 | [diff] [blame] | 200 | for (const auto* task : tasks_) { |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 201 | const auto ids(task->GetIds()); |
sorin | bc7df00 | 2015-11-05 00:50:47 | [diff] [blame] | 202 | if (std::find(ids.begin(), ids.end(), id) != ids.end()) { |
sorin | 08d153c | 2015-10-30 00:04:20 | [diff] [blame] | 203 | return true; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | return false; |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 208 | } |
| 209 | |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 210 | void UpdateClientImpl::Stop() { |
| 211 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 212 | |
| 213 | is_stopped_ = true; |
| 214 | |
| 215 | // In the current implementation it is sufficient to cancel the pending |
| 216 | // tasks only. The tasks that are run by the update engine will stop |
| 217 | // making progress naturally, as the main task runner stops running task |
| 218 | // actions. Upon the browser shutdown, the resources employed by the active |
| 219 | // tasks will leak, as the operating system kills the thread associated with |
| 220 | // the update engine task runner. Further refactoring may be needed in this |
| 221 | // area, to cancel the running tasks by canceling the current action update. |
| 222 | // This behavior would be expected, correct, and result in no resource leaks |
| 223 | // in all cases, in shutdown or not. |
| 224 | // |
| 225 | // Cancel the pending tasks. These tasks are safe to cancel and delete since |
| 226 | // they have not picked up by the update engine, and not shared with any |
| 227 | // task runner yet. |
| 228 | while (!task_queue_.empty()) { |
vmpstr | 2de366b | 2016-07-20 21:35:48 | [diff] [blame] | 229 | auto* task(task_queue_.front()); |
sorin | ecaad3e | 2015-11-13 19:15:52 | [diff] [blame] | 230 | task_queue_.pop(); |
| 231 | task->Cancel(); |
| 232 | } |
| 233 | } |
| 234 | |
sorin | 805aa0311 | 2016-01-14 23:01:31 | [diff] [blame] | 235 | void UpdateClientImpl::SendUninstallPing(const std::string& id, |
pwnall | db0b7241 | 2016-08-19 21:39:12 | [diff] [blame] | 236 | const base::Version& version, |
sorin | 805aa0311 | 2016-01-14 23:01:31 | [diff] [blame] | 237 | int reason) { |
| 238 | DCHECK(thread_checker_.CalledOnValidThread()); |
| 239 | |
| 240 | // The implementation of PingManager::SendPing contains a self-deleting |
| 241 | // object responsible for sending the ping. |
| 242 | CrxUpdateItem item; |
| 243 | item.state = CrxUpdateItem::State::kUninstalled; |
| 244 | item.id = id; |
| 245 | item.previous_version = version; |
| 246 | item.next_version = base::Version("0"); |
| 247 | item.extra_code1 = reason; |
| 248 | |
| 249 | ping_manager_->SendPing(&item); |
| 250 | } |
| 251 | |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 252 | scoped_refptr<UpdateClient> UpdateClientFactory( |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 253 | const scoped_refptr<Configurator>& config) { |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 254 | std::unique_ptr<PingManager> ping_manager(new PingManager(config)); |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 255 | return new UpdateClientImpl(config, std::move(ping_manager), |
sorin | 7c71762 | 2015-05-26 19:59:09 | [diff] [blame] | 256 | &UpdateChecker::Create, &CrxDownloader::Create); |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 257 | } |
| 258 | |
waffles | d2d9a33 | 2016-04-09 01:59:57 | [diff] [blame] | 259 | void RegisterPrefs(PrefRegistrySimple* registry) { |
| 260 | PersistedData::RegisterPrefs(registry); |
| 261 | } |
| 262 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 263 | } // namespace update_client |