1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/winhttp/network_fetcher.h"
#include <windows.h>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include "base/containers/flat_map.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/scoped_refptr.h"
#include "base/sequence_checker.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/updater/net/network.h"
#include "chrome/updater/policy/service.h"
#include "chrome/updater/updater_scope.h"
#include "chrome/updater/util/util.h"
#include "chrome/updater/util/win_util.h"
#include "chrome/updater/win/scoped_handle.h"
#include "chrome/updater/win/scoped_impersonation.h"
#include "chrome/updater/win/user_info.h"
#include "components/update_client/network.h"
#include "components/winhttp/proxy_configuration.h"
#include "components/winhttp/scoped_hinternet.h"
#include "url/gurl.h"
namespace updater {
namespace {
// Factory method for the proxy configuration strategy.
scoped_refptr<winhttp::ProxyConfiguration> GetProxyConfiguration(
std::optional<PolicyServiceProxyConfiguration>
policy_service_proxy_configuration) {
if (policy_service_proxy_configuration) {
VLOG(1) << "Using cloud policy configuration for proxy.";
return base::MakeRefCounted<winhttp::ProxyConfiguration>(winhttp::ProxyInfo{
policy_service_proxy_configuration->proxy_auto_detect,
base::SysUTF8ToWide(
policy_service_proxy_configuration->proxy_pac_url.value_or("")),
base::SysUTF8ToWide(
policy_service_proxy_configuration->proxy_url.value_or("")),
L""});
}
VLOG(1) << "Using the system configuration for proxy.";
return base::MakeRefCounted<winhttp::AutoProxyConfiguration>();
}
class NetworkFetcher : public update_client::NetworkFetcher {
public:
using ResponseStartedCallback =
update_client::NetworkFetcher::ResponseStartedCallback;
using ProgressCallback = update_client::NetworkFetcher::ProgressCallback;
using PostRequestCompleteCallback =
update_client::NetworkFetcher::PostRequestCompleteCallback;
using DownloadToFileCompleteCallback =
update_client::NetworkFetcher::DownloadToFileCompleteCallback;
NetworkFetcher(scoped_refptr<winhttp::SharedHInternet> session_handle,
scoped_refptr<winhttp::ProxyConfiguration> proxy_config);
~NetworkFetcher() override;
NetworkFetcher(const NetworkFetcher&) = delete;
NetworkFetcher& operator=(const NetworkFetcher&) = delete;
// NetworkFetcher overrides.
void PostRequest(
const GURL& url,
const std::string& post_data,
const std::string& content_type,
const base::flat_map<std::string, std::string>& post_additional_headers,
ResponseStartedCallback response_started_callback,
ProgressCallback progress_callback,
PostRequestCompleteCallback post_request_complete_callback) override;
base::OnceClosure DownloadToFile(
const GURL& url,
const base::FilePath& file_path,
ResponseStartedCallback response_started_callback,
ProgressCallback progress_callback,
DownloadToFileCompleteCallback download_to_file_complete_callback)
override;
private:
SEQUENCE_CHECKER(sequence_checker_);
void PostRequestComplete(int response_code);
void DownloadToFileComplete(int response_code);
scoped_refptr<winhttp::NetworkFetcher> winhttp_network_fetcher_;
DownloadToFileCompleteCallback download_to_file_complete_callback_;
PostRequestCompleteCallback post_request_complete_callback_;
};
NetworkFetcher::NetworkFetcher(
scoped_refptr<winhttp::SharedHInternet> session_handle,
scoped_refptr<winhttp::ProxyConfiguration> proxy_config)
: winhttp_network_fetcher_(
base::MakeRefCounted<winhttp::NetworkFetcher>(session_handle,
proxy_config)) {}
NetworkFetcher::~NetworkFetcher() {
winhttp_network_fetcher_->Close();
}
void NetworkFetcher::PostRequest(
const GURL& url,
const std::string& post_data,
const std::string& content_type,
const base::flat_map<std::string, std::string>& post_additional_headers,
ResponseStartedCallback response_started_callback,
ProgressCallback progress_callback,
PostRequestCompleteCallback post_request_complete_callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
VLOG(2) << __func__;
post_request_complete_callback_ = std::move(post_request_complete_callback);
winhttp_network_fetcher_->PostRequest(
url, post_data, content_type, post_additional_headers,
std::move(response_started_callback), std::move(progress_callback),
base::BindOnce(&NetworkFetcher::PostRequestComplete,
base::Unretained(this)));
}
base::OnceClosure NetworkFetcher::DownloadToFile(
const GURL& url,
const base::FilePath& file_path,
ResponseStartedCallback response_started_callback,
ProgressCallback progress_callback,
DownloadToFileCompleteCallback download_to_file_complete_callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
VLOG(2) << __func__;
download_to_file_complete_callback_ =
std::move(download_to_file_complete_callback);
return winhttp_network_fetcher_->DownloadToFile(
url, file_path, std::move(response_started_callback),
std::move(progress_callback),
base::BindOnce(&NetworkFetcher::DownloadToFileComplete,
base::Unretained(this)));
}
void NetworkFetcher::PostRequestComplete(int response_code) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
VLOG(2) << __func__ << ": response code=" << response_code;
// Attempt to get some response headers. Not all headers may be present so
// this is best effort only.
std::wstring x_cup_server_proof;
std::wstring etag;
std::wstring cookie;
int x_retry_after_sec = -1;
winhttp_network_fetcher_->QueryHeaderString(
base::SysUTF8ToWide(
update_client::NetworkFetcher::kHeaderXCupServerProof),
&x_cup_server_proof);
winhttp_network_fetcher_->QueryHeaderString(
base::SysUTF8ToWide(update_client::NetworkFetcher::kHeaderEtag), &etag);
winhttp_network_fetcher_->QueryHeaderString(
base::SysUTF8ToWide(update_client::NetworkFetcher::kHeaderCookie),
&cookie);
winhttp_network_fetcher_->QueryHeaderInt(
base::SysUTF8ToWide(update_client::NetworkFetcher::kHeaderXRetryAfter),
&x_retry_after_sec);
std::move(post_request_complete_callback_)
.Run(winhttp_network_fetcher_->GetResponseBody(),
winhttp_network_fetcher_->GetNetError(), base::SysWideToUTF8(etag),
base::SysWideToUTF8(x_cup_server_proof), base::SysWideToUTF8(cookie),
x_retry_after_sec);
}
void NetworkFetcher::DownloadToFileComplete(int /*response_code*/) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
VLOG(2) << __func__;
std::move(download_to_file_complete_callback_)
.Run(winhttp_network_fetcher_->GetNetError(),
winhttp_network_fetcher_->GetContentSize());
}
} // namespace
class NetworkFetcherFactory::Impl {
public:
explicit Impl(std::optional<PolicyServiceProxyConfiguration>
policy_service_proxy_configuration)
: proxy_configuration_(
GetProxyConfiguration(policy_service_proxy_configuration)) {
ScopedImpersonation impersonate;
if (IsSystemInstall()) {
HResultOr<ScopedKernelHANDLE> token = GetLoggedOnUserToken();
VLOG_IF(2, !token.has_value())
<< __func__ << ": GetLoggedOnUserToken failed: " << std::hex
<< token.error();
if (token.has_value()) {
const HRESULT hr = impersonate.Impersonate(token.value().get());
VLOG(2)
<< __func__
<< ": Successfully got logged on user token. Impersonate result: "
<< std::hex << hr;
}
}
session_handle_ = base::MakeRefCounted<winhttp::SharedHInternet>(
winhttp::CreateSessionHandle(base::SysUTF8ToWide(GetUpdaterUserAgent()),
proxy_configuration_->access_type(),
proxy_configuration_->proxy(),
proxy_configuration_->proxy_bypass()));
VLOG_IF(2, !session_handle_) << "Failed to create a winhttp session.";
}
std::unique_ptr<update_client::NetworkFetcher> Create() {
return session_handle_ ? std::make_unique<NetworkFetcher>(
session_handle_, proxy_configuration_)
: nullptr;
}
private:
scoped_refptr<winhttp::ProxyConfiguration> proxy_configuration_;
scoped_refptr<winhttp::SharedHInternet> session_handle_;
};
NetworkFetcherFactory::NetworkFetcherFactory(
std::optional<PolicyServiceProxyConfiguration>
policy_service_proxy_configuration)
: impl_(std::make_unique<Impl>(policy_service_proxy_configuration)) {}
NetworkFetcherFactory::~NetworkFetcherFactory() = default;
std::unique_ptr<update_client::NetworkFetcher> NetworkFetcherFactory::Create()
const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return impl_->Create();
}
} // namespace updater
|