ApplicationImpl cleanup, part 1:
[chromium-blink-merge.git] / components / update_client / update_engine.cc
blob6200a7d352d9ccf63c4dcd6639c7c4c322685f61
1 // Copyright 2015 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_engine.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/location.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/stl_util.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "components/update_client/action_update_check.h"
14 #include "components/update_client/configurator.h"
15 #include "components/update_client/crx_update_item.h"
16 #include "components/update_client/update_checker.h"
18 namespace update_client {
20 UpdateContext::UpdateContext(
21 const scoped_refptr<Configurator>& config,
22 bool is_foreground,
23 const std::vector<std::string>& ids,
24 const UpdateClient::CrxDataCallback& crx_data_callback,
25 const UpdateEngine::NotifyObserversCallback& notify_observers_callback,
26 const UpdateEngine::CompletionCallback& callback,
27 UpdateChecker::Factory update_checker_factory,
28 CrxDownloader::Factory crx_downloader_factory,
29 PingManager* ping_manager)
30 : config(config),
31 is_foreground(is_foreground),
32 ids(ids),
33 crx_data_callback(crx_data_callback),
34 notify_observers_callback(notify_observers_callback),
35 callback(callback),
36 main_task_runner(base::ThreadTaskRunnerHandle::Get()),
37 blocking_task_runner(config->GetSequencedTaskRunner()),
38 single_thread_task_runner(config->GetSingleThreadTaskRunner()),
39 update_checker_factory(update_checker_factory),
40 crx_downloader_factory(crx_downloader_factory),
41 ping_manager(ping_manager) {
44 UpdateContext::~UpdateContext() {
45 STLDeleteElements(&update_items);
48 UpdateEngine::UpdateEngine(
49 const scoped_refptr<Configurator>& config,
50 UpdateChecker::Factory update_checker_factory,
51 CrxDownloader::Factory crx_downloader_factory,
52 PingManager* ping_manager,
53 const NotifyObserversCallback& notify_observers_callback)
54 : config_(config),
55 update_checker_factory_(update_checker_factory),
56 crx_downloader_factory_(crx_downloader_factory),
57 ping_manager_(ping_manager),
58 notify_observers_callback_(notify_observers_callback) {
61 UpdateEngine::~UpdateEngine() {
62 DCHECK(thread_checker_.CalledOnValidThread());
65 bool UpdateEngine::IsUpdating(const std::string& id) const {
66 DCHECK(thread_checker_.CalledOnValidThread());
67 for (const auto& context : update_contexts_) {
68 const auto& ids = context->ids;
69 const auto it = std::find_if(
70 ids.begin(), ids.end(),
71 [id](const std::string& this_id) { return id == this_id; });
72 if (it != ids.end()) {
73 return true;
76 return false;
79 bool UpdateEngine::GetUpdateState(const std::string& id,
80 CrxUpdateItem* update_item) {
81 DCHECK(thread_checker_.CalledOnValidThread());
82 for (const auto& context : update_contexts_) {
83 const auto& update_items = context->update_items;
84 const auto it = std::find_if(update_items.begin(), update_items.end(),
85 [id](const CrxUpdateItem* update_item) {
86 return id == update_item->id;
87 });
88 if (it != update_items.end()) {
89 *update_item = **it;
90 return true;
93 return false;
96 void UpdateEngine::Update(
97 bool is_foreground,
98 const std::vector<std::string>& ids,
99 const UpdateClient::CrxDataCallback& crx_data_callback,
100 const CompletionCallback& callback) {
101 DCHECK(thread_checker_.CalledOnValidThread());
103 scoped_ptr<UpdateContext> update_context(new UpdateContext(
104 config_, is_foreground, ids, crx_data_callback,
105 notify_observers_callback_, callback, update_checker_factory_,
106 crx_downloader_factory_, ping_manager_));
108 CrxUpdateItem update_item;
109 scoped_ptr<ActionUpdateCheck> update_check_action(new ActionUpdateCheck(
110 (*update_context->update_checker_factory)(*config_).Pass(),
111 config_->GetBrowserVersion(), config_->ExtraRequestParams()));
113 update_context->current_action.reset(update_check_action.release());
114 update_contexts_.insert(update_context.get());
116 update_context->current_action->Run(
117 update_context.get(),
118 base::Bind(&UpdateEngine::UpdateComplete, base::Unretained(this),
119 update_context.get()));
121 ignore_result(update_context.release());
124 void UpdateEngine::UpdateComplete(UpdateContext* update_context, int error) {
125 DCHECK(thread_checker_.CalledOnValidThread());
126 DCHECK(update_contexts_.find(update_context) != update_contexts_.end());
128 auto callback = update_context->callback;
130 update_contexts_.erase(update_context);
131 delete update_context;
133 callback.Run(error);
136 } // namespace update_client