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..30bb21c98a67537a353558843b2602a522376cb7 |
--- /dev/null |
+++ b/content/browser/devtools/devtools_url_interceptor_request_job.h |
@@ -0,0 +1,228 @@ |
+// 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/base/net_errors.h" |
+#include "net/url_request/url_request.h" |
+#include "net/url_request/url_request_job.h" |
+ |
+namespace content { |
+namespace protocol { |
+class NetworkHandler; |
+} // namespace |
+ |
+// A URLRequestJob that allows programmatic request blocking / modification or |
+// response mocking. |
+class DevToolsURLInterceptorRequestJob : public net::URLRequestJob, |
Sami
2017/03/22 17:00:46
I'm wondering if there's any risk of wires getting
alex clarke (OOO till 29th)
2017/03/24 13:57:23
When we modify a request I think we do need to cre
|
+ public net::URLRequest::Delegate { |
+ public: |
+ DevToolsURLInterceptorRequestJob( |
+ base::WeakPtr<DevToolsURLRequestInterceptor::State> interceptor_state, |
+ const std::string& intercept_id, |
+ net::URLRequest* origional_request, |
+ net::NetworkDelegate* origional_network_delegate, |
+ 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; |
+ |
+ bool OnAllow(); |
Sami
2017/03/22 17:00:45
minor naming nit: These 4 seem like normal methods
alex clarke (OOO till 29th)
2017/03/24 13:57:23
Done.
|
+ bool OnBlock(net::Error error_reason); |
+ bool OnModifyRequest(protocol::Maybe<protocol::String> url, |
+ protocol::Maybe<protocol::String> method, |
+ protocol::Maybe<protocol::String> post_data, |
+ protocol::Maybe<protocol::Network::Headers> headers); |
+ bool OnMockResponse(const protocol::String& raw_response); |
+ |
+ void OnNetworkHandlerDeleted(); |
+ |
+ // 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, |
+ const std::string& 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::string post_data; |
+ net::HttpRequestHeaders extra_request_headers; |
+ net::RequestPriority priority; |
+ const net::URLRequestContext* url_request_context; |
+ }; |
+ |
+ private: |
+ class SubRequest; |
+ |
+ struct ModifyRequest { |
+ ModifyRequest(protocol::Maybe<protocol::String> url, |
+ protocol::Maybe<protocol::String> method, |
+ protocol::Maybe<protocol::String> post_data, |
+ protocol::Maybe<protocol::Network::Headers> headers); |
+ ModifyRequest(ModifyRequest&& other); |
+ ~ModifyRequest(); |
+ |
+ protocol::Maybe<protocol::String> url; |
+ protocol::Maybe<protocol::String> method; |
+ protocol::Maybe<protocol::String> post_data; |
+ protocol::Maybe<protocol::Network::Headers> headers; |
+ }; |
+ |
+ // 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. |
+ class SubRequest { |
+ public: |
+ SubRequest( |
+ const DevToolsURLInterceptorRequestJob::RequestDetails& request_details, |
+ base::WeakPtr<DevToolsURLRequestInterceptor::State> interceptor_state, |
+ DevToolsURLInterceptorRequestJob* devtools_interceptor_request_job); |
+ ~SubRequest(); |
+ |
+ void Cancel(); |
+ |
+ net::URLRequest* request() const { return request_.get(); } |
+ |
+ private: |
+ enum { kBufSize = 4096 }; |
+ |
+ std::unique_ptr<net::URLRequest> request_; |
+ |
+ // Buffer that |request| writes into. |
+ scoped_refptr<net::IOBuffer> buf_; |
Sami
2017/03/22 17:00:45
Is this used?
alex clarke (OOO till 29th)
2017/03/24 13:57:23
It was in earlier patch sets, can remove it now :)
|
+ |
+ base::WeakPtr<DevToolsURLRequestInterceptor::State> interceptor_state_; |
+ |
+ DevToolsURLInterceptorRequestJob* |
+ devtools_interceptor_request_job_; // NOT OWNED. |
+ |
+ bool fetch_in_progress_; |
+ }; |
+ |
+ class MockResponse { |
+ public: |
+ MockResponse(std::string response_bytes, |
+ base::TimeTicks response_time, |
+ const std::string& intercept_id); |
+ |
+ MockResponse( |
+ const scoped_refptr<net::HttpResponseHeaders>& response_headers, |
+ std::string response_bytes, |
+ size_t read_offset, |
+ base::TimeTicks response_time); |
+ |
+ ~MockResponse(); |
+ |
+ const scoped_refptr<net::HttpResponseHeaders>& response_headers() const { |
+ 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 ModifyRequestOnIoThread(std::unique_ptr<ModifyRequest> modify_request); |
+ void ProcessMockResponeOnIoThread(protocol::String response); |
+ void ProcessOnAllowOnIoThread(); |
+ void SendInterceptedRequestEventOnUiThread(); |
+ void SendInterceptedRedirectEventOnUiThread( |
+ protocol::DictionaryValue* headers_dict, |
+ int http_status_code, |
+ std::string redirect_url); |
+ |
+ struct IoThreadOnly { |
+ explicit IoThreadOnly(net::URLRequest* request); |
+ ~IoThreadOnly(); |
+ |
+ RequestDetails request_details; |
+ std::unique_ptr<SubRequest> sub_request; |
+ std::unique_ptr<MockResponse> mock_response; |
+ std::unique_ptr<net::RedirectInfo> redirect; |
+ }; |
+ |
+ struct UiThreadOnly { |
+ UiThreadOnly(); |
+ |
+ bool waiting_for_user_response; |
+ }; |
+ |
+ IoThreadOnly io_thread_only_; |
+ IoThreadOnly& io_thread_only() { |
Sami
2017/03/22 17:00:45
Thanks for making threading explicit!
alex clarke (OOO till 29th)
2017/03/24 13:57:23
Acknowledged.
|
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ return io_thread_only_; |
+ } |
+ const IoThreadOnly& io_thread_only() const { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ return io_thread_only_; |
+ } |
+ |
+ UiThreadOnly ui_thread_only_; |
+ UiThreadOnly& ui_thread_only() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ return ui_thread_only_; |
+ } |
+ const UiThreadOnly& ui_thread_only() const { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ return ui_thread_only_; |
+ } |
+ |
+ const std::string intercept_id_; |
+ protocol::NetworkHandler* network_handler_; // NOT OWNED |
+ const base::WeakPtr<DevToolsURLRequestInterceptor::State> interceptor_state_; |
+ const scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner_; |
+ const bool is_redirect_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(DevToolsURLInterceptorRequestJob); |
+}; |
+ |
+} // namespace content |
+ |
+#endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_URL_INTERCEPTOR_REQUEST_JOB_H_ |