Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / android_webview / browser / aw_request_interceptor.cc
blobf646e1fdeeb77497f66d3b624ad38d7d756940c3
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/aw_request_interceptor.h"
7 #include "android_webview/browser/aw_contents_io_thread_client.h"
8 #include "android_webview/browser/aw_web_resource_response.h"
9 #include "base/android/jni_string.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/render_view_host.h"
13 #include "content/public/browser/resource_request_info.h"
14 #include "net/url_request/url_request.h"
15 #include "net/url_request/url_request_context.h"
16 #include "net/url_request/url_request_context_getter.h"
17 #include "net/url_request/url_request_job.h"
19 using content::BrowserThread;
20 using content::RenderViewHost;
21 using content::ResourceRequestInfo;
23 namespace android_webview {
25 namespace {
27 const void* kRequestAlreadyQueriedDataKey = &kRequestAlreadyQueriedDataKey;
29 } // namespace
31 AwRequestInterceptor::AwRequestInterceptor() {
34 AwRequestInterceptor::~AwRequestInterceptor() {
37 scoped_ptr<AwWebResourceResponse>
38 AwRequestInterceptor::QueryForAwWebResourceResponse(
39 net::URLRequest* request) const {
40 DCHECK_CURRENTLY_ON(BrowserThread::IO);
41 int render_process_id, render_frame_id;
42 if (!ResourceRequestInfo::GetRenderFrameForRequest(
43 request, &render_process_id, &render_frame_id))
44 return scoped_ptr<AwWebResourceResponse>();
46 scoped_ptr<AwContentsIoThreadClient> io_thread_client =
47 AwContentsIoThreadClient::FromID(render_process_id, render_frame_id);
49 if (!io_thread_client.get())
50 return scoped_ptr<AwWebResourceResponse>();
52 GURL referrer(request->referrer());
53 if (referrer.is_valid() &&
54 (!request->is_pending() || request->is_redirecting())) {
55 request->SetExtraRequestHeaderByName(net::HttpRequestHeaders::kReferer,
56 referrer.spec(), true);
58 return io_thread_client->ShouldInterceptRequest(request).Pass();
61 net::URLRequestJob* AwRequestInterceptor::MaybeInterceptRequest(
62 net::URLRequest* request,
63 net::NetworkDelegate* network_delegate) const {
64 DCHECK_CURRENTLY_ON(BrowserThread::IO);
66 // See if we've already found out the aw_web_resource_response for this
67 // request.
68 // This is done not only for efficiency reasons, but also for correctness
69 // as it is possible for the Interceptor chain to be invoked more than once
70 // in which case we don't want to query the embedder multiple times.
71 // Note: The Interceptor chain is not invoked more than once if we create a
72 // URLRequestJob in this method, so this is only caching negative hits.
73 if (request->GetUserData(kRequestAlreadyQueriedDataKey))
74 return NULL;
75 request->SetUserData(kRequestAlreadyQueriedDataKey,
76 new base::SupportsUserData::Data());
78 scoped_ptr<AwWebResourceResponse> aw_web_resource_response =
79 QueryForAwWebResourceResponse(request);
81 if (!aw_web_resource_response)
82 return NULL;
84 // The newly created job will own the AwWebResourceResponse.
85 return AwWebResourceResponse::CreateJobFor(
86 aw_web_resource_response.Pass(), request, network_delegate);
89 } // namespace android_webview