| 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 23 matching lines...) Expand all Loading... |
| 56 // An app element looks like this: | 57 // An app element looks like this: |
| 57 // <app appid="hnimpnehoodheedghdeeijklkeaacbdc" | 58 // <app appid="hnimpnehoodheedghdeeijklkeaacbdc" |
| 58 // version="0.1.2.3" installsource="ondemand"> | 59 // version="0.1.2.3" installsource="ondemand"> |
| 59 // <updatecheck /> | 60 // <updatecheck /> |
| 60 // <packages> | 61 // <packages> |
| 61 // <package fp="abcd" /> | 62 // <package fp="abcd" /> |
| 62 // </packages> | 63 // </packages> |
| 63 // </app> | 64 // </app> |
| 64 std::string BuildUpdateCheckRequest(const Configurator& config, | 65 std::string BuildUpdateCheckRequest(const Configurator& config, |
| 65 const std::vector<CrxUpdateItem*>& items, | 66 const std::vector<CrxUpdateItem*>& items, |
| 67 const PersistedData& metadata, |
| 66 const std::string& additional_attributes) { | 68 const std::string& additional_attributes) { |
| 67 const std::string brand(SanitizeBrand(config.GetBrand())); | 69 const std::string brand(SanitizeBrand(config.GetBrand())); |
| 68 std::string app_elements; | 70 std::string app_elements; |
| 69 for (size_t i = 0; i != items.size(); ++i) { | 71 for (size_t i = 0; i != items.size(); ++i) { |
| 70 const CrxUpdateItem* item = items[i]; | 72 const CrxUpdateItem* item = items[i]; |
| 71 const std::string ap(SanitizeAp(item->component.ap)); | 73 const std::string ap(SanitizeAp(item->component.ap)); |
| 72 std::string app("<app "); | 74 std::string app("<app "); |
| 73 base::StringAppendF(&app, "appid=\"%s\" version=\"%s\"", item->id.c_str(), | 75 base::StringAppendF(&app, "appid=\"%s\" version=\"%s\"", item->id.c_str(), |
| 74 item->component.version.GetString().c_str()); | 76 item->component.version.GetString().c_str()); |
| 75 if (!brand.empty()) | 77 if (!brand.empty()) |
| 76 base::StringAppendF(&app, " brand=\"%s\"", brand.c_str()); | 78 base::StringAppendF(&app, " brand=\"%s\"", brand.c_str()); |
| 77 if (item->on_demand) | 79 if (item->on_demand) |
| 78 base::StringAppendF(&app, " installsource=\"ondemand\""); | 80 base::StringAppendF(&app, " installsource=\"ondemand\""); |
| 79 if (!ap.empty()) | 81 if (!ap.empty()) |
| 80 base::StringAppendF(&app, " ap=\"%s\"", ap.c_str()); | 82 base::StringAppendF(&app, " ap=\"%s\"", ap.c_str()); |
| 81 base::StringAppendF(&app, ">"); | 83 base::StringAppendF(&app, ">"); |
| 82 base::StringAppendF(&app, "<updatecheck />"); | 84 base::StringAppendF(&app, "<updatecheck />"); |
| 85 base::StringAppendF(&app, "<ping rd=\"%d\" />", |
| 86 metadata.GetDateLastRollCall(item->id)); |
| 83 if (!item->component.fingerprint.empty()) { | 87 if (!item->component.fingerprint.empty()) { |
| 84 base::StringAppendF(&app, | 88 base::StringAppendF(&app, |
| 85 "<packages>" | 89 "<packages>" |
| 86 "<package fp=\"%s\"/>" | 90 "<package fp=\"%s\"/>" |
| 87 "</packages>", | 91 "</packages>", |
| 88 item->component.fingerprint.c_str()); | 92 item->component.fingerprint.c_str()); |
| 89 } | 93 } |
| 90 base::StringAppendF(&app, "</app>"); | 94 base::StringAppendF(&app, "</app>"); |
| 91 app_elements.append(app); | 95 app_elements.append(app); |
| 92 VLOG(1) << "Appending to update request: " << app; | 96 VLOG(1) << "Appending to update request: " << app; |
| 93 } | 97 } |
| 94 | 98 |
| 95 return BuildProtocolRequest( | 99 return BuildProtocolRequest( |
| 96 config.GetBrowserVersion().GetString(), config.GetChannel(), | 100 config.GetBrowserVersion().GetString(), config.GetChannel(), |
| 97 config.GetLang(), config.GetOSLongName(), config.GetDownloadPreference(), | 101 config.GetLang(), config.GetOSLongName(), config.GetDownloadPreference(), |
| 98 app_elements, additional_attributes); | 102 app_elements, additional_attributes); |
| 99 } | 103 } |
| 100 | 104 |
| 101 class UpdateCheckerImpl : public UpdateChecker { | 105 class UpdateCheckerImpl : public UpdateChecker { |
| 102 public: | 106 public: |
| 103 explicit UpdateCheckerImpl(const scoped_refptr<Configurator>& config); | 107 UpdateCheckerImpl(const scoped_refptr<Configurator>& config, |
| 108 const PersistedData& metadata); |
| 104 ~UpdateCheckerImpl() override; | 109 ~UpdateCheckerImpl() override; |
| 105 | 110 |
| 106 // Overrides for UpdateChecker. | 111 // Overrides for UpdateChecker. |
| 107 bool CheckForUpdates( | 112 bool CheckForUpdates( |
| 108 const std::vector<CrxUpdateItem*>& items_to_check, | 113 const std::vector<CrxUpdateItem*>& items_to_check, |
| 109 const std::string& additional_attributes, | 114 const std::string& additional_attributes, |
| 110 const UpdateCheckCallback& update_check_callback) override; | 115 const UpdateCheckCallback& update_check_callback) override; |
| 111 | 116 |
| 112 private: | 117 private: |
| 113 void OnRequestSenderComplete(int error, | 118 void OnRequestSenderComplete(scoped_ptr<std::vector<std::string>> ids_checked, |
| 119 int error, |
| 114 const std::string& response, | 120 const std::string& response, |
| 115 int retry_after_sec); | 121 int retry_after_sec); |
| 116 base::ThreadChecker thread_checker_; | 122 base::ThreadChecker thread_checker_; |
| 117 | 123 |
| 118 const scoped_refptr<Configurator> config_; | 124 const scoped_refptr<Configurator> config_; |
| 125 const PersistedData& metadata_; |
| 119 UpdateCheckCallback update_check_callback_; | 126 UpdateCheckCallback update_check_callback_; |
| 120 scoped_ptr<RequestSender> request_sender_; | 127 scoped_ptr<RequestSender> request_sender_; |
| 121 | 128 |
| 122 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerImpl); | 129 DISALLOW_COPY_AND_ASSIGN(UpdateCheckerImpl); |
| 123 }; | 130 }; |
| 124 | 131 |
| 125 UpdateCheckerImpl::UpdateCheckerImpl(const scoped_refptr<Configurator>& config) | 132 UpdateCheckerImpl::UpdateCheckerImpl(const scoped_refptr<Configurator>& config, |
| 126 : config_(config) {} | 133 const PersistedData& metadata) |
| 134 : config_(config), metadata_(metadata) {} |
| 127 | 135 |
| 128 UpdateCheckerImpl::~UpdateCheckerImpl() { | 136 UpdateCheckerImpl::~UpdateCheckerImpl() { |
| 129 DCHECK(thread_checker_.CalledOnValidThread()); | 137 DCHECK(thread_checker_.CalledOnValidThread()); |
| 130 } | 138 } |
| 131 | 139 |
| 132 bool UpdateCheckerImpl::CheckForUpdates( | 140 bool UpdateCheckerImpl::CheckForUpdates( |
| 133 const std::vector<CrxUpdateItem*>& items_to_check, | 141 const std::vector<CrxUpdateItem*>& items_to_check, |
| 134 const std::string& additional_attributes, | 142 const std::string& additional_attributes, |
| 135 const UpdateCheckCallback& update_check_callback) { | 143 const UpdateCheckCallback& update_check_callback) { |
| 136 DCHECK(thread_checker_.CalledOnValidThread()); | 144 DCHECK(thread_checker_.CalledOnValidThread()); |
| 137 | 145 |
| 138 if (request_sender_.get()) { | 146 if (request_sender_.get()) { |
| 139 NOTREACHED(); | 147 NOTREACHED(); |
| 140 return false; // Another update check is in progress. | 148 return false; // Another update check is in progress. |
| 141 } | 149 } |
| 142 | 150 |
| 143 update_check_callback_ = update_check_callback; | 151 update_check_callback_ = update_check_callback; |
| 144 | 152 |
| 145 auto urls(config_->UpdateUrl()); | 153 auto urls(config_->UpdateUrl()); |
| 146 if (IsEncryptionRequired(items_to_check)) | 154 if (IsEncryptionRequired(items_to_check)) |
| 147 RemoveUnsecureUrls(&urls); | 155 RemoveUnsecureUrls(&urls); |
| 148 | 156 |
| 157 std::unique_ptr<std::vector<std::string>> ids_checked( |
| 158 new std::vector<std::string>()); |
| 159 for (auto crx : items_to_check) |
| 160 ids_checked->push_back(crx->id); |
| 149 request_sender_.reset(new RequestSender(config_)); | 161 request_sender_.reset(new RequestSender(config_)); |
| 150 request_sender_->Send( | 162 request_sender_->Send( |
| 151 config_->UseCupSigning(), | 163 config_->UseCupSigning(), |
| 152 BuildUpdateCheckRequest(*config_, items_to_check, additional_attributes), | 164 BuildUpdateCheckRequest(*config_, items_to_check, metadata_, |
| 165 additional_attributes), |
| 153 urls, base::Bind(&UpdateCheckerImpl::OnRequestSenderComplete, | 166 urls, base::Bind(&UpdateCheckerImpl::OnRequestSenderComplete, |
| 154 base::Unretained(this))); | 167 base::Unretained(this), base::Passed(&ids_checked))); |
| 155 return true; | 168 return true; |
| 156 } | 169 } |
| 157 | 170 |
| 158 void UpdateCheckerImpl::OnRequestSenderComplete(int error, | 171 void UpdateCheckerImpl::OnRequestSenderComplete( |
| 159 const std::string& response, | 172 std::unique_ptr<std::vector<std::string>> ids_checked, |
| 160 int retry_after_sec) { | 173 int error, |
| 174 const std::string& response, |
| 175 int retry_after_sec) { |
| 161 DCHECK(thread_checker_.CalledOnValidThread()); | 176 DCHECK(thread_checker_.CalledOnValidThread()); |
| 162 | 177 |
| 163 if (!error) { | 178 if (!error) { |
| 164 UpdateResponse update_response; | 179 UpdateResponse update_response; |
| 165 if (update_response.Parse(response)) { | 180 if (update_response.Parse(response)) { |
| 181 int daynum = update_response.results().daystart_elapsed_days; |
| 182 if (daynum != UpdateResponse::kNoDaystart) |
| 183 metadata_.SetDateLastRollCall(*ids_checked, daynum); |
| 166 base::ThreadTaskRunnerHandle::Get()->PostTask( | 184 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 167 FROM_HERE, base::Bind(update_check_callback_, error, | 185 FROM_HERE, base::Bind(update_check_callback_, error, |
| 168 update_response.results(), retry_after_sec)); | 186 update_response.results(), retry_after_sec)); |
| 169 return; | 187 return; |
| 170 } | 188 } |
| 171 | 189 |
| 172 error = -1; | 190 error = -1; |
| 173 VLOG(1) << "Parse failed " << update_response.errors(); | 191 VLOG(1) << "Parse failed " << update_response.errors(); |
| 174 } | 192 } |
| 175 | 193 |
| 176 base::ThreadTaskRunnerHandle::Get()->PostTask( | 194 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 177 FROM_HERE, base::Bind(update_check_callback_, error, | 195 FROM_HERE, base::Bind(update_check_callback_, error, |
| 178 UpdateResponse::Results(), retry_after_sec)); | 196 UpdateResponse::Results(), retry_after_sec)); |
| 179 } | 197 } |
| 180 | 198 |
| 181 } // namespace | 199 } // namespace |
| 182 | 200 |
| 183 scoped_ptr<UpdateChecker> UpdateChecker::Create( | 201 scoped_ptr<UpdateChecker> UpdateChecker::Create( |
| 184 const scoped_refptr<Configurator>& config) { | 202 const scoped_refptr<Configurator>& config, |
| 185 return scoped_ptr<UpdateChecker>(new UpdateCheckerImpl(config)); | 203 const PersistedData& persistent) { |
| 204 return scoped_ptr<UpdateChecker>(new UpdateCheckerImpl(config, persistent)); |
| 186 } | 205 } |
| 187 | 206 |
| 188 } // namespace update_client | 207 } // namespace update_client |
| OLD | NEW |