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/aw_request_interceptor.h" |
| 6 | 6 |
| 7 #include "android_webview/browser/aw_contents_io_thread_client.h" | 7 #include "android_webview/browser/aw_request_handler.h" |
| 8 #include "android_webview/browser/aw_web_resource_response.h" | 8 #include "base/supports_user_data.h" |
| 9 #include "base/android/jni_string.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "content/public/browser/browser_thread.h" | 9 #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" | 10 #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 | 11 |
| 23 namespace android_webview { | 12 namespace android_webview { |
| 24 | 13 |
| 25 namespace { | 14 namespace { |
| 26 | 15 |
| 27 const void* const kRequestAlreadyQueriedDataKey = | 16 const void* const kRequestHandlerDataKey = &kRequestHandlerDataKey; |
| 28 &kRequestAlreadyQueriedDataKey; | |
| 29 | 17 |
| 30 } // namespace | 18 } // namespace |
| 31 | 19 |
| 32 AwRequestInterceptor::AwRequestInterceptor() { | 20 AwRequestInterceptor::AwRequestInterceptor() { |
| 33 } | 21 } |
| 34 | 22 |
| 35 AwRequestInterceptor::~AwRequestInterceptor() { | 23 AwRequestInterceptor::~AwRequestInterceptor() { |
| 36 } | 24 } |
| 37 | 25 |
| 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( | 26 net::URLRequestJob* AwRequestInterceptor::MaybeInterceptRequest( |
| 63 net::URLRequest* request, | 27 net::URLRequest* request, |
| 64 net::NetworkDelegate* network_delegate) const { | 28 net::NetworkDelegate* network_delegate) const { |
| 65 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 29 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 66 | 30 |
| 67 // See if we've already found out the aw_web_resource_response for this | 31 AwRequestHandler* handler = base::UserDataAdapter<AwRequestHandler>::Get( |
|
boliu
2015/09/28 23:26:28
nit: typedef
mnaganov (inactive)
2015/09/29 00:14:06
Done.
| |
| 68 // request. | 32 request, kRequestHandlerDataKey); |
| 69 // This is done not only for efficiency reasons, but also for correctness | 33 if (!handler) { |
| 70 // as it is possible for the Interceptor chain to be invoked more than once | 34 handler = new AwRequestHandler(); |
| 71 // in which case we don't want to query the embedder multiple times. | 35 request->SetUserData(kRequestHandlerDataKey, |
| 72 // Note: The Interceptor chain is not invoked more than once if we create a | 36 new base::UserDataAdapter<AwRequestHandler>(handler)); |
| 73 // URLRequestJob in this method, so this is only caching negative hits. | 37 } |
| 74 if (request->GetUserData(kRequestAlreadyQueriedDataKey)) | 38 return handler->MaybeInterceptRequest(request, network_delegate); |
| 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 } | 39 } |
| 89 | 40 |
| 90 } // namespace android_webview | 41 } // namespace android_webview |
| OLD | NEW |