Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / component_updater / component_updater_utils.cc
blobd6cb1e0ee220d1d83f1babc51870441a63ef340e
1 // Copyright 2013 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 "chrome/browser/component_updater/component_updater_utils.h"
7 #include "base/file_util.h"
8 #include "base/files/file_path.h"
9 #include "base/guid.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_piece.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/sys_info.h"
15 #include "base/win/windows_version.h"
16 #include "chrome/browser/component_updater/component_updater_service.h"
17 #include "chrome/browser/component_updater/crx_update_item.h"
18 #include "chrome/browser/omaha_query_params/omaha_query_params.h"
19 #include "chrome/common/chrome_version_info.h"
20 #include "extensions/common/extension.h"
21 #include "net/base/load_flags.h"
22 #include "net/url_request/url_fetcher.h"
23 #include "net/url_request/url_request_context_getter.h"
24 #include "net/url_request/url_request_status.h"
26 namespace component_updater {
28 std::string BuildProtocolRequest(const std::string& request_body,
29 const std::string& additional_attributes) {
30 const std::string prod_id(chrome::OmahaQueryParams::GetProdIdString(
31 chrome::OmahaQueryParams::CHROME));
32 const chrome::VersionInfo chrome_version_info;
33 const std::string chrome_version(chrome_version_info.Version());
35 std::string request(
36 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
37 "<request protocol=\"3.0\" ");
39 if (!additional_attributes.empty())
40 base::StringAppendF(&request, "%s ", additional_attributes.c_str());
42 // Chrome version and platform information.
43 base::StringAppendF(
44 &request,
45 "version=\"%s-%s\" prodversion=\"%s\" "
46 "requestid=\"{%s}\" lang=\"%s\" updaterchannel=\"%s\" prodchannel=\"%s\" "
47 "os=\"%s\" arch=\"%s\" nacl_arch=\"%s\"",
48 prod_id.c_str(),
49 chrome_version.c_str(), // "version"
50 chrome_version.c_str(), // "prodversion"
51 base::GenerateGUID().c_str(), // "requestid"
52 chrome::OmahaQueryParams::GetLang(), // "lang",
53 chrome::OmahaQueryParams::GetChannelString(), // "updaterchannel"
54 chrome::OmahaQueryParams::GetChannelString(), // "prodchannel"
55 chrome::OmahaQueryParams::GetOS(), // "os"
56 chrome::OmahaQueryParams::GetArch(), // "arch"
57 chrome::OmahaQueryParams::GetNaclArch()); // "nacl_arch"
58 #if defined(OS_WIN)
59 const bool is_wow64(base::win::OSInfo::GetInstance()->wow64_status() ==
60 base::win::OSInfo::WOW64_ENABLED);
61 if (is_wow64)
62 base::StringAppendF(&request, " wow64=\"1\"");
63 #endif
64 base::StringAppendF(&request, ">");
66 // OS version and platform information.
67 base::StringAppendF(
68 &request,
69 "<os platform=\"%s\" version=\"%s\" arch=\"%s\"/>",
70 chrome::VersionInfo().OSType().c_str(), // "platform"
71 base::SysInfo().OperatingSystemVersion().c_str(), // "version"
72 base::SysInfo().OperatingSystemArchitecture().c_str()); // "arch"
74 // The actual payload of the request.
75 base::StringAppendF(&request, "%s</request>", request_body.c_str());
77 return request;
80 net::URLFetcher* SendProtocolRequest(
81 const GURL& url,
82 const std::string& protocol_request,
83 net::URLFetcherDelegate* url_fetcher_delegate,
84 net::URLRequestContextGetter* url_request_context_getter) {
85 net::URLFetcher* url_fetcher(net::URLFetcher::Create(
86 0, url, net::URLFetcher::POST, url_fetcher_delegate));
88 url_fetcher->SetUploadData("application/xml", protocol_request);
89 url_fetcher->SetRequestContext(url_request_context_getter);
90 url_fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
91 net::LOAD_DO_NOT_SAVE_COOKIES |
92 net::LOAD_DISABLE_CACHE);
93 url_fetcher->SetAutomaticallyRetryOn5xx(false);
94 url_fetcher->Start();
96 return url_fetcher;
99 bool FetchSuccess(const net::URLFetcher& fetcher) {
100 return GetFetchError(fetcher) == 0;
103 int GetFetchError(const net::URLFetcher& fetcher) {
104 const net::URLRequestStatus::Status status(fetcher.GetStatus().status());
105 switch (status) {
106 case net::URLRequestStatus::IO_PENDING:
107 case net::URLRequestStatus::CANCELED:
108 // Network status is a small positive number.
109 return status;
111 case net::URLRequestStatus::SUCCESS: {
112 // Response codes are positive numbers, greater than 100.
113 const int response_code(fetcher.GetResponseCode());
114 if (response_code == 200)
115 return 0;
116 else
117 return response_code ? response_code : -1;
120 case net::URLRequestStatus::FAILED: {
121 // Network errors are small negative numbers.
122 const int error = fetcher.GetStatus().error();
123 return error ? error : -1;
126 default:
127 return -1;
131 bool HasDiffUpdate(const CrxUpdateItem* update_item) {
132 return !update_item->crx_diffurls.empty();
135 bool IsHttpServerError(int status_code) {
136 return 500 <= status_code && status_code < 600;
139 bool DeleteFileAndEmptyParentDirectory(const base::FilePath& filepath) {
140 if (!base::DeleteFile(filepath, false))
141 return false;
143 const base::FilePath dirname(filepath.DirName());
144 if (!base::IsDirectoryEmpty(dirname))
145 return true;
147 return base::DeleteFile(dirname, false);
150 // Produces an extension-like friendly id.
151 std::string HexStringToID(const std::string& hexstr) {
152 std::string id;
153 for (size_t i = 0; i < hexstr.size(); ++i) {
154 int val = 0;
155 if (base::HexStringToInt(
156 base::StringPiece(hexstr.begin() + i, hexstr.begin() + i + 1),
157 &val)) {
158 id.append(1, val + 'a');
159 } else {
160 id.append(1, 'a');
163 DCHECK(extensions::Extension::IdIsValid(id));
164 return id;
167 std::string GetCrxComponentID(const CrxComponent& component) {
168 return HexStringToID(StringToLowerASCII(
169 base::HexEncode(&component.pk_hash[0], component.pk_hash.size() / 2)));
172 } // namespace component_updater