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/component_updater/component_updater_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/component_updater/component_updater_configurator.h"
20 #include "components/component_updater/crx_update_item.h"
21 #include "components/crx_file/id_util.h"
22 #include "components/update_client/update_query_params.h"
23 #include "net/base/load_flags.h"
24 #include "net/url_request/url_fetcher.h"
25 #include "net/url_request/url_request_context_getter.h"
26 #include "net/url_request/url_request_status.h"
28 using update_client::UpdateQueryParams
;
30 namespace component_updater
{
34 // Returns the amount of physical memory in GB, rounded to the nearest GB.
35 int GetPhysicalMemoryGB() {
36 const double kOneGB
= 1024 * 1024 * 1024;
37 const int64_t phys_mem
= base::SysInfo::AmountOfPhysicalMemory();
38 return static_cast<int>(std::floor(0.5 + phys_mem
/ kOneGB
));
41 // Produces an extension-like friendly id.
42 std::string
HexStringToID(const std::string
& hexstr
) {
44 for (size_t i
= 0; i
< hexstr
.size(); ++i
) {
46 if (base::HexStringToInt(
47 base::StringPiece(hexstr
.begin() + i
, hexstr
.begin() + i
+ 1),
49 id
.append(1, val
+ 'a');
55 DCHECK(crx_file::id_util::IdIsValid(id
));
62 std::string
BuildProtocolRequest(const std::string
& browser_version
,
63 const std::string
& channel
,
64 const std::string
& lang
,
65 const std::string
& os_long_name
,
66 const std::string
& request_body
,
67 const std::string
& additional_attributes
) {
68 const std::string
prod_id(
69 UpdateQueryParams::GetProdIdString(UpdateQueryParams::CHROME
));
72 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
73 "<request protocol=\"3.0\" ");
75 if (!additional_attributes
.empty())
76 base::StringAppendF(&request
, "%s ", additional_attributes
.c_str());
78 // Chrome version and platform information.
81 "version=\"%s-%s\" prodversion=\"%s\" "
82 "requestid=\"{%s}\" lang=\"%s\" updaterchannel=\"%s\" prodchannel=\"%s\" "
83 "os=\"%s\" arch=\"%s\" nacl_arch=\"%s\"",
85 browser_version
.c_str(), // "version"
86 browser_version
.c_str(), // "prodversion"
87 base::GenerateGUID().c_str(), // "requestid"
88 lang
.c_str(), // "lang",
89 channel
.c_str(), // "updaterchannel"
90 channel
.c_str(), // "prodchannel"
91 UpdateQueryParams::GetOS(), // "os"
92 UpdateQueryParams::GetArch(), // "arch"
93 UpdateQueryParams::GetNaclArch()); // "nacl_arch"
95 const bool is_wow64(base::win::OSInfo::GetInstance()->wow64_status() ==
96 base::win::OSInfo::WOW64_ENABLED
);
98 base::StringAppendF(&request
, " wow64=\"1\"");
100 base::StringAppendF(&request
, ">");
102 // HW platform information.
103 base::StringAppendF(&request
,
104 "<hw physmemory=\"%d\"/>",
105 GetPhysicalMemoryGB()); // "physmem" in GB.
107 // OS version and platform information.
110 "<os platform=\"%s\" version=\"%s\" arch=\"%s\"/>",
111 os_long_name
.c_str(), // "platform"
112 base::SysInfo().OperatingSystemVersion().c_str(), // "version"
113 base::SysInfo().OperatingSystemArchitecture().c_str()); // "arch"
115 // The actual payload of the request.
116 base::StringAppendF(&request
, "%s</request>", request_body
.c_str());
121 net::URLFetcher
* SendProtocolRequest(
123 const std::string
& protocol_request
,
124 net::URLFetcherDelegate
* url_fetcher_delegate
,
125 net::URLRequestContextGetter
* url_request_context_getter
) {
126 net::URLFetcher
* url_fetcher(net::URLFetcher::Create(
127 0, url
, net::URLFetcher::POST
, url_fetcher_delegate
));
129 url_fetcher
->SetUploadData("application/xml", protocol_request
);
130 url_fetcher
->SetRequestContext(url_request_context_getter
);
131 url_fetcher
->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES
|
132 net::LOAD_DO_NOT_SAVE_COOKIES
|
133 net::LOAD_DISABLE_CACHE
);
134 url_fetcher
->SetAutomaticallyRetryOn5xx(false);
135 url_fetcher
->Start();
140 bool FetchSuccess(const net::URLFetcher
& fetcher
) {
141 return GetFetchError(fetcher
) == 0;
144 int GetFetchError(const net::URLFetcher
& fetcher
) {
145 const net::URLRequestStatus::Status
status(fetcher
.GetStatus().status());
147 case net::URLRequestStatus::IO_PENDING
:
148 case net::URLRequestStatus::CANCELED
:
149 // Network status is a small positive number.
152 case net::URLRequestStatus::SUCCESS
: {
153 // Response codes are positive numbers, greater than 100.
154 const int response_code(fetcher
.GetResponseCode());
155 if (response_code
== 200)
158 return response_code
? response_code
: -1;
161 case net::URLRequestStatus::FAILED
: {
162 // Network errors are small negative numbers.
163 const int error
= fetcher
.GetStatus().error();
164 return error
? error
: -1;
172 bool HasDiffUpdate(const CrxUpdateItem
* update_item
) {
173 return !update_item
->crx_diffurls
.empty();
176 bool IsHttpServerError(int status_code
) {
177 return 500 <= status_code
&& status_code
< 600;
180 bool DeleteFileAndEmptyParentDirectory(const base::FilePath
& filepath
) {
181 if (!base::DeleteFile(filepath
, false))
184 const base::FilePath
dirname(filepath
.DirName());
185 if (!base::IsDirectoryEmpty(dirname
))
188 return base::DeleteFile(dirname
, false);
191 std::string
GetCrxComponentID(const CrxComponent
& component
) {
192 const size_t kCrxIdSize
= 16;
193 CHECK_GE(component
.pk_hash
.size(), kCrxIdSize
);
194 return HexStringToID(base::StringToLowerASCII(
195 base::HexEncode(&component
.pk_hash
[0], kCrxIdSize
)));
198 } // namespace component_updater