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