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.
5 #include "components/update_client/utils.h"
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"
29 namespace update_client
{
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
));
40 // Produces an extension-like friendly id.
41 std::string
HexStringToID(const std::string
& hexstr
) {
43 for (size_t i
= 0; i
< hexstr
.size(); ++i
) {
45 if (base::HexStringToInt(
46 base::StringPiece(hexstr
.begin() + i
, hexstr
.begin() + i
+ 1),
48 id
.append(1, val
+ 'a');
54 DCHECK(crx_file::id_util::IdIsValid(id
));
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
));
71 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
72 "<request protocol=\"3.0\" ");
74 if (!additional_attributes
.empty())
75 base::StringAppendF(&request
, "%s ", additional_attributes
.c_str());
77 // Chrome version and platform information.
80 "version=\"%s-%s\" prodversion=\"%s\" "
81 "requestid=\"{%s}\" lang=\"%s\" updaterchannel=\"%s\" prodchannel=\"%s\" "
82 "os=\"%s\" arch=\"%s\" nacl_arch=\"%s\"",
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"
94 const bool is_wow64(base::win::OSInfo::GetInstance()->wow64_status() ==
95 base::win::OSInfo::WOW64_ENABLED
);
97 base::StringAppendF(&request
, " wow64=\"1\"");
99 base::StringAppendF(&request
, ">");
101 // HW platform information.
102 base::StringAppendF(&request
, "<hw physmemory=\"%d\"/>",
103 GetPhysicalMemoryGB()); // "physmem" in GB.
105 // OS version and platform information.
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"
112 // The actual payload of the request.
113 base::StringAppendF(&request
, "%s</request>", request_body
.c_str());
118 net::URLFetcher
* SendProtocolRequest(
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
));
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();
137 bool FetchSuccess(const net::URLFetcher
& fetcher
) {
138 return GetFetchError(fetcher
) == 0;
141 int GetFetchError(const net::URLFetcher
& fetcher
) {
142 const net::URLRequestStatus::Status
status(fetcher
.GetStatus().status());
144 case net::URLRequestStatus::IO_PENDING
:
145 case net::URLRequestStatus::CANCELED
:
146 // Network status is a small positive number.
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)
155 return response_code
? response_code
: -1;
158 case net::URLRequestStatus::FAILED
: {
159 // Network errors are small negative numbers.
160 const int error
= fetcher
.GetStatus().error();
161 return error
? error
: -1;
169 bool HasDiffUpdate(const CrxUpdateItem
* update_item
) {
170 return !update_item
->crx_diffurls
.empty();
173 bool IsHttpServerError(int status_code
) {
174 return 500 <= status_code
&& status_code
< 600;
177 bool DeleteFileAndEmptyParentDirectory(const base::FilePath
& filepath
) {
178 if (!base::DeleteFile(filepath
, false))
181 const base::FilePath
dirname(filepath
.DirName());
182 if (!base::IsDirectoryEmpty(dirname
))
185 return base::DeleteFile(dirname
, false);
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
)));
195 } // namespace update_client