Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(297)

Side by Side Diff: components/update_client/update_checker.cc

Issue 2498873003: Refactor how the updater state data is serialized in update checks. (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_checker.h" 5 #include "components/update_client/update_checker.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <string> 10 #include <string>
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/location.h" 15 #include "base/location.h"
16 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/strings/stringprintf.h" 18 #include "base/strings/stringprintf.h"
19 #include "base/threading/thread_checker.h" 19 #include "base/threading/thread_checker.h"
20 #include "base/threading/thread_task_runner_handle.h" 20 #include "base/threading/thread_task_runner_handle.h"
21 #include "components/update_client/configurator.h" 21 #include "components/update_client/configurator.h"
22 #include "components/update_client/crx_update_item.h" 22 #include "components/update_client/crx_update_item.h"
23 #include "components/update_client/persisted_data.h" 23 #include "components/update_client/persisted_data.h"
24 #include "components/update_client/request_sender.h" 24 #include "components/update_client/request_sender.h"
25 #include "components/update_client/update_client.h" 25 #include "components/update_client/update_client.h"
26 #include "components/update_client/updater_state.h"
26 #include "components/update_client/utils.h" 27 #include "components/update_client/utils.h"
27 #include "url/gurl.h" 28 #include "url/gurl.h"
28 29
29 namespace update_client { 30 namespace update_client {
30 31
31 namespace { 32 namespace {
32 33
33 // Returns a sanitized version of the brand or an empty string otherwise. 34 // Returns a sanitized version of the brand or an empty string otherwise.
34 std::string SanitizeBrand(const std::string& brand) { 35 std::string SanitizeBrand(const std::string& brand) {
35 return IsValidBrand(brand) ? brand : std::string(""); 36 return IsValidBrand(brand) ? brand : std::string("");
(...skipping 26 matching lines...) Expand all
62 // the <request>. 63 // the <request>.
63 // 64 //
64 // An app element looks like this: 65 // An app element looks like this:
65 // <app appid="hnimpnehoodheedghdeeijklkeaacbdc" 66 // <app appid="hnimpnehoodheedghdeeijklkeaacbdc"
66 // version="0.1.2.3" installsource="ondemand"> 67 // version="0.1.2.3" installsource="ondemand">
67 // <updatecheck/> 68 // <updatecheck/>
68 // <packages> 69 // <packages>
69 // <package fp="abcd"/> 70 // <package fp="abcd"/>
70 // </packages> 71 // </packages>
71 // </app> 72 // </app>
72 std::string BuildUpdateCheckRequest(const Configurator& config, 73 std::string BuildUpdateCheckRequest(
73 const IdToCrxUpdateItemMap& items, 74 const Configurator& config,
74 PersistedData* metadata, 75 const IdToCrxUpdateItemMap& items,
75 const std::string& additional_attributes, 76 PersistedData* metadata,
76 bool enabled_component_updates) { 77 const std::string& additional_attributes,
78 bool enabled_component_updates,
79 const std::unique_ptr<UpdaterState::Attributes>& updater_state_attributes) {
77 const std::string brand(SanitizeBrand(config.GetBrand())); 80 const std::string brand(SanitizeBrand(config.GetBrand()));
78 std::string app_elements; 81 std::string app_elements;
79 for (const auto& item_pair : items) { 82 for (const auto& item_pair : items) {
80 const CrxUpdateItem* item = item_pair.second.get(); 83 const CrxUpdateItem* item = item_pair.second.get();
81 const update_client::InstallerAttributes installer_attributes( 84 const update_client::InstallerAttributes installer_attributes(
82 SanitizeInstallerAttributes(item->component.installer_attributes)); 85 SanitizeInstallerAttributes(item->component.installer_attributes));
83 std::string app("<app "); 86 std::string app("<app ");
84 base::StringAppendF(&app, "appid=\"%s\" version=\"%s\"", item->id.c_str(), 87 base::StringAppendF(&app, "appid=\"%s\" version=\"%s\"", item->id.c_str(),
85 item->component.version.GetString().c_str()); 88 item->component.version.GetString().c_str());
86 if (!brand.empty()) 89 if (!brand.empty())
(...skipping 30 matching lines...) Expand all
117 "<packages>" 120 "<packages>"
118 "<package fp=\"%s\"/>" 121 "<package fp=\"%s\"/>"
119 "</packages>", 122 "</packages>",
120 item->component.fingerprint.c_str()); 123 item->component.fingerprint.c_str());
121 } 124 }
122 base::StringAppendF(&app, "</app>"); 125 base::StringAppendF(&app, "</app>");
123 app_elements.append(app); 126 app_elements.append(app);
124 VLOG(1) << "Appending to update request: " << app; 127 VLOG(1) << "Appending to update request: " << app;
125 } 128 }
126 129
130 // Include the updater state in the update check request.
127 return BuildProtocolRequest( 131 return BuildProtocolRequest(
128 config.GetProdId(), config.GetBrowserVersion().GetString(), 132 config.GetProdId(), config.GetBrowserVersion().GetString(),
129 config.GetChannel(), config.GetLang(), config.GetOSLongName(), 133 config.GetChannel(), config.GetLang(), config.GetOSLongName(),
130 config.GetDownloadPreference(), app_elements, additional_attributes); 134 config.GetDownloadPreference(), app_elements, additional_attributes,
135 updater_state_attributes);
131 } 136 }
132 137
133 class UpdateCheckerImpl : public UpdateChecker { 138 class UpdateCheckerImpl : public UpdateChecker {
134 public: 139 public:
135 UpdateCheckerImpl(const scoped_refptr<Configurator>& config, 140 UpdateCheckerImpl(const scoped_refptr<Configurator>& config,
136 PersistedData* metadata); 141 PersistedData* metadata);
137 ~UpdateCheckerImpl() override; 142 ~UpdateCheckerImpl() override;
138 143
139 // Overrides for UpdateChecker. 144 // Overrides for UpdateChecker.
140 bool CheckForUpdates( 145 bool CheckForUpdates(
141 const IdToCrxUpdateItemMap& items_to_check, 146 const IdToCrxUpdateItemMap& items_to_check,
142 const std::string& additional_attributes, 147 const std::string& additional_attributes,
143 bool enabled_component_updates, 148 bool enabled_component_updates,
144 const UpdateCheckCallback& update_check_callback) override; 149 const UpdateCheckCallback& update_check_callback) override;
145 150
146 private: 151 private:
152 void ReadUpdaterStateAttributes();
153 void CheckForUpdatesHelper(const IdToCrxUpdateItemMap& items_to_check,
154 const std::string& additional_attributes,
155 bool enabled_component_updates);
156
147 void OnRequestSenderComplete( 157 void OnRequestSenderComplete(
148 std::unique_ptr<std::vector<std::string>> ids_checked, 158 std::unique_ptr<std::vector<std::string>> ids_checked,
149 int error, 159 int error,
150 const std::string& response, 160 const std::string& response,
151 int retry_after_sec); 161 int retry_after_sec);
152 base::ThreadChecker thread_checker_; 162 base::ThreadChecker thread_checker_;
153 163
154 const scoped_refptr<Configurator> config_; 164 const scoped_refptr<Configurator> config_;
155 PersistedData* metadata_; 165 PersistedData* metadata_;
156 UpdateCheckCallback update_check_callback_; 166 UpdateCheckCallback update_check_callback_;
167 std::unique_ptr<UpdaterState::Attributes> updater_state_attributes_;
157 std::unique_ptr<RequestSender> request_sender_; 168 std::unique_ptr<RequestSender> request_sender_;
158 169
159 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerImpl); 170 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerImpl);
160 }; 171 };
161 172
162 UpdateCheckerImpl::UpdateCheckerImpl(const scoped_refptr<Configurator>& config, 173 UpdateCheckerImpl::UpdateCheckerImpl(const scoped_refptr<Configurator>& config,
163 PersistedData* metadata) 174 PersistedData* metadata)
164 : config_(config), metadata_(metadata) {} 175 : config_(config), metadata_(metadata) {}
165 176
166 UpdateCheckerImpl::~UpdateCheckerImpl() { 177 UpdateCheckerImpl::~UpdateCheckerImpl() {
167 DCHECK(thread_checker_.CalledOnValidThread()); 178 DCHECK(thread_checker_.CalledOnValidThread());
168 } 179 }
169 180
170 bool UpdateCheckerImpl::CheckForUpdates( 181 bool UpdateCheckerImpl::CheckForUpdates(
171 const IdToCrxUpdateItemMap& items_to_check, 182 const IdToCrxUpdateItemMap& items_to_check,
172 const std::string& additional_attributes, 183 const std::string& additional_attributes,
173 bool enabled_component_updates, 184 bool enabled_component_updates,
174 const UpdateCheckCallback& update_check_callback) { 185 const UpdateCheckCallback& update_check_callback) {
175 DCHECK(thread_checker_.CalledOnValidThread()); 186 DCHECK(thread_checker_.CalledOnValidThread());
176 187
177 if (request_sender_.get()) { 188 update_check_callback_ = update_check_callback;
178 NOTREACHED();
179 return false; // Another update check is in progress.
180 }
181 189
182 update_check_callback_ = update_check_callback; 190 return config_->GetSequencedTaskRunner()->PostTaskAndReply(
191 FROM_HERE, base::Bind(&UpdateCheckerImpl::ReadUpdaterStateAttributes,
192 base::Unretained(this)),
193 base::Bind(&UpdateCheckerImpl::CheckForUpdatesHelper,
194 base::Unretained(this), base::ConstRef(items_to_check),
195 additional_attributes, enabled_component_updates));
196 }
197
198 // This function runs on the blocking pool task runner.
199 void UpdateCheckerImpl::ReadUpdaterStateAttributes() {
200 const bool is_machine_install = !config_->IsPerUserInstall();
201 updater_state_attributes_ = UpdaterState::GetState(is_machine_install);
202 }
203
204 void UpdateCheckerImpl::CheckForUpdatesHelper(
205 const IdToCrxUpdateItemMap& items_to_check,
206 const std::string& additional_attributes,
207 bool enabled_component_updates) {
208 DCHECK(thread_checker_.CalledOnValidThread());
183 209
184 auto urls(config_->UpdateUrl()); 210 auto urls(config_->UpdateUrl());
185 if (IsEncryptionRequired(items_to_check)) 211 if (IsEncryptionRequired(items_to_check))
186 RemoveUnsecureUrls(&urls); 212 RemoveUnsecureUrls(&urls);
187 213
188 std::unique_ptr<std::vector<std::string>> ids_checked( 214 std::unique_ptr<std::vector<std::string>> ids_checked(
189 new std::vector<std::string>()); 215 new std::vector<std::string>());
190 for (const auto& item : items_to_check) 216 for (const auto& item : items_to_check)
191 ids_checked->push_back(item.second->id); 217 ids_checked->push_back(item.second->id);
192 request_sender_.reset(new RequestSender(config_)); 218 request_sender_.reset(new RequestSender(config_));
193 request_sender_->Send( 219 request_sender_->Send(
194 config_->EnabledCupSigning(), 220 config_->EnabledCupSigning(),
195 BuildUpdateCheckRequest(*config_, items_to_check, metadata_, 221 BuildUpdateCheckRequest(*config_, items_to_check, metadata_,
196 additional_attributes, enabled_component_updates), 222 additional_attributes, enabled_component_updates,
223 updater_state_attributes_),
197 urls, base::Bind(&UpdateCheckerImpl::OnRequestSenderComplete, 224 urls, base::Bind(&UpdateCheckerImpl::OnRequestSenderComplete,
198 base::Unretained(this), base::Passed(&ids_checked))); 225 base::Unretained(this), base::Passed(&ids_checked)));
199 return true;
200 } 226 }
201 227
202 void UpdateCheckerImpl::OnRequestSenderComplete( 228 void UpdateCheckerImpl::OnRequestSenderComplete(
203 std::unique_ptr<std::vector<std::string>> ids_checked, 229 std::unique_ptr<std::vector<std::string>> ids_checked,
204 int error, 230 int error,
205 const std::string& response, 231 const std::string& response,
206 int retry_after_sec) { 232 int retry_after_sec) {
207 DCHECK(thread_checker_.CalledOnValidThread()); 233 DCHECK(thread_checker_.CalledOnValidThread());
208 234
209 if (!error) { 235 if (!error) {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 } // namespace 267 } // namespace
242 268
243 std::unique_ptr<UpdateChecker> UpdateChecker::Create( 269 std::unique_ptr<UpdateChecker> UpdateChecker::Create(
244 const scoped_refptr<Configurator>& config, 270 const scoped_refptr<Configurator>& config,
245 PersistedData* persistent) { 271 PersistedData* persistent) {
246 return std::unique_ptr<UpdateChecker>( 272 return std::unique_ptr<UpdateChecker>(
247 new UpdateCheckerImpl(config, persistent)); 273 new UpdateCheckerImpl(config, persistent));
248 } 274 }
249 275
250 } // namespace update_client 276 } // namespace update_client
OLDNEW
« no previous file with comments | « components/update_client/ping_manager.cc ('k') | components/update_client/update_checker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698