Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(334)

Side by Side Diff: android_webview/browser/renderer_host/aw_resource_dispatcher_host_delegate.cc

Issue 11362183: [Android WebView] AwContentsClient.shouldCreateWindow callback part 1. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase for landing Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "android_webview/browser/renderer_host/aw_resource_dispatcher_host_dele gate.h" 5 #include "android_webview/browser/renderer_host/aw_resource_dispatcher_host_dele gate.h"
6 6
7 #include "android_webview/browser/aw_login_delegate.h" 7 #include "android_webview/browser/aw_login_delegate.h"
8 #include "android_webview/browser/aw_contents_io_thread_client.h" 8 #include "android_webview/browser/aw_contents_io_thread_client.h"
9 #include "android_webview/common/url_constants.h" 9 #include "android_webview/common/url_constants.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/scoped_vector.h" 11 #include "base/memory/scoped_vector.h"
12 #include "content/components/navigation_interception/intercept_navigation_delega te.h" 12 #include "content/components/navigation_interception/intercept_navigation_delega te.h"
13 #include "content/public/browser/resource_controller.h" 13 #include "content/public/browser/resource_controller.h"
14 #include "content/public/browser/resource_dispatcher_host.h" 14 #include "content/public/browser/resource_dispatcher_host.h"
15 #include "content/public/browser/resource_dispatcher_host_login_delegate.h" 15 #include "content/public/browser/resource_dispatcher_host_login_delegate.h"
16 #include "content/public/browser/resource_throttle.h" 16 #include "content/public/browser/resource_throttle.h"
17 #include "content/public/common/url_constants.h" 17 #include "content/public/common/url_constants.h"
18 #include "net/base/load_flags.h" 18 #include "net/base/load_flags.h"
19 #include "net/url_request/url_request.h" 19 #include "net/url_request/url_request.h"
20 20
21 using content::InterceptNavigationDelegate; 21 using content::InterceptNavigationDelegate;
22 22
23 namespace { 23 namespace {
24 24
25 using android_webview::AwContentsIoThreadClient;
26
25 base::LazyInstance<android_webview::AwResourceDispatcherHostDelegate> 27 base::LazyInstance<android_webview::AwResourceDispatcherHostDelegate>
26 g_webview_resource_dispatcher_host_delegate = LAZY_INSTANCE_INITIALIZER; 28 g_webview_resource_dispatcher_host_delegate = LAZY_INSTANCE_INITIALIZER;
27 29
28 // Will unconditionally cancel this resource request. 30 void SetOnlyAllowLoadFromCache(
29 class CancelResourceThrottle : public content::ResourceThrottle { 31 net::URLRequest* request) {
32 int load_flags = request->load_flags();
33 load_flags &= ~(net::LOAD_BYPASS_CACHE &
34 net::LOAD_VALIDATE_CACHE &
35 net::LOAD_PREFERRING_CACHE);
36 load_flags |= net::LOAD_ONLY_FROM_CACHE;
37 request->set_load_flags(load_flags);
38 }
39
40 // May cancel this resource request based on result of Java callbacks.
41 class MaybeCancelResourceThrottle : public content::ResourceThrottle {
30 public: 42 public:
43 MaybeCancelResourceThrottle(int child_id,
44 int route_id,
45 net::URLRequest* request)
46 : child_id_(child_id),
47 route_id_(route_id),
48 request_(request) { }
31 virtual void WillStartRequest(bool* defer) OVERRIDE; 49 virtual void WillStartRequest(bool* defer) OVERRIDE;
50
51 scoped_ptr<AwContentsIoThreadClient> GetIoThreadClient() {
52 return AwContentsIoThreadClient::FromID(child_id_, route_id_);
53 }
54
55 private:
56 int child_id_;
57 int route_id_;
58 net::URLRequest* request_;
32 }; 59 };
33 60
34 void CancelResourceThrottle::WillStartRequest(bool* defer) { 61 void MaybeCancelResourceThrottle::WillStartRequest(bool* defer) {
35 controller()->CancelWithError(net::ERR_ACCESS_DENIED); 62 // If there is no IO thread client set at this point, use a
63 // restrictive policy. This can happen for blocked popup
64 // windows for example.
65 // TODO(benm): Revert this to a DCHECK when the we support
66 // pop up windows being created in the WebView, as at that
67 // time we should always have an IoThreadClient at this
68 // point (i.e., the one associated with the new popup).
69 if (!GetIoThreadClient()) {
70 controller()->CancelWithError(net::ERR_ACCESS_DENIED);
71 return;
72 }
73
74 // Part of implementation of WebSettings.allowContentAccess.
75 if (request_->url().SchemeIs(android_webview::kContentScheme) &&
76 GetIoThreadClient()->ShouldBlockContentUrls()) {
77 controller()->CancelWithError(net::ERR_ACCESS_DENIED);
78 return;
79 }
80
81 // Part of implementation of WebSettings.allowFileAccess.
82 if (request_->url().SchemeIsFile() &&
83 GetIoThreadClient()->ShouldBlockFileUrls()) {
84 const GURL& url = request_->url();
85 if (!url.has_path() ||
86 // Application's assets and resources are always available.
87 (url.path().find(android_webview::kAndroidResourcePath) != 0 &&
88 url.path().find(android_webview::kAndroidAssetPath) != 0)) {
89 controller()->CancelWithError(net::ERR_ACCESS_DENIED);
90 return;
91 }
92 }
93
94 if (GetIoThreadClient()->ShouldBlockNetworkLoads()) {
95 if (request_->url().SchemeIs(chrome::kFtpScheme)) {
96 controller()->CancelWithError(net::ERR_ACCESS_DENIED);
97 return;
98 }
99 SetOnlyAllowLoadFromCache(request_);
100 }
36 } 101 }
37 102
38 } // namespace 103 } // namespace
39 104
40 namespace android_webview { 105 namespace android_webview {
41 106
42 // static 107 // static
43 void AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated() { 108 void AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated() {
44 content::ResourceDispatcherHost::Get()->SetDelegate( 109 content::ResourceDispatcherHost::Get()->SetDelegate(
45 &g_webview_resource_dispatcher_host_delegate.Get()); 110 &g_webview_resource_dispatcher_host_delegate.Get());
46 } 111 }
47 112
48 AwResourceDispatcherHostDelegate::AwResourceDispatcherHostDelegate() 113 AwResourceDispatcherHostDelegate::AwResourceDispatcherHostDelegate()
49 : content::ResourceDispatcherHostDelegate() { 114 : content::ResourceDispatcherHostDelegate() {
50 } 115 }
51 116
52 AwResourceDispatcherHostDelegate::~AwResourceDispatcherHostDelegate() { 117 AwResourceDispatcherHostDelegate::~AwResourceDispatcherHostDelegate() {
53 } 118 }
54 119
55 void AwResourceDispatcherHostDelegate::RequestBeginning( 120 void AwResourceDispatcherHostDelegate::RequestBeginning(
56 net::URLRequest* request, 121 net::URLRequest* request,
57 content::ResourceContext* resource_context, 122 content::ResourceContext* resource_context,
58 appcache::AppCacheService* appcache_service, 123 appcache::AppCacheService* appcache_service,
59 ResourceType::Type resource_type, 124 ResourceType::Type resource_type,
60 int child_id, 125 int child_id,
61 int route_id, 126 int route_id,
62 bool is_continuation_of_transferred_request, 127 bool is_continuation_of_transferred_request,
63 ScopedVector<content::ResourceThrottle>* throttles) { 128 ScopedVector<content::ResourceThrottle>* throttles) {
64 129
65 scoped_ptr<AwContentsIoThreadClient> io_client = 130 throttles->push_back(new MaybeCancelResourceThrottle(
66 AwContentsIoThreadClient::FromID(child_id, route_id); 131 child_id, route_id, request));
67 DCHECK(io_client.get());
68
69 // Part of implementation of WebSettings.allowContentAccess.
70 if (request->url().SchemeIs(android_webview::kContentScheme) &&
71 io_client->ShouldBlockContentUrls()) {
72 throttles->push_back(new CancelResourceThrottle);
73 }
74
75 // Part of implementation of WebSettings.allowFileAccess.
76 if (request->url().SchemeIsFile() && io_client->ShouldBlockFileUrls()) {
77 const GURL& url = request->url();
78 if (!url.has_path() ||
79 // Application's assets and resources are always available.
80 (url.path().find(android_webview::kAndroidResourcePath) != 0 &&
81 url.path().find(android_webview::kAndroidAssetPath) != 0)) {
82 throttles->push_back(new CancelResourceThrottle);
83 }
84 }
85
86 // Part of implementation of WebSettings.blockNetworkLoads.
87 if (io_client->ShouldBlockNetworkLoads()) {
88 // Need to cancel ftp since it does not support net::LOAD_ONLY_FROM_CACHE
89 // flag, so must cancel the request if network load is blocked.
90 if (request->url().SchemeIs(chrome::kFtpScheme)) {
91 throttles->push_back(new CancelResourceThrottle);
92 } else {
93 SetOnlyAllowLoadFromCache(request);
94 }
95 }
96 132
97 // We ignore POST requests because of BUG=155250. 133 // We ignore POST requests because of BUG=155250.
98 if (resource_type == ResourceType::MAIN_FRAME && 134 if (resource_type == ResourceType::MAIN_FRAME &&
99 request->method() != "POST") { 135 request->method() != "POST") {
100 throttles->push_back(InterceptNavigationDelegate::CreateThrottleFor( 136 throttles->push_back(InterceptNavigationDelegate::CreateThrottleFor(
101 request)); 137 request));
102 } 138 }
103 } 139 }
104 140
105 bool AwResourceDispatcherHostDelegate::AcceptAuthRequest( 141 bool AwResourceDispatcherHostDelegate::AcceptAuthRequest(
(...skipping 11 matching lines...) Expand all
117 153
118 bool AwResourceDispatcherHostDelegate::HandleExternalProtocol(const GURL& url, 154 bool AwResourceDispatcherHostDelegate::HandleExternalProtocol(const GURL& url,
119 int child_id, 155 int child_id,
120 int route_id) { 156 int route_id) {
121 // The AwURLRequestJobFactory implementation should ensure this method never 157 // The AwURLRequestJobFactory implementation should ensure this method never
122 // gets called. 158 // gets called.
123 NOTREACHED(); 159 NOTREACHED();
124 return false; 160 return false;
125 } 161 }
126 162
127 void AwResourceDispatcherHostDelegate::SetOnlyAllowLoadFromCache(
128 net::URLRequest* request) {
129 int load_flags = request->load_flags();
130 load_flags &= ~(net::LOAD_BYPASS_CACHE &
131 net::LOAD_VALIDATE_CACHE &
132 net::LOAD_PREFERRING_CACHE);
133 load_flags |= net::LOAD_ONLY_FROM_CACHE;
134 request->set_load_flags(load_flags);
135 } 163 }
136
137 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698