| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "android_webview/browser/aw_request_interceptor.h" | |
| 6 | |
| 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" | |
| 18 | |
| 19 using content::BrowserThread; | |
| 20 using content::RenderViewHost; | |
| 21 using content::ResourceRequestInfo; | |
| 22 | |
| 23 namespace android_webview { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 const void* const kRequestAlreadyQueriedDataKey = | |
| 28 &kRequestAlreadyQueriedDataKey; | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 AwRequestInterceptor::AwRequestInterceptor() { | |
| 33 } | |
| 34 | |
| 35 AwRequestInterceptor::~AwRequestInterceptor() { | |
| 36 } | |
| 37 | |
| 38 scoped_ptr<AwWebResourceResponse> | |
| 39 AwRequestInterceptor::QueryForAwWebResourceResponse( | |
| 40 net::URLRequest* request) const { | |
| 41 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 42 int render_process_id, render_frame_id; | |
| 43 if (!ResourceRequestInfo::GetRenderFrameForRequest( | |
| 44 request, &render_process_id, &render_frame_id)) | |
| 45 return scoped_ptr<AwWebResourceResponse>(); | |
| 46 | |
| 47 scoped_ptr<AwContentsIoThreadClient> io_thread_client = | |
| 48 AwContentsIoThreadClient::FromID(render_process_id, render_frame_id); | |
| 49 | |
| 50 if (!io_thread_client.get()) | |
| 51 return scoped_ptr<AwWebResourceResponse>(); | |
| 52 | |
| 53 GURL referrer(request->referrer()); | |
| 54 if (referrer.is_valid() && | |
| 55 (!request->is_pending() || request->is_redirecting())) { | |
| 56 request->SetExtraRequestHeaderByName(net::HttpRequestHeaders::kReferer, | |
| 57 referrer.spec(), true); | |
| 58 } | |
| 59 return io_thread_client->ShouldInterceptRequest(request).Pass(); | |
| 60 } | |
| 61 | |
| 62 net::URLRequestJob* AwRequestInterceptor::MaybeInterceptRequest( | |
| 63 net::URLRequest* request, | |
| 64 net::NetworkDelegate* network_delegate) const { | |
| 65 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 66 | |
| 67 // See if we've already found out the aw_web_resource_response for this | |
| 68 // request. | |
| 69 // This is done not only for efficiency reasons, but also for correctness | |
| 70 // as it is possible for the Interceptor chain to be invoked more than once | |
| 71 // in which case we don't want to query the embedder multiple times. | |
| 72 // Note: The Interceptor chain is not invoked more than once if we create a | |
| 73 // URLRequestJob in this method, so this is only caching negative hits. | |
| 74 if (request->GetUserData(kRequestAlreadyQueriedDataKey)) | |
| 75 return NULL; | |
| 76 request->SetUserData(kRequestAlreadyQueriedDataKey, | |
| 77 new base::SupportsUserData::Data()); | |
| 78 | |
| 79 scoped_ptr<AwWebResourceResponse> aw_web_resource_response = | |
| 80 QueryForAwWebResourceResponse(request); | |
| 81 | |
| 82 if (!aw_web_resource_response) | |
| 83 return NULL; | |
| 84 | |
| 85 // The newly created job will own the AwWebResourceResponse. | |
| 86 return AwWebResourceResponse::CreateJobFor( | |
| 87 aw_web_resource_response.Pass(), request, network_delegate); | |
| 88 } | |
| 89 | |
| 90 } // namespace android_webview | |
| OLD | NEW |