OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "components/update_client/action.h" | |
6 | |
7 #include <algorithm> | |
8 #include <memory> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/bind_helpers.h" | |
12 #include "base/callback.h" | |
13 #include "base/location.h" | |
14 #include "base/memory/ref_counted.h" | |
15 #include "base/single_thread_task_runner.h" | |
16 #include "base/threading/thread_task_runner_handle.h" | |
17 #include "components/update_client/action_update.h" | |
18 #include "components/update_client/action_wait.h" | |
19 #include "components/update_client/configurator.h" | |
20 #include "components/update_client/update_client_errors.h" | |
21 #include "components/update_client/update_engine.h" | |
22 #include "components/update_client/utils.h" | |
23 | |
24 namespace update_client { | |
25 | |
26 using Events = UpdateClient::Observer::Events; | |
27 | |
28 namespace { | |
29 | |
30 // Returns true if a differential update is available, it has not failed yet, | |
31 // and the configuration allows this update. | |
32 bool CanTryDiffUpdate(const CrxUpdateItem* update_item, | |
33 const scoped_refptr<Configurator>& config) { | |
34 return HasDiffUpdate(update_item) && !update_item->diff_update_failed && | |
35 config->EnabledDeltas(); | |
36 } | |
37 | |
38 } // namespace | |
39 | |
40 ActionImpl::ActionImpl() : update_context_(nullptr) { | |
41 } | |
42 | |
43 ActionImpl::~ActionImpl() { | |
44 DCHECK(thread_checker_.CalledOnValidThread()); | |
45 } | |
46 | |
47 void ActionImpl::Run(UpdateContext* update_context, Callback callback) { | |
48 DCHECK(thread_checker_.CalledOnValidThread()); | |
49 | |
50 update_context_ = update_context; | |
51 callback_ = callback; | |
52 } | |
53 | |
54 CrxUpdateItem* ActionImpl::FindUpdateItemById(const std::string& id) const { | |
55 DCHECK(thread_checker_.CalledOnValidThread()); | |
56 | |
57 const auto it = update_context_->update_items.find(id); | |
58 | |
59 return it != update_context_->update_items.end() ? it->second.get() : nullptr; | |
60 } | |
61 | |
62 void ActionImpl::ChangeItemState(CrxUpdateItem* item, CrxUpdateItem::State to) { | |
63 DCHECK(thread_checker_.CalledOnValidThread()); | |
64 | |
65 item->state = to; | |
66 | |
67 using Events = UpdateClient::Observer::Events; | |
68 | |
69 const std::string& id(item->id); | |
70 switch (to) { | |
71 case CrxUpdateItem::State::kChecking: | |
72 NotifyObservers(Events::COMPONENT_CHECKING_FOR_UPDATES, id); | |
73 break; | |
74 case CrxUpdateItem::State::kCanUpdate: | |
75 NotifyObservers(Events::COMPONENT_UPDATE_FOUND, id); | |
76 break; | |
77 case CrxUpdateItem::State::kUpdatingDiff: | |
78 case CrxUpdateItem::State::kUpdating: | |
79 NotifyObservers(Events::COMPONENT_UPDATE_READY, id); | |
80 break; | |
81 case CrxUpdateItem::State::kUpdated: | |
82 NotifyObservers(Events::COMPONENT_UPDATED, id); | |
83 break; | |
84 case CrxUpdateItem::State::kUpToDate: | |
85 case CrxUpdateItem::State::kNoUpdate: | |
86 NotifyObservers(Events::COMPONENT_NOT_UPDATED, id); | |
87 break; | |
88 case CrxUpdateItem::State::kNew: | |
89 case CrxUpdateItem::State::kDownloading: | |
90 case CrxUpdateItem::State::kDownloadingDiff: | |
91 case CrxUpdateItem::State::kDownloaded: | |
92 case CrxUpdateItem::State::kUninstalled: | |
93 case CrxUpdateItem::State::kLastStatus: | |
94 // No notification for these states. | |
95 break; | |
96 } | |
97 } | |
98 | |
99 size_t ActionImpl::ChangeAllItemsState(CrxUpdateItem::State from, | |
100 CrxUpdateItem::State to) { | |
101 DCHECK(thread_checker_.CalledOnValidThread()); | |
102 size_t count = 0; | |
103 for (const auto& item : update_context_->update_items) { | |
104 if (item.second->state == from) { | |
105 ChangeItemState(item.second.get(), to); | |
106 ++count; | |
107 } | |
108 } | |
109 return count; | |
110 } | |
111 | |
112 void ActionImpl::NotifyObservers(UpdateClient::Observer::Events event, | |
113 const std::string& id) { | |
114 DCHECK(thread_checker_.CalledOnValidThread()); | |
115 update_context_->notify_observers_callback.Run(event, id); | |
116 } | |
117 | |
118 void ActionImpl::UpdateCrx() { | |
119 DCHECK(thread_checker_.CalledOnValidThread()); | |
120 DCHECK(!update_context_->queue.empty()); | |
121 | |
122 const std::string& id = update_context_->queue.front(); | |
123 CrxUpdateItem* item = FindUpdateItemById(id); | |
124 DCHECK(item); | |
125 | |
126 item->update_begin = base::TimeTicks::Now(); | |
127 | |
128 if (item->component.supports_group_policy_enable_component_updates && | |
129 !update_context_->enabled_component_updates) { | |
130 item->error_category = static_cast<int>(ErrorCategory::kServiceError); | |
131 item->error_code = static_cast<int>(ServiceError::UPDATE_DISABLED); | |
132 item->extra_code1 = 0; | |
133 ChangeItemState(item, CrxUpdateItem::State::kNoUpdate); | |
134 | |
135 UpdateCrxComplete(item); | |
136 return; | |
137 } | |
138 | |
139 std::unique_ptr<Action> update_action( | |
140 CanTryDiffUpdate(item, update_context_->config) | |
141 ? ActionUpdateDiff::Create() | |
142 : ActionUpdateFull::Create()); | |
143 | |
144 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
145 FROM_HERE, base::Bind(&Action::Run, base::Unretained(update_action.get()), | |
146 update_context_, callback_)); | |
147 | |
148 update_context_->current_action = std::move(update_action); | |
149 } | |
150 | |
151 void ActionImpl::UpdateCrxComplete(CrxUpdateItem* item) { | |
152 DCHECK(thread_checker_.CalledOnValidThread()); | |
153 DCHECK(item); | |
154 | |
155 update_context_->ping_manager->SendPing(item); | |
156 | |
157 update_context_->queue.pop(); | |
158 | |
159 if (update_context_->queue.empty()) { | |
160 UpdateComplete(Error::NONE); | |
161 } else { | |
162 DCHECK(!item->update_begin.is_null()); | |
163 | |
164 // Assume that the cost of applying the update is proportional with how | |
165 // long it took to apply it. Then delay the next update by the same time | |
166 // interval or the value provided by the configurator, whichever is less. | |
167 const base::TimeDelta max_update_delay = | |
168 base::TimeDelta::FromSeconds(update_context_->config->UpdateDelay()); | |
169 const base::TimeDelta update_cost(base::TimeTicks::Now() - | |
170 item->update_begin); | |
171 DCHECK(update_cost >= base::TimeDelta()); | |
172 | |
173 std::unique_ptr<ActionWait> action_wait( | |
174 new ActionWait(std::min(update_cost, max_update_delay))); | |
175 | |
176 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
177 FROM_HERE, base::Bind(&Action::Run, base::Unretained(action_wait.get()), | |
178 update_context_, callback_)); | |
179 | |
180 update_context_->current_action = std::move(action_wait); | |
181 } | |
182 } | |
183 | |
184 void ActionImpl::UpdateComplete(Error error) { | |
185 DCHECK(thread_checker_.CalledOnValidThread()); | |
186 | |
187 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
188 base::Bind(callback_, error)); | |
189 } | |
190 | |
191 } // namespace update_client | |
OLD | NEW |