Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(252)

Side by Side Diff: content/browser/devtools/devtools_url_interceptor_request_job.h

Issue 2739323003: DevTools protocol interception, blocking & modification of requests (Closed)
Patch Set: Address remaining TODOs Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 #ifndef CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_URL_INTERCEPTOR_REQUEST_JOB_H_
6 #define CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_URL_INTERCEPTOR_REQUEST_JOB_H_
7
8 #include "base/macros.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/single_thread_task_runner.h"
11 #include "content/browser/devtools/devtools_url_request_interceptor.h"
12 #include "content/browser/devtools/protocol/network.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "net/url_request/url_request.h"
15 #include "net/url_request/url_request_job.h"
16
17 namespace net {
18 class UploadElementReader;
19 }
20
21 namespace content {
22
23 // A URLRequestJob that allows programmatic request blocking / modification or
24 // response mocking. This class should only be accessed on the IO thread.
25 class DevToolsURLInterceptorRequestJob : public net::URLRequestJob,
26 public net::URLRequest::Delegate {
27 public:
28 DevToolsURLInterceptorRequestJob(
29 base::WeakPtr<DevToolsURLRequestInterceptor::State>
30 devtools_url_request_interceptor_state,
31 const std::string& interception_id,
32 net::URLRequest* original_request,
33 net::NetworkDelegate* original_network_delegate,
34 base::WeakPtr<protocol::NetworkHandler> network_handler,
35 bool is_redirect);
36
37 ~DevToolsURLInterceptorRequestJob() override;
38
39 // net::URLRequestJob implementation:
40 void SetExtraRequestHeaders(const net::HttpRequestHeaders& headers) override;
41 void Start() override;
42 void Kill() override;
43 int ReadRawData(net::IOBuffer* buf, int buf_size) override;
44 int GetResponseCode() const override;
45 void GetResponseInfo(net::HttpResponseInfo* info) override;
46 bool GetMimeType(std::string* mime_type) const override;
47 bool GetCharset(std::string* charset) override;
48 void GetLoadTimingInfo(net::LoadTimingInfo* load_timing_info) const override;
49
50 // net::URLRequest::Delegate methods:
51 void OnAuthRequired(net::URLRequest* request,
52 net::AuthChallengeInfo* auth_info) override;
53 void OnCertificateRequested(
54 net::URLRequest* request,
55 net::SSLCertRequestInfo* cert_request_info) override;
56 void OnSSLCertificateError(net::URLRequest* request,
57 const net::SSLInfo& ssl_info,
58 bool fatal) override;
59 void OnResponseStarted(net::URLRequest* request, int net_error) override;
60 void OnReadCompleted(net::URLRequest* request, int bytes_read) override;
61 void OnReceivedRedirect(net::URLRequest* request,
62 const net::RedirectInfo& redirect_info,
63 bool* defer_redirect) override;
64
65 DevToolsURLRequestInterceptor::CommandStatus ContinueRequest(
66 std::unique_ptr<DevToolsURLRequestInterceptor::Modifications>
67 modifications);
68
69 private:
70 class SubRequest;
71
72 // We keep a copy of the original request details to facilitate the
73 // Network.modifyRequest command which could potentially change any of these
74 // fields.
75 struct RequestDetails {
76 RequestDetails(const GURL& url,
77 const std::string& method,
78 std::unique_ptr<net::UploadElementReader> post_data,
79 const net::HttpRequestHeaders& extra_request_headers,
80 const net::RequestPriority& priority,
81 const net::URLRequestContext* url_request_context);
82 ~RequestDetails();
83
84 GURL url;
85 std::string method;
86 std::unique_ptr<net::UploadElementReader> post_data;
87 net::HttpRequestHeaders extra_request_headers;
88 net::RequestPriority priority;
89 const net::URLRequestContext* url_request_context;
90 };
91
92 // If the request was either allowed or modified, a SubRequest will be used to
93 // perform the fetch and the results proxied to the original request. This
94 // gives us the flexibility to pretend redirects didn't happen if the user
95 // chooses to mock the response. Note this SubRequest is ignored by the
96 // interceptor.
97 class SubRequest {
98 public:
99 SubRequest(
100 DevToolsURLInterceptorRequestJob::RequestDetails& request_details,
101 DevToolsURLInterceptorRequestJob* devtools_interceptor_request_job);
102 ~SubRequest();
103
104 void Cancel();
105
106 net::URLRequest* request() const { return request_.get(); }
107
108 private:
109 std::unique_ptr<net::URLRequest> request_;
110
111 DevToolsURLInterceptorRequestJob*
112 devtools_interceptor_request_job_; // NOT OWNED.
113
114 base::WeakPtr<DevToolsURLRequestInterceptor::State>
115 devtools_url_request_interceptor_state_;
116 bool fetch_in_progress_;
117 };
118
119 class MockResponseDetails {
120 public:
121 MockResponseDetails(std::string response_bytes,
122 base::TimeTicks response_time);
123
124 MockResponseDetails(
125 const scoped_refptr<net::HttpResponseHeaders>& response_headers,
126 std::string response_bytes,
127 size_t read_offset,
128 base::TimeTicks response_time);
129
130 ~MockResponseDetails();
131
132 scoped_refptr<net::HttpResponseHeaders>& response_headers() {
133 return response_headers_;
134 }
135
136 base::TimeTicks response_time() const { return response_time_; }
137
138 int ReadRawData(net::IOBuffer* buf, int buf_size);
139
140 private:
141 scoped_refptr<net::HttpResponseHeaders> response_headers_;
142 std::string response_bytes_;
143 size_t read_offset_;
144 base::TimeTicks response_time_;
145 };
146
147 // Retrieves the response headers from either the |sub_request_| or the
148 // |mock_response_|. In some cases (e.g. file access) this may be null.
149 const net::HttpResponseHeaders* GetHttpResponseHeaders() const;
150
151 void SendRequestInterceptedEventOnUiThread();
152 void SendRedirectInterceptedEventOnUiThread(
153 protocol::DictionaryValue* headers_dict,
154 int http_status_code,
155 std::string redirect_url);
156
157 base::WeakPtr<DevToolsURLRequestInterceptor::State>
158 devtools_url_request_interceptor_state_;
159 RequestDetails request_details_;
160 std::unique_ptr<SubRequest> sub_request_;
161 std::unique_ptr<MockResponseDetails> mock_response_details_;
162 std::unique_ptr<net::RedirectInfo> redirect_;
163 bool waiting_for_user_response_;
164
165 const std::string interception_id_;
166 base::WeakPtr<protocol::NetworkHandler> network_handler_;
167 const scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner_;
168 const bool is_redirect_;
169 base::WeakPtrFactory<DevToolsURLInterceptorRequestJob> weak_ptr_factory_;
170
171 DISALLOW_COPY_AND_ASSIGN(DevToolsURLInterceptorRequestJob);
172 };
173
174 } // namespace content
175
176 #endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_URL_INTERCEPTOR_REQUEST_JOB_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698