1 // Copyright 2020 The Chromium Authors
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 "net/proxy_resolution/win/windows_system_proxy_resolution_request.h"
6
7 #include <utility>
8
9 #include "net/base/net_errors.h"
10 #include "net/proxy_resolution/proxy_info.h"
11 #include "net/proxy_resolution/proxy_list.h"
12 #include "net/proxy_resolution/win/windows_system_proxy_resolution_service.h"
13 #include "net/traffic_annotation/network_traffic_annotation.h"
14
15 namespace net {
16
17 namespace {
18
19 constexpr net::NetworkTrafficAnnotationTag kWindowsResolverTrafficAnnotation =
20 net::DefineNetworkTrafficAnnotation("proxy_config_windows_resolver", R"(
21 semantics {
22 sender: "Proxy Config for Windows System Resolver"
23 description:
24 "Establishing a connection through a proxy server using system proxy "
25 "settings and Windows system proxy resolution code."
26 trigger:
27 "Whenever a network request is made when the system proxy settings "
28 "are used, the Windows system proxy resolver is enabled, and the "
29 "result indicates usage of a proxy server."
30 data:
31 "Proxy configuration."
32 destination: OTHER
33 destination_other:
34 "The proxy server specified in the configuration."
35 }
36 policy {
37 cookies_allowed: NO
38 setting:
39 "User cannot override system proxy settings, but can change them "
40 "through 'Advanced/System/Open proxy settings'."
41 policy_exception_justification:
42 "Using either of 'ProxyMode', 'ProxyServer', or 'ProxyPacUrl' "
43 "policies can set Chrome to use a specific proxy settings and avoid "
44 "system proxy."
45 })");
46
47 } // namespace
48
WindowsSystemProxyResolutionRequest(WindowsSystemProxyResolutionService * service,const GURL & url,const std::string & method,ProxyInfo * results,CompletionOnceCallback user_callback,const NetLogWithSource & net_log,WindowsSystemProxyResolver * windows_system_proxy_resolver)49 WindowsSystemProxyResolutionRequest::WindowsSystemProxyResolutionRequest(
50 WindowsSystemProxyResolutionService* service,
51 const GURL& url,
52 const std::string& method,
53 ProxyInfo* results,
54 CompletionOnceCallback user_callback,
55 const NetLogWithSource& net_log,
56 WindowsSystemProxyResolver* windows_system_proxy_resolver)
57 : service_(service),
58 user_callback_(std::move(user_callback)),
59 results_(results),
60 url_(url),
61 method_(method),
62 net_log_(net_log),
63 creation_time_(base::TimeTicks::Now()) {
64 DCHECK(!user_callback_.is_null());
65 DCHECK(windows_system_proxy_resolver);
66 proxy_resolution_request_ =
67 windows_system_proxy_resolver->GetProxyForUrl(url, this);
68 }
69
~WindowsSystemProxyResolutionRequest()70 WindowsSystemProxyResolutionRequest::~WindowsSystemProxyResolutionRequest() {
71 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
72 if (service_) {
73 service_->RemovePendingRequest(this);
74 net_log_.AddEvent(NetLogEventType::CANCELLED);
75
76 CancelResolveRequest();
77
78 net_log_.EndEvent(NetLogEventType::PROXY_RESOLUTION_SERVICE);
79 }
80 }
81
GetLoadState() const82 LoadState WindowsSystemProxyResolutionRequest::GetLoadState() const {
83 // TODO(https://crbug.com/1032820): Consider adding a LoadState for "We're
84 // waiting on system APIs to do their thing".
85 return LOAD_STATE_RESOLVING_PROXY_FOR_URL;
86 }
87
CancelResolveRequest()88 void WindowsSystemProxyResolutionRequest::CancelResolveRequest() {
89 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
90 proxy_resolution_request_.reset();
91 }
92
ProxyResolutionComplete(const ProxyList & proxy_list,WinHttpStatus winhttp_status,int windows_error)93 void WindowsSystemProxyResolutionRequest::ProxyResolutionComplete(
94 const ProxyList& proxy_list,
95 WinHttpStatus winhttp_status,
96 int windows_error) {
97 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
98 DCHECK(!was_completed());
99 // TODO(https://crbug.com/1032820): Log Windows error |windows_error|.
100
101 proxy_resolution_request_.reset();
102 results_->UseProxyList(proxy_list);
103
104 // Note that DidFinishResolvingProxy might modify |results_|.
105 int net_error = service_->DidFinishResolvingProxy(url_, method_, results_,
106 winhttp_status, net_log_);
107
108 // Make a note in the results which configuration was in use at the
109 // time of the resolve.
110 results_->set_proxy_resolve_start_time(creation_time_);
111 results_->set_proxy_resolve_end_time(base::TimeTicks::Now());
112 results_->set_traffic_annotation(
113 MutableNetworkTrafficAnnotationTag(kWindowsResolverTrafficAnnotation));
114
115 CompletionOnceCallback callback = std::move(user_callback_);
116
117 service_->RemovePendingRequest(this);
118 service_ = nullptr;
119 user_callback_.Reset();
120 std::move(callback).Run(net_error);
121 }
122
123 WindowsSystemProxyResolver::Request*
GetProxyResolutionRequestForTesting()124 WindowsSystemProxyResolutionRequest::GetProxyResolutionRequestForTesting() {
125 return proxy_resolution_request_.get();
126 }
127
128 void WindowsSystemProxyResolutionRequest::
ResetProxyResolutionRequestForTesting()129 ResetProxyResolutionRequestForTesting() {
130 proxy_resolution_request_.reset();
131 }
132
133 } // namespace net
134