John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 1 | // Copyright 2017 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 "content/public/test/url_loader_interceptor.h" |
| 6 | |
Lukasz Anforowicz | 98ccf1c | 2018-08-23 18:14:31 | [diff] [blame] | 7 | #include <string> |
| 8 | #include <utility> |
| 9 | |
Sebastien Marchand | f8cbfab | 2019-01-25 16:02:30 | [diff] [blame] | 10 | #include "base/bind.h" |
David Benjamin | e430b8a | 2019-03-21 20:46:27 | [diff] [blame] | 11 | #include "base/containers/unique_ptr_adapters.h" |
John Abd-El-Malek | 30a8689 | 2018-05-26 00:14:16 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
| 13 | #include "base/files/file_util.h" |
| 14 | #include "base/path_service.h" |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 15 | #include "base/synchronization/lock.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 16 | #include "base/task/post_task.h" |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 17 | #include "base/test/bind_test_util.h" |
John Abd-El-Malek | 30a8689 | 2018-05-26 00:14:16 | [diff] [blame] | 18 | #include "base/threading/thread_restrictions.h" |
John Abd-El-Malek | 1d1ff918 | 2018-08-10 16:18:23 | [diff] [blame] | 19 | #include "build/build_config.h" |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 20 | #include "content/browser/frame_host/render_frame_host_impl.h" |
arthursonzogni | 1fd60e6 | 2018-05-09 15:57:32 | [diff] [blame] | 21 | #include "content/browser/loader/navigation_url_loader_impl.h" |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 22 | #include "content/browser/loader/resource_message_filter.h" |
| 23 | #include "content/browser/loader/url_loader_factory_impl.h" |
Tsuyoshi Horo | 0fd45315 | 2018-10-16 01:45:20 | [diff] [blame] | 24 | #include "content/browser/service_worker/embedded_worker_instance.h" |
John Abd-El-Malek | d5f92034 | 2018-02-12 17:37:36 | [diff] [blame] | 25 | #include "content/browser/storage_partition_impl.h" |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 26 | #include "content/browser/url_loader_factory_getter.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 27 | #include "content/public/browser/browser_task_traits.h" |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 28 | #include "content/public/browser/browser_thread.h" |
Ken Rockot | 0cf16c3 | 2018-03-01 16:50:49 | [diff] [blame] | 29 | #include "mojo/public/cpp/bindings/binding_set.h" |
John Abd-El-Malek | a796fa5 | 2017-12-12 17:12:32 | [diff] [blame] | 30 | #include "net/http/http_util.h" |
John Abd-El-Malek | 30a8689 | 2018-05-26 00:14:16 | [diff] [blame] | 31 | #include "net/test/embedded_test_server/request_handler_util.h" |
Yutaka Hirano | d8789f9 | 2018-01-30 09:59:51 | [diff] [blame] | 32 | #include "services/network/public/cpp/features.h" |
Ken Rockot | 54311e6 | 2018-02-10 19:01:52 | [diff] [blame] | 33 | #include "services/network/public/mojom/url_loader.mojom.h" |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 34 | |
| 35 | namespace content { |
| 36 | |
John Abd-El-Malek | 30a8689 | 2018-05-26 00:14:16 | [diff] [blame] | 37 | namespace { |
| 38 | |
| 39 | base::FilePath GetDataFilePath(const std::string& relative_path) { |
| 40 | base::FilePath root_path; |
| 41 | CHECK(base::PathService::Get(base::DIR_SOURCE_ROOT, &root_path)); |
| 42 | return root_path.AppendASCII(relative_path); |
| 43 | } |
| 44 | |
John Abd-El-Malek | 1d1ff918 | 2018-08-10 16:18:23 | [diff] [blame] | 45 | static std::string ReadFile(const base::FilePath& path) { |
John Abd-El-Malek | 30a8689 | 2018-05-26 00:14:16 | [diff] [blame] | 46 | std::string contents; |
John Abd-El-Malek | 1d1ff918 | 2018-08-10 16:18:23 | [diff] [blame] | 47 | CHECK(base::ReadFileToString(path, &contents)); |
John Abd-El-Malek | 30a8689 | 2018-05-26 00:14:16 | [diff] [blame] | 48 | return contents; |
| 49 | } |
| 50 | |
| 51 | } // namespace |
| 52 | |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 53 | // Part of URLLoaderInterceptor which lives on the IO thread. Outlives |
| 54 | // URLLoaderInterceptor. |
| 55 | class URLLoaderInterceptor::IOState |
Lukasz Anforowicz | 2f985a4 | 2019-07-19 02:56:52 | [diff] [blame] | 56 | : public base::RefCountedThreadSafe<URLLoaderInterceptor::IOState, |
| 57 | BrowserThread::DeleteOnIOThread> { |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 58 | public: |
| 59 | explicit IOState(URLLoaderInterceptor* parent) : parent_(parent) {} |
Yao Xiao | 43ae3bc | 2019-07-09 05:22:23 | [diff] [blame] | 60 | void Initialize( |
| 61 | const URLLoaderCompletionStatusCallback& completion_status_callback, |
| 62 | base::OnceClosure closure); |
| 63 | |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 64 | // Called when a SubresourceWrapper's binding has an error. |
| 65 | void SubresourceWrapperBindingError(SubresourceWrapper* wrapper); |
| 66 | |
| 67 | // Unsets the parent pointer. Prevents URLLoaderInterceptor::Intercept from |
| 68 | // being called. |
| 69 | void UnsetParent() { |
| 70 | base::AutoLock lock(intercept_lock_); |
| 71 | parent_ = nullptr; |
| 72 | } |
| 73 | |
| 74 | void Shutdown(base::OnceClosure closure) { |
| 75 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 76 | url_loader_factory_getter_wrappers_.clear(); |
| 77 | subresource_wrappers_.clear(); |
Clark DuVall | 649c05f | 2019-02-05 23:58:47 | [diff] [blame] | 78 | navigation_wrappers_.clear(); |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 79 | |
John Abd-El-Malek | 8730158 | 2019-07-27 04:50:52 | [diff] [blame^] | 80 | URLLoaderFactoryGetter::SetGetNetworkFactoryCallbackForTesting( |
| 81 | URLLoaderFactoryGetter::GetNetworkFactoryCallback()); |
| 82 | if (!NavigationURLLoaderImpl::IsNavigationLoaderOnUIEnabled()) { |
| 83 | NavigationURLLoaderImpl::SetURLLoaderFactoryInterceptorForTesting( |
| 84 | NavigationURLLoaderImpl::URLLoaderFactoryInterceptor()); |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) { |
| 88 | ResourceMessageFilter::SetNetworkFactoryForTesting(nullptr); |
| 89 | } |
| 90 | |
| 91 | if (closure) |
| 92 | std::move(closure).Run(); |
| 93 | } |
| 94 | |
| 95 | // Callback on IO thread whenever a |
| 96 | // URLLoaderFactoryGetter::GetNetworkContext is called on an object that |
| 97 | // doesn't have a test factory set up. |
| 98 | void GetNetworkFactoryCallback( |
| 99 | URLLoaderFactoryGetter* url_loader_factory_getter); |
| 100 | |
| 101 | void CreateURLLoaderFactoryForSubresources( |
| 102 | network::mojom::URLLoaderFactoryRequest request, |
| 103 | int process_id, |
| 104 | network::mojom::URLLoaderFactoryPtrInfo original_factory); |
| 105 | |
| 106 | bool Intercept(RequestParams* params) { |
| 107 | // The lock ensures that |URLLoaderInterceptor| can't be deleted while it |
| 108 | // is processing an intercept. Before |URLLoaderInterceptor| is deleted, |
| 109 | // parent_ is set to null so that requests can't be intercepted after |
| 110 | // |URLLoaderInterceptor| is deleted. |
| 111 | base::AutoLock lock(intercept_lock_); |
| 112 | if (!parent_) |
| 113 | return false; |
| 114 | return parent_->Intercept(params); |
| 115 | } |
| 116 | |
| 117 | bool BeginNavigationCallback( |
| 118 | network::mojom::URLLoaderRequest* request, |
| 119 | int32_t routing_id, |
| 120 | int32_t request_id, |
| 121 | uint32_t options, |
| 122 | const network::ResourceRequest& url_request, |
| 123 | network::mojom::URLLoaderClientPtr* client, |
| 124 | const net::MutableNetworkTrafficAnnotationTag& traffic_annotation) { |
| 125 | RequestParams params; |
| 126 | params.process_id = 0; |
| 127 | params.request = std::move(*request); |
| 128 | params.routing_id = routing_id; |
| 129 | params.request_id = request_id; |
| 130 | params.options = options; |
| 131 | params.url_request = url_request; |
| 132 | params.client = std::move(*client); |
| 133 | params.traffic_annotation = traffic_annotation; |
| 134 | |
| 135 | if (Intercept(¶ms)) |
| 136 | return true; |
| 137 | |
| 138 | *request = std::move(params.request); |
| 139 | *client = std::move(params.client); |
| 140 | return false; |
| 141 | } |
| 142 | |
Clark DuVall | 649c05f | 2019-02-05 23:58:47 | [diff] [blame] | 143 | // Callback on IO thread whenever NavigationURLLoaderImpl needs a |
John Abd-El-Malek | 08e9995 | 2019-04-12 22:46:22 | [diff] [blame] | 144 | // URLLoaderFactory with a network::mojom::TrustedURLLoaderHeaderClient or |
| 145 | // for a non-network-service scheme. |
Clark DuVall | 649c05f | 2019-02-05 23:58:47 | [diff] [blame] | 146 | void InterceptNavigationRequestCallback( |
| 147 | network::mojom::URLLoaderFactoryRequest* request) { |
| 148 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 149 | |
| 150 | auto proxied_request = std::move(*request); |
| 151 | network::mojom::URLLoaderFactoryPtr target_factory; |
| 152 | *request = mojo::MakeRequest(&target_factory); |
| 153 | |
| 154 | navigation_wrappers_.emplace( |
| 155 | std::make_unique<URLLoaderFactoryNavigationWrapper>( |
| 156 | std::move(proxied_request), std::move(target_factory), this)); |
| 157 | } |
| 158 | |
Yao Xiao | 43ae3bc | 2019-07-09 05:22:23 | [diff] [blame] | 159 | URLLoaderCompletionStatusCallback GetCompletionStatusCallback() { |
| 160 | return completion_status_callback_; |
| 161 | } |
| 162 | |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 163 | private: |
Lukasz Anforowicz | 2f985a4 | 2019-07-19 02:56:52 | [diff] [blame] | 164 | friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>; |
| 165 | friend class base::DeleteHelper<IOState>; |
| 166 | |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 167 | ~IOState() {} |
| 168 | |
| 169 | // This lock guarantees that when URLLoaderInterceptor is destroyed, |
| 170 | // no intercept callbacks will be called. |
| 171 | base::Lock intercept_lock_; |
| 172 | URLLoaderInterceptor* parent_ GUARDED_BY(intercept_lock_); |
| 173 | |
Yao Xiao | 43ae3bc | 2019-07-09 05:22:23 | [diff] [blame] | 174 | URLLoaderCompletionStatusCallback completion_status_callback_; |
| 175 | |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 176 | // For intercepting frame requests with network service. There is one per |
| 177 | // StoragePartition. Only accessed on IO thread. |
| 178 | std::set<std::unique_ptr<URLLoaderFactoryGetterWrapper>> |
| 179 | url_loader_factory_getter_wrappers_; |
| 180 | // For intercepting subresources without network service in |
| 181 | // ResourceMessageFilter. |
| 182 | std::unique_ptr<Interceptor> rmf_interceptor_; |
| 183 | // For intercepting subresources with network service. There is one per |
| 184 | // active render frame commit. Only accessed on IO thread. |
David Benjamin | e430b8a | 2019-03-21 20:46:27 | [diff] [blame] | 185 | std::set<std::unique_ptr<SubresourceWrapper>, base::UniquePtrComparator> |
| 186 | subresource_wrappers_; |
Clark DuVall | 649c05f | 2019-02-05 23:58:47 | [diff] [blame] | 187 | std::set<std::unique_ptr<URLLoaderFactoryNavigationWrapper>> |
| 188 | navigation_wrappers_; |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 189 | |
| 190 | DISALLOW_COPY_AND_ASSIGN(IOState); |
| 191 | }; |
| 192 | |
Yao Xiao | 43ae3bc | 2019-07-09 05:22:23 | [diff] [blame] | 193 | class URLLoaderClientInterceptor : public network::mojom::URLLoaderClient { |
| 194 | public: |
| 195 | explicit URLLoaderClientInterceptor( |
| 196 | const base::Callback<network::mojom::URLLoaderFactory*()>& factory_getter, |
| 197 | URLLoaderInterceptor::RequestParams params, |
| 198 | const URLLoaderInterceptor::URLLoaderCompletionStatusCallback& |
| 199 | completion_status_callback) |
| 200 | : original_client_(std::move(params.client)), |
| 201 | delegating_client_binding_(this), |
| 202 | completion_status_callback_(std::move(completion_status_callback)), |
| 203 | request_url_(params.url_request.url) { |
| 204 | network::mojom::URLLoaderClientPtr delegating_client; |
| 205 | delegating_client_binding_.Bind(mojo::MakeRequest(&delegating_client)); |
| 206 | factory_getter.Run()->CreateLoaderAndStart( |
| 207 | std::move(params.request), params.routing_id, params.request_id, |
| 208 | params.options, std::move(params.url_request), |
| 209 | std::move(delegating_client), params.traffic_annotation); |
| 210 | } |
| 211 | |
| 212 | void OnReceiveResponse(const network::ResourceResponseHead& head) override { |
| 213 | original_client_->OnReceiveResponse(head); |
| 214 | } |
| 215 | |
| 216 | void OnReceiveRedirect(const net::RedirectInfo& redirect_info, |
| 217 | const network::ResourceResponseHead& head) override { |
| 218 | original_client_->OnReceiveRedirect(redirect_info, head); |
| 219 | } |
| 220 | |
| 221 | void OnUploadProgress(int64_t current_position, |
| 222 | int64_t total_size, |
| 223 | base::OnceCallback<void()> callback) override { |
| 224 | original_client_->OnUploadProgress(current_position, total_size, |
| 225 | std::move(callback)); |
| 226 | } |
| 227 | |
| 228 | void OnReceiveCachedMetadata(mojo_base::BigBuffer data) override { |
| 229 | original_client_->OnReceiveCachedMetadata(std::move(data)); |
| 230 | } |
| 231 | |
| 232 | void OnTransferSizeUpdated(int32_t transfer_size_diff) override { |
| 233 | original_client_->OnTransferSizeUpdated(transfer_size_diff); |
| 234 | } |
| 235 | |
| 236 | void OnStartLoadingResponseBody( |
| 237 | mojo::ScopedDataPipeConsumerHandle body) override { |
| 238 | original_client_->OnStartLoadingResponseBody(std::move(body)); |
| 239 | } |
| 240 | |
| 241 | void OnComplete(const network::URLLoaderCompletionStatus& status) override { |
| 242 | if (!completion_status_callback_.is_null()) |
| 243 | completion_status_callback_.Run(request_url_, status); |
| 244 | original_client_->OnComplete(status); |
| 245 | } |
| 246 | |
| 247 | private: |
| 248 | network::mojom::URLLoaderClientPtr original_client_; |
| 249 | mojo::Binding<network::mojom::URLLoaderClient> delegating_client_binding_; |
| 250 | URLLoaderInterceptor::URLLoaderCompletionStatusCallback |
| 251 | completion_status_callback_; |
| 252 | GURL request_url_; |
| 253 | }; |
| 254 | |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 255 | class URLLoaderInterceptor::Interceptor |
| 256 | : public network::mojom::URLLoaderFactory { |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 257 | public: |
| 258 | using ProcessIdGetter = base::Callback<int()>; |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 259 | using OriginalFactoryGetter = |
| 260 | base::Callback<network::mojom::URLLoaderFactory*()>; |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 261 | |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 262 | Interceptor(URLLoaderInterceptor::IOState* parent, |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 263 | const ProcessIdGetter& process_id_getter, |
| 264 | const OriginalFactoryGetter& original_factory_getter) |
| 265 | : parent_(parent), |
| 266 | process_id_getter_(process_id_getter), |
Ken Rockot | 0cf16c3 | 2018-03-01 16:50:49 | [diff] [blame] | 267 | original_factory_getter_(original_factory_getter) { |
| 268 | bindings_.set_connection_error_handler(base::BindRepeating( |
| 269 | &Interceptor::OnConnectionError, base::Unretained(this))); |
| 270 | } |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 271 | |
| 272 | ~Interceptor() override {} |
| 273 | |
Ken Rockot | 0cf16c3 | 2018-03-01 16:50:49 | [diff] [blame] | 274 | void BindRequest(network::mojom::URLLoaderFactoryRequest request) { |
| 275 | bindings_.AddBinding(this, std::move(request)); |
| 276 | } |
| 277 | |
| 278 | void SetConnectionErrorHandler(base::OnceClosure handler) { |
| 279 | error_handler_ = std::move(handler); |
| 280 | } |
| 281 | |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 282 | private: |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 283 | // network::mojom::URLLoaderFactory implementation: |
| 284 | void CreateLoaderAndStart(network::mojom::URLLoaderRequest request, |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 285 | int32_t routing_id, |
| 286 | int32_t request_id, |
| 287 | uint32_t options, |
John Abd-El-Malek | 1df6179 | 2018-01-12 20:40:45 | [diff] [blame] | 288 | const network::ResourceRequest& url_request, |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 289 | network::mojom::URLLoaderClientPtr client, |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 290 | const net::MutableNetworkTrafficAnnotationTag& |
| 291 | traffic_annotation) override { |
| 292 | RequestParams params; |
| 293 | params.process_id = process_id_getter_.Run(); |
| 294 | params.request = std::move(request); |
| 295 | params.routing_id = routing_id; |
| 296 | params.request_id = request_id; |
| 297 | params.options = options; |
| 298 | params.url_request = std::move(url_request); |
| 299 | params.client = std::move(client); |
| 300 | params.traffic_annotation = traffic_annotation; |
arthursonzogni | b8a0255 | 2018-04-24 23:03:42 | [diff] [blame] | 301 | |
John Abd-El-Malek | db3a13b | 2018-05-01 17:52:02 | [diff] [blame] | 302 | if (parent_->Intercept(¶ms)) |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 303 | return; |
| 304 | |
Yao Xiao | 43ae3bc | 2019-07-09 05:22:23 | [diff] [blame] | 305 | url_loader_client_interceptors_.push_back( |
| 306 | std::make_unique<URLLoaderClientInterceptor>( |
| 307 | std::move(original_factory_getter_), std::move(params), |
| 308 | parent_->GetCompletionStatusCallback())); |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 309 | } |
| 310 | |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 311 | void Clone(network::mojom::URLLoaderFactoryRequest request) override { |
Ken Rockot | 0cf16c3 | 2018-03-01 16:50:49 | [diff] [blame] | 312 | BindRequest(std::move(request)); |
| 313 | } |
| 314 | |
| 315 | void OnConnectionError() { |
| 316 | if (bindings_.empty() && error_handler_) |
| 317 | std::move(error_handler_).Run(); |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 318 | } |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 319 | |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 320 | URLLoaderInterceptor::IOState* parent_; |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 321 | ProcessIdGetter process_id_getter_; |
| 322 | OriginalFactoryGetter original_factory_getter_; |
Ken Rockot | 0cf16c3 | 2018-03-01 16:50:49 | [diff] [blame] | 323 | mojo::BindingSet<network::mojom::URLLoaderFactory> bindings_; |
| 324 | base::OnceClosure error_handler_; |
Yao Xiao | 43ae3bc | 2019-07-09 05:22:23 | [diff] [blame] | 325 | std::vector<std::unique_ptr<URLLoaderClientInterceptor>> |
| 326 | url_loader_client_interceptors_; |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 327 | |
| 328 | DISALLOW_COPY_AND_ASSIGN(Interceptor); |
| 329 | }; |
| 330 | |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 331 | // This class intercepts calls to each StoragePartition's URLLoaderFactoryGetter |
| 332 | // so that it can intercept frame requests. |
| 333 | class URLLoaderInterceptor::URLLoaderFactoryGetterWrapper { |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 334 | public: |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 335 | URLLoaderFactoryGetterWrapper( |
| 336 | URLLoaderFactoryGetter* url_loader_factory_getter, |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 337 | URLLoaderInterceptor::IOState* parent) |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 338 | : url_loader_factory_getter_(url_loader_factory_getter) { |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 339 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 340 | frame_interceptor_ = std::make_unique<Interceptor>( |
| 341 | parent, base::BindRepeating([]() { return 0; }), |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 342 | base::BindLambdaForTesting([=]() -> network::mojom::URLLoaderFactory* { |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 343 | return url_loader_factory_getter |
| 344 | ->original_network_factory_for_testing() |
| 345 | ->get(); |
| 346 | })); |
| 347 | url_loader_factory_getter_->SetNetworkFactoryForTesting( |
Lukasz Anforowicz | 288027b | 2019-01-17 01:51:27 | [diff] [blame] | 348 | frame_interceptor_.get()); |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | ~URLLoaderFactoryGetterWrapper() { |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 352 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
Lukasz Anforowicz | 288027b | 2019-01-17 01:51:27 | [diff] [blame] | 353 | url_loader_factory_getter_->SetNetworkFactoryForTesting(nullptr); |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | private: |
| 357 | std::unique_ptr<Interceptor> frame_interceptor_; |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 358 | URLLoaderFactoryGetter* url_loader_factory_getter_; |
| 359 | }; |
| 360 | |
Clark DuVall | a5e25b75 | 2018-11-27 22:08:16 | [diff] [blame] | 361 | class URLLoaderInterceptor::URLLoaderFactoryNavigationWrapper { |
| 362 | public: |
| 363 | URLLoaderFactoryNavigationWrapper( |
| 364 | network::mojom::URLLoaderFactoryRequest request, |
| 365 | network::mojom::URLLoaderFactoryPtr target_factory, |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 366 | URLLoaderInterceptor::IOState* parent) |
Clark DuVall | a5e25b75 | 2018-11-27 22:08:16 | [diff] [blame] | 367 | : target_factory_(std::move(target_factory)) { |
| 368 | interceptor_ = std::make_unique<Interceptor>( |
| 369 | parent, base::BindRepeating([]() { return 0; }), |
| 370 | base::BindLambdaForTesting([=]() -> network::mojom::URLLoaderFactory* { |
| 371 | return this->target_factory_.get(); |
| 372 | })); |
| 373 | interceptor_->BindRequest(std::move(request)); |
| 374 | } |
| 375 | |
| 376 | private: |
| 377 | std::unique_ptr<Interceptor> interceptor_; |
| 378 | network::mojom::URLLoaderFactoryPtr target_factory_; |
| 379 | }; |
| 380 | |
John Abd-El-Malek | d5f92034 | 2018-02-12 17:37:36 | [diff] [blame] | 381 | // This class intercepts calls to |
| 382 | // StoragePartition::GetURLLoaderFactoryForBrowserProcess. |
| 383 | class URLLoaderInterceptor::BrowserProcessWrapper { |
| 384 | public: |
| 385 | BrowserProcessWrapper(network::mojom::URLLoaderFactoryRequest factory_request, |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 386 | URLLoaderInterceptor::IOState* parent, |
John Abd-El-Malek | d5f92034 | 2018-02-12 17:37:36 | [diff] [blame] | 387 | network::mojom::URLLoaderFactoryPtr original_factory) |
| 388 | : interceptor_( |
| 389 | parent, |
| 390 | base::BindRepeating([]() { return 0; }), |
| 391 | base::BindRepeating(&BrowserProcessWrapper::GetOriginalFactory, |
| 392 | base::Unretained(this))), |
Ken Rockot | 0cf16c3 | 2018-03-01 16:50:49 | [diff] [blame] | 393 | original_factory_(std::move(original_factory)) { |
| 394 | interceptor_.BindRequest(std::move(factory_request)); |
| 395 | } |
John Abd-El-Malek | d5f92034 | 2018-02-12 17:37:36 | [diff] [blame] | 396 | |
| 397 | ~BrowserProcessWrapper() {} |
| 398 | |
| 399 | private: |
| 400 | network::mojom::URLLoaderFactory* GetOriginalFactory() { |
| 401 | return original_factory_.get(); |
| 402 | } |
| 403 | |
| 404 | Interceptor interceptor_; |
John Abd-El-Malek | d5f92034 | 2018-02-12 17:37:36 | [diff] [blame] | 405 | network::mojom::URLLoaderFactoryPtr original_factory_; |
| 406 | |
| 407 | DISALLOW_COPY_AND_ASSIGN(BrowserProcessWrapper); |
| 408 | }; |
| 409 | |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 410 | // This class is sent along a RenderFrame commit message as a subresource |
| 411 | // loader so that it can intercept subresource requests. |
| 412 | class URLLoaderInterceptor::SubresourceWrapper { |
| 413 | public: |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 414 | SubresourceWrapper(network::mojom::URLLoaderFactoryRequest factory_request, |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 415 | int process_id, |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 416 | URLLoaderInterceptor::IOState* parent, |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 417 | network::mojom::URLLoaderFactoryPtrInfo original_factory) |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 418 | : interceptor_( |
| 419 | parent, |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 420 | base::BindRepeating([](int process_id) { return process_id; }, |
| 421 | process_id), |
| 422 | base::BindRepeating(&SubresourceWrapper::GetOriginalFactory, |
| 423 | base::Unretained(this))), |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 424 | original_factory_(std::move(original_factory)) { |
Ken Rockot | 0cf16c3 | 2018-03-01 16:50:49 | [diff] [blame] | 425 | interceptor_.BindRequest(std::move(factory_request)); |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 426 | interceptor_.SetConnectionErrorHandler(base::BindOnce( |
| 427 | &URLLoaderInterceptor::IOState::SubresourceWrapperBindingError, |
| 428 | base::Unretained(parent), this)); |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 429 | } |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 430 | |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 431 | ~SubresourceWrapper() {} |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 432 | |
| 433 | private: |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 434 | network::mojom::URLLoaderFactory* GetOriginalFactory() { |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 435 | return original_factory_.get(); |
| 436 | } |
| 437 | |
| 438 | Interceptor interceptor_; |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 439 | network::mojom::URLLoaderFactoryPtr original_factory_; |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 440 | |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 441 | DISALLOW_COPY_AND_ASSIGN(SubresourceWrapper); |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 442 | }; |
| 443 | |
| 444 | URLLoaderInterceptor::RequestParams::RequestParams() = default; |
| 445 | URLLoaderInterceptor::RequestParams::~RequestParams() = default; |
John Abd-El-Malek | df84c449 | 2018-02-23 02:32:44 | [diff] [blame] | 446 | URLLoaderInterceptor::RequestParams::RequestParams(RequestParams&& other) = |
| 447 | default; |
| 448 | URLLoaderInterceptor::RequestParams& URLLoaderInterceptor::RequestParams:: |
| 449 | operator=(RequestParams&& other) = default; |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 450 | |
Dan Harrington | a11fc96 | 2019-01-07 22:15:17 | [diff] [blame] | 451 | URLLoaderInterceptor::URLLoaderInterceptor(const InterceptCallback& callback) |
Yao Xiao | 43ae3bc | 2019-07-09 05:22:23 | [diff] [blame] | 452 | : URLLoaderInterceptor(callback, {}, {}) {} |
Dan Harrington | a11fc96 | 2019-01-07 22:15:17 | [diff] [blame] | 453 | |
Yao Xiao | 43ae3bc | 2019-07-09 05:22:23 | [diff] [blame] | 454 | URLLoaderInterceptor::URLLoaderInterceptor( |
| 455 | const InterceptCallback& callback, |
| 456 | const URLLoaderCompletionStatusCallback& completion_status_callback, |
| 457 | base::OnceClosure ready_callback) |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 458 | : callback_(callback), io_thread_(base::MakeRefCounted<IOState>(this)) { |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 459 | DCHECK(!BrowserThread::IsThreadInitialized(BrowserThread::UI) || |
| 460 | BrowserThread::CurrentlyOn(BrowserThread::UI)); |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 461 | use_runloop_ = !ready_callback; |
Matt Falkenhagen | 4332b16 | 2019-03-04 08:58:52 | [diff] [blame] | 462 | RenderFrameHostImpl::SetNetworkFactoryForTesting(base::BindRepeating( |
| 463 | &URLLoaderInterceptor::CreateURLLoaderFactoryForSubresources, |
| 464 | base::Unretained(this))); |
| 465 | SharedWorkerHost::SetNetworkFactoryForTesting(base::BindRepeating( |
| 466 | &URLLoaderInterceptor::CreateURLLoaderFactoryForSubresources, |
| 467 | base::Unretained(this))); |
| 468 | // Note: This URLLoaderFactory creation callback will be used not only for |
| 469 | // subresource loading from service workers (i.e., fetch()), but also for |
| 470 | // loading non-installed service worker scripts. |
| 471 | EmbeddedWorkerInstance::SetNetworkFactoryForTesting(base::BindRepeating( |
| 472 | &URLLoaderInterceptor::CreateURLLoaderFactoryForSubresources, |
| 473 | base::Unretained(this))); |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 474 | |
John Abd-El-Malek | d5f92034 | 2018-02-12 17:37:36 | [diff] [blame] | 475 | StoragePartitionImpl:: |
| 476 | SetGetURLLoaderFactoryForBrowserProcessCallbackForTesting( |
| 477 | base::BindRepeating( |
| 478 | &URLLoaderInterceptor::GetURLLoaderFactoryForBrowserProcess, |
| 479 | base::Unretained(this))); |
| 480 | |
Clark DuVall | 0d278b43 | 2019-06-25 18:24:57 | [diff] [blame] | 481 | if (NavigationURLLoaderImpl::IsNavigationLoaderOnUIEnabled()) { |
| 482 | NavigationURLLoaderImpl::SetURLLoaderFactoryInterceptorForTesting( |
| 483 | base::BindRepeating( |
| 484 | &URLLoaderInterceptor::InterceptNavigationRequestCallback, |
| 485 | base::Unretained(this))); |
| 486 | } |
| 487 | |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 488 | if (BrowserThread::IsThreadInitialized(BrowserThread::IO)) { |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 489 | if (use_runloop_) { |
| 490 | base::RunLoop run_loop; |
| 491 | base::PostTaskWithTraits( |
| 492 | FROM_HERE, {BrowserThread::IO}, |
| 493 | base::BindOnce(&URLLoaderInterceptor::IOState::Initialize, io_thread_, |
Yao Xiao | 43ae3bc | 2019-07-09 05:22:23 | [diff] [blame] | 494 | std::move(completion_status_callback), |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 495 | run_loop.QuitClosure())); |
| 496 | run_loop.Run(); |
| 497 | } else { |
| 498 | base::OnceClosure wrapped_callback = base::BindOnce( |
| 499 | [](base::OnceClosure callback) { |
| 500 | base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI}, |
| 501 | std::move(callback)); |
| 502 | }, |
| 503 | std::move(ready_callback)); |
| 504 | |
| 505 | base::PostTaskWithTraits( |
| 506 | FROM_HERE, {BrowserThread::IO}, |
| 507 | base::BindOnce(&URLLoaderInterceptor::IOState::Initialize, io_thread_, |
Yao Xiao | 43ae3bc | 2019-07-09 05:22:23 | [diff] [blame] | 508 | std::move(completion_status_callback), |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 509 | std::move(wrapped_callback))); |
| 510 | } |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 511 | } else { |
Yao Xiao | 43ae3bc | 2019-07-09 05:22:23 | [diff] [blame] | 512 | io_thread_->Initialize(std::move(completion_status_callback), |
| 513 | std::move(ready_callback)); |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
| 517 | URLLoaderInterceptor::~URLLoaderInterceptor() { |
| 518 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 519 | io_thread_->UnsetParent(); |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 520 | |
Matt Falkenhagen | 4332b16 | 2019-03-04 08:58:52 | [diff] [blame] | 521 | RenderFrameHostImpl::SetNetworkFactoryForTesting( |
| 522 | RenderFrameHostImpl::CreateNetworkFactoryCallback()); |
| 523 | SharedWorkerHost::SetNetworkFactoryForTesting( |
| 524 | RenderFrameHostImpl::CreateNetworkFactoryCallback()); |
| 525 | EmbeddedWorkerInstance::SetNetworkFactoryForTesting( |
| 526 | RenderFrameHostImpl::CreateNetworkFactoryCallback()); |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 527 | |
John Abd-El-Malek | d5f92034 | 2018-02-12 17:37:36 | [diff] [blame] | 528 | StoragePartitionImpl:: |
| 529 | SetGetURLLoaderFactoryForBrowserProcessCallbackForTesting( |
| 530 | StoragePartitionImpl::CreateNetworkFactoryCallback()); |
| 531 | |
Clark DuVall | 0d278b43 | 2019-06-25 18:24:57 | [diff] [blame] | 532 | if (NavigationURLLoaderImpl::IsNavigationLoaderOnUIEnabled()) { |
| 533 | NavigationURLLoaderImpl::SetURLLoaderFactoryInterceptorForTesting( |
| 534 | NavigationURLLoaderImpl::URLLoaderFactoryInterceptor()); |
| 535 | } |
| 536 | |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 537 | if (use_runloop_) { |
| 538 | base::RunLoop run_loop; |
| 539 | base::PostTaskWithTraits( |
| 540 | FROM_HERE, {BrowserThread::IO}, |
| 541 | base::BindOnce(&URLLoaderInterceptor::IOState::Shutdown, io_thread_, |
| 542 | run_loop.QuitClosure())); |
| 543 | run_loop.Run(); |
| 544 | } else { |
| 545 | base::PostTaskWithTraits( |
| 546 | FROM_HERE, {BrowserThread::IO}, |
| 547 | base::BindOnce(&URLLoaderInterceptor::IOState::Shutdown, io_thread_, |
| 548 | base::OnceClosure())); |
| 549 | } |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 550 | } |
| 551 | |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 552 | void URLLoaderInterceptor::WriteResponse( |
| 553 | const std::string& headers, |
| 554 | const std::string& body, |
Ovidio Henriquez | f8e2d93a | 2018-08-01 15:28:42 | [diff] [blame] | 555 | network::mojom::URLLoaderClient* client, |
| 556 | base::Optional<net::SSLInfo> ssl_info) { |
John Abd-El-Malek | a796fa5 | 2017-12-12 17:12:32 | [diff] [blame] | 557 | net::HttpResponseInfo info; |
David Benjamin | 1384c040 | 2019-04-29 18:55:52 | [diff] [blame] | 558 | info.headers = base::MakeRefCounted<net::HttpResponseHeaders>( |
| 559 | net::HttpUtil::AssembleRawHeaders(headers)); |
John Abd-El-Malek | 4624803 | 2018-01-17 19:11:23 | [diff] [blame] | 560 | network::ResourceResponseHead response; |
John Abd-El-Malek | a796fa5 | 2017-12-12 17:12:32 | [diff] [blame] | 561 | response.headers = info.headers; |
| 562 | response.headers->GetMimeType(&response.mime_type); |
Ovidio Henriquez | f8e2d93a | 2018-08-01 15:28:42 | [diff] [blame] | 563 | response.ssl_info = std::move(ssl_info); |
Marijn Kruisselbrink | 9ebd7ba | 2018-06-11 23:18:04 | [diff] [blame] | 564 | client->OnReceiveResponse(response); |
John Abd-El-Malek | a796fa5 | 2017-12-12 17:12:32 | [diff] [blame] | 565 | |
| 566 | uint32_t bytes_written = body.size(); |
Andrey Kosyakov | 8846844e | 2018-07-10 00:49:22 | [diff] [blame] | 567 | mojo::DataPipe data_pipe(body.size()); |
John Abd-El-Malek | d5f92034 | 2018-02-12 17:37:36 | [diff] [blame] | 568 | CHECK_EQ(MOJO_RESULT_OK, |
| 569 | data_pipe.producer_handle->WriteData( |
| 570 | body.data(), &bytes_written, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE)); |
John Abd-El-Malek | a796fa5 | 2017-12-12 17:12:32 | [diff] [blame] | 571 | client->OnStartLoadingResponseBody(std::move(data_pipe.consumer_handle)); |
| 572 | |
| 573 | network::URLLoaderCompletionStatus status; |
John Abd-El-Malek | d5f92034 | 2018-02-12 17:37:36 | [diff] [blame] | 574 | status.decoded_body_length = body.size(); |
John Abd-El-Malek | a796fa5 | 2017-12-12 17:12:32 | [diff] [blame] | 575 | status.error_code = net::OK; |
| 576 | client->OnComplete(status); |
| 577 | } |
| 578 | |
John Abd-El-Malek | 30a8689 | 2018-05-26 00:14:16 | [diff] [blame] | 579 | void URLLoaderInterceptor::WriteResponse( |
| 580 | const std::string& relative_path, |
| 581 | network::mojom::URLLoaderClient* client, |
Ovidio Henriquez | f8e2d93a | 2018-08-01 15:28:42 | [diff] [blame] | 582 | const std::string* headers, |
| 583 | base::Optional<net::SSLInfo> ssl_info) { |
John Abd-El-Malek | 1d1ff918 | 2018-08-10 16:18:23 | [diff] [blame] | 584 | return WriteResponse(GetDataFilePath(relative_path), client, headers, |
| 585 | std::move(ssl_info)); |
| 586 | } |
| 587 | |
| 588 | void URLLoaderInterceptor::WriteResponse( |
| 589 | const base::FilePath& file_path, |
| 590 | network::mojom::URLLoaderClient* client, |
| 591 | const std::string* headers, |
| 592 | base::Optional<net::SSLInfo> ssl_info) { |
John Abd-El-Malek | 30a8689 | 2018-05-26 00:14:16 | [diff] [blame] | 593 | base::ScopedAllowBlockingForTesting allow_io; |
| 594 | std::string headers_str; |
| 595 | if (headers) { |
| 596 | headers_str = *headers; |
| 597 | } else { |
John Abd-El-Malek | 1d1ff918 | 2018-08-10 16:18:23 | [diff] [blame] | 598 | base::FilePath::StringPieceType mock_headers_extension; |
| 599 | #if defined(OS_WIN) |
| 600 | base::string16 temp = |
| 601 | base::ASCIIToUTF16(net::test_server::kMockHttpHeadersExtension); |
| 602 | mock_headers_extension = temp; |
| 603 | #else |
| 604 | mock_headers_extension = net::test_server::kMockHttpHeadersExtension; |
| 605 | #endif |
| 606 | |
| 607 | base::FilePath headers_path(file_path.AddExtension(mock_headers_extension)); |
| 608 | if (base::PathExists(headers_path)) { |
John Abd-El-Malek | 30a8689 | 2018-05-26 00:14:16 | [diff] [blame] | 609 | headers_str = ReadFile(headers_path); |
| 610 | } else { |
| 611 | headers_str = "HTTP/1.0 200 OK\nContent-type: " + |
John Abd-El-Malek | 1d1ff918 | 2018-08-10 16:18:23 | [diff] [blame] | 612 | net::test_server::GetContentType(file_path) + "\n\n"; |
John Abd-El-Malek | 30a8689 | 2018-05-26 00:14:16 | [diff] [blame] | 613 | } |
| 614 | } |
John Abd-El-Malek | 1d1ff918 | 2018-08-10 16:18:23 | [diff] [blame] | 615 | WriteResponse(headers_str, ReadFile(file_path), client, std::move(ssl_info)); |
John Abd-El-Malek | 30a8689 | 2018-05-26 00:14:16 | [diff] [blame] | 616 | } |
| 617 | |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 618 | void URLLoaderInterceptor::CreateURLLoaderFactoryForSubresources( |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 619 | network::mojom::URLLoaderFactoryRequest request, |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 620 | int process_id, |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 621 | network::mojom::URLLoaderFactoryPtrInfo original_factory) { |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 622 | if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 623 | base::PostTaskWithTraits( |
| 624 | FROM_HERE, {BrowserThread::IO}, |
tzik | ff02d28 | 2018-02-22 06:11:08 | [diff] [blame] | 625 | base::BindOnce( |
| 626 | &URLLoaderInterceptor::CreateURLLoaderFactoryForSubresources, |
tzik | 983fb23 | 2018-04-06 15:47:50 | [diff] [blame] | 627 | base::Unretained(this), std::move(request), process_id, |
| 628 | std::move(original_factory))); |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 629 | return; |
| 630 | } |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 631 | io_thread_->CreateURLLoaderFactoryForSubresources( |
| 632 | std::move(request), process_id, std::move(original_factory)); |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 633 | } |
| 634 | |
John Abd-El-Malek | d5f92034 | 2018-02-12 17:37:36 | [diff] [blame] | 635 | network::mojom::URLLoaderFactoryPtr |
| 636 | URLLoaderInterceptor::GetURLLoaderFactoryForBrowserProcess( |
| 637 | network::mojom::URLLoaderFactoryPtr original_factory) { |
| 638 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 639 | network::mojom::URLLoaderFactoryPtr loader_factory; |
| 640 | browser_process_interceptors_.emplace(std::make_unique<BrowserProcessWrapper>( |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 641 | mojo::MakeRequest(&loader_factory), io_thread_.get(), |
| 642 | std::move(original_factory))); |
John Abd-El-Malek | d5f92034 | 2018-02-12 17:37:36 | [diff] [blame] | 643 | return loader_factory; |
| 644 | } |
| 645 | |
Clark DuVall | 0d278b43 | 2019-06-25 18:24:57 | [diff] [blame] | 646 | void URLLoaderInterceptor::InterceptNavigationRequestCallback( |
| 647 | network::mojom::URLLoaderFactoryRequest* request) { |
| 648 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 649 | |
| 650 | auto proxied_request = std::move(*request); |
| 651 | network::mojom::URLLoaderFactoryPtr target_factory; |
| 652 | *request = mojo::MakeRequest(&target_factory); |
| 653 | |
| 654 | navigation_wrappers_.emplace( |
| 655 | std::make_unique<URLLoaderFactoryNavigationWrapper>( |
| 656 | std::move(proxied_request), std::move(target_factory), |
| 657 | io_thread_.get())); |
| 658 | } |
| 659 | |
John Abd-El-Malek | db3a13b | 2018-05-01 17:52:02 | [diff] [blame] | 660 | bool URLLoaderInterceptor::Intercept(RequestParams* params) { |
| 661 | if (callback_.Run(params)) |
| 662 | return true; |
| 663 | |
| 664 | // mock.failed.request is a special request whereby the query indicates what |
| 665 | // error code to respond with. |
| 666 | if (params->url_request.url.DomainIs("mock.failed.request")) { |
| 667 | std::string query = params->url_request.url.query(); |
| 668 | std::string error_code = query.substr(query.find("=") + 1); |
| 669 | |
| 670 | int error = 0; |
| 671 | base::StringToInt(error_code, &error); |
| 672 | network::URLLoaderCompletionStatus status; |
| 673 | status.error_code = error; |
| 674 | params->client->OnComplete(status); |
| 675 | return true; |
| 676 | } |
| 677 | |
| 678 | return false; |
| 679 | } |
| 680 | |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 681 | void URLLoaderInterceptor::IOState::SubresourceWrapperBindingError( |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 682 | SubresourceWrapper* wrapper) { |
John Abd-El-Malek | d5f92034 | 2018-02-12 17:37:36 | [diff] [blame] | 683 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
David Benjamin | e430b8a | 2019-03-21 20:46:27 | [diff] [blame] | 684 | auto it = subresource_wrappers_.find(wrapper); |
| 685 | DCHECK(it != subresource_wrappers_.end()); |
| 686 | subresource_wrappers_.erase(it); |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 687 | } |
| 688 | |
Yao Xiao | 43ae3bc | 2019-07-09 05:22:23 | [diff] [blame] | 689 | void URLLoaderInterceptor::IOState::Initialize( |
| 690 | const URLLoaderCompletionStatusCallback& completion_status_callback, |
| 691 | base::OnceClosure closure) { |
| 692 | completion_status_callback_ = std::move(completion_status_callback); |
John Abd-El-Malek | 8730158 | 2019-07-27 04:50:52 | [diff] [blame^] | 693 | URLLoaderFactoryGetter::SetGetNetworkFactoryCallbackForTesting( |
| 694 | base::BindRepeating( |
| 695 | &URLLoaderInterceptor::IOState::GetNetworkFactoryCallback, |
| 696 | base::Unretained(this))); |
Clark DuVall | 649c05f | 2019-02-05 23:58:47 | [diff] [blame] | 697 | |
John Abd-El-Malek | 8730158 | 2019-07-27 04:50:52 | [diff] [blame^] | 698 | if (!NavigationURLLoaderImpl::IsNavigationLoaderOnUIEnabled()) { |
| 699 | NavigationURLLoaderImpl::SetURLLoaderFactoryInterceptorForTesting( |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 700 | base::BindRepeating( |
John Abd-El-Malek | 8730158 | 2019-07-27 04:50:52 | [diff] [blame^] | 701 | &URLLoaderInterceptor::IOState::InterceptNavigationRequestCallback, |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 702 | base::Unretained(this))); |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 703 | } |
| 704 | |
John Abd-El-Malek | d5f92034 | 2018-02-12 17:37:36 | [diff] [blame] | 705 | if (!base::FeatureList::IsEnabled(network::features::kNetworkService)) { |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 706 | rmf_interceptor_ = std::make_unique<Interceptor>( |
| 707 | this, base::BindRepeating([]() { |
| 708 | return ResourceMessageFilter::GetCurrentForTesting()->child_id(); |
| 709 | }), |
| 710 | base::BindRepeating([]() { |
John Abd-El-Malek | b165dc5 | 2018-01-18 17:12:18 | [diff] [blame] | 711 | network::mojom::URLLoaderFactory* factory = |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 712 | ResourceMessageFilter::GetCurrentForTesting(); |
| 713 | return factory; |
| 714 | })); |
| 715 | ResourceMessageFilter::SetNetworkFactoryForTesting(rmf_interceptor_.get()); |
| 716 | } |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 717 | if (closure) |
| 718 | std::move(closure).Run(); |
| 719 | } |
| 720 | |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 721 | void URLLoaderInterceptor::IOState::GetNetworkFactoryCallback( |
| 722 | URLLoaderFactoryGetter* url_loader_factory_getter) { |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 723 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 724 | url_loader_factory_getter_wrappers_.emplace( |
| 725 | std::make_unique<URLLoaderFactoryGetterWrapper>(url_loader_factory_getter, |
| 726 | this)); |
| 727 | } |
John Abd-El-Malek | 63ff5f3 | 2017-12-18 22:14:58 | [diff] [blame] | 728 | |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 729 | void URLLoaderInterceptor::IOState::CreateURLLoaderFactoryForSubresources( |
| 730 | network::mojom::URLLoaderFactoryRequest request, |
| 731 | int process_id, |
| 732 | network::mojom::URLLoaderFactoryPtrInfo original_factory) { |
| 733 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 734 | subresource_wrappers_.emplace(std::make_unique<SubresourceWrapper>( |
| 735 | std::move(request), process_id, this, std::move(original_factory))); |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 736 | } |
| 737 | |
Lukasz Anforowicz | 98ccf1c | 2018-08-23 18:14:31 | [diff] [blame] | 738 | // static |
| 739 | std::unique_ptr<content::URLLoaderInterceptor> |
| 740 | URLLoaderInterceptor::SetupRequestFailForURL(const GURL& url, |
Dan Harrington | 942019f | 2019-01-04 17:58:03 | [diff] [blame] | 741 | net::Error error, |
| 742 | base::OnceClosure ready_callback) { |
| 743 | return std::make_unique<content::URLLoaderInterceptor>( |
| 744 | base::BindRepeating( |
| 745 | [](const GURL& url, net::Error error, |
| 746 | content::URLLoaderInterceptor::RequestParams* params) { |
| 747 | if (params->url_request.url != url) |
| 748 | return false; |
| 749 | params->client->OnComplete( |
| 750 | network::URLLoaderCompletionStatus(error)); |
| 751 | return true; |
| 752 | }, |
| 753 | url, error), |
Yao Xiao | 43ae3bc | 2019-07-09 05:22:23 | [diff] [blame] | 754 | URLLoaderCompletionStatusCallback(), std::move(ready_callback)); |
Lukasz Anforowicz | 98ccf1c | 2018-08-23 18:14:31 | [diff] [blame] | 755 | } |
| 756 | |
John Abd-El-Malek | a4bb87f | 2017-11-27 21:32:19 | [diff] [blame] | 757 | } // namespace content |