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