1 // Copyright 2015 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 "ios/chrome/browser/net/ios_chrome_network_delegate.h"
9 #include "base/base_paths.h"
10 #include "base/debug/alias.h"
11 #include "base/debug/dump_without_crashing.h"
12 #include "base/debug/stack_trace.h"
13 #include "base/logging.h"
14 #include "base/metrics/histogram.h"
15 #include "base/metrics/sparse_histogram.h"
16 #include "base/metrics/user_metrics.h"
17 #include "base/path_service.h"
18 #include "base/prefs/pref_member.h"
19 #include "base/prefs/pref_service.h"
20 #include "base/profiler/scoped_tracker.h"
21 #include "components/domain_reliability/monitor.h"
22 #include "ios/chrome/browser/pref_names.h"
23 #include "ios/web/public/web_thread.h"
24 #include "net/base/load_flags.h"
25 #include "net/base/net_errors.h"
26 #include "net/cookies/cookie_options.h"
27 #include "net/http/http_status_code.h"
28 #include "net/log/net_log.h"
29 #include "net/url_request/url_request.h"
31 #if defined(ENABLE_CONFIGURATION_POLICY)
32 #include "components/policy/core/browser/url_blacklist_manager.h"
37 const char kDNTHeader
[] = "DNT";
39 void ReportInvalidReferrerSendOnUI() {
41 base::UserMetricsAction("Net.URLRequest_StartJob_InvalidReferrer"));
44 void ReportInvalidReferrerSend(const GURL
& target_url
,
45 const GURL
& referrer_url
) {
46 LOG(ERROR
) << "Cancelling request to " << target_url
47 << " with invalid referrer " << referrer_url
;
48 // Record information to help debug http://crbug.com/422871
49 if (!target_url
.SchemeIsHTTPOrHTTPS())
51 web::WebThread::PostTask(web::WebThread::UI
, FROM_HERE
,
52 base::Bind(&ReportInvalidReferrerSendOnUI
));
53 base::debug::DumpWithoutCrashing();
57 // Record network errors that HTTP requests complete with, including OK and
59 void RecordNetworkErrorHistograms(const net::URLRequest
* request
) {
60 if (request
->url().SchemeIs("http")) {
61 UMA_HISTOGRAM_SPARSE_SLOWLY("Net.HttpRequestCompletionErrorCodes",
62 std::abs(request
->status().error()));
64 if (request
->load_flags() & net::LOAD_MAIN_FRAME
) {
65 UMA_HISTOGRAM_SPARSE_SLOWLY(
66 "Net.HttpRequestCompletionErrorCodes.MainFrame",
67 std::abs(request
->status().error()));
74 IOSChromeNetworkDelegate::IOSChromeNetworkDelegate()
75 : enable_do_not_track_(nullptr),
76 #if defined(ENABLE_CONFIGURATION_POLICY)
77 url_blacklist_manager_(nullptr),
79 domain_reliability_monitor_(nullptr) {
82 IOSChromeNetworkDelegate::~IOSChromeNetworkDelegate() {}
85 void IOSChromeNetworkDelegate::InitializePrefsOnUIThread(
86 BooleanPrefMember
* enable_do_not_track
,
87 PrefService
* pref_service
) {
88 DCHECK_CURRENTLY_ON_WEB_THREAD(web::WebThread::UI
);
89 if (enable_do_not_track
) {
90 enable_do_not_track
->Init(ios::prefs::kEnableDoNotTrack
, pref_service
);
91 enable_do_not_track
->MoveToThread(
92 web::WebThread::GetTaskRunnerForThread(web::WebThread::IO
));
96 int IOSChromeNetworkDelegate::OnBeforeURLRequest(
97 net::URLRequest
* request
,
98 const net::CompletionCallback
& callback
,
100 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
101 tracked_objects::ScopedTracker
tracking_profile1(
102 FROM_HERE_WITH_EXPLICIT_FUNCTION(
103 "456327 URLRequest::IOSChromeNetworkDelegate::OnBeforeURLRequest"));
105 #if defined(ENABLE_CONFIGURATION_POLICY)
106 int error
= net::ERR_BLOCKED_BY_ADMINISTRATOR
;
107 // iOS cannot check the resource type, block everything.
108 // See http://crbug.com/338283 and http://crbug.com/489704
109 if (url_blacklist_manager_
&&
110 url_blacklist_manager_
->ShouldBlockRequestForFrame(request
->url(),
112 // URL access blocked by policy.
113 request
->net_log().AddEvent(
114 net::NetLog::TYPE_CHROME_POLICY_ABORTED_REQUEST
,
115 net::NetLog::StringCallback("url",
116 &request
->url().possibly_invalid_spec()));
121 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
122 tracked_objects::ScopedTracker
tracking_profile2(
123 FROM_HERE_WITH_EXPLICIT_FUNCTION(
124 "456327 URLRequest::IOSChromeNetworkDelegate::OnBeforeURLRequest 2"));
126 if (enable_do_not_track_
&& enable_do_not_track_
->GetValue())
127 request
->SetExtraRequestHeaderByName(kDNTHeader
, "1", true /* override */);
129 // TODO(mmenke): Remove ScopedTracker below once crbug.com/456327 is fixed.
130 tracked_objects::ScopedTracker
tracking_profile4(
131 FROM_HERE_WITH_EXPLICIT_FUNCTION(
132 "456327 URLRequest::IOSChromeNetworkDelegate::OnBeforeURLRequest 4"));
137 void IOSChromeNetworkDelegate::OnBeforeRedirect(net::URLRequest
* request
,
138 const GURL
& new_location
) {
139 if (domain_reliability_monitor_
)
140 domain_reliability_monitor_
->OnBeforeRedirect(request
);
143 void IOSChromeNetworkDelegate::OnCompleted(net::URLRequest
* request
,
145 RecordNetworkErrorHistograms(request
);
146 if (domain_reliability_monitor_
)
147 domain_reliability_monitor_
->OnCompleted(request
, started
);
150 net::NetworkDelegate::AuthRequiredResponse
151 IOSChromeNetworkDelegate::OnAuthRequired(
152 net::URLRequest
* request
,
153 const net::AuthChallengeInfo
& auth_info
,
154 const AuthCallback
& callback
,
155 net::AuthCredentials
* credentials
) {
156 return net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION
;
159 bool IOSChromeNetworkDelegate::OnCanGetCookies(
160 const net::URLRequest
& request
,
161 const net::CookieList
& cookie_list
) {
162 // Null during tests, or when we're running in the system context.
163 if (!cookie_settings_
)
166 return cookie_settings_
->IsReadingCookieAllowed(
167 request
.url(), request
.first_party_for_cookies());
170 bool IOSChromeNetworkDelegate::OnCanSetCookie(const net::URLRequest
& request
,
171 const std::string
& cookie_line
,
172 net::CookieOptions
* options
) {
173 // Null during tests, or when we're running in the system context.
174 if (!cookie_settings_
)
177 return cookie_settings_
->IsSettingCookieAllowed(
178 request
.url(), request
.first_party_for_cookies());
181 bool IOSChromeNetworkDelegate::OnCanAccessFile(
182 const net::URLRequest
& request
,
183 const base::FilePath
& path
) const {
187 bool IOSChromeNetworkDelegate::OnCanEnablePrivacyMode(
189 const GURL
& first_party_for_cookies
) const {
190 // Null during tests, or when we're running in the system context.
191 if (!cookie_settings_
.get())
194 bool reading_cookie_allowed
=
195 cookie_settings_
->IsReadingCookieAllowed(url
, first_party_for_cookies
);
196 bool setting_cookie_allowed
=
197 cookie_settings_
->IsSettingCookieAllowed(url
, first_party_for_cookies
);
198 bool privacy_mode
= !(reading_cookie_allowed
&& setting_cookie_allowed
);
202 bool IOSChromeNetworkDelegate::
203 OnCancelURLRequestWithPolicyViolatingReferrerHeader(
204 const net::URLRequest
& request
,
205 const GURL
& target_url
,
206 const GURL
& referrer_url
) const {
207 ReportInvalidReferrerSend(target_url
, referrer_url
);