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

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: Fix a wonky indent and add a TODO for mkosiba's comment. 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, int route_id)
44 : child_id_(child_id),
45 route_id_(route_id) { }
31 virtual void WillStartRequest(bool* defer) OVERRIDE; 46 virtual void WillStartRequest(bool* defer) OVERRIDE;
47 virtual bool ShouldCancel() = 0;
48
49 scoped_ptr<AwContentsIoThreadClient> GetIoThreadClient() {
50 return AwContentsIoThreadClient::FromID(child_id_, route_id_);
51 }
52
53 private:
54 int child_id_;
55 int route_id_;
32 }; 56 };
33 57
34 void CancelResourceThrottle::WillStartRequest(bool* defer) { 58 void MaybeCancelResourceThrottle::WillStartRequest(bool* defer) {
35 controller()->CancelWithError(net::ERR_ACCESS_DENIED); 59 // If there is no IO thread client set at this point, use a
60 // restrictive policy. This can happen for blocked popup
61 // windows for example.
62 // TODO(benm): Revert this to a DCHECK when the we support
63 // pop up windows being created in the WebView, as at that
64 // time we should always have an IoThreadClient at this
65 // point (i.e., the one associated with the new popup).
66 if (!GetIoThreadClient() || ShouldCancel()) {
67 controller()->CancelWithError(net::ERR_ACCESS_DENIED);
68 }
36 } 69 }
37 70
71 class MaybeCancelContentResourceThrottle : public MaybeCancelResourceThrottle {
72 public:
73 MaybeCancelContentResourceThrottle(int child_id, int route_id)
74 : MaybeCancelResourceThrottle(child_id, route_id) { }
75
76 bool ShouldCancel() {
77 return GetIoThreadClient()->ShouldBlockContentUrls();
78 }
79 };
80
81 class MaybeCancelFileResourceThrottle : public MaybeCancelResourceThrottle {
82 public:
83 MaybeCancelFileResourceThrottle(int child_id, int route_id)
84 : MaybeCancelResourceThrottle(child_id, route_id) { }
85
86 bool ShouldCancel() {
87 return GetIoThreadClient()->ShouldBlockFileUrls();
88 }
89 };
90
91 class MaybeCancelNetworkResourceThrottle : public MaybeCancelResourceThrottle {
92 public:
93 MaybeCancelNetworkResourceThrottle(int child_id, int route_id,
94 net::URLRequest* request)
95 : MaybeCancelResourceThrottle(child_id, route_id),
96 request_(request) { }
97
98 bool ShouldCancel() {
99 if (GetIoThreadClient()->ShouldBlockNetworkLoads()) {
100 if (request_->url().SchemeIs(chrome::kFtpScheme)) {
101 return true;
102 }
103 SetOnlyAllowLoadFromCache(request_);
104 }
105 return false;
106 }
107
108 private:
109 net::URLRequest* request_;
110 };
111
38 } // namespace 112 } // namespace
39 113
40 namespace android_webview { 114 namespace android_webview {
41 115
42 // static 116 // static
43 void AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated() { 117 void AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated() {
44 content::ResourceDispatcherHost::Get()->SetDelegate( 118 content::ResourceDispatcherHost::Get()->SetDelegate(
45 &g_webview_resource_dispatcher_host_delegate.Get()); 119 &g_webview_resource_dispatcher_host_delegate.Get());
46 } 120 }
47 121
48 AwResourceDispatcherHostDelegate::AwResourceDispatcherHostDelegate() 122 AwResourceDispatcherHostDelegate::AwResourceDispatcherHostDelegate()
49 : content::ResourceDispatcherHostDelegate() { 123 : content::ResourceDispatcherHostDelegate() {
50 } 124 }
51 125
52 AwResourceDispatcherHostDelegate::~AwResourceDispatcherHostDelegate() { 126 AwResourceDispatcherHostDelegate::~AwResourceDispatcherHostDelegate() {
53 } 127 }
54 128
55 void AwResourceDispatcherHostDelegate::RequestBeginning( 129 void AwResourceDispatcherHostDelegate::RequestBeginning(
56 net::URLRequest* request, 130 net::URLRequest* request,
57 content::ResourceContext* resource_context, 131 content::ResourceContext* resource_context,
58 appcache::AppCacheService* appcache_service, 132 appcache::AppCacheService* appcache_service,
59 ResourceType::Type resource_type, 133 ResourceType::Type resource_type,
60 int child_id, 134 int child_id,
61 int route_id, 135 int route_id,
62 bool is_continuation_of_transferred_request, 136 bool is_continuation_of_transferred_request,
63 ScopedVector<content::ResourceThrottle>* throttles) { 137 ScopedVector<content::ResourceThrottle>* throttles) {
64 138
65 scoped_ptr<AwContentsIoThreadClient> io_client =
66 AwContentsIoThreadClient::FromID(child_id, route_id);
67 DCHECK(io_client.get());
68
69 // Part of implementation of WebSettings.allowContentAccess. 139 // Part of implementation of WebSettings.allowContentAccess.
70 if (request->url().SchemeIs(android_webview::kContentScheme) && 140 if (request->url().SchemeIs(android_webview::kContentScheme)) {
71 io_client->ShouldBlockContentUrls()) { 141 throttles->push_back(new MaybeCancelContentResourceThrottle(
72 throttles->push_back(new CancelResourceThrottle); 142 child_id, route_id));
73 } 143 }
74 144
75 // Part of implementation of WebSettings.allowFileAccess. 145 // Part of implementation of WebSettings.allowFileAccess.
76 if (request->url().SchemeIsFile() && io_client->ShouldBlockFileUrls()) { 146 if (request->url().SchemeIsFile()) {
77 const GURL& url = request->url(); 147 const GURL& url = request->url();
78 if (!url.has_path() || 148 if (!url.has_path() ||
79 // Application's assets and resources are always available. 149 // Application's assets and resources are always available.
80 (url.path().find(android_webview::kAndroidResourcePath) != 0 && 150 (url.path().find(android_webview::kAndroidResourcePath) != 0 &&
81 url.path().find(android_webview::kAndroidAssetPath) != 0)) { 151 url.path().find(android_webview::kAndroidAssetPath) != 0)) {
82 throttles->push_back(new CancelResourceThrottle); 152 throttles->push_back(new MaybeCancelFileResourceThrottle(
153 child_id, route_id));
83 } 154 }
84 } 155 }
85 156
86 // Part of implementation of WebSettings.blockNetworkLoads. 157 throttles->push_back(new MaybeCancelNetworkResourceThrottle(
87 if (io_client->ShouldBlockNetworkLoads()) { 158 child_id, route_id, request));
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 159
97 // We ignore POST requests because of BUG=155250. 160 // We ignore POST requests because of BUG=155250.
98 if (resource_type == ResourceType::MAIN_FRAME && 161 if (resource_type == ResourceType::MAIN_FRAME &&
99 request->method() != "POST") { 162 request->method() != "POST") {
100 throttles->push_back(InterceptNavigationDelegate::CreateThrottleFor( 163 throttles->push_back(InterceptNavigationDelegate::CreateThrottleFor(
101 request)); 164 request));
102 } 165 }
103 } 166 }
104 167
105 bool AwResourceDispatcherHostDelegate::AcceptAuthRequest( 168 bool AwResourceDispatcherHostDelegate::AcceptAuthRequest(
(...skipping 11 matching lines...) Expand all
117 180
118 bool AwResourceDispatcherHostDelegate::HandleExternalProtocol(const GURL& url, 181 bool AwResourceDispatcherHostDelegate::HandleExternalProtocol(const GURL& url,
119 int child_id, 182 int child_id,
120 int route_id) { 183 int route_id) {
121 // The AwURLRequestJobFactory implementation should ensure this method never 184 // The AwURLRequestJobFactory implementation should ensure this method never
122 // gets called. 185 // gets called.
123 NOTREACHED(); 186 NOTREACHED();
124 return false; 187 return false;
125 } 188 }
126 189
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 } 190 }
136
137 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698