Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / components / component_updater / component_updater_utils.cc
blob9ef561ebcf8593666ca4a1466eef8b0607df59d6
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"
7 #include <cmath>
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 "components/component_updater/component_updater_configurator.h"
19 #include "components/component_updater/crx_update_item.h"
20 #include "components/crx_file/id_util.h"
21 #include "components/omaha_query_params/omaha_query_params.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"
27 using omaha_query_params::OmahaQueryParams;
29 namespace component_updater {
31 namespace {
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));
40 } // namespace
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));
51 std::string request(
52 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
53 "<request protocol=\"3.0\" ");
55 if (!additional_attributes.empty())
56 base::StringAppendF(&request, "%s ", additional_attributes.c_str());
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, ">");
82 // HW platform information.
83 base::StringAppendF(&request,
84 "<hw physmemory=\"%d\"/>",
85 GetPhysicalMemoryGB()); // "physmem" in GB.
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"
95 // The actual payload of the request.
96 base::StringAppendF(&request, "%s</request>", request_body.c_str());
98 return request;
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));
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();
117 return url_fetcher;
120 bool FetchSuccess(const net::URLFetcher& fetcher) {
121 return GetFetchError(fetcher) == 0;
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;
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;
141 case net::URLRequestStatus::FAILED: {
142 // Network errors are small negative numbers.
143 const int error = fetcher.GetStatus().error();
144 return error ? error : -1;
147 default:
148 return -1;
152 bool HasDiffUpdate(const CrxUpdateItem* update_item) {
153 return !update_item->crx_diffurls.empty();
156 bool IsHttpServerError(int status_code) {
157 return 500 <= status_code && status_code < 600;
160 bool DeleteFileAndEmptyParentDirectory(const base::FilePath& filepath) {
161 if (!base::DeleteFile(filepath, false))
162 return false;
164 const base::FilePath dirname(filepath.DirName());
165 if (!base::IsDirectoryEmpty(dirname))
166 return true;
168 return base::DeleteFile(dirname, false);
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');
185 DCHECK(crx_file::id_util::IdIsValid(id));
187 return id;
190 std::string GetCrxComponentID(const CrxComponent& component) {
191 return HexStringToID(base::StringToLowerASCII(
192 base::HexEncode(&component.pk_hash[0], component.pk_hash.size() / 2)));
195 } // namespace component_updater