| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/component_updater/component_updater_utils.h" | |
| 6 | |
| 7 #include <cmath> | |
| 8 | |
| 9 #include "base/file_util.h" | |
| 10 #include "base/files/file_path.h" | |
| 11 #include "base/guid.h" | |
| 12 #include "base/strings/string_number_conversions.h" | |
| 13 #include "base/strings/string_piece.h" | |
| 14 #include "base/strings/string_util.h" | |
| 15 #include "base/strings/stringprintf.h" | |
| 16 #include "base/sys_info.h" | |
| 17 #include "base/win/windows_version.h" | |
| 18 #include "chrome/browser/component_updater/component_updater_configurator.h" | |
| 19 #include "chrome/browser/component_updater/crx_update_item.h" | |
| 20 #include "components/omaha_query_params/omaha_query_params.h" | |
| 21 #include "extensions/common/extension.h" | |
| 22 #include "net/base/load_flags.h" | |
| 23 #include "net/url_request/url_fetcher.h" | |
| 24 #include "net/url_request/url_request_context_getter.h" | |
| 25 #include "net/url_request/url_request_status.h" | |
| 26 | |
| 27 using omaha_query_params::OmahaQueryParams; | |
| 28 | |
| 29 namespace component_updater { | |
| 30 | |
| 31 namespace { | |
| 32 | |
| 33 // Returns the amount of physical memory in GB, rounded to the nearest GB. | |
| 34 int GetPhysicalMemoryGB() { | |
| 35 const double kOneGB = 1024 * 1024 * 1024; | |
| 36 const int64 phys_mem = base::SysInfo::AmountOfPhysicalMemory(); | |
| 37 return static_cast<int>(std::floor(0.5 + phys_mem / kOneGB)); | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 std::string BuildProtocolRequest(const std::string& browser_version, | |
| 43 const std::string& channel, | |
| 44 const std::string& lang, | |
| 45 const std::string& os_long_name, | |
| 46 const std::string& request_body, | |
| 47 const std::string& additional_attributes) { | |
| 48 const std::string prod_id( | |
| 49 OmahaQueryParams::GetProdIdString(OmahaQueryParams::CHROME)); | |
| 50 | |
| 51 std::string request( | |
| 52 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" | |
| 53 "<request protocol=\"3.0\" "); | |
| 54 | |
| 55 if (!additional_attributes.empty()) | |
| 56 base::StringAppendF(&request, "%s ", additional_attributes.c_str()); | |
| 57 | |
| 58 // Chrome version and platform information. | |
| 59 base::StringAppendF( | |
| 60 &request, | |
| 61 "version=\"%s-%s\" prodversion=\"%s\" " | |
| 62 "requestid=\"{%s}\" lang=\"%s\" updaterchannel=\"%s\" prodchannel=\"%s\" " | |
| 63 "os=\"%s\" arch=\"%s\" nacl_arch=\"%s\"", | |
| 64 prod_id.c_str(), | |
| 65 browser_version.c_str(), // "version" | |
| 66 browser_version.c_str(), // "prodversion" | |
| 67 base::GenerateGUID().c_str(), // "requestid" | |
| 68 lang.c_str(), // "lang", | |
| 69 channel.c_str(), // "updaterchannel" | |
| 70 channel.c_str(), // "prodchannel" | |
| 71 OmahaQueryParams::GetOS(), // "os" | |
| 72 OmahaQueryParams::GetArch(), // "arch" | |
| 73 OmahaQueryParams::GetNaclArch()); // "nacl_arch" | |
| 74 #if defined(OS_WIN) | |
| 75 const bool is_wow64(base::win::OSInfo::GetInstance()->wow64_status() == | |
| 76 base::win::OSInfo::WOW64_ENABLED); | |
| 77 if (is_wow64) | |
| 78 base::StringAppendF(&request, " wow64=\"1\""); | |
| 79 #endif | |
| 80 base::StringAppendF(&request, ">"); | |
| 81 | |
| 82 // HW platform information. | |
| 83 base::StringAppendF(&request, | |
| 84 "<hw physmemory=\"%d\"/>", | |
| 85 GetPhysicalMemoryGB()); // "physmem" in GB. | |
| 86 | |
| 87 // OS version and platform information. | |
| 88 base::StringAppendF( | |
| 89 &request, | |
| 90 "<os platform=\"%s\" version=\"%s\" arch=\"%s\"/>", | |
| 91 os_long_name.c_str(), // "platform" | |
| 92 base::SysInfo().OperatingSystemVersion().c_str(), // "version" | |
| 93 base::SysInfo().OperatingSystemArchitecture().c_str()); // "arch" | |
| 94 | |
| 95 // The actual payload of the request. | |
| 96 base::StringAppendF(&request, "%s</request>", request_body.c_str()); | |
| 97 | |
| 98 return request; | |
| 99 } | |
| 100 | |
| 101 net::URLFetcher* SendProtocolRequest( | |
| 102 const GURL& url, | |
| 103 const std::string& protocol_request, | |
| 104 net::URLFetcherDelegate* url_fetcher_delegate, | |
| 105 net::URLRequestContextGetter* url_request_context_getter) { | |
| 106 net::URLFetcher* url_fetcher(net::URLFetcher::Create( | |
| 107 0, url, net::URLFetcher::POST, url_fetcher_delegate)); | |
| 108 | |
| 109 url_fetcher->SetUploadData("application/xml", protocol_request); | |
| 110 url_fetcher->SetRequestContext(url_request_context_getter); | |
| 111 url_fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | |
| 112 net::LOAD_DO_NOT_SAVE_COOKIES | | |
| 113 net::LOAD_DISABLE_CACHE); | |
| 114 url_fetcher->SetAutomaticallyRetryOn5xx(false); | |
| 115 url_fetcher->Start(); | |
| 116 | |
| 117 return url_fetcher; | |
| 118 } | |
| 119 | |
| 120 bool FetchSuccess(const net::URLFetcher& fetcher) { | |
| 121 return GetFetchError(fetcher) == 0; | |
| 122 } | |
| 123 | |
| 124 int GetFetchError(const net::URLFetcher& fetcher) { | |
| 125 const net::URLRequestStatus::Status status(fetcher.GetStatus().status()); | |
| 126 switch (status) { | |
| 127 case net::URLRequestStatus::IO_PENDING: | |
| 128 case net::URLRequestStatus::CANCELED: | |
| 129 // Network status is a small positive number. | |
| 130 return status; | |
| 131 | |
| 132 case net::URLRequestStatus::SUCCESS: { | |
| 133 // Response codes are positive numbers, greater than 100. | |
| 134 const int response_code(fetcher.GetResponseCode()); | |
| 135 if (response_code == 200) | |
| 136 return 0; | |
| 137 else | |
| 138 return response_code ? response_code : -1; | |
| 139 } | |
| 140 | |
| 141 case net::URLRequestStatus::FAILED: { | |
| 142 // Network errors are small negative numbers. | |
| 143 const int error = fetcher.GetStatus().error(); | |
| 144 return error ? error : -1; | |
| 145 } | |
| 146 | |
| 147 default: | |
| 148 return -1; | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 bool HasDiffUpdate(const CrxUpdateItem* update_item) { | |
| 153 return !update_item->crx_diffurls.empty(); | |
| 154 } | |
| 155 | |
| 156 bool IsHttpServerError(int status_code) { | |
| 157 return 500 <= status_code && status_code < 600; | |
| 158 } | |
| 159 | |
| 160 bool DeleteFileAndEmptyParentDirectory(const base::FilePath& filepath) { | |
| 161 if (!base::DeleteFile(filepath, false)) | |
| 162 return false; | |
| 163 | |
| 164 const base::FilePath dirname(filepath.DirName()); | |
| 165 if (!base::IsDirectoryEmpty(dirname)) | |
| 166 return true; | |
| 167 | |
| 168 return base::DeleteFile(dirname, false); | |
| 169 } | |
| 170 | |
| 171 // Produces an extension-like friendly id. | |
| 172 std::string HexStringToID(const std::string& hexstr) { | |
| 173 std::string id; | |
| 174 for (size_t i = 0; i < hexstr.size(); ++i) { | |
| 175 int val = 0; | |
| 176 if (base::HexStringToInt( | |
| 177 base::StringPiece(hexstr.begin() + i, hexstr.begin() + i + 1), | |
| 178 &val)) { | |
| 179 id.append(1, val + 'a'); | |
| 180 } else { | |
| 181 id.append(1, 'a'); | |
| 182 } | |
| 183 } | |
| 184 DCHECK(extensions::Extension::IdIsValid(id)); | |
| 185 return id; | |
| 186 } | |
| 187 | |
| 188 std::string GetCrxComponentID(const CrxComponent& component) { | |
| 189 return HexStringToID(base::StringToLowerASCII( | |
| 190 base::HexEncode(&component.pk_hash[0], component.pk_hash.size() / 2))); | |
| 191 } | |
| 192 | |
| 193 } // namespace component_updater | |
| OLD | NEW |