OLD | NEW |
---|---|
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" | |
9 #include "android_webview/common/url_constants.h" | 8 #include "android_webview/common/url_constants.h" |
10 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
11 #include "base/memory/scoped_vector.h" | 10 #include "base/memory/scoped_vector.h" |
12 #include "content/components/navigation_interception/intercept_navigation_delega te.h" | 11 #include "content/components/navigation_interception/intercept_navigation_delega te.h" |
12 #include "content/public/browser/browser_thread.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" | |
17 #include "content/public/common/url_constants.h" | 16 #include "content/public/common/url_constants.h" |
18 #include "net/base/load_flags.h" | 17 #include "net/base/load_flags.h" |
19 #include "net/url_request/url_request.h" | 18 #include "net/url_request/url_request.h" |
20 | 19 |
20 using android_webview::AwContentsIoThreadClient; | |
21 using content::BrowserThread; | |
21 using content::InterceptNavigationDelegate; | 22 using content::InterceptNavigationDelegate; |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 using android_webview::AwContentsIoThreadClient; | |
26 | |
27 base::LazyInstance<android_webview::AwResourceDispatcherHostDelegate> | 26 base::LazyInstance<android_webview::AwResourceDispatcherHostDelegate> |
28 g_webview_resource_dispatcher_host_delegate = LAZY_INSTANCE_INITIALIZER; | 27 g_webview_resource_dispatcher_host_delegate = LAZY_INSTANCE_INITIALIZER; |
29 | 28 |
30 void SetCacheControlFlag( | 29 void SetCacheControlFlag( |
31 net::URLRequest* request, int flag) { | 30 net::URLRequest* request, int flag) { |
32 const int all_cache_control_flags = net::LOAD_BYPASS_CACHE | | 31 const int all_cache_control_flags = net::LOAD_BYPASS_CACHE | |
33 net::LOAD_VALIDATE_CACHE | | 32 net::LOAD_VALIDATE_CACHE | |
34 net::LOAD_PREFERRING_CACHE | | 33 net::LOAD_PREFERRING_CACHE | |
35 net::LOAD_ONLY_FROM_CACHE; | 34 net::LOAD_ONLY_FROM_CACHE; |
36 DCHECK((flag & all_cache_control_flags) == flag); | 35 DCHECK((flag & all_cache_control_flags) == flag); |
37 int load_flags = request->load_flags(); | 36 int load_flags = request->load_flags(); |
38 load_flags &= ~all_cache_control_flags; | 37 load_flags &= ~all_cache_control_flags; |
39 load_flags |= flag; | 38 load_flags |= flag; |
40 request->set_load_flags(load_flags); | 39 request->set_load_flags(load_flags); |
41 } | 40 } |
42 | 41 |
43 // Calls through the IoThreadClient to check the embedders settings to determine | 42 } // namespace |
44 // if the request should be cancelled. | |
45 class IoThreadClientThrottle : public content::ResourceThrottle { | |
46 public: | |
47 IoThreadClientThrottle(int child_id, | |
48 int route_id, | |
49 net::URLRequest* request) | |
50 : child_id_(child_id), | |
51 route_id_(route_id), | |
52 request_(request) { } | |
53 virtual void WillStartRequest(bool* defer) OVERRIDE; | |
54 | 43 |
55 scoped_ptr<AwContentsIoThreadClient> GetIoThreadClient() { | 44 namespace android_webview { |
56 return AwContentsIoThreadClient::FromID(child_id_, route_id_); | |
57 } | |
58 | 45 |
59 private: | 46 AwResourceDispatcherHostDelegate::PendingThrottleMap* |
60 int child_id_; | 47 AwResourceDispatcherHostDelegate::pending_throttles_ = 0; |
61 int route_id_; | 48 |
62 net::URLRequest* request_; | 49 IoThreadClientThrottle::IoThreadClientThrottle(int child_id, |
63 }; | 50 int route_id, |
51 net::URLRequest* request) | |
52 : child_id_(child_id), | |
53 route_id_(route_id), | |
54 request_(request) { } | |
55 | |
56 IoThreadClientThrottle::~IoThreadClientThrottle() { | |
joth
2012/11/29 21:02:33
It maybe best to do the map.erase(this) call from
benm (inactive)
2012/11/30 12:54:18
Done.
| |
57 } | |
64 | 58 |
65 void IoThreadClientThrottle::WillStartRequest(bool* defer) { | 59 void IoThreadClientThrottle::WillStartRequest(bool* defer) { |
66 // If there is no IO thread client set at this point, use a | 60 if (!MaybeDeferRequest(defer)) { |
67 // restrictive policy. This can happen for blocked popup | 61 MaybeBlockRequest(); |
68 // windows for example. | 62 } |
69 // TODO(benm): Revert this to a DCHECK when the we support | 63 } |
70 // pop up windows being created in the WebView, as at that | 64 |
71 // time we should always have an IoThreadClient at this | 65 void IoThreadClientThrottle::WillRedirectRequest(const GURL& new_url, |
72 // point (i.e., the one associated with the new popup). | 66 bool* defer) { |
73 if (!GetIoThreadClient()) { | 67 WillStartRequest(defer); |
68 } | |
69 | |
70 bool IoThreadClientThrottle::MaybeDeferRequest(bool* defer) { | |
71 scoped_ptr<AwContentsIoThreadClient> io_client = | |
72 AwContentsIoThreadClient::FromID(child_id_, route_id_); | |
73 *defer = false; | |
74 if (!io_client.get()) { | |
75 *defer = true; | |
76 AwResourceDispatcherHostDelegate::AddPendingThrottle( | |
77 child_id_, route_id_, this); | |
78 } | |
79 return *defer; | |
80 } | |
81 | |
82 void IoThreadClientThrottle::OnIoThreadClientReady(int new_child_id, | |
83 int new_route_id) { | |
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
85 | |
86 if (!MaybeBlockRequest()) { | |
87 controller()->Resume(); | |
88 } | |
89 } | |
90 | |
91 bool IoThreadClientThrottle::MaybeBlockRequest() { | |
92 if (ShouldBlockRequest()) { | |
74 controller()->CancelWithError(net::ERR_ACCESS_DENIED); | 93 controller()->CancelWithError(net::ERR_ACCESS_DENIED); |
75 return; | 94 return true; |
76 } | 95 } |
96 return false; | |
97 } | |
98 | |
99 bool IoThreadClientThrottle::ShouldBlockRequest() { | |
100 scoped_ptr<AwContentsIoThreadClient> io_client = | |
101 AwContentsIoThreadClient::FromID(child_id_, route_id_); | |
102 DCHECK(io_client.get()); | |
77 | 103 |
78 // Part of implementation of WebSettings.allowContentAccess. | 104 // Part of implementation of WebSettings.allowContentAccess. |
79 if (request_->url().SchemeIs(android_webview::kContentScheme) && | 105 if (request_->url().SchemeIs(android_webview::kContentScheme) && |
80 GetIoThreadClient()->ShouldBlockContentUrls()) { | 106 io_client->ShouldBlockContentUrls()) { |
81 controller()->CancelWithError(net::ERR_ACCESS_DENIED); | 107 return true; |
82 return; | |
83 } | 108 } |
84 | 109 |
85 // Part of implementation of WebSettings.allowFileAccess. | 110 // Part of implementation of WebSettings.allowFileAccess. |
86 if (request_->url().SchemeIsFile() && | 111 if (request_->url().SchemeIsFile() && |
87 GetIoThreadClient()->ShouldBlockFileUrls()) { | 112 io_client->ShouldBlockFileUrls()) { |
88 const GURL& url = request_->url(); | 113 const GURL& url = request_->url(); |
89 if (!url.has_path() || | 114 if (!url.has_path() || |
90 // Application's assets and resources are always available. | 115 // Application's assets and resources are always available. |
91 (url.path().find(android_webview::kAndroidResourcePath) != 0 && | 116 (url.path().find(android_webview::kAndroidResourcePath) != 0 && |
92 url.path().find(android_webview::kAndroidAssetPath) != 0)) { | 117 url.path().find(android_webview::kAndroidAssetPath) != 0)) { |
93 controller()->CancelWithError(net::ERR_ACCESS_DENIED); | 118 return true; |
94 return; | |
95 } | 119 } |
96 } | 120 } |
97 | 121 |
98 if (GetIoThreadClient()->ShouldBlockNetworkLoads()) { | 122 if (io_client->ShouldBlockNetworkLoads()) { |
99 if (request_->url().SchemeIs(chrome::kFtpScheme)) { | 123 if (request_->url().SchemeIs(chrome::kFtpScheme)) { |
100 controller()->CancelWithError(net::ERR_ACCESS_DENIED); | 124 return true; |
101 return; | |
102 } | 125 } |
103 SetCacheControlFlag(request_, net::LOAD_ONLY_FROM_CACHE); | 126 SetCacheControlFlag(request_, net::LOAD_ONLY_FROM_CACHE); |
104 } else { | 127 } else { |
105 AwContentsIoThreadClient::CacheMode cache_mode = | 128 AwContentsIoThreadClient::CacheMode cache_mode = |
106 GetIoThreadClient()->GetCacheMode(); | 129 GetIoThreadClient()->GetCacheMode(); |
107 switch(cache_mode) { | 130 switch(cache_mode) { |
108 case AwContentsIoThreadClient::LOAD_CACHE_ELSE_NETWORK: | 131 case AwContentsIoThreadClient::LOAD_CACHE_ELSE_NETWORK: |
109 SetCacheControlFlag(request_, net::LOAD_PREFERRING_CACHE); | 132 SetCacheControlFlag(request_, net::LOAD_PREFERRING_CACHE); |
110 break; | 133 break; |
111 case AwContentsIoThreadClient::LOAD_NO_CACHE: | 134 case AwContentsIoThreadClient::LOAD_NO_CACHE: |
112 SetCacheControlFlag(request_, net::LOAD_BYPASS_CACHE); | 135 SetCacheControlFlag(request_, net::LOAD_BYPASS_CACHE); |
113 break; | 136 break; |
114 case AwContentsIoThreadClient::LOAD_CACHE_ONLY: | 137 case AwContentsIoThreadClient::LOAD_CACHE_ONLY: |
115 SetCacheControlFlag(request_, net::LOAD_ONLY_FROM_CACHE); | 138 SetCacheControlFlag(request_, net::LOAD_ONLY_FROM_CACHE); |
116 break; | 139 break; |
117 default: | 140 default: |
118 break; | 141 break; |
119 } | 142 } |
120 } | 143 } |
144 return false; | |
121 } | 145 } |
122 | 146 |
123 } // namespace | 147 scoped_ptr<AwContentsIoThreadClient> |
124 | 148 IoThreadClientThrottle::GetIoThreadClient() { |
125 namespace android_webview { | 149 return AwContentsIoThreadClient::FromID(child_id_, route_id_); |
150 } | |
126 | 151 |
127 // static | 152 // static |
128 void AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated() { | 153 void AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated() { |
129 content::ResourceDispatcherHost::Get()->SetDelegate( | 154 content::ResourceDispatcherHost::Get()->SetDelegate( |
130 &g_webview_resource_dispatcher_host_delegate.Get()); | 155 &g_webview_resource_dispatcher_host_delegate.Get()); |
131 } | 156 } |
132 | 157 |
133 AwResourceDispatcherHostDelegate::AwResourceDispatcherHostDelegate() | 158 AwResourceDispatcherHostDelegate::AwResourceDispatcherHostDelegate() |
134 : content::ResourceDispatcherHostDelegate() { | 159 : content::ResourceDispatcherHostDelegate() { |
135 } | 160 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
186 | 211 |
187 bool AwResourceDispatcherHostDelegate::HandleExternalProtocol(const GURL& url, | 212 bool AwResourceDispatcherHostDelegate::HandleExternalProtocol(const GURL& url, |
188 int child_id, | 213 int child_id, |
189 int route_id) { | 214 int route_id) { |
190 // The AwURLRequestJobFactory implementation should ensure this method never | 215 // The AwURLRequestJobFactory implementation should ensure this method never |
191 // gets called. | 216 // gets called. |
192 NOTREACHED(); | 217 NOTREACHED(); |
193 return false; | 218 return false; |
194 } | 219 } |
195 | 220 |
221 // static | |
222 void AwResourceDispatcherHostDelegate::OnIoThreadClientReady( | |
223 int new_child_id, | |
224 int new_route_id) { | |
225 if (!pending_throttles_) | |
226 return; | |
227 | |
228 PendingThrottleMap::iterator it = pending_throttles_->find( | |
229 ChildRouteIDPair(new_child_id, new_route_id)); | |
230 | |
231 if (it != pending_throttles_->end()) { | |
232 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
233 base::Bind(&IoThreadClientThrottle::OnIoThreadClientReady, | |
234 base::Unretained(it->second), new_child_id, new_route_id)); | |
235 pending_throttles_->erase(it); | |
236 } | |
196 } | 237 } |
238 | |
239 // static | |
240 void AwResourceDispatcherHostDelegate::AddPendingThrottle( | |
241 int child_id, | |
242 int route_id, | |
243 IoThreadClientThrottle* pending_throttle) { | |
244 if (!pending_throttles_) { | |
245 pending_throttles_ = new PendingThrottleMap(); | |
246 } | |
247 | |
248 pending_throttles_->insert( | |
joth
2012/11/29 21:02:33
does this get run on IO thread? we're searching an
benm (inactive)
2012/11/30 12:54:18
Done.
| |
249 std::pair<ChildRouteIDPair, IoThreadClientThrottle*>( | |
250 ChildRouteIDPair(child_id, route_id), pending_throttle)); | |
251 } | |
252 | |
253 } // namespace android_webview | |
OLD | NEW |