| 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/utils.h" | 5 #include "components/update_client/utils.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | |
| 9 | 8 |
| 10 #include <algorithm> | 9 #include <algorithm> |
| 11 #include <cmath> | 10 #include <cmath> |
| 12 #include <cstring> | 11 #include <cstring> |
| 13 #include <map> | 12 #include <map> |
| 14 #include <vector> | 13 #include <vector> |
| 15 | 14 |
| 16 #include "base/callback.h" | 15 #include "base/callback.h" |
| 17 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
| 18 #include "base/files/file_util.h" | 17 #include "base/files/file_util.h" |
| 19 #include "base/files/memory_mapped_file.h" | 18 #include "base/files/memory_mapped_file.h" |
| 20 #include "base/guid.h" | |
| 21 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 22 #include "base/strings/string_piece.h" | 20 #include "base/strings/string_piece.h" |
| 23 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
| 24 #include "base/strings/stringprintf.h" | |
| 25 #include "base/sys_info.h" | |
| 26 #include "build/build_config.h" | |
| 27 #include "components/crx_file/id_util.h" | 22 #include "components/crx_file/id_util.h" |
| 28 #include "components/data_use_measurement/core/data_use_user_data.h" | 23 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 29 #include "components/update_client/component.h" | 24 #include "components/update_client/component.h" |
| 30 #include "components/update_client/configurator.h" | 25 #include "components/update_client/configurator.h" |
| 31 #include "components/update_client/crx_update_item.h" | |
| 32 #include "components/update_client/update_client.h" | 26 #include "components/update_client/update_client.h" |
| 33 #include "components/update_client/update_client_errors.h" | 27 #include "components/update_client/update_client_errors.h" |
| 34 #include "components/update_client/update_query_params.h" | |
| 35 #include "components/update_client/updater_state.h" | |
| 36 #include "crypto/secure_hash.h" | 28 #include "crypto/secure_hash.h" |
| 37 #include "crypto/sha2.h" | 29 #include "crypto/sha2.h" |
| 38 #include "net/base/load_flags.h" | 30 #include "net/base/load_flags.h" |
| 39 #include "net/url_request/url_fetcher.h" | 31 #include "net/url_request/url_fetcher.h" |
| 40 #include "net/url_request/url_request_context_getter.h" | 32 #include "net/url_request/url_request_context_getter.h" |
| 41 #include "net/url_request/url_request_status.h" | 33 #include "net/url_request/url_request_status.h" |
| 42 #include "url/gurl.h" | 34 #include "url/gurl.h" |
| 43 | 35 |
| 44 #if defined(OS_WIN) | |
| 45 #include "base/win/windows_version.h" | |
| 46 #endif | |
| 47 | |
| 48 namespace update_client { | 36 namespace update_client { |
| 49 | 37 |
| 50 namespace { | 38 namespace { |
| 51 | 39 |
| 52 // Returns the amount of physical memory in GB, rounded to the nearest GB. | |
| 53 int GetPhysicalMemoryGB() { | |
| 54 const double kOneGB = 1024 * 1024 * 1024; | |
| 55 const int64_t phys_mem = base::SysInfo::AmountOfPhysicalMemory(); | |
| 56 return static_cast<int>(std::floor(0.5 + phys_mem / kOneGB)); | |
| 57 } | |
| 58 | |
| 59 // Produces an extension-like friendly id. | 40 // Produces an extension-like friendly id. |
| 60 std::string HexStringToID(const std::string& hexstr) { | 41 std::string HexStringToID(const std::string& hexstr) { |
| 61 std::string id; | 42 std::string id; |
| 62 for (size_t i = 0; i < hexstr.size(); ++i) { | 43 for (size_t i = 0; i < hexstr.size(); ++i) { |
| 63 int val = 0; | 44 int val = 0; |
| 64 if (base::HexStringToInt( | 45 if (base::HexStringToInt( |
| 65 base::StringPiece(hexstr.begin() + i, hexstr.begin() + i + 1), | 46 base::StringPiece(hexstr.begin() + i, hexstr.begin() + i + 1), |
| 66 &val)) { | 47 &val)) { |
| 67 id.append(1, val + 'a'); | 48 id.append(1, val + 'a'); |
| 68 } else { | 49 } else { |
| 69 id.append(1, 'a'); | 50 id.append(1, 'a'); |
| 70 } | 51 } |
| 71 } | 52 } |
| 72 | 53 |
| 73 DCHECK(crx_file::id_util::IdIsValid(id)); | 54 DCHECK(crx_file::id_util::IdIsValid(id)); |
| 74 | 55 |
| 75 return id; | 56 return id; |
| 76 } | 57 } |
| 77 | 58 |
| 78 std::string GetOSVersion() { | |
| 79 #if defined(OS_WIN) | |
| 80 const auto ver = base::win::OSInfo::GetInstance()->version_number(); | |
| 81 return base::StringPrintf("%d.%d.%d.%d", ver.major, ver.minor, ver.build, | |
| 82 ver.patch); | |
| 83 #else | |
| 84 return base::SysInfo().OperatingSystemVersion(); | |
| 85 #endif | |
| 86 } | |
| 87 | |
| 88 std::string GetServicePack() { | |
| 89 #if defined(OS_WIN) | |
| 90 return base::win::OSInfo::GetInstance()->service_pack_str(); | |
| 91 #else | |
| 92 return std::string(); | |
| 93 #endif | |
| 94 } | |
| 95 | |
| 96 } // namespace | 59 } // namespace |
| 97 | 60 |
| 98 // Builds a protocol message. | |
| 99 std::string BuildProtocolRequest( | |
| 100 const std::string& prod_id, | |
| 101 const std::string& browser_version, | |
| 102 const std::string& channel, | |
| 103 const std::string& lang, | |
| 104 const std::string& os_long_name, | |
| 105 const std::string& download_preference, | |
| 106 const std::string& request_body, | |
| 107 const std::string& additional_attributes, | |
| 108 const std::unique_ptr<UpdaterState::Attributes>& updater_state_attributes) { | |
| 109 std::string request = base::StringPrintf( | |
| 110 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" | |
| 111 "<request protocol=\"%s\" ", | |
| 112 kProtocolVersion); | |
| 113 | |
| 114 if (!additional_attributes.empty()) | |
| 115 base::StringAppendF(&request, "%s ", additional_attributes.c_str()); | |
| 116 | |
| 117 // Chrome version and platform information. | |
| 118 base::StringAppendF( | |
| 119 &request, | |
| 120 "version=\"%s-%s\" prodversion=\"%s\" " | |
| 121 "requestid=\"{%s}\" lang=\"%s\" updaterchannel=\"%s\" prodchannel=\"%s\" " | |
| 122 "os=\"%s\" arch=\"%s\" nacl_arch=\"%s\"", | |
| 123 prod_id.c_str(), // "version" is prefixed by prod_id. | |
| 124 browser_version.c_str(), | |
| 125 browser_version.c_str(), // "prodversion" | |
| 126 base::GenerateGUID().c_str(), // "requestid" | |
| 127 lang.c_str(), // "lang" | |
| 128 channel.c_str(), // "updaterchannel" | |
| 129 channel.c_str(), // "prodchannel" | |
| 130 UpdateQueryParams::GetOS(), // "os" | |
| 131 UpdateQueryParams::GetArch(), // "arch" | |
| 132 UpdateQueryParams::GetNaclArch()); // "nacl_arch" | |
| 133 #if defined(OS_WIN) | |
| 134 const bool is_wow64(base::win::OSInfo::GetInstance()->wow64_status() == | |
| 135 base::win::OSInfo::WOW64_ENABLED); | |
| 136 if (is_wow64) | |
| 137 base::StringAppendF(&request, " wow64=\"1\""); | |
| 138 #endif | |
| 139 if (!download_preference.empty()) | |
| 140 base::StringAppendF(&request, " dlpref=\"%s\"", | |
| 141 download_preference.c_str()); | |
| 142 if (updater_state_attributes && | |
| 143 updater_state_attributes->count(UpdaterState::kIsEnterpriseManaged)) { | |
| 144 base::StringAppendF( | |
| 145 &request, " %s=\"%s\"", // domainjoined | |
| 146 UpdaterState::kIsEnterpriseManaged, | |
| 147 (*updater_state_attributes)[UpdaterState::kIsEnterpriseManaged] | |
| 148 .c_str()); | |
| 149 } | |
| 150 base::StringAppendF(&request, ">"); | |
| 151 | |
| 152 // HW platform information. | |
| 153 base::StringAppendF(&request, "<hw physmemory=\"%d\"/>", | |
| 154 GetPhysicalMemoryGB()); // "physmem" in GB. | |
| 155 | |
| 156 // OS version and platform information. | |
| 157 const std::string os_version = GetOSVersion(); | |
| 158 const std::string os_sp = GetServicePack(); | |
| 159 base::StringAppendF( | |
| 160 &request, "<os platform=\"%s\" arch=\"%s\"", | |
| 161 os_long_name.c_str(), // "platform" | |
| 162 base::SysInfo().OperatingSystemArchitecture().c_str()); // "arch" | |
| 163 if (!os_version.empty()) | |
| 164 base::StringAppendF(&request, " version=\"%s\"", os_version.c_str()); | |
| 165 if (!os_sp.empty()) | |
| 166 base::StringAppendF(&request, " sp=\"%s\"", os_sp.c_str()); | |
| 167 base::StringAppendF(&request, "/>"); | |
| 168 | |
| 169 #if defined(GOOGLE_CHROME_BUILD) | |
| 170 // Updater state. | |
| 171 if (updater_state_attributes) { | |
| 172 base::StringAppendF(&request, "<updater"); | |
| 173 for (const auto& attr : *updater_state_attributes) { | |
| 174 if (attr.first != UpdaterState::kIsEnterpriseManaged) { | |
| 175 base::StringAppendF(&request, " %s=\"%s\"", attr.first.c_str(), | |
| 176 attr.second.c_str()); | |
| 177 } | |
| 178 } | |
| 179 base::StringAppendF(&request, "/>"); | |
| 180 } | |
| 181 #endif // GOOGLE_CHROME_BUILD | |
| 182 | |
| 183 // The actual payload of the request. | |
| 184 base::StringAppendF(&request, "%s</request>", request_body.c_str()); | |
| 185 | |
| 186 return request; | |
| 187 } | |
| 188 | 61 |
| 189 std::unique_ptr<net::URLFetcher> SendProtocolRequest( | 62 std::unique_ptr<net::URLFetcher> SendProtocolRequest( |
| 190 const GURL& url, | 63 const GURL& url, |
| 191 const std::string& protocol_request, | 64 const std::string& protocol_request, |
| 192 net::URLFetcherDelegate* url_fetcher_delegate, | 65 net::URLFetcherDelegate* url_fetcher_delegate, |
| 193 net::URLRequestContextGetter* url_request_context_getter) { | 66 net::URLRequestContextGetter* url_request_context_getter) { |
| 194 std::unique_ptr<net::URLFetcher> url_fetcher = net::URLFetcher::Create( | 67 std::unique_ptr<net::URLFetcher> url_fetcher = net::URLFetcher::Create( |
| 195 0, url, net::URLFetcher::POST, url_fetcher_delegate); | 68 0, url, net::URLFetcher::POST, url_fetcher_delegate); |
| 196 if (!url_fetcher.get()) | 69 if (!url_fetcher.get()) |
| 197 return url_fetcher; | 70 return url_fetcher; |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 [](const GURL& url) { return !url.SchemeIsCryptographic(); }), | 216 [](const GURL& url) { return !url.SchemeIsCryptographic(); }), |
| 344 urls->end()); | 217 urls->end()); |
| 345 } | 218 } |
| 346 | 219 |
| 347 CrxInstaller::Result InstallFunctionWrapper(base::Callback<bool()> callback) { | 220 CrxInstaller::Result InstallFunctionWrapper(base::Callback<bool()> callback) { |
| 348 return CrxInstaller::Result(callback.Run() ? InstallError::NONE | 221 return CrxInstaller::Result(callback.Run() ? InstallError::NONE |
| 349 : InstallError::GENERIC_ERROR); | 222 : InstallError::GENERIC_ERROR); |
| 350 } | 223 } |
| 351 | 224 |
| 352 } // namespace update_client | 225 } // namespace update_client |
| OLD | NEW |