OLD | NEW |
---|---|
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 <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
14 #include "base/location.h" | 14 #include "base/location.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
19 #include "base/thread_task_runner_handle.h" | 19 #include "base/thread_task_runner_handle.h" |
20 #include "base/threading/thread_checker.h" | 20 #include "base/threading/thread_checker.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/request_sender.h" | 24 #include "components/update_client/request_sender.h" |
24 #include "components/update_client/utils.h" | 25 #include "components/update_client/utils.h" |
25 #include "url/gurl.h" | 26 #include "url/gurl.h" |
26 | 27 |
27 namespace update_client { | 28 namespace update_client { |
28 | 29 |
29 namespace { | 30 namespace { |
30 | 31 |
31 // Returns a sanitized version of the brand or an empty string otherwise. | 32 // Returns a sanitized version of the brand or an empty string otherwise. |
32 std::string SanitizeBrand(const std::string& brand) { | 33 std::string SanitizeBrand(const std::string& brand) { |
(...skipping 18 matching lines...) Expand all Loading... | |
51 // An app element looks like this: | 52 // An app element looks like this: |
52 // <app appid="hnimpnehoodheedghdeeijklkeaacbdc" | 53 // <app appid="hnimpnehoodheedghdeeijklkeaacbdc" |
53 // version="0.1.2.3" installsource="ondemand"> | 54 // version="0.1.2.3" installsource="ondemand"> |
54 // <updatecheck /> | 55 // <updatecheck /> |
55 // <packages> | 56 // <packages> |
56 // <package fp="abcd" /> | 57 // <package fp="abcd" /> |
57 // </packages> | 58 // </packages> |
58 // </app> | 59 // </app> |
59 std::string BuildUpdateCheckRequest(const Configurator& config, | 60 std::string BuildUpdateCheckRequest(const Configurator& config, |
60 const std::vector<CrxUpdateItem*>& items, | 61 const std::vector<CrxUpdateItem*>& items, |
62 const PersistedData& metadata, | |
61 const std::string& additional_attributes) { | 63 const std::string& additional_attributes) { |
62 const std::string brand(SanitizeBrand(config.GetBrand())); | 64 const std::string brand(SanitizeBrand(config.GetBrand())); |
63 std::string app_elements; | 65 std::string app_elements; |
64 for (size_t i = 0; i != items.size(); ++i) { | 66 for (size_t i = 0; i != items.size(); ++i) { |
65 const CrxUpdateItem* item = items[i]; | 67 const CrxUpdateItem* item = items[i]; |
66 std::string app("<app "); | 68 std::string app("<app "); |
67 base::StringAppendF(&app, "appid=\"%s\" version=\"%s\"", item->id.c_str(), | 69 base::StringAppendF(&app, "appid=\"%s\" version=\"%s\"", item->id.c_str(), |
68 item->component.version.GetString().c_str()); | 70 item->component.version.GetString().c_str()); |
69 if (!brand.empty()) | 71 if (!brand.empty()) |
70 base::StringAppendF(&app, " brand=\"%s\"", brand.c_str()); | 72 base::StringAppendF(&app, " brand=\"%s\"", brand.c_str()); |
71 if (item->on_demand) | 73 if (item->on_demand) |
72 base::StringAppendF(&app, " installsource=\"ondemand\""); | 74 base::StringAppendF(&app, " installsource=\"ondemand\""); |
73 base::StringAppendF(&app, ">"); | 75 base::StringAppendF(&app, ">"); |
74 base::StringAppendF(&app, "<updatecheck />"); | 76 base::StringAppendF(&app, "<updatecheck />"); |
77 base::StringAppendF( | |
78 &app, "<ping rd=\"%d\" />", metadata.DateLastRollCall(item->id)); | |
75 if (!item->component.fingerprint.empty()) { | 79 if (!item->component.fingerprint.empty()) { |
76 base::StringAppendF(&app, | 80 base::StringAppendF(&app, |
77 "<packages>" | 81 "<packages>" |
78 "<package fp=\"%s\"/>" | 82 "<package fp=\"%s\"/>" |
79 "</packages>", | 83 "</packages>", |
80 item->component.fingerprint.c_str()); | 84 item->component.fingerprint.c_str()); |
81 } | 85 } |
82 base::StringAppendF(&app, "</app>"); | 86 base::StringAppendF(&app, "</app>"); |
83 app_elements.append(app); | 87 app_elements.append(app); |
84 VLOG(1) << "Appending to update request: " << app; | 88 VLOG(1) << "Appending to update request: " << app; |
85 } | 89 } |
86 | 90 |
87 return BuildProtocolRequest( | 91 return BuildProtocolRequest( |
88 config.GetBrowserVersion().GetString(), config.GetChannel(), | 92 config.GetBrowserVersion().GetString(), config.GetChannel(), |
89 config.GetLang(), config.GetOSLongName(), config.GetDownloadPreference(), | 93 config.GetLang(), config.GetOSLongName(), config.GetDownloadPreference(), |
90 app_elements, additional_attributes); | 94 app_elements, additional_attributes); |
91 } | 95 } |
92 | 96 |
93 class UpdateCheckerImpl : public UpdateChecker { | 97 class UpdateCheckerImpl : public UpdateChecker { |
94 public: | 98 public: |
95 explicit UpdateCheckerImpl(const scoped_refptr<Configurator>& config); | 99 UpdateCheckerImpl( |
100 const scoped_refptr<Configurator>& config, | |
101 const scoped_refptr<PersistedData>& metadata); | |
96 ~UpdateCheckerImpl() override; | 102 ~UpdateCheckerImpl() override; |
97 | 103 |
98 // Overrides for UpdateChecker. | 104 // Overrides for UpdateChecker. |
99 bool CheckForUpdates( | 105 bool CheckForUpdates( |
100 const std::vector<CrxUpdateItem*>& items_to_check, | 106 const std::vector<CrxUpdateItem*>& items_to_check, |
101 const std::string& additional_attributes, | 107 const std::string& additional_attributes, |
102 const UpdateCheckCallback& update_check_callback) override; | 108 const UpdateCheckCallback& update_check_callback) override; |
103 | 109 |
104 private: | 110 private: |
105 void OnRequestSenderComplete(int error, | 111 void OnRequestSenderComplete(scoped_ptr<std::vector<std::string>> ids_checked, |
112 int error, | |
106 const std::string& response, | 113 const std::string& response, |
107 int retry_after_sec); | 114 int retry_after_sec); |
108 base::ThreadChecker thread_checker_; | 115 base::ThreadChecker thread_checker_; |
109 | 116 |
110 const scoped_refptr<Configurator> config_; | 117 const scoped_refptr<Configurator> config_; |
118 const scoped_refptr<PersistedData>& metadata_; | |
Bernhard Bauer
2016/04/08 09:06:10
Uh, this is a bit... unconventional. Why not just
waffles
2016/04/08 19:11:01
Done. (I don't think I originally meant to have th
| |
111 UpdateCheckCallback update_check_callback_; | 119 UpdateCheckCallback update_check_callback_; |
112 scoped_ptr<RequestSender> request_sender_; | 120 scoped_ptr<RequestSender> request_sender_; |
113 | 121 |
114 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerImpl); | 122 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerImpl); |
115 }; | 123 }; |
116 | 124 |
117 UpdateCheckerImpl::UpdateCheckerImpl(const scoped_refptr<Configurator>& config) | 125 UpdateCheckerImpl::UpdateCheckerImpl( |
118 : config_(config) {} | 126 const scoped_refptr<Configurator>& config, |
127 const scoped_refptr<PersistedData>& metadata) | |
128 : config_(config), metadata_(metadata) {} | |
119 | 129 |
120 UpdateCheckerImpl::~UpdateCheckerImpl() { | 130 UpdateCheckerImpl::~UpdateCheckerImpl() { |
121 DCHECK(thread_checker_.CalledOnValidThread()); | 131 DCHECK(thread_checker_.CalledOnValidThread()); |
122 } | 132 } |
123 | 133 |
124 bool UpdateCheckerImpl::CheckForUpdates( | 134 bool UpdateCheckerImpl::CheckForUpdates( |
125 const std::vector<CrxUpdateItem*>& items_to_check, | 135 const std::vector<CrxUpdateItem*>& items_to_check, |
126 const std::string& additional_attributes, | 136 const std::string& additional_attributes, |
127 const UpdateCheckCallback& update_check_callback) { | 137 const UpdateCheckCallback& update_check_callback) { |
128 DCHECK(thread_checker_.CalledOnValidThread()); | 138 DCHECK(thread_checker_.CalledOnValidThread()); |
129 | 139 |
130 if (request_sender_.get()) { | 140 if (request_sender_.get()) { |
131 NOTREACHED(); | 141 NOTREACHED(); |
132 return false; // Another update check is in progress. | 142 return false; // Another update check is in progress. |
133 } | 143 } |
134 | 144 |
135 update_check_callback_ = update_check_callback; | 145 update_check_callback_ = update_check_callback; |
136 | 146 |
137 auto urls(config_->UpdateUrl()); | 147 auto urls(config_->UpdateUrl()); |
138 if (IsEncryptionRequired(items_to_check)) | 148 if (IsEncryptionRequired(items_to_check)) |
139 RemoveUnsecureUrls(&urls); | 149 RemoveUnsecureUrls(&urls); |
140 | 150 |
151 scoped_ptr<std::vector<std::string>> ids_checked( | |
152 new std::vector<std::string>()); | |
153 for (auto crx : items_to_check) { | |
154 ids_checked->push_back(crx->id); | |
Bernhard Bauer
2016/04/08 09:06:10
Nit: Braces are optional for one-line bodies (and
waffles
2016/04/08 19:11:01
Done.
| |
155 } | |
141 request_sender_.reset(new RequestSender(config_)); | 156 request_sender_.reset(new RequestSender(config_)); |
142 request_sender_->Send( | 157 request_sender_->Send( |
143 config_->UseCupSigning(), | 158 config_->UseCupSigning(), |
144 BuildUpdateCheckRequest(*config_, items_to_check, additional_attributes), | 159 BuildUpdateCheckRequest(*config_, items_to_check, *metadata_, |
160 additional_attributes), | |
145 urls, base::Bind(&UpdateCheckerImpl::OnRequestSenderComplete, | 161 urls, base::Bind(&UpdateCheckerImpl::OnRequestSenderComplete, |
146 base::Unretained(this))); | 162 base::Unretained(this), base::Passed(&ids_checked))); |
Bernhard Bauer
2016/04/08 09:06:10
Since base::Passed is now implemented in terms of
waffles
2016/04/08 19:11:01
Hm, not sure I'm understand what you're suggesting
Bernhard Bauer
2016/04/08 21:54:07
No, base::Passed has a form where it takes a point
| |
147 return true; | 163 return true; |
148 } | 164 } |
149 | 165 |
150 void UpdateCheckerImpl::OnRequestSenderComplete(int error, | 166 void UpdateCheckerImpl::OnRequestSenderComplete( |
151 const std::string& response, | 167 scoped_ptr<std::vector<std::string>> ids_checked, |
Bernhard Bauer
2016/04/08 09:06:10
std::unique_ptr
waffles
2016/04/08 19:11:01
Done.
| |
152 int retry_after_sec) { | 168 int error, |
169 const std::string& response, | |
170 int retry_after_sec) { | |
153 DCHECK(thread_checker_.CalledOnValidThread()); | 171 DCHECK(thread_checker_.CalledOnValidThread()); |
154 | 172 |
155 if (!error) { | 173 if (!error) { |
156 UpdateResponse update_response; | 174 UpdateResponse update_response; |
157 if (update_response.Parse(response)) { | 175 if (update_response.Parse(response)) { |
176 int daynum = update_response.results().daystart_elapsed_days; | |
177 if (daynum != UpdateResponse::kNoDaystart) { | |
Bernhard Bauer
2016/04/08 09:06:10
No braces.
waffles
2016/04/08 19:11:01
Done.
| |
178 metadata_->SetDateLastRollCall(*ids_checked, daynum); | |
179 } | |
158 base::ThreadTaskRunnerHandle::Get()->PostTask( | 180 base::ThreadTaskRunnerHandle::Get()->PostTask( |
159 FROM_HERE, base::Bind(update_check_callback_, error, | 181 FROM_HERE, base::Bind(update_check_callback_, error, |
160 update_response.results(), retry_after_sec)); | 182 update_response.results(), retry_after_sec)); |
161 return; | 183 return; |
162 } | 184 } |
163 | 185 |
164 error = -1; | 186 error = -1; |
165 VLOG(1) << "Parse failed " << update_response.errors(); | 187 VLOG(1) << "Parse failed " << update_response.errors(); |
166 } | 188 } |
167 | 189 |
168 base::ThreadTaskRunnerHandle::Get()->PostTask( | 190 base::ThreadTaskRunnerHandle::Get()->PostTask( |
169 FROM_HERE, base::Bind(update_check_callback_, error, | 191 FROM_HERE, base::Bind(update_check_callback_, error, |
170 UpdateResponse::Results(), retry_after_sec)); | 192 UpdateResponse::Results(), retry_after_sec)); |
171 } | 193 } |
172 | 194 |
173 } // namespace | 195 } // namespace |
174 | 196 |
175 scoped_ptr<UpdateChecker> UpdateChecker::Create( | 197 scoped_ptr<UpdateChecker> UpdateChecker::Create( |
176 const scoped_refptr<Configurator>& config) { | 198 const scoped_refptr<Configurator>& config, |
177 return scoped_ptr<UpdateChecker>(new UpdateCheckerImpl(config)); | 199 const scoped_refptr<PersistedData>& persistent) { |
200 return scoped_ptr<UpdateChecker>(new UpdateCheckerImpl(config, persistent)); | |
178 } | 201 } |
179 | 202 |
180 } // namespace update_client | 203 } // namespace update_client |
OLD | NEW |