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" |
11 #include "android_webview/browser/net/android_stream_reader_url_request_job.h" | 11 #include "android_webview/browser/net/android_stream_reader_url_request_job.h" |
12 #include "android_webview/browser/net/aw_web_resource_response.h" | 12 #include "android_webview/browser/net/aw_web_resource_response.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
16 #include "base/supports_user_data.h" | 16 #include "base/supports_user_data.h" |
17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
18 #include "content/public/browser/resource_request_info.h" | 18 #include "content/public/browser/resource_request_info.h" |
19 #include "net/http/http_response_headers.h" | 19 #include "net/http/http_response_headers.h" |
20 #include "net/url_request/url_request_job.h" | 20 #include "net/url_request/url_request_job.h" |
| 21 #include "url/url_constants.h" |
21 | 22 |
22 namespace android_webview { | 23 namespace android_webview { |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 const void* const kRequestAlreadyHasJobDataKey = &kRequestAlreadyHasJobDataKey; | 27 const void* const kRequestAlreadyHasJobDataKey = &kRequestAlreadyHasJobDataKey; |
27 | 28 |
28 class StreamReaderJobDelegateImpl | 29 class StreamReaderJobDelegateImpl |
29 : public AndroidStreamReaderURLRequestJob::Delegate { | 30 : public AndroidStreamReaderURLRequestJob::Delegate { |
30 public: | 31 public: |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 Callback callback_; | 113 Callback callback_; |
113 base::WeakPtrFactory<ShouldInterceptRequestAdaptor> weak_factory_; | 114 base::WeakPtrFactory<ShouldInterceptRequestAdaptor> weak_factory_; |
114 | 115 |
115 DISALLOW_COPY_AND_ASSIGN(ShouldInterceptRequestAdaptor); | 116 DISALLOW_COPY_AND_ASSIGN(ShouldInterceptRequestAdaptor); |
116 }; | 117 }; |
117 | 118 |
118 std::unique_ptr<AwContentsIoThreadClient> GetCorrespondingIoThreadClient( | 119 std::unique_ptr<AwContentsIoThreadClient> GetCorrespondingIoThreadClient( |
119 net::URLRequest* request) { | 120 net::URLRequest* request) { |
120 if (content::ResourceRequestInfo::OriginatedFromServiceWorker(request)) | 121 if (content::ResourceRequestInfo::OriginatedFromServiceWorker(request)) |
121 return AwContentsIoThreadClient::GetServiceWorkerIoThreadClient(); | 122 return AwContentsIoThreadClient::GetServiceWorkerIoThreadClient(); |
122 | |
123 int render_process_id, render_frame_id; | 123 int render_process_id, render_frame_id; |
124 if (!content::ResourceRequestInfo::GetRenderFrameForRequest( | 124 if (!content::ResourceRequestInfo::GetRenderFrameForRequest( |
125 request, &render_process_id, &render_frame_id)) { | 125 request, &render_process_id, &render_frame_id)) { |
126 return nullptr; | 126 return nullptr; |
127 } | 127 } |
128 | 128 |
| 129 if (render_process_id == -1 || render_frame_id == -1) { |
| 130 const content::ResourceRequestInfo* resourceRequestInfo = |
| 131 content::ResourceRequestInfo::ForRequest(request); |
| 132 if (resourceRequestInfo == nullptr) { |
| 133 return nullptr; |
| 134 } |
| 135 return AwContentsIoThreadClient::FromID( |
| 136 resourceRequestInfo->GetFrameTreeNodeId()); |
| 137 } |
| 138 |
129 return AwContentsIoThreadClient::FromID(render_process_id, render_frame_id); | 139 return AwContentsIoThreadClient::FromID(render_process_id, render_frame_id); |
130 } | 140 } |
131 | 141 |
132 } // namespace | 142 } // namespace |
133 | 143 |
134 AwRequestInterceptor::AwRequestInterceptor() {} | 144 AwRequestInterceptor::AwRequestInterceptor() {} |
135 | 145 |
136 AwRequestInterceptor::~AwRequestInterceptor() {} | 146 AwRequestInterceptor::~AwRequestInterceptor() {} |
137 | 147 |
138 net::URLRequestJob* AwRequestInterceptor::MaybeInterceptRequest( | 148 net::URLRequestJob* AwRequestInterceptor::MaybeInterceptRequest( |
139 net::URLRequest* request, | 149 net::URLRequest* request, |
140 net::NetworkDelegate* network_delegate) const { | 150 net::NetworkDelegate* network_delegate) const { |
141 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 151 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
142 | 152 |
143 // MaybeInterceptRequest can be called multiple times for the same request. | 153 // MaybeInterceptRequest can be called multiple times for the same request. |
144 if (request->GetUserData(kRequestAlreadyHasJobDataKey)) | 154 if (request->GetUserData(kRequestAlreadyHasJobDataKey)) |
145 return nullptr; | 155 return nullptr; |
146 | 156 |
| 157 // With PlzNavigate, we now seem to receive blob URLs in interceptor. |
| 158 // Ignore these URLs. |
| 159 // TODO(sgurun) is this the best place to do that? Talk with jam@. |
| 160 if (request->url().SchemeIs(url::kBlobScheme)) { |
| 161 return nullptr; |
| 162 } |
| 163 |
147 std::unique_ptr<AwContentsIoThreadClient> io_thread_client = | 164 std::unique_ptr<AwContentsIoThreadClient> io_thread_client = |
148 GetCorrespondingIoThreadClient(request); | 165 GetCorrespondingIoThreadClient(request); |
149 | 166 |
150 if (!io_thread_client) | 167 if (!io_thread_client) |
151 return nullptr; | 168 return nullptr; |
152 | 169 |
153 GURL referrer(request->referrer()); | 170 GURL referrer(request->referrer()); |
154 if (referrer.is_valid() && | 171 if (referrer.is_valid() && |
155 (!request->is_pending() || request->is_redirecting())) { | 172 (!request->is_pending() || request->is_redirecting())) { |
156 request->SetExtraRequestHeaderByName(net::HttpRequestHeaders::kReferer, | 173 request->SetExtraRequestHeaderByName(net::HttpRequestHeaders::kReferer, |
157 referrer.spec(), true); | 174 referrer.spec(), true); |
158 } | 175 } |
159 request->SetUserData(kRequestAlreadyHasJobDataKey, | 176 request->SetUserData(kRequestAlreadyHasJobDataKey, |
160 new base::SupportsUserData::Data()); | 177 new base::SupportsUserData::Data()); |
161 return new AndroidStreamReaderURLRequestJob( | 178 return new AndroidStreamReaderURLRequestJob( |
162 request, network_delegate, | 179 request, network_delegate, |
163 base::MakeUnique<ShouldInterceptRequestAdaptor>( | 180 base::MakeUnique<ShouldInterceptRequestAdaptor>( |
164 std::move(io_thread_client)), | 181 std::move(io_thread_client)), |
165 true); | 182 true); |
166 } | 183 } |
167 | 184 |
168 } // namespace android_webview | 185 } // namespace android_webview |
OLD | NEW |