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

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

Issue 2739323003: DevTools protocol interception, blocking & modification of requests (Closed)
Patch Set: Fix 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_REQUEST_INTERCEPTOR_H_
6 #define CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_URL_REQUEST_INTERCEPTOR_H_
7
8 #include "base/containers/flat_map.h"
9 #include "base/containers/flat_set.h"
10 #include "base/macros.h"
11 #include "base/memory/weak_ptr.h"
12 #include "content/browser/devtools/protocol/network.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "net/base/net_errors.h"
15 #include "net/url_request/url_request_interceptor.h"
16
17 namespace content {
18 namespace protocol {
19 class NetworkHandler;
20 } // namespace
21
22 class BrowserContext;
23 class DevToolsURLInterceptorRequestJob;
24 class WebContents;
25
26 // An interceptor that creates DevToolsURLInterceptorRequestJobs for requests
27 // from pages where interception has been enabled via
28 // Network.enableFetchInterception.
29 class DevToolsURLRequestInterceptor : public net::URLRequestInterceptor {
30 public:
31 explicit DevToolsURLRequestInterceptor(BrowserContext* browser_context);
32 ~DevToolsURLRequestInterceptor() override;
33
34 // Must be called on UI thread.
35 static DevToolsURLRequestInterceptor* FromBrowserContext(
36 BrowserContext* context);
37
38 // net::URLRequestInterceptor implementation:
39 net::URLRequestJob* MaybeInterceptRequest(
40 net::URLRequest* request,
41 net::NetworkDelegate* network_delegate) const override;
42
43 net::URLRequestJob* MaybeInterceptRedirect(
44 net::URLRequest* request,
45 net::NetworkDelegate* network_delegate,
46 const GURL& location) const override;
47
48 net::URLRequestJob* MaybeInterceptResponse(
49 net::URLRequest* request,
50 net::NetworkDelegate* network_delegate) const override;
51
52 enum class CommandStatus {
53 OK,
54 UnknownInterceptionId,
55 CommandAlreadyProcessed,
56 };
57
58 using CommandCallback = base::OnceCallback<void(const CommandStatus& status)>;
59
60 struct Modifications {
61 Modifications(protocol::Maybe<std::string> url,
62 protocol::Maybe<std::string> method,
63 protocol::Maybe<std::string> post_data,
64 protocol::Maybe<protocol::Network::Headers> headers);
65 ~Modifications();
66
67 protocol::Maybe<std::string> url;
68 protocol::Maybe<std::string> method;
69 protocol::Maybe<std::string> post_data;
70 protocol::Maybe<protocol::Network::Headers> headers;
71 };
72
73 // Unfortunately the interface methods are const so we need a mutable inner
74 // class to hold our state.
75 class State {
Sami 2017/05/19 17:15:44 We could also consider making the methods non-cons
alex clarke (OOO till 29th) 2017/05/23 15:46:56 That'd touch a lot of files, I'd prefer to do that
76 public:
77 State();
78 ~State();
79
80 void AllowRequest(std::string interception_id, CommandCallback callback);
81
82 // Must be called on the IO thread.
83 void BlockRequest(std::string interception_id,
84 net::Error error_reason,
85 CommandCallback callback);
86
87 // Must be called on the IO thread.
88 void ModifyRequest(std::string interception_id,
89 std::unique_ptr<Modifications> modifications,
90 CommandCallback callback);
91
92 // Must be called on the IO thread.
93 void MockResponse(std::string interception_id,
94 std::string raw_response,
95 CommandCallback callback);
96
97 // Returns true if |request| should be intercepted and sets
98 // |network_handler|. Must be called on the IO thread.
99 bool ShouldInterceptRequest(
100 const net::URLRequest* request,
101 base::WeakPtr<protocol::NetworkHandler>* network_handler) const;
102
103 // Must be called on the IO thread.
104 void StartInterceptingRequests(
105 WebContents* web_contents,
106 base::WeakPtr<protocol::NetworkHandler> network_handler);
107
108 // Must be called on the IO thread.
109 void StopInterceptingRequests(WebContents* web_contents);
110
111 // Registers a |sub_request| that should not be intercepted.
112 void RegisterSubRequest(const net::URLRequest* sub_request);
113
114 // Unregisters a |sub_request|. Must be called on the IO thread.
115 void UnregisterSubRequest(const net::URLRequest* sub_request);
116
117 // To make the user's life easier we make sure requests in a redirect chain
118 // all have the same id. Must be called on the IO thread.
119 void ExpectRequestAfterRedirect(const net::URLRequest* request,
120 std::string id);
121
122 // Must be called on the IO thread.
123 std::string GetIdForRequest(const net::URLRequest* request,
124 bool* is_redirect);
125
126 // Must be called on the IO thread.
127 void RegisterJob(DevToolsURLInterceptorRequestJob* job,
128 const std::string& interception_id);
129
130 // Must be called on the IO thread.
131 void UnregisterJob(const std::string& interception_id);
132
133 base::WeakPtr<State> GetWeakPtr() { return weak_ptr_factory_.GetWeakPtr(); }
134
135 private:
136 class InterceptedWebContentsObserver;
137
138 void StartInterceptingRequestsInternal(
139 int render_frame_id,
140 int frame_tree_node_id,
141 base::WeakPtr<protocol::NetworkHandler> network_handler);
142
143 void StopInterceptingRequestsInternal(int render_frame_id,
144 int frame_tree_node_id);
145
146 // Returns a WeakPtr to the DevToolsURLInterceptorRequestJob corresponding
147 // to |interception_id|. Must be called on the IO thread.
148 DevToolsURLInterceptorRequestJob* GetJob(
149 const std::string& interception_id) const;
150
151 base::flat_map<int, base::WeakPtr<protocol::NetworkHandler>>
152 intercepted_render_frames_;
153
154 base::flat_map<int, base::WeakPtr<protocol::NetworkHandler>>
155 intercepted_frame_tree_nodes_;
156
157 base::flat_map<WebContents*,
158 std::unique_ptr<InterceptedWebContentsObserver>>
159 observers_;
160
161 base::flat_map<std::string, DevToolsURLInterceptorRequestJob*>
162 interception_id_to_job_map_;
163
164 base::flat_set<const net::URLRequest*> sub_requests_;
165 base::flat_map<const net::URLRequest*, std::string> expected_redirects_;
166 size_t next_id_;
167
168 base::WeakPtrFactory<State> weak_ptr_factory_;
169 };
170
171 State& state() const { return *state_; }
172
173 private:
174 BrowserContext* const browser_context_;
175
176 std::unique_ptr<State> state_;
177
178 DISALLOW_COPY_AND_ASSIGN(DevToolsURLRequestInterceptor);
179 };
180
181 } // namespace content
182
183 #endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_URL_REQUEST_INTERCEPTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698