OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "components/web_restriction/web_restriction_resource_throttle.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "components/web_restriction/web_restriction_provider.h" |
| 9 #include "content/public/browser/resource_controller.h" |
| 10 #include "net/base/net_errors.h" |
| 11 #include "net/url_request/redirect_info.h" |
| 12 #include "net/url_request/url_request.h" |
| 13 |
| 14 namespace web_restriction { |
| 15 |
| 16 WebRestrictionResourceThrottle::WebRestrictionResourceThrottle( |
| 17 WebRestrictionProvider* provider, |
| 18 const GURL& request_url, |
| 19 bool is_main_frame) |
| 20 : provider_(provider), |
| 21 request_url_(request_url), |
| 22 is_main_frame_(is_main_frame), |
| 23 weak_ptr_factory_(this) {} |
| 24 |
| 25 WebRestrictionResourceThrottle::~WebRestrictionResourceThrottle() {} |
| 26 |
| 27 void WebRestrictionResourceThrottle::WillStartRequest(bool* defer) { |
| 28 *defer = ShouldDefer(request_url_); |
| 29 } |
| 30 |
| 31 void WebRestrictionResourceThrottle::WillRedirectRequest( |
| 32 const net::RedirectInfo& redirect_info, |
| 33 bool* defer) { |
| 34 *defer = ShouldDefer(redirect_info.new_url); |
| 35 } |
| 36 |
| 37 const char* WebRestrictionResourceThrottle::GetNameForLogging() const { |
| 38 return "WebRestrictionResourceThrottle"; |
| 39 } |
| 40 |
| 41 bool WebRestrictionResourceThrottle::ShouldDefer(const GURL& url) { |
| 42 // For requests to function correctly, we need to allow subresources. |
| 43 if (provider_->SupportsRequest() && !is_main_frame_) |
| 44 return false; |
| 45 UrlAccess access = provider_->ShouldProceed( |
| 46 is_main_frame_, url, |
| 47 base::Bind(&WebRestrictionResourceThrottle::OnCheckResult, |
| 48 weak_ptr_factory_.GetWeakPtr())); |
| 49 if (access == DISALLOW) |
| 50 OnCheckResult(false); |
| 51 return access == PENDING; |
| 52 } |
| 53 |
| 54 void WebRestrictionResourceThrottle::OnCheckResult(const bool should_proceed) { |
| 55 if (should_proceed) { |
| 56 controller()->Resume(); |
| 57 } else { |
| 58 controller()->CancelWithError(net::ERR_BLOCKED_BY_ADMINISTRATOR); |
| 59 } |
| 60 } |
| 61 |
| 62 } // namespace web_restriction |
OLD | NEW |