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(&app, "<ping rd=\"%d\" />", |
| 78 metadata.GetDateLastRollCall(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(const scoped_refptr<Configurator>& config, |
| 100 const PersistedData& metadata); |
96 ~UpdateCheckerImpl() override; | 101 ~UpdateCheckerImpl() override; |
97 | 102 |
98 // Overrides for UpdateChecker. | 103 // Overrides for UpdateChecker. |
99 bool CheckForUpdates( | 104 bool CheckForUpdates( |
100 const std::vector<CrxUpdateItem*>& items_to_check, | 105 const std::vector<CrxUpdateItem*>& items_to_check, |
101 const std::string& additional_attributes, | 106 const std::string& additional_attributes, |
102 const UpdateCheckCallback& update_check_callback) override; | 107 const UpdateCheckCallback& update_check_callback) override; |
103 | 108 |
104 private: | 109 private: |
105 void OnRequestSenderComplete(int error, | 110 void OnRequestSenderComplete(scoped_ptr<std::vector<std::string>> ids_checked, |
| 111 int error, |
106 const std::string& response, | 112 const std::string& response, |
107 int retry_after_sec); | 113 int retry_after_sec); |
108 base::ThreadChecker thread_checker_; | 114 base::ThreadChecker thread_checker_; |
109 | 115 |
110 const scoped_refptr<Configurator> config_; | 116 const scoped_refptr<Configurator> config_; |
| 117 const PersistedData& metadata_; |
111 UpdateCheckCallback update_check_callback_; | 118 UpdateCheckCallback update_check_callback_; |
112 scoped_ptr<RequestSender> request_sender_; | 119 scoped_ptr<RequestSender> request_sender_; |
113 | 120 |
114 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerImpl); | 121 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerImpl); |
115 }; | 122 }; |
116 | 123 |
117 UpdateCheckerImpl::UpdateCheckerImpl(const scoped_refptr<Configurator>& config) | 124 UpdateCheckerImpl::UpdateCheckerImpl(const scoped_refptr<Configurator>& config, |
118 : config_(config) {} | 125 const PersistedData& metadata) |
| 126 : config_(config), metadata_(metadata) {} |
119 | 127 |
120 UpdateCheckerImpl::~UpdateCheckerImpl() { | 128 UpdateCheckerImpl::~UpdateCheckerImpl() { |
121 DCHECK(thread_checker_.CalledOnValidThread()); | 129 DCHECK(thread_checker_.CalledOnValidThread()); |
122 } | 130 } |
123 | 131 |
124 bool UpdateCheckerImpl::CheckForUpdates( | 132 bool UpdateCheckerImpl::CheckForUpdates( |
125 const std::vector<CrxUpdateItem*>& items_to_check, | 133 const std::vector<CrxUpdateItem*>& items_to_check, |
126 const std::string& additional_attributes, | 134 const std::string& additional_attributes, |
127 const UpdateCheckCallback& update_check_callback) { | 135 const UpdateCheckCallback& update_check_callback) { |
128 DCHECK(thread_checker_.CalledOnValidThread()); | 136 DCHECK(thread_checker_.CalledOnValidThread()); |
129 | 137 |
130 if (request_sender_.get()) { | 138 if (request_sender_.get()) { |
131 NOTREACHED(); | 139 NOTREACHED(); |
132 return false; // Another update check is in progress. | 140 return false; // Another update check is in progress. |
133 } | 141 } |
134 | 142 |
135 update_check_callback_ = update_check_callback; | 143 update_check_callback_ = update_check_callback; |
136 | 144 |
137 auto urls(config_->UpdateUrl()); | 145 auto urls(config_->UpdateUrl()); |
138 if (IsEncryptionRequired(items_to_check)) | 146 if (IsEncryptionRequired(items_to_check)) |
139 RemoveUnsecureUrls(&urls); | 147 RemoveUnsecureUrls(&urls); |
140 | 148 |
| 149 std::unique_ptr<std::vector<std::string>> ids_checked( |
| 150 new std::vector<std::string>()); |
| 151 for (auto crx : items_to_check) |
| 152 ids_checked->push_back(crx->id); |
141 request_sender_.reset(new RequestSender(config_)); | 153 request_sender_.reset(new RequestSender(config_)); |
142 request_sender_->Send( | 154 request_sender_->Send( |
143 config_->UseCupSigning(), | 155 config_->UseCupSigning(), |
144 BuildUpdateCheckRequest(*config_, items_to_check, additional_attributes), | 156 BuildUpdateCheckRequest(*config_, items_to_check, metadata_, |
| 157 additional_attributes), |
145 urls, base::Bind(&UpdateCheckerImpl::OnRequestSenderComplete, | 158 urls, base::Bind(&UpdateCheckerImpl::OnRequestSenderComplete, |
146 base::Unretained(this))); | 159 base::Unretained(this), base::Passed(&ids_checked))); |
147 return true; | 160 return true; |
148 } | 161 } |
149 | 162 |
150 void UpdateCheckerImpl::OnRequestSenderComplete(int error, | 163 void UpdateCheckerImpl::OnRequestSenderComplete( |
151 const std::string& response, | 164 std::unique_ptr<std::vector<std::string>> ids_checked, |
152 int retry_after_sec) { | 165 int error, |
| 166 const std::string& response, |
| 167 int retry_after_sec) { |
153 DCHECK(thread_checker_.CalledOnValidThread()); | 168 DCHECK(thread_checker_.CalledOnValidThread()); |
154 | 169 |
155 if (!error) { | 170 if (!error) { |
156 UpdateResponse update_response; | 171 UpdateResponse update_response; |
157 if (update_response.Parse(response)) { | 172 if (update_response.Parse(response)) { |
| 173 int daynum = update_response.results().daystart_elapsed_days; |
| 174 if (daynum != UpdateResponse::kNoDaystart) |
| 175 metadata_.SetDateLastRollCall(*ids_checked, daynum); |
158 base::ThreadTaskRunnerHandle::Get()->PostTask( | 176 base::ThreadTaskRunnerHandle::Get()->PostTask( |
159 FROM_HERE, base::Bind(update_check_callback_, error, | 177 FROM_HERE, base::Bind(update_check_callback_, error, |
160 update_response.results(), retry_after_sec)); | 178 update_response.results(), retry_after_sec)); |
161 return; | 179 return; |
162 } | 180 } |
163 | 181 |
164 error = -1; | 182 error = -1; |
165 VLOG(1) << "Parse failed " << update_response.errors(); | 183 VLOG(1) << "Parse failed " << update_response.errors(); |
166 } | 184 } |
167 | 185 |
168 base::ThreadTaskRunnerHandle::Get()->PostTask( | 186 base::ThreadTaskRunnerHandle::Get()->PostTask( |
169 FROM_HERE, base::Bind(update_check_callback_, error, | 187 FROM_HERE, base::Bind(update_check_callback_, error, |
170 UpdateResponse::Results(), retry_after_sec)); | 188 UpdateResponse::Results(), retry_after_sec)); |
171 } | 189 } |
172 | 190 |
173 } // namespace | 191 } // namespace |
174 | 192 |
175 scoped_ptr<UpdateChecker> UpdateChecker::Create( | 193 scoped_ptr<UpdateChecker> UpdateChecker::Create( |
176 const scoped_refptr<Configurator>& config) { | 194 const scoped_refptr<Configurator>& config, |
177 return scoped_ptr<UpdateChecker>(new UpdateCheckerImpl(config)); | 195 const PersistedData& persistent) { |
| 196 return scoped_ptr<UpdateChecker>(new UpdateCheckerImpl(config, persistent)); |
178 } | 197 } |
179 | 198 |
180 } // namespace update_client | 199 } // namespace update_client |
OLD | NEW |