Revert of Refactor the avatar button/icon class (patchset #14 id:320001 of https...
[chromium-blink-merge.git] / components / update_client / action_update_check.cc
blobc480de81fecdc6e055894a0c01ccc643aad74514
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/action_update_check.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/version.h"
15 #include "components/update_client/action_update.h"
16 #include "components/update_client/configurator.h"
17 #include "components/update_client/update_checker.h"
18 #include "components/update_client/update_client.h"
19 #include "components/update_client/utils.h"
21 using std::string;
22 using std::vector;
24 namespace update_client {
26 namespace {
28 // Returns true if the |proposed| version is newer than |current| version.
29 bool IsVersionNewer(const Version& current, const std::string& proposed) {
30 Version proposed_ver(proposed);
31 return proposed_ver.IsValid() && current.CompareTo(proposed_ver) < 0;
34 } // namespace
36 ActionUpdateCheck::ActionUpdateCheck(
37 scoped_ptr<UpdateChecker> update_checker,
38 const base::Version& browser_version,
39 const std::string& extra_request_parameters)
40 : update_checker_(update_checker.Pass()),
41 browser_version_(browser_version),
42 extra_request_parameters_(extra_request_parameters) {
45 ActionUpdateCheck::~ActionUpdateCheck() {
46 DCHECK(thread_checker_.CalledOnValidThread());
49 void ActionUpdateCheck::Run(UpdateContext* update_context, Callback callback) {
50 DCHECK(thread_checker_.CalledOnValidThread());
52 ActionImpl::Run(update_context, callback);
54 // Calls out to get the corresponding CrxComponent data for the CRXs in this
55 // update context.
56 vector<CrxComponent> crx_components;
57 update_context_->crx_data_callback.Run(update_context_->ids, &crx_components);
59 update_context_->update_items.reserve(crx_components.size());
61 for (size_t i = 0; i != crx_components.size(); ++i) {
62 scoped_ptr<CrxUpdateItem> item(new CrxUpdateItem);
63 const CrxComponent& crx_component = crx_components[i];
65 item->id = GetCrxComponentID(crx_component);
66 item->component = crx_component;
67 item->last_check = base::Time::Now();
68 item->crx_urls.clear();
69 item->crx_diffurls.clear();
70 item->previous_version = crx_component.version;
71 item->next_version = Version();
72 item->previous_fp = crx_component.fingerprint;
73 item->next_fp.clear();
74 item->diff_update_failed = false;
75 item->error_category = 0;
76 item->error_code = 0;
77 item->extra_code1 = 0;
78 item->diff_error_category = 0;
79 item->diff_error_code = 0;
80 item->diff_extra_code1 = 0;
81 item->download_metrics.clear();
83 ChangeItemState(item.get(), CrxUpdateItem::State::kChecking);
85 update_context_->update_items.push_back(item.release());
88 update_checker_->CheckForUpdates(
89 update_context_->update_items, extra_request_parameters_,
90 base::Bind(&ActionUpdateCheck::UpdateCheckComplete,
91 base::Unretained(this)));
94 void ActionUpdateCheck::UpdateCheckComplete(
95 const GURL& original_url,
96 int error,
97 const std::string& error_message,
98 const UpdateResponse::Results& results) {
99 DCHECK(thread_checker_.CalledOnValidThread());
101 VLOG(1) << "Update check completed from: " << original_url.spec();
103 if (!error)
104 OnUpdateCheckSucceeded(results);
105 else
106 OnUpdateCheckFailed(error, error_message);
109 void ActionUpdateCheck::OnUpdateCheckSucceeded(
110 const UpdateResponse::Results& results) {
111 DCHECK(thread_checker_.CalledOnValidThread());
112 VLOG(1) << "Update check succeeded.";
113 std::vector<UpdateResponse::Result>::const_iterator it;
114 for (it = results.list.begin(); it != results.list.end(); ++it) {
115 CrxUpdateItem* crx = FindUpdateItemById(it->extension_id);
116 if (!crx)
117 continue;
119 if (crx->state != CrxUpdateItem::State::kChecking) {
120 NOTREACHED();
121 continue; // Not updating this CRX now.
124 if (it->manifest.version.empty()) {
125 // No version means no update available.
126 ChangeItemState(crx, CrxUpdateItem::State::kNoUpdate);
127 VLOG(1) << "No update available for CRX: " << crx->id;
128 continue;
131 if (!IsVersionNewer(crx->component.version, it->manifest.version)) {
132 // The CRX is up to date.
133 ChangeItemState(crx, CrxUpdateItem::State::kUpToDate);
134 VLOG(1) << "Component already up-to-date: " << crx->id;
135 continue;
138 if (!it->manifest.browser_min_version.empty()) {
139 if (IsVersionNewer(browser_version_, it->manifest.browser_min_version)) {
140 // The CRX is not compatible with this Chrome version.
141 VLOG(1) << "Ignoring incompatible CRX: " << crx->id;
142 ChangeItemState(crx, CrxUpdateItem::State::kNoUpdate);
143 continue;
147 if (it->manifest.packages.size() != 1) {
148 // Assume one and only one package per CRX.
149 VLOG(1) << "Ignoring multiple packages for CRX: " << crx->id;
150 ChangeItemState(crx, CrxUpdateItem::State::kNoUpdate);
151 continue;
154 // Parse the members of the result and queue an upgrade for this CRX.
155 crx->next_version = Version(it->manifest.version);
157 VLOG(1) << "Update found for CRX: " << crx->id;
159 const auto& package(it->manifest.packages[0]);
160 crx->next_fp = package.fingerprint;
162 // Resolve the urls by combining the base urls with the package names.
163 for (size_t i = 0; i != it->crx_urls.size(); ++i) {
164 const GURL url(it->crx_urls[i].Resolve(package.name));
165 if (url.is_valid())
166 crx->crx_urls.push_back(url);
168 for (size_t i = 0; i != it->crx_diffurls.size(); ++i) {
169 const GURL url(it->crx_diffurls[i].Resolve(package.namediff));
170 if (url.is_valid())
171 crx->crx_diffurls.push_back(url);
174 ChangeItemState(crx, CrxUpdateItem::State::kCanUpdate);
176 update_context_->queue.push(crx->id);
179 // All components that are not included in the update response are
180 // considered up to date.
181 ChangeAllItemsState(CrxUpdateItem::State::kChecking,
182 CrxUpdateItem::State::kUpToDate);
184 if (update_context_->queue.empty()) {
185 VLOG(1) << "Update check completed but no update is needed.";
186 UpdateComplete(0);
187 return;
190 // Starts the execution flow of updating the CRXs in this context.
191 UpdateCrx();
194 void ActionUpdateCheck::OnUpdateCheckFailed(int error,
195 const std::string& error_message) {
196 DCHECK(thread_checker_.CalledOnValidThread());
197 DCHECK(error);
199 VLOG(1) << "Update check failed." << error;
201 UpdateComplete(error);
204 } // namespace update_client