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/component_updater/component_updater_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/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" | |
27 | |
28 using update_client::UpdateQueryParams; | |
29 | |
30 namespace component_updater { | |
31 | |
32 namespace { | |
33 | |
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)); | |
39 } | |
40 | |
41 // Produces an extension-like friendly id. | |
42 std::string HexStringToID(const std::string& hexstr) { | |
43 std::string id; | |
44 for (size_t i = 0; i < hexstr.size(); ++i) { | |
45 int val = 0; | |
46 if (base::HexStringToInt( | |
47 base::StringPiece(hexstr.begin() + i, hexstr.begin() + i + 1), | |
48 &val)) { | |
49 id.append(1, val + 'a'); | |
50 } else { | |
51 id.append(1, 'a'); | |
52 } | |
53 } | |
54 | |
55 DCHECK(crx_file::id_util::IdIsValid(id)); | |
56 | |
57 return id; | |
58 } | |
59 | |
60 } // namespace | |
61 | |
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)); | |
70 | |
71 std::string request( | |
72 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" | |
73 "<request protocol=\"3.0\" "); | |
74 | |
75 if (!additional_attributes.empty()) | |
76 base::StringAppendF(&request, "%s ", additional_attributes.c_str()); | |
77 | |
78 // Chrome version and platform information. | |
79 base::StringAppendF( | |
80 &request, | |
81 "version=\"%s-%s\" prodversion=\"%s\" " | |
82 "requestid=\"{%s}\" lang=\"%s\" updaterchannel=\"%s\" prodchannel=\"%s\" " | |
83 "os=\"%s\" arch=\"%s\" nacl_arch=\"%s\"", | |
84 prod_id.c_str(), | |
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" | |
94 #if defined(OS_WIN) | |
95 const bool is_wow64(base::win::OSInfo::GetInstance()->wow64_status() == | |
96 base::win::OSInfo::WOW64_ENABLED); | |
97 if (is_wow64) | |
98 base::StringAppendF(&request, " wow64=\"1\""); | |
99 #endif | |
100 base::StringAppendF(&request, ">"); | |
101 | |
102 // HW platform information. | |
103 base::StringAppendF(&request, | |
104 "<hw physmemory=\"%d\"/>", | |
105 GetPhysicalMemoryGB()); // "physmem" in GB. | |
106 | |
107 // OS version and platform information. | |
108 base::StringAppendF( | |
109 &request, | |
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" | |
114 | |
115 // The actual payload of the request. | |
116 base::StringAppendF(&request, "%s</request>", request_body.c_str()); | |
117 | |
118 return request; | |
119 } | |
120 | |
121 net::URLFetcher* SendProtocolRequest( | |
122 const GURL& url, | |
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)); | |
128 | |
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(); | |
136 | |
137 return url_fetcher; | |
138 } | |
139 | |
140 bool FetchSuccess(const net::URLFetcher& fetcher) { | |
141 return GetFetchError(fetcher) == 0; | |
142 } | |
143 | |
144 int GetFetchError(const net::URLFetcher& fetcher) { | |
145 const net::URLRequestStatus::Status status(fetcher.GetStatus().status()); | |
146 switch (status) { | |
147 case net::URLRequestStatus::IO_PENDING: | |
148 case net::URLRequestStatus::CANCELED: | |
149 // Network status is a small positive number. | |
150 return status; | |
151 | |
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) | |
156 return 0; | |
157 else | |
158 return response_code ? response_code : -1; | |
159 } | |
160 | |
161 case net::URLRequestStatus::FAILED: { | |
162 // Network errors are small negative numbers. | |
163 const int error = fetcher.GetStatus().error(); | |
164 return error ? error : -1; | |
165 } | |
166 | |
167 default: | |
168 return -1; | |
169 } | |
170 } | |
171 | |
172 bool HasDiffUpdate(const CrxUpdateItem* update_item) { | |
173 return !update_item->crx_diffurls.empty(); | |
174 } | |
175 | |
176 bool IsHttpServerError(int status_code) { | |
177 return 500 <= status_code && status_code < 600; | |
178 } | |
179 | |
180 bool DeleteFileAndEmptyParentDirectory(const base::FilePath& filepath) { | |
181 if (!base::DeleteFile(filepath, false)) | |
182 return false; | |
183 | |
184 const base::FilePath dirname(filepath.DirName()); | |
185 if (!base::IsDirectoryEmpty(dirname)) | |
186 return true; | |
187 | |
188 return base::DeleteFile(dirname, false); | |
189 } | |
190 | |
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))); | |
196 } | |
197 | |
198 } // namespace component_updater | |
OLD | NEW |