| 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/net/aw_request_interceptor.h" | 5 #include "android_webview/browser/net/aw_request_interceptor.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "android_webview/browser/aw_contents_io_thread_client.h" | 9 #include "android_webview/browser/aw_contents_io_thread_client.h" |
| 10 #include "android_webview/browser/input_stream.h" | 10 #include "android_webview/browser/input_stream.h" |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 } | 107 } |
| 108 } | 108 } |
| 109 | 109 |
| 110 scoped_ptr<AwContentsIoThreadClient> io_thread_client_; | 110 scoped_ptr<AwContentsIoThreadClient> io_thread_client_; |
| 111 Callback callback_; | 111 Callback callback_; |
| 112 base::WeakPtrFactory<ShouldInterceptRequestAdaptor> weak_factory_; | 112 base::WeakPtrFactory<ShouldInterceptRequestAdaptor> weak_factory_; |
| 113 | 113 |
| 114 DISALLOW_COPY_AND_ASSIGN(ShouldInterceptRequestAdaptor); | 114 DISALLOW_COPY_AND_ASSIGN(ShouldInterceptRequestAdaptor); |
| 115 }; | 115 }; |
| 116 | 116 |
| 117 scoped_ptr<AwContentsIoThreadClient> GetCorrespondingIoThreadClient( |
| 118 net::URLRequest* request) { |
| 119 int render_process_id, render_frame_id; |
| 120 if (!content::ResourceRequestInfo::GetRenderFrameForRequest( |
| 121 request, &render_process_id, &render_frame_id)) { |
| 122 // When there is no associated render frame, or the frame_id is |
| 123 // invalid we assume the request come from a service worker. |
| 124 // TODO: add a more explicit service worker check. |
| 125 return AwContentsIoThreadClient::GetServiceWorkerIoThreadClient(); |
| 126 } |
| 127 |
| 128 scoped_ptr<AwContentsIoThreadClient> io_thread_client = |
| 129 AwContentsIoThreadClient::FromID(render_process_id, render_frame_id); |
| 130 |
| 131 if (!io_thread_client) { |
| 132 // This means the frame id is invalid/not set, this currently happens |
| 133 // with e.g. fetch requests coming from service workers. |
| 134 return AwContentsIoThreadClient::GetServiceWorkerIoThreadClient(); |
| 135 } |
| 136 |
| 137 return io_thread_client; |
| 138 } |
| 139 |
| 117 } // namespace | 140 } // namespace |
| 118 | 141 |
| 119 AwRequestInterceptor::AwRequestInterceptor() {} | 142 AwRequestInterceptor::AwRequestInterceptor() {} |
| 120 | 143 |
| 121 AwRequestInterceptor::~AwRequestInterceptor() {} | 144 AwRequestInterceptor::~AwRequestInterceptor() {} |
| 122 | 145 |
| 123 net::URLRequestJob* AwRequestInterceptor::MaybeInterceptRequest( | 146 net::URLRequestJob* AwRequestInterceptor::MaybeInterceptRequest( |
| 124 net::URLRequest* request, | 147 net::URLRequest* request, |
| 125 net::NetworkDelegate* network_delegate) const { | 148 net::NetworkDelegate* network_delegate) const { |
| 126 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 149 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 127 | 150 |
| 128 // MaybeInterceptRequest can be called multiple times for the same request. | 151 // MaybeInterceptRequest can be called multiple times for the same request. |
| 129 if (request->GetUserData(kRequestAlreadyHasJobDataKey)) | 152 if (request->GetUserData(kRequestAlreadyHasJobDataKey)) |
| 130 return nullptr; | 153 return nullptr; |
| 131 | 154 |
| 132 int render_process_id, render_frame_id; | |
| 133 if (!content::ResourceRequestInfo::GetRenderFrameForRequest( | |
| 134 request, &render_process_id, &render_frame_id)) { | |
| 135 return nullptr; | |
| 136 } | |
| 137 | |
| 138 scoped_ptr<AwContentsIoThreadClient> io_thread_client = | 155 scoped_ptr<AwContentsIoThreadClient> io_thread_client = |
| 139 AwContentsIoThreadClient::FromID(render_process_id, render_frame_id); | 156 GetCorrespondingIoThreadClient(request); |
| 140 | 157 |
| 141 if (!io_thread_client) | 158 if (!io_thread_client) |
| 142 return nullptr; | 159 return nullptr; |
| 143 | 160 |
| 144 GURL referrer(request->referrer()); | 161 GURL referrer(request->referrer()); |
| 145 if (referrer.is_valid() && | 162 if (referrer.is_valid() && |
| 146 (!request->is_pending() || request->is_redirecting())) { | 163 (!request->is_pending() || request->is_redirecting())) { |
| 147 request->SetExtraRequestHeaderByName(net::HttpRequestHeaders::kReferer, | 164 request->SetExtraRequestHeaderByName(net::HttpRequestHeaders::kReferer, |
| 148 referrer.spec(), true); | 165 referrer.spec(), true); |
| 149 } | 166 } |
| 150 request->SetUserData(kRequestAlreadyHasJobDataKey, | 167 request->SetUserData(kRequestAlreadyHasJobDataKey, |
| 151 new base::SupportsUserData::Data()); | 168 new base::SupportsUserData::Data()); |
| 152 return new AndroidStreamReaderURLRequestJob( | 169 return new AndroidStreamReaderURLRequestJob( |
| 153 request, network_delegate, | 170 request, network_delegate, |
| 154 make_scoped_ptr( | 171 make_scoped_ptr( |
| 155 new ShouldInterceptRequestAdaptor(std::move(io_thread_client))), | 172 new ShouldInterceptRequestAdaptor(std::move(io_thread_client))), |
| 156 true); | 173 true); |
| 157 } | 174 } |
| 158 | 175 |
| 159 } // namespace android_webview | 176 } // namespace android_webview |
| OLD | NEW |