1 // Copyright 2012 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/base/network_delegate.h"
6
7 #include <utility>
8
9 #include "base/logging.h"
10 #include "base/ranges/algorithm.h"
11 #include "net/base/load_flags.h"
12 #include "net/base/net_errors.h"
13 #include "net/base/trace_constants.h"
14 #include "net/base/tracing.h"
15 #include "net/cookies/cookie_setting_override.h"
16 #include "net/cookies/cookie_util.h"
17 #include "net/proxy_resolution/proxy_info.h"
18 #include "net/url_request/url_request.h"
19
20 namespace net {
21
~NetworkDelegate()22 NetworkDelegate::~NetworkDelegate() {
23 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
24 }
25
NotifyBeforeURLRequest(URLRequest * request,CompletionOnceCallback callback,GURL * new_url)26 int NetworkDelegate::NotifyBeforeURLRequest(URLRequest* request,
27 CompletionOnceCallback callback,
28 GURL* new_url) {
29 TRACE_EVENT0(NetTracingCategory(), "NetworkDelegate::NotifyBeforeURLRequest");
30 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
31 DCHECK(request);
32 DCHECK(!callback.is_null());
33
34 // ClusterFuzz depends on the following VLOG. See: crbug.com/715656
35 VLOG(1) << "NetworkDelegate::NotifyBeforeURLRequest: " << request->url();
36 return OnBeforeURLRequest(request, std::move(callback), new_url);
37 }
38
NotifyBeforeStartTransaction(URLRequest * request,const HttpRequestHeaders & headers,OnBeforeStartTransactionCallback callback)39 int NetworkDelegate::NotifyBeforeStartTransaction(
40 URLRequest* request,
41 const HttpRequestHeaders& headers,
42 OnBeforeStartTransactionCallback callback) {
43 TRACE_EVENT0(NetTracingCategory(),
44 "NetworkDelegate::NotifyBeforeStartTransation");
45 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
46 DCHECK(!callback.is_null());
47 return OnBeforeStartTransaction(request, headers, std::move(callback));
48 }
49
NotifyHeadersReceived(URLRequest * request,CompletionOnceCallback callback,const HttpResponseHeaders * original_response_headers,scoped_refptr<HttpResponseHeaders> * override_response_headers,const IPEndPoint & endpoint,absl::optional<GURL> * preserve_fragment_on_redirect_url)50 int NetworkDelegate::NotifyHeadersReceived(
51 URLRequest* request,
52 CompletionOnceCallback callback,
53 const HttpResponseHeaders* original_response_headers,
54 scoped_refptr<HttpResponseHeaders>* override_response_headers,
55 const IPEndPoint& endpoint,
56 absl::optional<GURL>* preserve_fragment_on_redirect_url) {
57 TRACE_EVENT0(NetTracingCategory(), "NetworkDelegate::NotifyHeadersReceived");
58 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
59 DCHECK(original_response_headers);
60 DCHECK(!callback.is_null());
61 DCHECK(!preserve_fragment_on_redirect_url->has_value());
62 return OnHeadersReceived(request, std::move(callback),
63 original_response_headers, override_response_headers,
64 endpoint, preserve_fragment_on_redirect_url);
65 }
66
NotifyResponseStarted(URLRequest * request,int net_error)67 void NetworkDelegate::NotifyResponseStarted(URLRequest* request,
68 int net_error) {
69 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
70 DCHECK(request);
71
72 OnResponseStarted(request, net_error);
73 }
74
NotifyBeforeRedirect(URLRequest * request,const GURL & new_location)75 void NetworkDelegate::NotifyBeforeRedirect(URLRequest* request,
76 const GURL& new_location) {
77 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
78 DCHECK(request);
79 OnBeforeRedirect(request, new_location);
80 }
81
NotifyCompleted(URLRequest * request,bool started,int net_error)82 void NetworkDelegate::NotifyCompleted(URLRequest* request,
83 bool started,
84 int net_error) {
85 TRACE_EVENT0(NetTracingCategory(), "NetworkDelegate::NotifyCompleted");
86 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
87 DCHECK(request);
88 OnCompleted(request, started, net_error);
89 }
90
NotifyURLRequestDestroyed(URLRequest * request)91 void NetworkDelegate::NotifyURLRequestDestroyed(URLRequest* request) {
92 TRACE_EVENT0(NetTracingCategory(),
93 "NetworkDelegate::NotifyURLRequestDestroyed");
94 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
95 DCHECK(request);
96 OnURLRequestDestroyed(request);
97 }
98
NotifyPACScriptError(int line_number,const std::u16string & error)99 void NetworkDelegate::NotifyPACScriptError(int line_number,
100 const std::u16string& error) {
101 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
102 OnPACScriptError(line_number, error);
103 }
104
AnnotateAndMoveUserBlockedCookies(const URLRequest & request,const net::FirstPartySetMetadata & first_party_set_metadata,net::CookieAccessResultList & maybe_included_cookies,net::CookieAccessResultList & excluded_cookies)105 bool NetworkDelegate::AnnotateAndMoveUserBlockedCookies(
106 const URLRequest& request,
107 const net::FirstPartySetMetadata& first_party_set_metadata,
108 net::CookieAccessResultList& maybe_included_cookies,
109 net::CookieAccessResultList& excluded_cookies) {
110 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
111 bool allowed = OnAnnotateAndMoveUserBlockedCookies(
112 request, first_party_set_metadata, maybe_included_cookies,
113 excluded_cookies);
114 cookie_util::DCheckIncludedAndExcludedCookieLists(maybe_included_cookies,
115 excluded_cookies);
116 return allowed;
117 }
118
CanSetCookie(const URLRequest & request,const CanonicalCookie & cookie,CookieOptions * options)119 bool NetworkDelegate::CanSetCookie(const URLRequest& request,
120 const CanonicalCookie& cookie,
121 CookieOptions* options) {
122 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
123 DCHECK(!(request.load_flags() & LOAD_DO_NOT_SAVE_COOKIES));
124 return OnCanSetCookie(request, cookie, options);
125 }
126
ForcePrivacyMode(const URLRequest & request) const127 NetworkDelegate::PrivacySetting NetworkDelegate::ForcePrivacyMode(
128 const URLRequest& request) const {
129 TRACE_EVENT0(NetTracingCategory(), "NetworkDelegate::ForcePrivacyMode");
130 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
131 return OnForcePrivacyMode(request);
132 }
133
CancelURLRequestWithPolicyViolatingReferrerHeader(const URLRequest & request,const GURL & target_url,const GURL & referrer_url) const134 bool NetworkDelegate::CancelURLRequestWithPolicyViolatingReferrerHeader(
135 const URLRequest& request,
136 const GURL& target_url,
137 const GURL& referrer_url) const {
138 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
139 return OnCancelURLRequestWithPolicyViolatingReferrerHeader(
140 request, target_url, referrer_url);
141 }
142
CanQueueReportingReport(const url::Origin & origin) const143 bool NetworkDelegate::CanQueueReportingReport(const url::Origin& origin) const {
144 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
145 return OnCanQueueReportingReport(origin);
146 }
147
CanSendReportingReports(std::set<url::Origin> origins,base::OnceCallback<void (std::set<url::Origin>)> result_callback) const148 void NetworkDelegate::CanSendReportingReports(
149 std::set<url::Origin> origins,
150 base::OnceCallback<void(std::set<url::Origin>)> result_callback) const {
151 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
152 OnCanSendReportingReports(std::move(origins), std::move(result_callback));
153 }
154
CanSetReportingClient(const url::Origin & origin,const GURL & endpoint) const155 bool NetworkDelegate::CanSetReportingClient(const url::Origin& origin,
156 const GURL& endpoint) const {
157 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
158 return OnCanSetReportingClient(origin, endpoint);
159 }
160
CanUseReportingClient(const url::Origin & origin,const GURL & endpoint) const161 bool NetworkDelegate::CanUseReportingClient(const url::Origin& origin,
162 const GURL& endpoint) const {
163 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
164 return OnCanUseReportingClient(origin, endpoint);
165 }
166
167 absl::optional<FirstPartySetsCacheFilter::MatchInfo>
GetFirstPartySetsCacheFilterMatchInfoMaybeAsync(const SchemefulSite & request_site,base::OnceCallback<void (FirstPartySetsCacheFilter::MatchInfo)> callback) const168 NetworkDelegate::GetFirstPartySetsCacheFilterMatchInfoMaybeAsync(
169 const SchemefulSite& request_site,
170 base::OnceCallback<void(FirstPartySetsCacheFilter::MatchInfo)> callback)
171 const {
172 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
173 return OnGetFirstPartySetsCacheFilterMatchInfoMaybeAsync(request_site,
174 std::move(callback));
175 }
176
177 // static
ExcludeAllCookies(net::CookieInclusionStatus::ExclusionReason reason,net::CookieAccessResultList & maybe_included_cookies,net::CookieAccessResultList & excluded_cookies)178 void NetworkDelegate::ExcludeAllCookies(
179 net::CookieInclusionStatus::ExclusionReason reason,
180 net::CookieAccessResultList& maybe_included_cookies,
181 net::CookieAccessResultList& excluded_cookies) {
182 excluded_cookies.insert(
183 excluded_cookies.end(),
184 std::make_move_iterator(maybe_included_cookies.begin()),
185 std::make_move_iterator(maybe_included_cookies.end()));
186 maybe_included_cookies.clear();
187 // Add the ExclusionReason for all cookies.
188 for (net::CookieWithAccessResult& cookie : excluded_cookies) {
189 cookie.access_result.status.AddExclusionReason(reason);
190 }
191 }
192
193 // static
MoveExcludedCookies(net::CookieAccessResultList & maybe_included_cookies,net::CookieAccessResultList & excluded_cookies)194 void NetworkDelegate::MoveExcludedCookies(
195 net::CookieAccessResultList& maybe_included_cookies,
196 net::CookieAccessResultList& excluded_cookies) {
197 const auto to_be_moved = base::ranges::stable_partition(
198 maybe_included_cookies, [](const CookieWithAccessResult& cookie) {
199 return cookie.access_result.status.IsInclude();
200 });
201 excluded_cookies.insert(
202 excluded_cookies.end(), std::make_move_iterator(to_be_moved),
203 std::make_move_iterator(maybe_included_cookies.end()));
204 maybe_included_cookies.erase(to_be_moved, maybe_included_cookies.end());
205 }
206 } // namespace net
207