Chromium Code Reviews| Index: content/browser/devtools/devtools_url_interceptor_request_job.h |
| diff --git a/content/browser/devtools/devtools_url_interceptor_request_job.h b/content/browser/devtools/devtools_url_interceptor_request_job.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..efa314298e0b68cf837cf8b7216aa5e37f2887e2 |
| --- /dev/null |
| +++ b/content/browser/devtools/devtools_url_interceptor_request_job.h |
| @@ -0,0 +1,178 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_URL_INTERCEPTOR_REQUEST_JOB_H_ |
| +#define CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_URL_INTERCEPTOR_REQUEST_JOB_H_ |
| + |
| +#include "base/macros.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "base/single_thread_task_runner.h" |
| +#include "content/browser/devtools/devtools_url_request_interceptor.h" |
| +#include "content/browser/devtools/protocol/network.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "net/url_request/url_request.h" |
| +#include "net/url_request/url_request_job.h" |
| + |
| +namespace net { |
| +class UploadElementReader; |
| +} |
| + |
| +namespace content { |
| + |
| +// A URLRequestJob that allows programmatic request blocking / modification or |
| +// response mocking. This class should only be accessed on the IO thread. |
| +class DevToolsURLInterceptorRequestJob : public net::URLRequestJob, |
| + public net::URLRequest::Delegate { |
| + public: |
| + DevToolsURLInterceptorRequestJob( |
| + base::WeakPtr<DevToolsURLRequestInterceptor::State> |
| + devtools_url_request_interceptor_state, |
| + const std::string& intercept_id, |
| + net::URLRequest* original_request, |
| + net::NetworkDelegate* original_network_delegate, |
| + base::WeakPtr<protocol::NetworkHandler> network_handler, |
| + bool is_redirect); |
| + |
| + ~DevToolsURLInterceptorRequestJob() override; |
| + |
| + // net::URLRequestJob implementation: |
| + void SetExtraRequestHeaders(const net::HttpRequestHeaders& headers) override; |
| + void Start() override; |
| + void Kill() override; |
| + int ReadRawData(net::IOBuffer* buf, int buf_size) override; |
| + int GetResponseCode() const override; |
| + void GetResponseInfo(net::HttpResponseInfo* info) override; |
| + bool GetMimeType(std::string* mime_type) const override; |
| + bool GetCharset(std::string* charset) override; |
| + void GetLoadTimingInfo(net::LoadTimingInfo* load_timing_info) const override; |
| + |
| + // net::URLRequest::Delegate methods: |
| + void OnAuthRequired(net::URLRequest* request, |
| + net::AuthChallengeInfo* auth_info) override; |
| + void OnSSLCertificateError(net::URLRequest* request, |
| + const net::SSLInfo& ssl_info, |
| + bool fatal) override; |
| + void OnResponseStarted(net::URLRequest* request, int net_error) override; |
| + void OnReadCompleted(net::URLRequest* request, int bytes_read) override; |
| + void OnReceivedRedirect(net::URLRequest* request, |
| + const net::RedirectInfo& redirect_info, |
| + bool* defer_redirect) override; |
| + |
| + DevToolsURLRequestInterceptor::CommandStatus AllowRequest(); |
| + DevToolsURLRequestInterceptor::CommandStatus BlockRequest( |
| + net::Error error_reason); |
| + DevToolsURLRequestInterceptor::CommandStatus ModifyRequest( |
| + std::unique_ptr<DevToolsURLRequestInterceptor::Modifications> |
| + modifications); |
| + DevToolsURLRequestInterceptor::CommandStatus MockResponse( |
| + std::string raw_response); |
|
Sami
2017/05/19 17:15:43
nit: const ref
alex clarke (OOO till 29th)
2017/05/23 15:46:55
Can't do that safely since this is called via post
|
| + |
| + private: |
| + class SubRequest; |
| + |
| + // We keep a copy of the original request details to facilitate the |
| + // Network.modifyRequest command which could potentially change any of these |
| + // fields. |
| + struct RequestDetails { |
| + RequestDetails(const GURL& url, |
| + const std::string& method, |
| + std::unique_ptr<net::UploadElementReader> post_data, |
| + const net::HttpRequestHeaders& extra_request_headers, |
| + const net::RequestPriority& priority, |
| + const net::URLRequestContext* url_request_context); |
| + ~RequestDetails(); |
| + |
| + GURL url; |
| + std::string method; |
| + std::unique_ptr<net::UploadElementReader> post_data; |
| + net::HttpRequestHeaders extra_request_headers; |
| + net::RequestPriority priority; |
| + const net::URLRequestContext* url_request_context; |
| + }; |
| + |
| + // If the request was either allowed or modified, a SubRequest will be used to |
| + // perform the fetch and the results proxied to the original request. This |
| + // gives us the flexibility to pretend redirects didn't happen if the user |
| + // chooses to mock the response. |
|
Sami
2017/05/19 17:15:43
Maybe also worth mentioning that the subrequest is
alex clarke (OOO till 29th)
2017/05/23 15:46:56
Done.
|
| + class SubRequest { |
| + public: |
| + SubRequest( |
| + DevToolsURLInterceptorRequestJob::RequestDetails& request_details, |
| + DevToolsURLInterceptorRequestJob* devtools_interceptor_request_job); |
| + ~SubRequest(); |
| + |
| + void Cancel(); |
| + |
| + net::URLRequest* request() const { return request_.get(); } |
| + |
| + private: |
| + std::unique_ptr<net::URLRequest> request_; |
| + |
| + DevToolsURLInterceptorRequestJob* |
| + devtools_interceptor_request_job_; // NOT OWNED. |
| + |
| + base::WeakPtr<DevToolsURLRequestInterceptor::State> |
| + devtools_url_request_interceptor_state_; |
| + bool fetch_in_progress_; |
| + }; |
| + |
| + class MockResponseDetails { |
| + public: |
| + MockResponseDetails(std::string response_bytes, |
| + base::TimeTicks response_time); |
| + |
| + MockResponseDetails( |
| + const scoped_refptr<net::HttpResponseHeaders>& response_headers, |
| + std::string response_bytes, |
| + size_t read_offset, |
| + base::TimeTicks response_time); |
| + |
| + ~MockResponseDetails(); |
| + |
| + const scoped_refptr<net::HttpResponseHeaders>& response_headers() const { |
|
Sami
2017/05/19 17:15:43
nit: Maybe just return net::HttpResponseHeaders* s
alex clarke (OOO till 29th)
2017/05/23 15:46:55
Actually that's exactly what DevToolsURLIntercepto
|
| + return response_headers_; |
| + } |
| + |
| + base::TimeTicks response_time() const { return response_time_; } |
| + |
| + int ReadRawData(net::IOBuffer* buf, int buf_size); |
| + |
| + private: |
| + scoped_refptr<net::HttpResponseHeaders> response_headers_; |
| + std::string response_bytes_; |
| + size_t read_offset_; |
| + base::TimeTicks response_time_; |
| + }; |
| + |
| + // Retrieves the response headers from either the |sub_request_| or the |
| + // |mock_response_|. In some cases (e.g. file access) this may be null. |
| + const net::HttpResponseHeaders* GetHttpResponseHeaders() const; |
| + |
| + void DispatchError(net::Error reason); |
| + void SendInterceptedRequestEventOnUiThread(); |
| + void SendInterceptedRedirectEventOnUiThread( |
| + protocol::DictionaryValue* headers_dict, |
| + int http_status_code, |
| + std::string redirect_url); |
|
Sami
2017/05/19 17:15:43
nit: const ref
alex clarke (OOO till 29th)
2017/05/23 15:46:56
Can't do that due to PostTask and the source going
|
| + |
| + base::WeakPtr<DevToolsURLRequestInterceptor::State> |
| + devtools_url_request_interceptor_state_; |
| + RequestDetails request_details_; |
| + std::unique_ptr<SubRequest> sub_request_; |
| + std::unique_ptr<MockResponseDetails> mock_response_details_; |
| + std::unique_ptr<net::RedirectInfo> redirect_; |
| + bool waiting_for_user_response_; |
|
Sami
2017/05/19 17:15:43
Uninitialized?
alex clarke (OOO till 29th)
2017/05/23 15:46:56
Done.
|
| + |
| + const std::string intercept_id_; |
| + base::WeakPtr<protocol::NetworkHandler> network_handler_; |
| + const scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner_; |
| + const bool is_redirect_; |
| + base::WeakPtrFactory<DevToolsURLInterceptorRequestJob> weak_ptr_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(DevToolsURLInterceptorRequestJob); |
| +}; |
| + |
| +} // namespace content |
| + |
| +#endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_URL_INTERCEPTOR_REQUEST_JOB_H_ |