Chromium Code Reviews| 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/aw_request_interceptor.h" | 5 #include "android_webview/browser/net/aw_request_interceptor.h" |
| 6 | 6 |
| 7 #include "android_webview/browser/aw_contents_io_thread_client.h" | 7 #include "android_webview/browser/aw_contents_io_thread_client.h" |
| 8 #include "android_webview/browser/aw_web_resource_response.h" | 8 #include "android_webview/browser/net/aw_stream_reader_job_delegate_impl.h" |
| 9 #include "base/android/jni_string.h" | 9 #include "base/supports_user_data.h" |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "content/public/browser/browser_thread.h" | 10 #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" | 11 #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" | 12 #include "net/url_request/url_request_job.h" |
| 18 | 13 |
| 19 using content::BrowserThread; | |
| 20 using content::RenderViewHost; | |
| 21 using content::ResourceRequestInfo; | |
| 22 | |
| 23 namespace android_webview { | 14 namespace android_webview { |
| 24 | 15 |
| 25 namespace { | 16 namespace { |
| 26 | 17 |
| 27 const void* const kRequestAlreadyQueriedDataKey = | 18 const void* const kRequestAlreadyHasJobDataKey = &kRequestAlreadyHasJobDataKey; |
| 28 &kRequestAlreadyQueriedDataKey; | |
| 29 | 19 |
| 30 } // namespace | 20 } // namespace |
| 31 | 21 |
| 32 AwRequestInterceptor::AwRequestInterceptor() { | 22 AwRequestInterceptor::AwRequestInterceptor() { |
| 33 } | 23 } |
| 34 | 24 |
| 35 AwRequestInterceptor::~AwRequestInterceptor() { | 25 AwRequestInterceptor::~AwRequestInterceptor() { |
| 36 } | 26 } |
| 37 | 27 |
| 38 scoped_ptr<AwWebResourceResponse> | 28 net::URLRequestJob* AwRequestInterceptor::MaybeInterceptRequest( |
| 39 AwRequestInterceptor::QueryForAwWebResourceResponse( | 29 net::URLRequest* request, |
| 40 net::URLRequest* request) const { | 30 net::NetworkDelegate* network_delegate) const { |
| 41 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 31 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 32 | |
| 33 // MaybeInterceptRequest can be called multiple times for the same request. | |
| 34 if (request->GetUserData(kRequestAlreadyHasJobDataKey)) | |
| 35 return nullptr; | |
| 36 | |
| 42 int render_process_id, render_frame_id; | 37 int render_process_id, render_frame_id; |
| 43 if (!ResourceRequestInfo::GetRenderFrameForRequest( | 38 if (!content::ResourceRequestInfo::GetRenderFrameForRequest( |
| 44 request, &render_process_id, &render_frame_id)) | 39 request, &render_process_id, &render_frame_id)) { |
| 45 return scoped_ptr<AwWebResourceResponse>(); | 40 return nullptr; |
| 41 } | |
| 46 | 42 |
| 47 scoped_ptr<AwContentsIoThreadClient> io_thread_client = | 43 scoped_ptr<AwContentsIoThreadClient> io_thread_client = |
| 48 AwContentsIoThreadClient::FromID(render_process_id, render_frame_id); | 44 AwContentsIoThreadClient::FromID(render_process_id, render_frame_id); |
| 49 | 45 |
| 50 if (!io_thread_client.get()) | 46 if (!io_thread_client) |
| 51 return scoped_ptr<AwWebResourceResponse>(); | 47 return nullptr; |
| 52 | 48 |
| 53 GURL referrer(request->referrer()); | 49 GURL referrer(request->referrer()); |
| 54 if (referrer.is_valid() && | 50 if (referrer.is_valid() && |
| 55 (!request->is_pending() || request->is_redirecting())) { | 51 (!request->is_pending() || request->is_redirecting())) { |
| 56 request->SetExtraRequestHeaderByName(net::HttpRequestHeaders::kReferer, | 52 request->SetExtraRequestHeaderByName(net::HttpRequestHeaders::kReferer, |
| 57 referrer.spec(), true); | 53 referrer.spec(), true); |
| 58 } | 54 } |
| 59 return io_thread_client->ShouldInterceptRequest(request).Pass(); | 55 AndroidStreamReaderURLRequestJob* job = |
| 60 } | 56 new AndroidStreamReaderURLRequestJob(request, network_delegate); |
| 61 | 57 request->SetUserData(kRequestAlreadyHasJobDataKey, |
| 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()); | 58 new base::SupportsUserData::Data()); |
| 78 | 59 io_thread_client->ShouldInterceptRequestAsync( |
| 79 scoped_ptr<AwWebResourceResponse> aw_web_resource_response = | 60 request, job->GetWebResourceResponseCallback()); |
|
mmenke
2015/09/30 21:13:34
Up to you, but I think this makes more sense in An
mnaganov (inactive)
2015/10/01 18:04:30
Thanks for suggestion! I've made that. This also a
| |
| 80 QueryForAwWebResourceResponse(request); | 61 return job; |
| 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 } | 62 } |
| 89 | 63 |
| 90 } // namespace android_webview | 64 } // namespace android_webview |
| OLD | NEW |