We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / android_webview / browser / renderer_host / aw_resource_dispatcher_host_delegate.cc
blob9b1e8dc93386f0a072da4720f319a555af0656b4
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
3 // found in the LICENSE file.
5 #include "android_webview/browser/renderer_host/aw_resource_dispatcher_host_delegate.h"
7 #include <string>
9 #include "android_webview/browser/aw_contents_io_thread_client.h"
10 #include "android_webview/browser/aw_login_delegate.h"
11 #include "android_webview/browser/aw_resource_context.h"
12 #include "android_webview/common/url_constants.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "components/auto_login_parser/auto_login_parser.h"
16 #include "components/navigation_interception/intercept_navigation_delegate.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/resource_controller.h"
19 #include "content/public/browser/resource_dispatcher_host.h"
20 #include "content/public/browser/resource_dispatcher_host_login_delegate.h"
21 #include "content/public/browser/resource_request_info.h"
22 #include "content/public/browser/resource_throttle.h"
23 #include "net/base/load_flags.h"
24 #include "net/base/net_errors.h"
25 #include "net/http/http_response_headers.h"
26 #include "net/url_request/url_request.h"
27 #include "net/url_request/url_request_status.h"
28 #include "url/url_constants.h"
30 using android_webview::AwContentsIoThreadClient;
31 using content::BrowserThread;
32 using content::ResourceType;
33 using navigation_interception::InterceptNavigationDelegate;
35 namespace {
37 base::LazyInstance<android_webview::AwResourceDispatcherHostDelegate>
38 g_webview_resource_dispatcher_host_delegate = LAZY_INSTANCE_INITIALIZER;
40 void SetCacheControlFlag(
41 net::URLRequest* request, int flag) {
42 const int all_cache_control_flags = net::LOAD_BYPASS_CACHE |
43 net::LOAD_VALIDATE_CACHE |
44 net::LOAD_PREFERRING_CACHE |
45 net::LOAD_ONLY_FROM_CACHE;
46 DCHECK_EQ((flag & all_cache_control_flags), flag);
47 int load_flags = request->load_flags();
48 load_flags &= ~all_cache_control_flags;
49 load_flags |= flag;
50 request->SetLoadFlags(load_flags);
53 } // namespace
55 namespace android_webview {
57 // Calls through the IoThreadClient to check the embedders settings to determine
58 // if the request should be cancelled. There may not always be an IoThreadClient
59 // available for the |render_process_id|, |render_frame_id| pair (in the case of
60 // newly created pop up windows, for example) and in that case the request and
61 // the client callbacks will be deferred the request until a client is ready.
62 class IoThreadClientThrottle : public content::ResourceThrottle {
63 public:
64 IoThreadClientThrottle(int render_process_id,
65 int render_frame_id,
66 net::URLRequest* request);
67 ~IoThreadClientThrottle() override;
69 // From content::ResourceThrottle
70 void WillStartRequest(bool* defer) override;
71 void WillRedirectRequest(const net::RedirectInfo& redirect_info,
72 bool* defer) override;
73 const char* GetNameForLogging() const override;
75 void OnIoThreadClientReady(int new_render_process_id,
76 int new_render_frame_id);
77 bool MaybeBlockRequest();
78 bool ShouldBlockRequest();
79 int render_process_id() const { return render_process_id_; }
80 int render_frame_id() const { return render_frame_id_; }
82 private:
83 int render_process_id_;
84 int render_frame_id_;
85 net::URLRequest* request_;
88 IoThreadClientThrottle::IoThreadClientThrottle(int render_process_id,
89 int render_frame_id,
90 net::URLRequest* request)
91 : render_process_id_(render_process_id),
92 render_frame_id_(render_frame_id),
93 request_(request) { }
95 IoThreadClientThrottle::~IoThreadClientThrottle() {
96 DCHECK_CURRENTLY_ON(BrowserThread::IO);
97 g_webview_resource_dispatcher_host_delegate.Get().
98 RemovePendingThrottleOnIoThread(this);
101 const char* IoThreadClientThrottle::GetNameForLogging() const {
102 return "IoThreadClientThrottle";
105 void IoThreadClientThrottle::WillStartRequest(bool* defer) {
106 DCHECK_CURRENTLY_ON(BrowserThread::IO);
107 if (render_frame_id_ < 1)
108 return;
109 DCHECK(render_process_id_);
110 *defer = false;
112 // Defer all requests of a pop up that is still not associated with Java
113 // client so that the client will get a chance to override requests.
114 scoped_ptr<AwContentsIoThreadClient> io_client =
115 AwContentsIoThreadClient::FromID(render_process_id_, render_frame_id_);
116 if (io_client && io_client->PendingAssociation()) {
117 *defer = true;
118 AwResourceDispatcherHostDelegate::AddPendingThrottle(
119 render_process_id_, render_frame_id_, this);
120 } else {
121 MaybeBlockRequest();
125 void IoThreadClientThrottle::WillRedirectRequest(
126 const net::RedirectInfo& redirect_info,
127 bool* defer) {
128 WillStartRequest(defer);
131 void IoThreadClientThrottle::OnIoThreadClientReady(int new_render_process_id,
132 int new_render_frame_id) {
133 DCHECK_CURRENTLY_ON(BrowserThread::IO);
135 if (!MaybeBlockRequest()) {
136 controller()->Resume();
140 bool IoThreadClientThrottle::MaybeBlockRequest() {
141 if (ShouldBlockRequest()) {
142 controller()->CancelWithError(net::ERR_ACCESS_DENIED);
143 return true;
145 return false;
148 bool IoThreadClientThrottle::ShouldBlockRequest() {
149 scoped_ptr<AwContentsIoThreadClient> io_client =
150 AwContentsIoThreadClient::FromID(render_process_id_, render_frame_id_);
151 if (!io_client)
152 return false;
154 // Part of implementation of WebSettings.allowContentAccess.
155 if (request_->url().SchemeIs(android_webview::kContentScheme) &&
156 io_client->ShouldBlockContentUrls()) {
157 return true;
160 // Part of implementation of WebSettings.allowFileAccess.
161 if (request_->url().SchemeIsFile() &&
162 io_client->ShouldBlockFileUrls()) {
163 const GURL& url = request_->url();
164 if (!url.has_path() ||
165 // Application's assets and resources are always available.
166 (url.path().find(android_webview::kAndroidResourcePath) != 0 &&
167 url.path().find(android_webview::kAndroidAssetPath) != 0)) {
168 return true;
172 if (io_client->ShouldBlockNetworkLoads()) {
173 if (request_->url().SchemeIs(url::kFtpScheme)) {
174 return true;
176 SetCacheControlFlag(request_, net::LOAD_ONLY_FROM_CACHE);
177 } else {
178 AwContentsIoThreadClient::CacheMode cache_mode = io_client->GetCacheMode();
179 switch(cache_mode) {
180 case AwContentsIoThreadClient::LOAD_CACHE_ELSE_NETWORK:
181 SetCacheControlFlag(request_, net::LOAD_PREFERRING_CACHE);
182 break;
183 case AwContentsIoThreadClient::LOAD_NO_CACHE:
184 SetCacheControlFlag(request_, net::LOAD_BYPASS_CACHE);
185 break;
186 case AwContentsIoThreadClient::LOAD_CACHE_ONLY:
187 SetCacheControlFlag(request_, net::LOAD_ONLY_FROM_CACHE);
188 break;
189 default:
190 break;
193 return false;
196 // static
197 void AwResourceDispatcherHostDelegate::ResourceDispatcherHostCreated() {
198 content::ResourceDispatcherHost::Get()->SetDelegate(
199 &g_webview_resource_dispatcher_host_delegate.Get());
202 AwResourceDispatcherHostDelegate::AwResourceDispatcherHostDelegate()
203 : content::ResourceDispatcherHostDelegate() {
206 AwResourceDispatcherHostDelegate::~AwResourceDispatcherHostDelegate() {
209 void AwResourceDispatcherHostDelegate::RequestBeginning(
210 net::URLRequest* request,
211 content::ResourceContext* resource_context,
212 content::AppCacheService* appcache_service,
213 ResourceType resource_type,
214 ScopedVector<content::ResourceThrottle>* throttles) {
216 AddExtraHeadersIfNeeded(request, resource_context);
218 const content::ResourceRequestInfo* request_info =
219 content::ResourceRequestInfo::ForRequest(request);
221 // We always push the throttles here. Checking the existence of io_client
222 // is racy when a popup window is created. That is because RequestBeginning
223 // is called whether or not requests are blocked via BlockRequestForRoute()
224 // however io_client may or may not be ready at the time depending on whether
225 // webcontents is created.
226 throttles->push_back(new IoThreadClientThrottle(
227 request_info->GetChildID(), request_info->GetRenderFrameID(), request));
229 // We allow intercepting only navigations within main frames. This
230 // is used to post onPageStarted. We handle shouldOverrideUrlLoading
231 // via a sync IPC.
232 if (resource_type == content::RESOURCE_TYPE_MAIN_FRAME) {
233 throttles->push_back(InterceptNavigationDelegate::CreateThrottleFor(
234 request));
235 } else if (resource_type == content::RESOURCE_TYPE_XHR) {
236 InterceptNavigationDelegate::UpdateUserGestureCarryoverInfo(request);
240 void AwResourceDispatcherHostDelegate::OnRequestRedirected(
241 const GURL& redirect_url,
242 net::URLRequest* request,
243 content::ResourceContext* resource_context,
244 content::ResourceResponse* response) {
245 AddExtraHeadersIfNeeded(request, resource_context);
248 void AwResourceDispatcherHostDelegate::RequestComplete(
249 net::URLRequest* request) {
250 if (request && !request->status().is_success()) {
251 const content::ResourceRequestInfo* request_info =
252 content::ResourceRequestInfo::ForRequest(request);
253 scoped_ptr<AwContentsIoThreadClient> io_client =
254 AwContentsIoThreadClient::FromID(request_info->GetChildID(),
255 request_info->GetRenderFrameID());
256 if (io_client) {
257 io_client->OnReceivedError(request);
258 } else {
259 DLOG(WARNING) << "io_client is null, onReceivedError dropped for " <<
260 request->url();
266 void AwResourceDispatcherHostDelegate::DownloadStarting(
267 net::URLRequest* request,
268 content::ResourceContext* resource_context,
269 int child_id,
270 int route_id,
271 int request_id,
272 bool is_content_initiated,
273 bool must_download,
274 ScopedVector<content::ResourceThrottle>* throttles) {
275 GURL url(request->url());
276 std::string user_agent;
277 std::string content_disposition;
278 std::string mime_type;
279 int64 content_length = request->GetExpectedContentSize();
281 request->extra_request_headers().GetHeader(
282 net::HttpRequestHeaders::kUserAgent, &user_agent);
285 net::HttpResponseHeaders* response_headers = request->response_headers();
286 if (response_headers) {
287 response_headers->GetNormalizedHeader("content-disposition",
288 &content_disposition);
289 response_headers->GetMimeType(&mime_type);
292 request->Cancel();
294 const content::ResourceRequestInfo* request_info =
295 content::ResourceRequestInfo::ForRequest(request);
297 scoped_ptr<AwContentsIoThreadClient> io_client =
298 AwContentsIoThreadClient::FromID(
299 child_id, request_info->GetRenderFrameID());
301 // POST request cannot be repeated in general, so prevent client from
302 // retrying the same request, even if it is with a GET.
303 if ("GET" == request->method() && io_client) {
304 io_client->NewDownload(url,
305 user_agent,
306 content_disposition,
307 mime_type,
308 content_length);
312 content::ResourceDispatcherHostLoginDelegate*
313 AwResourceDispatcherHostDelegate::CreateLoginDelegate(
314 net::AuthChallengeInfo* auth_info,
315 net::URLRequest* request) {
316 return new AwLoginDelegate(auth_info, request);
319 bool AwResourceDispatcherHostDelegate::HandleExternalProtocol(const GURL& url,
320 int child_id,
321 int route_id) {
322 // The AwURLRequestJobFactory implementation should ensure this method never
323 // gets called.
324 NOTREACHED();
325 return false;
328 void AwResourceDispatcherHostDelegate::OnResponseStarted(
329 net::URLRequest* request,
330 content::ResourceContext* resource_context,
331 content::ResourceResponse* response,
332 IPC::Sender* sender) {
333 const content::ResourceRequestInfo* request_info =
334 content::ResourceRequestInfo::ForRequest(request);
335 if (!request_info) {
336 DLOG(FATAL) << "Started request without associated info: " <<
337 request->url();
338 return;
341 if (request_info->GetResourceType() == content::RESOURCE_TYPE_MAIN_FRAME) {
342 // Check for x-auto-login header.
343 auto_login_parser::HeaderData header_data;
344 if (auto_login_parser::ParserHeaderInResponse(
345 request, auto_login_parser::ALLOW_ANY_REALM, &header_data)) {
346 scoped_ptr<AwContentsIoThreadClient> io_client =
347 AwContentsIoThreadClient::FromID(request_info->GetChildID(),
348 request_info->GetRenderFrameID());
349 if (io_client) {
350 io_client->NewLoginRequest(
351 header_data.realm, header_data.account, header_data.args);
357 void AwResourceDispatcherHostDelegate::RemovePendingThrottleOnIoThread(
358 IoThreadClientThrottle* throttle) {
359 DCHECK_CURRENTLY_ON(BrowserThread::IO);
360 PendingThrottleMap::iterator it = pending_throttles_.find(
361 FrameRouteIDPair(throttle->render_process_id(),
362 throttle->render_frame_id()));
363 if (it != pending_throttles_.end()) {
364 pending_throttles_.erase(it);
368 // static
369 void AwResourceDispatcherHostDelegate::OnIoThreadClientReady(
370 int new_render_process_id,
371 int new_render_frame_id) {
372 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
373 base::Bind(
374 &AwResourceDispatcherHostDelegate::OnIoThreadClientReadyInternal,
375 base::Unretained(
376 g_webview_resource_dispatcher_host_delegate.Pointer()),
377 new_render_process_id, new_render_frame_id));
380 // static
381 void AwResourceDispatcherHostDelegate::AddPendingThrottle(
382 int render_process_id,
383 int render_frame_id,
384 IoThreadClientThrottle* pending_throttle) {
385 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
386 base::Bind(
387 &AwResourceDispatcherHostDelegate::AddPendingThrottleOnIoThread,
388 base::Unretained(
389 g_webview_resource_dispatcher_host_delegate.Pointer()),
390 render_process_id, render_frame_id, pending_throttle));
393 void AwResourceDispatcherHostDelegate::AddPendingThrottleOnIoThread(
394 int render_process_id,
395 int render_frame_id_id,
396 IoThreadClientThrottle* pending_throttle) {
397 DCHECK_CURRENTLY_ON(BrowserThread::IO);
398 pending_throttles_.insert(
399 std::pair<FrameRouteIDPair, IoThreadClientThrottle*>(
400 FrameRouteIDPair(render_process_id, render_frame_id_id),
401 pending_throttle));
404 void AwResourceDispatcherHostDelegate::OnIoThreadClientReadyInternal(
405 int new_render_process_id,
406 int new_render_frame_id) {
407 DCHECK_CURRENTLY_ON(BrowserThread::IO);
408 PendingThrottleMap::iterator it = pending_throttles_.find(
409 FrameRouteIDPair(new_render_process_id, new_render_frame_id));
411 if (it != pending_throttles_.end()) {
412 IoThreadClientThrottle* throttle = it->second;
413 throttle->OnIoThreadClientReady(new_render_process_id, new_render_frame_id);
414 pending_throttles_.erase(it);
418 void AwResourceDispatcherHostDelegate::AddExtraHeadersIfNeeded(
419 net::URLRequest* request,
420 content::ResourceContext* resource_context) {
421 const content::ResourceRequestInfo* request_info =
422 content::ResourceRequestInfo::ForRequest(request);
423 if (!request_info)
424 return;
425 if (request_info->GetResourceType() != content::RESOURCE_TYPE_MAIN_FRAME)
426 return;
428 const ui::PageTransition transition = request_info->GetPageTransition();
429 const bool is_load_url =
430 transition & ui::PAGE_TRANSITION_FROM_API;
431 const bool is_go_back_forward =
432 transition & ui::PAGE_TRANSITION_FORWARD_BACK;
433 const bool is_reload = ui::PageTransitionCoreTypeIs(
434 transition, ui::PAGE_TRANSITION_RELOAD);
435 if (is_load_url || is_go_back_forward || is_reload) {
436 AwResourceContext* awrc = static_cast<AwResourceContext*>(resource_context);
437 std::string extra_headers = awrc->GetExtraHeaders(request->url());
438 if (!extra_headers.empty()) {
439 net::HttpRequestHeaders headers;
440 headers.AddHeadersFromString(extra_headers);
441 for (net::HttpRequestHeaders::Iterator it(headers); it.GetNext(); ) {
442 request->SetExtraRequestHeaderByName(it.name(), it.value(), false);
448 } // namespace android_webview