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" | 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/browser_thread.h" | |
13 #include "content/public/browser/resource_controller.h" | 14 #include "content/public/browser/resource_controller.h" |
14 #include "content/public/browser/resource_dispatcher_host.h" | 15 #include "content/public/browser/resource_dispatcher_host.h" |
15 #include "content/public/browser/resource_dispatcher_host_login_delegate.h" | 16 #include "content/public/browser/resource_dispatcher_host_login_delegate.h" |
16 #include "content/public/browser/resource_throttle.h" | 17 #include "content/public/browser/resource_throttle.h" |
17 #include "content/public/common/url_constants.h" | 18 #include "content/public/common/url_constants.h" |
18 #include "net/base/load_flags.h" | 19 #include "net/base/load_flags.h" |
19 #include "net/url_request/url_request.h" | 20 #include "net/url_request/url_request.h" |
20 | 21 |
21 using content::InterceptNavigationDelegate; | 22 using content::InterceptNavigationDelegate; |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 using android_webview::AwContentsIoThreadClient; | 26 using android_webview::AwContentsIoThreadClient; |
27 using content::BrowserThread; | |
26 | 28 |
27 base::LazyInstance<android_webview::AwResourceDispatcherHostDelegate> | 29 base::LazyInstance<android_webview::AwResourceDispatcherHostDelegate> |
28 g_webview_resource_dispatcher_host_delegate = LAZY_INSTANCE_INITIALIZER; | 30 g_webview_resource_dispatcher_host_delegate = LAZY_INSTANCE_INITIALIZER; |
29 | 31 |
30 void SetOnlyAllowLoadFromCache( | 32 void SetOnlyAllowLoadFromCache( |
31 net::URLRequest* request) { | 33 net::URLRequest* request) { |
32 int load_flags = request->load_flags(); | 34 int load_flags = request->load_flags(); |
33 load_flags &= ~(net::LOAD_BYPASS_CACHE & | 35 load_flags &= ~(net::LOAD_BYPASS_CACHE & |
34 net::LOAD_VALIDATE_CACHE & | 36 net::LOAD_VALIDATE_CACHE & |
35 net::LOAD_PREFERRING_CACHE); | 37 net::LOAD_PREFERRING_CACHE); |
36 load_flags |= net::LOAD_ONLY_FROM_CACHE; | 38 load_flags |= net::LOAD_ONLY_FROM_CACHE; |
37 request->set_load_flags(load_flags); | 39 request->set_load_flags(load_flags); |
38 } | 40 } |
39 | 41 |
40 // May cancel this resource request based on result of Java callbacks. | 42 // Calls through the IoThreadClient to check the embedders settings to determine |
41 class MaybeCancelResourceThrottle : public content::ResourceThrottle { | 43 // if the request should be cancelled. There may not always be an IoThreadClient |
44 // available for the |child_id|, |route_id| pair (in the case of newly created | |
45 // pop up windows, for example) and in that case the request and the client | |
46 // callbacks will be deferred the request until a client is ready. | |
47 class IoThreadClientThrottle | |
48 : public content::ResourceThrottle, | |
49 public AwContentsIoThreadClient::ReadyObserver { | |
42 public: | 50 public: |
43 MaybeCancelResourceThrottle(int child_id, | 51 IoThreadClientThrottle(int child_id, int route_id, net::URLRequest* request) |
44 int route_id, | 52 : child_id_(child_id), |
45 net::URLRequest* request) | 53 route_id_(route_id), |
46 : child_id_(child_id), | 54 request_(request), |
47 route_id_(route_id), | 55 handled_(false), |
48 request_(request) { } | 56 added_observer_(false) { |
49 virtual void WillStartRequest(bool* defer) OVERRIDE; | |
50 | |
51 scoped_ptr<AwContentsIoThreadClient> GetIoThreadClient() { | |
52 return AwContentsIoThreadClient::FromID(child_id_, route_id_); | |
53 } | 57 } |
54 | 58 |
55 private: | 59 virtual ~IoThreadClientThrottle() { |
60 if (added_observer_) { | |
61 AwContentsIoThreadClient::RemoveReadyObserver(this); | |
62 } | |
63 } | |
64 | |
65 virtual void WillStartRequest(bool* defer) OVERRIDE { | |
66 if (!MaybeDeferRequest(defer)) { | |
67 MaybeBlockRequest(); | |
68 } | |
69 } | |
70 | |
71 virtual void WillRedirectRequest(const GURL& new_url, bool* defer) OVERRIDE { | |
72 WillStartRequest(defer); | |
73 } | |
74 | |
75 bool MaybeDeferRequest(bool* defer) { | |
76 scoped_ptr<AwContentsIoThreadClient> io_client = | |
77 AwContentsIoThreadClient::FromID(child_id_, route_id_); | |
78 if (!io_client.get()) { | |
79 *defer = true; | |
80 AwContentsIoThreadClient::AddReadyObserver(this); | |
81 added_observer_ = true; | |
82 return true; | |
83 } else { | |
84 *defer = false; | |
85 } | |
86 return false; | |
87 } | |
88 | |
89 void OnIoThreadClientReady(int new_child_id, int new_route_id) { | |
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
91 // If we've now got the IoThreadClient that we were waiting on, | |
92 // resume the request. | |
93 if (!handled_ && new_child_id == child_id_ && new_route_id == route_id_) { | |
94 if (!MaybeBlockRequest()) { | |
95 controller()->Resume(); | |
96 handled_ = true; | |
97 } | |
98 } | |
joth
2012/11/16 21:52:57
add:
AwContentsIoThreadClient::RemoveReadyObserve
benm (inactive)
2012/11/28 20:00:05
This should all go away and become simpler in next
| |
99 } | |
100 | |
101 bool MaybeBlockRequest() { | |
102 if (ShouldBlockRequest()) { | |
103 controller()->CancelWithError(net::ERR_ACCESS_DENIED); | |
104 handled_ = true; | |
105 return true; | |
106 } | |
107 return false; | |
108 } | |
109 | |
110 bool ShouldBlockRequest() { | |
111 scoped_ptr<AwContentsIoThreadClient> io_client = | |
112 AwContentsIoThreadClient::FromID(child_id_, route_id_); | |
113 DCHECK(io_client.get()); | |
114 | |
115 // Part of implementation of WebSettings.allowContentAccess. | |
116 if (request_->url().SchemeIs(android_webview::kContentScheme) && | |
117 io_client->ShouldBlockContentUrls()) { | |
118 return true; | |
119 } | |
120 | |
121 // Part of implementation of WebSettings.allowFileAccess. | |
122 if (request_->url().SchemeIsFile() && | |
123 io_client->ShouldBlockFileUrls()) { | |
124 const GURL& url = request_->url(); | |
125 if (!url.has_path() || | |
126 // Application's assets and resources are always available. | |
127 (url.path().find(android_webview::kAndroidResourcePath) != 0 && | |
128 url.path().find(android_webview::kAndroidAssetPath) != 0)) { | |
129 return true; | |
130 } | |
131 } | |
132 | |
133 if (io_client->ShouldBlockNetworkLoads()) { | |
134 if (request_->url().SchemeIs(chrome::kFtpScheme)) { | |
135 return true; | |
136 } | |
137 SetOnlyAllowLoadFromCache(request_); | |
138 } | |
139 | |
140 return false; | |
141 } | |
142 | |
143 private: | |
56 int child_id_; | 144 int child_id_; |
57 int route_id_; | 145 int route_id_; |
58 net::URLRequest* request_; | 146 net::URLRequest* request_; |
147 bool handled_; | |
148 bool added_observer_; | |
59 }; | 149 }; |
60 | 150 |
61 void MaybeCancelResourceThrottle::WillStartRequest(bool* defer) { | |
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 } | |
101 } | |
102 | |
103 } // namespace | 151 } // namespace |
104 | 152 |
105 namespace android_webview { | 153 namespace android_webview { |
106 | 154 |
107 // static | 155 // static |
108 void AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated() { | 156 void AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated() { |
109 content::ResourceDispatcherHost::Get()->SetDelegate( | 157 content::ResourceDispatcherHost::Get()->SetDelegate( |
110 &g_webview_resource_dispatcher_host_delegate.Get()); | 158 &g_webview_resource_dispatcher_host_delegate.Get()); |
111 } | 159 } |
112 | 160 |
113 AwResourceDispatcherHostDelegate::AwResourceDispatcherHostDelegate() | 161 AwResourceDispatcherHostDelegate::AwResourceDispatcherHostDelegate() |
114 : content::ResourceDispatcherHostDelegate() { | 162 : content::ResourceDispatcherHostDelegate() { |
115 } | 163 } |
116 | 164 |
117 AwResourceDispatcherHostDelegate::~AwResourceDispatcherHostDelegate() { | 165 AwResourceDispatcherHostDelegate::~AwResourceDispatcherHostDelegate() { |
118 } | 166 } |
119 | 167 |
120 void AwResourceDispatcherHostDelegate::RequestBeginning( | 168 void AwResourceDispatcherHostDelegate::RequestBeginning( |
121 net::URLRequest* request, | 169 net::URLRequest* request, |
122 content::ResourceContext* resource_context, | 170 content::ResourceContext* resource_context, |
123 appcache::AppCacheService* appcache_service, | 171 appcache::AppCacheService* appcache_service, |
124 ResourceType::Type resource_type, | 172 ResourceType::Type resource_type, |
125 int child_id, | 173 int child_id, |
126 int route_id, | 174 int route_id, |
127 bool is_continuation_of_transferred_request, | 175 bool is_continuation_of_transferred_request, |
128 ScopedVector<content::ResourceThrottle>* throttles) { | 176 ScopedVector<content::ResourceThrottle>* throttles) { |
129 | 177 |
130 throttles->push_back(new MaybeCancelResourceThrottle( | 178 throttles->push_back(new IoThreadClientThrottle(child_id, route_id, request)); |
131 child_id, route_id, request)); | |
132 | 179 |
133 if (resource_type == ResourceType::MAIN_FRAME) { | 180 if (resource_type == ResourceType::MAIN_FRAME) { |
134 throttles->push_back(InterceptNavigationDelegate::CreateThrottleFor( | 181 throttles->push_back(InterceptNavigationDelegate::CreateThrottleFor( |
135 request)); | 182 request)); |
136 } | 183 } |
137 } | 184 } |
138 | 185 |
139 bool AwResourceDispatcherHostDelegate::AcceptAuthRequest( | 186 bool AwResourceDispatcherHostDelegate::AcceptAuthRequest( |
140 net::URLRequest* request, | 187 net::URLRequest* request, |
141 net::AuthChallengeInfo* auth_info) { | 188 net::AuthChallengeInfo* auth_info) { |
(...skipping 10 matching lines...) Expand all Loading... | |
152 bool AwResourceDispatcherHostDelegate::HandleExternalProtocol(const GURL& url, | 199 bool AwResourceDispatcherHostDelegate::HandleExternalProtocol(const GURL& url, |
153 int child_id, | 200 int child_id, |
154 int route_id) { | 201 int route_id) { |
155 // The AwURLRequestJobFactory implementation should ensure this method never | 202 // The AwURLRequestJobFactory implementation should ensure this method never |
156 // gets called. | 203 // gets called. |
157 NOTREACHED(); | 204 NOTREACHED(); |
158 return false; | 205 return false; |
159 } | 206 } |
160 | 207 |
161 } | 208 } |
OLD | NEW |