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

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: 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 if (!GetIoThreadClient() || ShouldCancel()) {
mkosiba (inactive) 2012/11/13 17:25:11 I'd prefer to assert that an IoThreadClient exists
benm (inactive) 2012/11/13 17:34:10 I changed it to this as even with the "false path"
63 controller()->CancelWithError(net::ERR_ACCESS_DENIED);
64 }
36 } 65 }
37 66
67 class MaybeCancelContentResourceThrottle : public MaybeCancelResourceThrottle {
68 public:
69 MaybeCancelContentResourceThrottle(int child_id, int route_id)
70 : MaybeCancelResourceThrottle(child_id, route_id) { }
71
72 bool ShouldCancel() {
73 return GetIoThreadClient()->ShouldBlockContentUrls();
74 }
75 };
76
77 class MaybeCancelFileResourceThrottle : public MaybeCancelResourceThrottle {
78 public:
79 MaybeCancelFileResourceThrottle(int child_id, int route_id)
80 : MaybeCancelResourceThrottle(child_id, route_id) { }
81
82 bool ShouldCancel() {
83 return GetIoThreadClient()->ShouldBlockFileUrls();
84 }
85 };
86
87 class MaybeCancelNetworkResourceThrottle : public MaybeCancelResourceThrottle {
88 public:
89 MaybeCancelNetworkResourceThrottle(int child_id, int route_id,
90 net::URLRequest* request)
91 : MaybeCancelResourceThrottle(child_id, route_id),
92 request_(request) { }
93
94 bool ShouldCancel() {
95 if (GetIoThreadClient()->ShouldBlockNetworkLoads()) {
96 if (request_->url().SchemeIs(chrome::kFtpScheme)) {
97 return true;
98 }
99 SetOnlyAllowLoadFromCache(request_);
100 }
101 return false;
102 }
103
104 private:
105 net::URLRequest* request_;
106 };
107
38 } // namespace 108 } // namespace
39 109
40 namespace android_webview { 110 namespace android_webview {
41 111
42 // static 112 // static
43 void AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated() { 113 void AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated() {
44 content::ResourceDispatcherHost::Get()->SetDelegate( 114 content::ResourceDispatcherHost::Get()->SetDelegate(
45 &g_webview_resource_dispatcher_host_delegate.Get()); 115 &g_webview_resource_dispatcher_host_delegate.Get());
46 } 116 }
47 117
48 AwResourceDispatcherHostDelegate::AwResourceDispatcherHostDelegate() 118 AwResourceDispatcherHostDelegate::AwResourceDispatcherHostDelegate()
49 : content::ResourceDispatcherHostDelegate() { 119 : content::ResourceDispatcherHostDelegate() {
50 } 120 }
51 121
52 AwResourceDispatcherHostDelegate::~AwResourceDispatcherHostDelegate() { 122 AwResourceDispatcherHostDelegate::~AwResourceDispatcherHostDelegate() {
53 } 123 }
54 124
55 void AwResourceDispatcherHostDelegate::RequestBeginning( 125 void AwResourceDispatcherHostDelegate::RequestBeginning(
56 net::URLRequest* request, 126 net::URLRequest* request,
57 content::ResourceContext* resource_context, 127 content::ResourceContext* resource_context,
58 appcache::AppCacheService* appcache_service, 128 appcache::AppCacheService* appcache_service,
59 ResourceType::Type resource_type, 129 ResourceType::Type resource_type,
60 int child_id, 130 int child_id,
61 int route_id, 131 int route_id,
62 bool is_continuation_of_transferred_request, 132 bool is_continuation_of_transferred_request,
63 ScopedVector<content::ResourceThrottle>* throttles) { 133 ScopedVector<content::ResourceThrottle>* throttles) {
64 134
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. 135 // Part of implementation of WebSettings.allowContentAccess.
70 if (request->url().SchemeIs(android_webview::kContentScheme) && 136 if (request->url().SchemeIs(android_webview::kContentScheme)) {
71 io_client->ShouldBlockContentUrls()) { 137 throttles->push_back(new MaybeCancelContentResourceThrottle(
72 throttles->push_back(new CancelResourceThrottle); 138 child_id, route_id));
73 } 139 }
74 140
75 // Part of implementation of WebSettings.allowFileAccess. 141 // Part of implementation of WebSettings.allowFileAccess.
76 if (request->url().SchemeIsFile() && io_client->ShouldBlockFileUrls()) { 142 if (request->url().SchemeIsFile()) {
77 const GURL& url = request->url(); 143 const GURL& url = request->url();
78 if (!url.has_path() || 144 if (!url.has_path() ||
79 // Application's assets and resources are always available. 145 // Application's assets and resources are always available.
80 (url.path().find(android_webview::kAndroidResourcePath) != 0 && 146 (url.path().find(android_webview::kAndroidResourcePath) != 0 &&
81 url.path().find(android_webview::kAndroidAssetPath) != 0)) { 147 url.path().find(android_webview::kAndroidAssetPath) != 0)) {
82 throttles->push_back(new CancelResourceThrottle); 148 throttles->push_back(new MaybeCancelFileResourceThrottle(
149 child_id, route_id));
83 } 150 }
84 } 151 }
85 152
86 // Part of implementation of WebSettings.blockNetworkLoads. 153 throttles->push_back(new MaybeCancelNetworkResourceThrottle(
87 if (io_client->ShouldBlockNetworkLoads()) { 154 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 155
97 // We ignore POST requests because of BUG=155250. 156 // We ignore POST requests because of BUG=155250.
98 if (resource_type == ResourceType::MAIN_FRAME && 157 if (resource_type == ResourceType::MAIN_FRAME &&
99 request->method() != "POST") { 158 request->method() != "POST") {
100 throttles->push_back(InterceptNavigationDelegate::CreateThrottleFor( 159 throttles->push_back(InterceptNavigationDelegate::CreateThrottleFor(
101 request)); 160 request));
102 } 161 }
103 } 162 }
104 163
105 bool AwResourceDispatcherHostDelegate::AcceptAuthRequest( 164 bool AwResourceDispatcherHostDelegate::AcceptAuthRequest(
(...skipping 11 matching lines...) Expand all
117 176
118 bool AwResourceDispatcherHostDelegate::HandleExternalProtocol(const GURL& url, 177 bool AwResourceDispatcherHostDelegate::HandleExternalProtocol(const GURL& url,
119 int child_id, 178 int child_id,
120 int route_id) { 179 int route_id) {
121 // The AwURLRequestJobFactory implementation should ensure this method never 180 // The AwURLRequestJobFactory implementation should ensure this method never
122 // gets called. 181 // gets called.
123 NOTREACHED(); 182 NOTREACHED();
124 return false; 183 return false;
125 } 184 }
126 185
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 } 186 }
136
137 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698