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

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: Add a comment Created 3 years, 6 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 "content/browser/devtools/devtools_url_request_interceptor.h"
11 #include "content/browser/devtools/protocol/network.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "net/url_request/url_request.h"
14 #include "net/url_request/url_request_job.h"
15
16 namespace net {
17 class AuthChallengeInfo;
18 class UploadDataStream;
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 scoped_refptr<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 WebContents* web_contents,
35 base::WeakPtr<protocol::NetworkHandler> network_handler,
36 bool is_redirect);
37
38 ~DevToolsURLInterceptorRequestJob() override;
39
40 // net::URLRequestJob implementation:
41 void SetExtraRequestHeaders(const net::HttpRequestHeaders& headers) override;
42 void Start() override;
43 void Kill() override;
44 int ReadRawData(net::IOBuffer* buf, int buf_size) override;
45 int GetResponseCode() const override;
46 void GetResponseInfo(net::HttpResponseInfo* info) override;
47 bool GetMimeType(std::string* mime_type) const override;
48 bool GetCharset(std::string* charset) override;
49 void GetLoadTimingInfo(net::LoadTimingInfo* load_timing_info) const override;
50 bool NeedsAuth() override;
51 void GetAuthChallengeInfo(
52 scoped_refptr<net::AuthChallengeInfo>* auth_info) override;
53
54 void SetAuth(const net::AuthCredentials& credentials) override;
55 void CancelAuth() override;
56
57 // net::URLRequest::Delegate methods:
58 void OnAuthRequired(net::URLRequest* request,
59 net::AuthChallengeInfo* auth_info) override;
60 void OnCertificateRequested(
61 net::URLRequest* request,
62 net::SSLCertRequestInfo* cert_request_info) override;
63 void OnSSLCertificateError(net::URLRequest* request,
64 const net::SSLInfo& ssl_info,
65 bool fatal) override;
66 void OnResponseStarted(net::URLRequest* request, int net_error) override;
67 void OnReadCompleted(net::URLRequest* request, int bytes_read) override;
68 void OnReceivedRedirect(net::URLRequest* request,
69 const net::RedirectInfo& redirect_info,
70 bool* defer_redirect) override;
71
72 // Must be called on IO thread.
73 void StopIntercepting();
74
75 // Must be called only once per interception. Returns true on success or false
76 // otherwise. Must be called on IO thread.
77 bool ContinueInterceptedRequest(
78 std::unique_ptr<DevToolsURLRequestInterceptor::Modifications>
79 modifications);
80
81 WebContents* web_contents() const { return web_contents_; }
82
83 private:
84 class SubRequest;
85
86 // We keep a copy of the original request details to facilitate the
87 // Network.modifyRequest command which could potentially change any of these
88 // fields.
89 struct RequestDetails {
90 RequestDetails(const GURL& url,
91 const std::string& method,
92 std::unique_ptr<net::UploadDataStream> post_data,
93 const net::HttpRequestHeaders& extra_request_headers,
94 const net::RequestPriority& priority,
95 const net::URLRequestContext* url_request_context);
96 ~RequestDetails();
97
98 GURL url;
99 std::string method;
100 std::unique_ptr<net::UploadDataStream> post_data;
101 net::HttpRequestHeaders extra_request_headers;
102 net::RequestPriority priority;
103 const net::URLRequestContext* url_request_context;
104 };
105
106 // If the request was either allowed or modified, a SubRequest will be used to
107 // perform the fetch and the results proxied to the original request. This
108 // gives us the flexibility to pretend redirects didn't happen if the user
109 // chooses to mock the response. Note this SubRequest is ignored by the
110 // interceptor.
111 class SubRequest {
112 public:
113 SubRequest(
114 DevToolsURLInterceptorRequestJob::RequestDetails& request_details,
115 DevToolsURLInterceptorRequestJob* devtools_interceptor_request_job,
116 scoped_refptr<DevToolsURLRequestInterceptor::State>
117 devtools_url_request_interceptor_state);
118 ~SubRequest();
119
120 void Cancel();
121
122 net::URLRequest* request() const { return request_.get(); }
123
124 private:
125 std::unique_ptr<net::URLRequest> request_;
126
127 DevToolsURLInterceptorRequestJob*
128 devtools_interceptor_request_job_; // NOT OWNED.
129
130 scoped_refptr<DevToolsURLRequestInterceptor::State>
131 devtools_url_request_interceptor_state_;
132 bool fetch_in_progress_;
133 };
134
135 class MockResponseDetails {
136 public:
137 MockResponseDetails(std::string response_bytes,
138 base::TimeTicks response_time);
139
140 MockResponseDetails(
141 const scoped_refptr<net::HttpResponseHeaders>& response_headers,
142 std::string response_bytes,
143 size_t read_offset,
144 base::TimeTicks response_time);
145
146 ~MockResponseDetails();
147
148 scoped_refptr<net::HttpResponseHeaders>& response_headers() {
149 return response_headers_;
150 }
151
152 base::TimeTicks response_time() const { return response_time_; }
153
154 int ReadRawData(net::IOBuffer* buf, int buf_size);
155
156 private:
157 scoped_refptr<net::HttpResponseHeaders> response_headers_;
158 std::string response_bytes_;
159 size_t read_offset_;
160 base::TimeTicks response_time_;
161 };
162
163 // Retrieves the response headers from either the |sub_request_| or the
164 // |mock_response_|. In some cases (e.g. file access) this may be null.
165 const net::HttpResponseHeaders* GetHttpResponseHeaders() const;
166
167 scoped_refptr<DevToolsURLRequestInterceptor::State>
168 devtools_url_request_interceptor_state_;
169 RequestDetails request_details_;
170 std::unique_ptr<SubRequest> sub_request_;
171 std::unique_ptr<MockResponseDetails> mock_response_details_;
172 std::unique_ptr<net::RedirectInfo> redirect_;
173 bool waiting_for_user_response_;
174 bool intercepting_requests_;
175 bool killed_;
176 scoped_refptr<net::AuthChallengeInfo> auth_info_;
177
178 const std::string interception_id_;
179 WebContents* const web_contents_;
180 const base::WeakPtr<protocol::NetworkHandler> network_handler_;
181 const bool is_redirect_;
182 base::WeakPtrFactory<DevToolsURLInterceptorRequestJob> weak_ptr_factory_;
183
184 DISALLOW_COPY_AND_ASSIGN(DevToolsURLInterceptorRequestJob);
185 };
186
187 } // namespace content
188
189 #endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_URL_INTERCEPTOR_REQUEST_JOB_H_
OLDNEW
« no previous file with comments | « content/browser/BUILD.gn ('k') | content/browser/devtools/devtools_url_interceptor_request_job.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698