1 // Copyright 2014 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/supervised_user/supervised_user_resource_throttle.h"
8 #include "base/metrics/histogram.h"
9 #include "chrome/browser/supervised_user/supervised_user_interstitial.h"
10 #include "chrome/browser/supervised_user/supervised_user_navigation_observer.h"
11 #include "chrome/browser/supervised_user/supervised_user_url_filter.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/resource_controller.h"
14 #include "content/public/browser/resource_request_info.h"
15 #include "net/url_request/redirect_info.h"
16 #include "net/url_request/url_request.h"
17 #include "ui/base/page_transition_types.h"
19 using content::BrowserThread
;
23 // These values corresponds to SupervisedUserSafetyFilterResult in
24 // tools/metrics/histograms/histograms.xml. If you change anything here, make
25 // sure to also update histograms.xml accordingly.
27 FILTERING_BEHAVIOR_ALLOW
= 1,
28 FILTERING_BEHAVIOR_ALLOW_UNCERTAIN
,
29 FILTERING_BEHAVIOR_BLOCK_BLACKLIST
,
30 FILTERING_BEHAVIOR_BLOCK_SAFESITES
,
31 FILTERING_BEHAVIOR_MAX
= FILTERING_BEHAVIOR_BLOCK_SAFESITES
33 const int kHistogramFilteringBehaviorSpacing
= 100;
34 const int kHistogramPageTransitionMaxKnownValue
=
35 static_cast<int>(ui::PAGE_TRANSITION_KEYWORD_GENERATED
);
36 const int kHistogramPageTransitionFallbackValue
=
37 kHistogramFilteringBehaviorSpacing
- 1;
38 const int kHistogramMax
= 500;
40 static_assert(kHistogramPageTransitionMaxKnownValue
<
41 kHistogramPageTransitionFallbackValue
,
42 "HistogramPageTransition MaxKnownValue must be < FallbackValue");
43 static_assert(FILTERING_BEHAVIOR_MAX
* kHistogramFilteringBehaviorSpacing
+
44 kHistogramPageTransitionFallbackValue
< kHistogramMax
,
45 "Invalid HistogramMax value");
47 int GetHistogramValueForFilteringBehavior(
48 SupervisedUserURLFilter::FilteringBehavior behavior
,
49 SupervisedUserURLFilter::FilteringBehaviorReason reason
,
52 case SupervisedUserURLFilter::ALLOW
:
53 case SupervisedUserURLFilter::WARN
:
54 return uncertain
? FILTERING_BEHAVIOR_ALLOW_UNCERTAIN
55 : FILTERING_BEHAVIOR_ALLOW
;
56 case SupervisedUserURLFilter::BLOCK
:
57 if (reason
== SupervisedUserURLFilter::BLACKLIST
)
58 return FILTERING_BEHAVIOR_BLOCK_BLACKLIST
;
59 else if (reason
== SupervisedUserURLFilter::ASYNC_CHECKER
)
60 return FILTERING_BEHAVIOR_BLOCK_SAFESITES
;
68 int GetHistogramValueForTransitionType(ui::PageTransition transition_type
) {
70 static_cast<int>(ui::PageTransitionStripQualifier(transition_type
));
71 if (0 <= value
&& value
<= kHistogramPageTransitionMaxKnownValue
)
74 return kHistogramPageTransitionFallbackValue
;
77 void RecordFilterResultEvent(
78 SupervisedUserURLFilter::FilteringBehavior behavior
,
79 SupervisedUserURLFilter::FilteringBehaviorReason reason
,
81 ui::PageTransition transition_type
) {
82 DCHECK(reason
== SupervisedUserURLFilter::ASYNC_CHECKER
||
83 reason
== SupervisedUserURLFilter::BLACKLIST
);
85 GetHistogramValueForFilteringBehavior(behavior
, reason
, uncertain
) *
86 kHistogramFilteringBehaviorSpacing
+
87 GetHistogramValueForTransitionType(transition_type
);
88 DCHECK_LT(value
, kHistogramMax
);
89 UMA_HISTOGRAM_ENUMERATION("ManagedUsers.SafetyFilter",
90 value
, kHistogramMax
);
95 SupervisedUserResourceThrottle::SupervisedUserResourceThrottle(
96 const net::URLRequest
* request
,
98 const SupervisedUserURLFilter
* url_filter
)
100 is_main_frame_(is_main_frame
),
101 url_filter_(url_filter
),
103 behavior_(SupervisedUserURLFilter::HISTOGRAM_BOUNDING_VALUE
),
104 weak_ptr_factory_(this) {}
106 SupervisedUserResourceThrottle::~SupervisedUserResourceThrottle() {}
108 void SupervisedUserResourceThrottle::ShowInterstitialIfNeeded(bool is_redirect
,
111 // Only treat main frame requests for now (ignoring subresources).
116 DCHECK_EQ(SupervisedUserURLFilter::HISTOGRAM_BOUNDING_VALUE
, behavior_
);
117 bool got_result
= url_filter_
->GetFilteringBehaviorForURLWithAsyncChecks(
119 base::Bind(&SupervisedUserResourceThrottle::OnCheckDone
,
120 weak_ptr_factory_
.GetWeakPtr(), url
));
121 DCHECK_EQ(got_result
,
122 (behavior_
!= SupervisedUserURLFilter::HISTOGRAM_BOUNDING_VALUE
));
123 // If we got a "not blocked" result synchronously, don't defer.
124 *defer
= deferred_
= !got_result
||
125 (behavior_
== SupervisedUserURLFilter::BLOCK
);
127 behavior_
= SupervisedUserURLFilter::HISTOGRAM_BOUNDING_VALUE
;
130 void SupervisedUserResourceThrottle::ShowInterstitial(
132 SupervisedUserURLFilter::FilteringBehaviorReason reason
) {
133 const content::ResourceRequestInfo
* info
=
134 content::ResourceRequestInfo::ForRequest(request_
);
135 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
136 base::Bind(&SupervisedUserNavigationObserver::OnRequestBlocked
,
137 info
->GetChildID(), info
->GetRouteID(), url
, reason
,
139 &SupervisedUserResourceThrottle::OnInterstitialResult
,
140 weak_ptr_factory_
.GetWeakPtr())));
143 void SupervisedUserResourceThrottle::WillStartRequest(bool* defer
) {
144 ShowInterstitialIfNeeded(false, request_
->url(), defer
);
147 void SupervisedUserResourceThrottle::WillRedirectRequest(
148 const net::RedirectInfo
& redirect_info
,
150 ShowInterstitialIfNeeded(true, redirect_info
.new_url
, defer
);
153 const char* SupervisedUserResourceThrottle::GetNameForLogging() const {
154 return "SupervisedUserResourceThrottle";
157 void SupervisedUserResourceThrottle::OnCheckDone(
159 SupervisedUserURLFilter::FilteringBehavior behavior
,
160 SupervisedUserURLFilter::FilteringBehaviorReason reason
,
162 DCHECK_EQ(SupervisedUserURLFilter::HISTOGRAM_BOUNDING_VALUE
, behavior_
);
163 // If we got a result synchronously, pass it back to ShowInterstitialIfNeeded.
165 behavior_
= behavior
;
167 // If both the static blacklist and SafeSites are enabled, record UMA events.
168 if (url_filter_
->HasBlacklist() && url_filter_
->HasAsyncURLChecker() &&
169 (reason
== SupervisedUserURLFilter::ASYNC_CHECKER
||
170 reason
== SupervisedUserURLFilter::BLACKLIST
)) {
171 const content::ResourceRequestInfo
* info
=
172 content::ResourceRequestInfo::ForRequest(request_
);
173 RecordFilterResultEvent(behavior
, reason
, uncertain
,
174 info
->GetPageTransition());
177 if (behavior
== SupervisedUserURLFilter::BLOCK
)
178 ShowInterstitial(url
, reason
);
180 controller()->Resume();
183 void SupervisedUserResourceThrottle::OnInterstitialResult(
184 bool continue_request
) {
185 if (continue_request
)
186 controller()->Resume();
188 controller()->Cancel();