Index: components/web_restriction/web_restriction_resource_throttle.cc |
diff --git a/components/web_restriction/web_restriction_resource_throttle.cc b/components/web_restriction/web_restriction_resource_throttle.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f7bfd76246f20f8ccc04616b77b8befdb82c1b1a |
--- /dev/null |
+++ b/components/web_restriction/web_restriction_resource_throttle.cc |
@@ -0,0 +1,71 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/web_restriction/web_restriction_resource_throttle.h" |
+ |
+#include "base/bind.h" |
+#include "components/web_restriction/web_restriction_provider.h" |
+#include "content/public/browser/resource_controller.h" |
+#include "net/base/net_errors.h" |
+#include "net/url_request/redirect_info.h" |
+#include "net/url_request/url_request.h" |
+ |
+namespace web_restriction { |
+ |
+WebRestrictionResourceThrottle::WebRestrictionResourceThrottle( |
+ WebRestrictionProvider* provider, |
+ const GURL& request_url, |
+ bool is_main_frame) |
+ : provider_(provider), |
+ request_url_(request_url), |
+ is_main_frame_(is_main_frame), |
+ weak_ptr_factory_(this) {} |
+ |
+WebRestrictionResourceThrottle::~WebRestrictionResourceThrottle() {} |
+ |
+void WebRestrictionResourceThrottle::WillStartRequest(bool* defer) { |
+ *defer = ShouldDefer(request_url_); |
+} |
+ |
+void WebRestrictionResourceThrottle::WillRedirectRequest( |
+ const net::RedirectInfo& redirect_info, |
+ bool* defer) { |
+ *defer = ShouldDefer(redirect_info.new_url); |
+} |
+ |
+const char* WebRestrictionResourceThrottle::GetNameForLogging() const { |
+ return "WebRestrictionResourceThrottle"; |
+} |
+ |
+bool WebRestrictionResourceThrottle::ShouldDefer(const GURL& url) { |
+ // For requests to function correctly, we need to allow subresources. |
+ if (provider_->SupportsRequest() && !is_main_frame_) |
+ return false; |
+ UrlAccess access = provider_->ShouldProceed(is_main_frame_, url); |
Bernhard Bauer
2016/01/22 17:32:32
Hm… we call this method, and if it returns UNKNOWN
knn
2016/01/25 11:54:24
This looks much better now! Thanks!
|
+ switch (access) { |
+ case UNKNOWN: |
+ provider_->ShouldProceed( |
+ is_main_frame_, url, |
+ base::Bind(&WebRestrictionResourceThrottle::OnCheckResult, |
+ weak_ptr_factory_.GetWeakPtr())); |
+ break; |
+ case DISALLOW: |
+ OnCheckResult(false); |
+ case ALLOW: |
+ break; |
+ default: |
Bernhard Bauer
2016/01/22 17:32:32
Remove this; the compiler will check it for you.
knn
2016/01/25 11:54:24
Done.
|
+ NOTREACHED(); |
+ } |
+ return access == UNKNOWN; |
+} |
+ |
+void WebRestrictionResourceThrottle::OnCheckResult(const bool should_proceed) { |
+ if (should_proceed) { |
+ controller()->Resume(); |
+ } else { |
+ controller()->CancelWithError(net::ERR_BLOCKED_BY_ADMINISTRATOR); |
+ } |
+} |
+ |
+} // namespace web_restriction |