OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/supervised_user/supervised_user_resource_throttle.h" | 5 #include "chrome/browser/supervised_user/supervised_user_resource_throttle.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/metrics/sparse_histogram.h" | 8 #include "base/metrics/sparse_histogram.h" |
9 #include "chrome/browser/supervised_user/supervised_user_interstitial.h" | 9 #include "chrome/browser/supervised_user/supervised_user_interstitial.h" |
10 #include "chrome/browser/supervised_user/supervised_user_navigation_observer.h" | 10 #include "chrome/browser/supervised_user/supervised_user_navigation_observer.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 | 42 |
43 static_assert(kHistogramPageTransitionMaxKnownValue < | 43 static_assert(kHistogramPageTransitionMaxKnownValue < |
44 kHistogramPageTransitionFallbackValue, | 44 kHistogramPageTransitionFallbackValue, |
45 "HistogramPageTransition MaxKnownValue must be < FallbackValue"); | 45 "HistogramPageTransition MaxKnownValue must be < FallbackValue"); |
46 static_assert(FILTERING_BEHAVIOR_MAX * kHistogramFilteringBehaviorSpacing + | 46 static_assert(FILTERING_BEHAVIOR_MAX * kHistogramFilteringBehaviorSpacing + |
47 kHistogramPageTransitionFallbackValue < kHistogramMax, | 47 kHistogramPageTransitionFallbackValue < kHistogramMax, |
48 "Invalid HistogramMax value"); | 48 "Invalid HistogramMax value"); |
49 | 49 |
50 int GetHistogramValueForFilteringBehavior( | 50 int GetHistogramValueForFilteringBehavior( |
51 SupervisedUserURLFilter::FilteringBehavior behavior, | 51 SupervisedUserURLFilter::FilteringBehavior behavior, |
52 SupervisedUserURLFilter::FilteringBehaviorReason reason, | 52 supervised_user_error_page::FilteringBehaviorReason reason, |
53 bool uncertain) { | 53 bool uncertain) { |
54 switch (behavior) { | 54 switch (behavior) { |
55 case SupervisedUserURLFilter::ALLOW: | 55 case SupervisedUserURLFilter::ALLOW: |
56 case SupervisedUserURLFilter::WARN: | 56 case SupervisedUserURLFilter::WARN: |
57 if (reason == SupervisedUserURLFilter::WHITELIST) | 57 if (reason == supervised_user_error_page::WHITELIST) |
58 return FILTERING_BEHAVIOR_ALLOW_WHITELIST; | 58 return FILTERING_BEHAVIOR_ALLOW_WHITELIST; |
59 return uncertain ? FILTERING_BEHAVIOR_ALLOW_UNCERTAIN | 59 return uncertain ? FILTERING_BEHAVIOR_ALLOW_UNCERTAIN |
60 : FILTERING_BEHAVIOR_ALLOW; | 60 : FILTERING_BEHAVIOR_ALLOW; |
61 case SupervisedUserURLFilter::BLOCK: | 61 case SupervisedUserURLFilter::BLOCK: |
62 switch (reason) { | 62 switch (reason) { |
63 case SupervisedUserURLFilter::BLACKLIST: | 63 case supervised_user_error_page::BLACKLIST: |
64 return FILTERING_BEHAVIOR_BLOCK_BLACKLIST; | 64 return FILTERING_BEHAVIOR_BLOCK_BLACKLIST; |
65 case SupervisedUserURLFilter::ASYNC_CHECKER: | 65 case supervised_user_error_page::ASYNC_CHECKER: |
66 return FILTERING_BEHAVIOR_BLOCK_SAFESITES; | 66 return FILTERING_BEHAVIOR_BLOCK_SAFESITES; |
67 case SupervisedUserURLFilter::WHITELIST: | 67 case supervised_user_error_page::WHITELIST: |
68 NOTREACHED(); | 68 NOTREACHED(); |
69 break; | 69 break; |
70 case SupervisedUserURLFilter::MANUAL: | 70 case supervised_user_error_page::MANUAL: |
71 return FILTERING_BEHAVIOR_BLOCK_MANUAL; | 71 return FILTERING_BEHAVIOR_BLOCK_MANUAL; |
72 case SupervisedUserURLFilter::DEFAULT: | 72 case supervised_user_error_page::DEFAULT: |
73 return FILTERING_BEHAVIOR_BLOCK_DEFAULT; | 73 return FILTERING_BEHAVIOR_BLOCK_DEFAULT; |
74 } | 74 } |
75 case SupervisedUserURLFilter::INVALID: | 75 case SupervisedUserURLFilter::INVALID: |
76 NOTREACHED(); | 76 NOTREACHED(); |
77 } | 77 } |
78 return 0; | 78 return 0; |
79 } | 79 } |
80 | 80 |
81 int GetHistogramValueForTransitionType(ui::PageTransition transition_type) { | 81 int GetHistogramValueForTransitionType(ui::PageTransition transition_type) { |
82 int value = | 82 int value = |
83 static_cast<int>(ui::PageTransitionStripQualifier(transition_type)); | 83 static_cast<int>(ui::PageTransitionStripQualifier(transition_type)); |
84 if (0 <= value && value <= kHistogramPageTransitionMaxKnownValue) | 84 if (0 <= value && value <= kHistogramPageTransitionMaxKnownValue) |
85 return value; | 85 return value; |
86 NOTREACHED(); | 86 NOTREACHED(); |
87 return kHistogramPageTransitionFallbackValue; | 87 return kHistogramPageTransitionFallbackValue; |
88 } | 88 } |
89 | 89 |
90 void RecordFilterResultEvent( | 90 void RecordFilterResultEvent( |
91 bool safesites_histogram, | 91 bool safesites_histogram, |
92 SupervisedUserURLFilter::FilteringBehavior behavior, | 92 SupervisedUserURLFilter::FilteringBehavior behavior, |
93 SupervisedUserURLFilter::FilteringBehaviorReason reason, | 93 supervised_user_error_page::FilteringBehaviorReason reason, |
94 bool uncertain, | 94 bool uncertain, |
95 ui::PageTransition transition_type) { | 95 ui::PageTransition transition_type) { |
96 int value = | 96 int value = |
97 GetHistogramValueForFilteringBehavior(behavior, reason, uncertain) * | 97 GetHistogramValueForFilteringBehavior(behavior, reason, uncertain) * |
98 kHistogramFilteringBehaviorSpacing + | 98 kHistogramFilteringBehaviorSpacing + |
99 GetHistogramValueForTransitionType(transition_type); | 99 GetHistogramValueForTransitionType(transition_type); |
100 DCHECK_LT(value, kHistogramMax); | 100 DCHECK_LT(value, kHistogramMax); |
101 // Note: We can't pass in the histogram name as a parameter to this function | 101 // Note: We can't pass in the histogram name as a parameter to this function |
102 // because of how the macro works (look up the histogram on the first | 102 // because of how the macro works (look up the histogram on the first |
103 // invocation and cache it in a static variable). | 103 // invocation and cache it in a static variable). |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 DCHECK_EQ(got_result, behavior_ != SupervisedUserURLFilter::INVALID); | 138 DCHECK_EQ(got_result, behavior_ != SupervisedUserURLFilter::INVALID); |
139 // If we got a "not blocked" result synchronously, don't defer. | 139 // If we got a "not blocked" result synchronously, don't defer. |
140 *defer = deferred_ = !got_result || | 140 *defer = deferred_ = !got_result || |
141 (behavior_ == SupervisedUserURLFilter::BLOCK); | 141 (behavior_ == SupervisedUserURLFilter::BLOCK); |
142 if (got_result) | 142 if (got_result) |
143 behavior_ = SupervisedUserURLFilter::INVALID; | 143 behavior_ = SupervisedUserURLFilter::INVALID; |
144 } | 144 } |
145 | 145 |
146 void SupervisedUserResourceThrottle::ShowInterstitial( | 146 void SupervisedUserResourceThrottle::ShowInterstitial( |
147 const GURL& url, | 147 const GURL& url, |
148 SupervisedUserURLFilter::FilteringBehaviorReason reason) { | 148 supervised_user_error_page::FilteringBehaviorReason reason) { |
149 const content::ResourceRequestInfo* info = | 149 const content::ResourceRequestInfo* info = |
150 content::ResourceRequestInfo::ForRequest(request_); | 150 content::ResourceRequestInfo::ForRequest(request_); |
151 BrowserThread::PostTask( | 151 BrowserThread::PostTask( |
152 BrowserThread::UI, FROM_HERE, | 152 BrowserThread::UI, FROM_HERE, |
153 base::Bind( | 153 base::Bind( |
154 &SupervisedUserNavigationObserver::OnRequestBlocked, | 154 &SupervisedUserNavigationObserver::OnRequestBlocked, |
155 info->GetWebContentsGetterForRequest(), url, reason, | 155 info->GetWebContentsGetterForRequest(), url, reason, |
156 base::Bind(&SupervisedUserResourceThrottle::OnInterstitialResult, | 156 base::Bind(&SupervisedUserResourceThrottle::OnInterstitialResult, |
157 weak_ptr_factory_.GetWeakPtr()))); | 157 weak_ptr_factory_.GetWeakPtr()))); |
158 } | 158 } |
159 | 159 |
160 void SupervisedUserResourceThrottle::WillStartRequest(bool* defer) { | 160 void SupervisedUserResourceThrottle::WillStartRequest(bool* defer) { |
161 ShowInterstitialIfNeeded(false, request_->url(), defer); | 161 ShowInterstitialIfNeeded(false, request_->url(), defer); |
162 } | 162 } |
163 | 163 |
164 void SupervisedUserResourceThrottle::WillRedirectRequest( | 164 void SupervisedUserResourceThrottle::WillRedirectRequest( |
165 const net::RedirectInfo& redirect_info, | 165 const net::RedirectInfo& redirect_info, |
166 bool* defer) { | 166 bool* defer) { |
167 ShowInterstitialIfNeeded(true, redirect_info.new_url, defer); | 167 ShowInterstitialIfNeeded(true, redirect_info.new_url, defer); |
168 } | 168 } |
169 | 169 |
170 const char* SupervisedUserResourceThrottle::GetNameForLogging() const { | 170 const char* SupervisedUserResourceThrottle::GetNameForLogging() const { |
171 return "SupervisedUserResourceThrottle"; | 171 return "SupervisedUserResourceThrottle"; |
172 } | 172 } |
173 | 173 |
174 void SupervisedUserResourceThrottle::OnCheckDone( | 174 void SupervisedUserResourceThrottle::OnCheckDone( |
175 const GURL& url, | 175 const GURL& url, |
176 SupervisedUserURLFilter::FilteringBehavior behavior, | 176 SupervisedUserURLFilter::FilteringBehavior behavior, |
177 SupervisedUserURLFilter::FilteringBehaviorReason reason, | 177 supervised_user_error_page::FilteringBehaviorReason reason, |
178 bool uncertain) { | 178 bool uncertain) { |
179 DCHECK_EQ(SupervisedUserURLFilter::INVALID, behavior_); | 179 DCHECK_EQ(SupervisedUserURLFilter::INVALID, behavior_); |
180 // If we got a result synchronously, pass it back to ShowInterstitialIfNeeded. | 180 // If we got a result synchronously, pass it back to ShowInterstitialIfNeeded. |
181 if (!deferred_) | 181 if (!deferred_) |
182 behavior_ = behavior; | 182 behavior_ = behavior; |
183 | 183 |
184 ui::PageTransition transition = | 184 ui::PageTransition transition = |
185 content::ResourceRequestInfo::ForRequest(request_)->GetPageTransition(); | 185 content::ResourceRequestInfo::ForRequest(request_)->GetPageTransition(); |
186 | 186 |
187 RecordFilterResultEvent(false, behavior, reason, uncertain, transition); | 187 RecordFilterResultEvent(false, behavior, reason, uncertain, transition); |
188 | 188 |
189 // If both the static blacklist and the async checker are enabled, also record | 189 // If both the static blacklist and the async checker are enabled, also record |
190 // SafeSites-only UMA events. | 190 // SafeSites-only UMA events. |
191 if (url_filter_->HasBlacklist() && url_filter_->HasAsyncURLChecker() && | 191 if (url_filter_->HasBlacklist() && url_filter_->HasAsyncURLChecker() && |
192 (reason == SupervisedUserURLFilter::ASYNC_CHECKER || | 192 (reason == supervised_user_error_page::ASYNC_CHECKER || |
193 reason == SupervisedUserURLFilter::BLACKLIST)) { | 193 reason == supervised_user_error_page::BLACKLIST)) { |
194 RecordFilterResultEvent(true, behavior, reason, uncertain, transition); | 194 RecordFilterResultEvent(true, behavior, reason, uncertain, transition); |
195 } | 195 } |
196 | 196 |
197 if (behavior == SupervisedUserURLFilter::BLOCK) | 197 if (behavior == SupervisedUserURLFilter::BLOCK) |
198 ShowInterstitial(url, reason); | 198 ShowInterstitial(url, reason); |
199 else if (deferred_) | 199 else if (deferred_) |
200 controller()->Resume(); | 200 controller()->Resume(); |
201 } | 201 } |
202 | 202 |
203 void SupervisedUserResourceThrottle::OnInterstitialResult( | 203 void SupervisedUserResourceThrottle::OnInterstitialResult( |
204 bool continue_request) { | 204 bool continue_request) { |
205 if (continue_request) | 205 if (continue_request) |
206 controller()->Resume(); | 206 controller()->Resume(); |
207 else | 207 else |
208 controller()->Cancel(); | 208 controller()->Cancel(); |
209 } | 209 } |
OLD | NEW |