Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / supervised_user / supervised_user_resource_throttle.cc
blob1224a78b644cf8095bf52e83267a9de0b5ab6e3e
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"
7 #include "base/bind.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;
21 namespace {
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.
26 enum {
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,
50 bool uncertain) {
51 switch (behavior) {
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;
61 // Fall through.
62 default:
63 NOTREACHED();
65 return 0;
68 int GetHistogramValueForTransitionType(ui::PageTransition transition_type) {
69 int value =
70 static_cast<int>(ui::PageTransitionStripQualifier(transition_type));
71 if (0 <= value && value <= kHistogramPageTransitionMaxKnownValue)
72 return value;
73 NOTREACHED();
74 return kHistogramPageTransitionFallbackValue;
77 void RecordFilterResultEvent(
78 SupervisedUserURLFilter::FilteringBehavior behavior,
79 SupervisedUserURLFilter::FilteringBehaviorReason reason,
80 bool uncertain,
81 ui::PageTransition transition_type) {
82 DCHECK(reason == SupervisedUserURLFilter::ASYNC_CHECKER ||
83 reason == SupervisedUserURLFilter::BLACKLIST);
84 int value =
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);
93 } // namespace
95 SupervisedUserResourceThrottle::SupervisedUserResourceThrottle(
96 const net::URLRequest* request,
97 bool is_main_frame,
98 const SupervisedUserURLFilter* url_filter)
99 : request_(request),
100 is_main_frame_(is_main_frame),
101 url_filter_(url_filter),
102 deferred_(false),
103 behavior_(SupervisedUserURLFilter::HISTOGRAM_BOUNDING_VALUE),
104 weak_ptr_factory_(this) {}
106 SupervisedUserResourceThrottle::~SupervisedUserResourceThrottle() {}
108 void SupervisedUserResourceThrottle::ShowInterstitialIfNeeded(bool is_redirect,
109 const GURL& url,
110 bool* defer) {
111 // Only treat main frame requests for now (ignoring subresources).
112 if (!is_main_frame_)
113 return;
115 deferred_ = false;
116 DCHECK_EQ(SupervisedUserURLFilter::HISTOGRAM_BOUNDING_VALUE, behavior_);
117 bool got_result = url_filter_->GetFilteringBehaviorForURLWithAsyncChecks(
118 url,
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);
126 if (got_result)
127 behavior_ = SupervisedUserURLFilter::HISTOGRAM_BOUNDING_VALUE;
130 void SupervisedUserResourceThrottle::ShowInterstitial(
131 const GURL& url,
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,
138 base::Bind(
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,
149 bool* defer) {
150 ShowInterstitialIfNeeded(true, redirect_info.new_url, defer);
153 const char* SupervisedUserResourceThrottle::GetNameForLogging() const {
154 return "SupervisedUserResourceThrottle";
157 void SupervisedUserResourceThrottle::OnCheckDone(
158 const GURL& url,
159 SupervisedUserURLFilter::FilteringBehavior behavior,
160 SupervisedUserURLFilter::FilteringBehaviorReason reason,
161 bool uncertain) {
162 DCHECK_EQ(SupervisedUserURLFilter::HISTOGRAM_BOUNDING_VALUE, behavior_);
163 // If we got a result synchronously, pass it back to ShowInterstitialIfNeeded.
164 if (!deferred_)
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);
179 else if (deferred_)
180 controller()->Resume();
183 void SupervisedUserResourceThrottle::OnInterstitialResult(
184 bool continue_request) {
185 if (continue_request)
186 controller()->Resume();
187 else
188 controller()->Cancel();