OLD | NEW |
(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 |
| 25 // An interceptor that creates DevToolsURLInterceptorRequestJobs for requests |
| 26 // from pages where interception has been enabled via |
| 27 // Network.enableFetchInterception. |
| 28 class DevToolsURLRequestInterceptor : public net::URLRequestInterceptor { |
| 29 public: |
| 30 explicit DevToolsURLRequestInterceptor(BrowserContext* browser_context); |
| 31 ~DevToolsURLRequestInterceptor() override; |
| 32 |
| 33 // Must be called on UI thread. |
| 34 static DevToolsURLRequestInterceptor* FromBrowserContext( |
| 35 BrowserContext* context); |
| 36 |
| 37 // net::URLRequestInterceptor implementation: |
| 38 net::URLRequestJob* MaybeInterceptRequest( |
| 39 net::URLRequest* request, |
| 40 net::NetworkDelegate* network_delegate) const override; |
| 41 |
| 42 net::URLRequestJob* MaybeInterceptRedirect( |
| 43 net::URLRequest* request, |
| 44 net::NetworkDelegate* network_delegate, |
| 45 const GURL& location) const override; |
| 46 |
| 47 net::URLRequestJob* MaybeInterceptResponse( |
| 48 net::URLRequest* request, |
| 49 net::NetworkDelegate* network_delegate) const override; |
| 50 |
| 51 enum class CommandStatus { |
| 52 OK, |
| 53 UnknownInterceptionId, |
| 54 CommandAlreadyProcessed, |
| 55 }; |
| 56 |
| 57 using CommandCallback = base::OnceCallback<void(const CommandStatus& status)>; |
| 58 |
| 59 struct Modifications { |
| 60 Modifications(protocol::Maybe<protocol::String> url, |
| 61 protocol::Maybe<protocol::String> method, |
| 62 protocol::Maybe<protocol::String> post_data, |
| 63 protocol::Maybe<protocol::Network::Headers> headers); |
| 64 ~Modifications(); |
| 65 |
| 66 protocol::Maybe<protocol::String> url; |
| 67 protocol::Maybe<protocol::String> method; |
| 68 protocol::Maybe<protocol::String> post_data; |
| 69 protocol::Maybe<protocol::Network::Headers> headers; |
| 70 }; |
| 71 |
| 72 // Unfortunately the interface methods are const so we need a mutable inner |
| 73 // class to hold our state. |
| 74 class State { |
| 75 public: |
| 76 State(); |
| 77 ~State(); |
| 78 |
| 79 void AllowRequest(protocol::String interception_id, |
| 80 CommandCallback callback); |
| 81 |
| 82 // Must be called on the IO thread. |
| 83 void BlockRequest(protocol::String interception_id, |
| 84 net::Error error_reason, |
| 85 CommandCallback callback); |
| 86 |
| 87 // Must be called on the IO thread. |
| 88 void ModifyRequest(protocol::String interception_id, |
| 89 std::unique_ptr<Modifications> modifications, |
| 90 CommandCallback callback); |
| 91 |
| 92 // Must be called on the IO thread. |
| 93 void MockResponse(protocol::String interception_id, |
| 94 protocol::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 StartInterceptingRequestsFrom( |
| 105 int render_frame_id, |
| 106 int frame_tree_node_id, |
| 107 base::WeakPtr<protocol::NetworkHandler> network_handler); |
| 108 |
| 109 // Must be called on the IO thread. |
| 110 void StopInterceptingRequestsFrom(int render_frame_id, |
| 111 int frame_tree_node_id); |
| 112 |
| 113 // Registers a |sub_request| that should not be intercepted. |
| 114 void RegisterSubRequest(const net::URLRequest* sub_request); |
| 115 |
| 116 // Unregisters a |sub_request|. Must be called on the IO thread. |
| 117 void UnregisterSubRequest(const net::URLRequest* sub_request); |
| 118 |
| 119 // To make the user's life easier we make sure requests in a redirect chain |
| 120 // all have the same id. Must be called on the IO thread. |
| 121 void ExpectRequestAfterRedirect(const net::URLRequest* request, |
| 122 std::string id); |
| 123 |
| 124 // Must be called on the IO thread. |
| 125 std::string GetIdForRequest(const net::URLRequest* request, |
| 126 bool* is_redirect); |
| 127 |
| 128 // Must be called on the IO thread. |
| 129 void RegisterJob(DevToolsURLInterceptorRequestJob* job, |
| 130 const std::string& interception_id); |
| 131 |
| 132 // Must be called on the IO thread. |
| 133 void UnregisterJob(const std::string& interception_id); |
| 134 |
| 135 base::WeakPtr<State> GetWeakPtr() { return weak_ptr_factory_.GetWeakPtr(); } |
| 136 |
| 137 private: |
| 138 // Returns a WeakPtr to the DevToolsURLInterceptorRequestJob corresponding |
| 139 // to |interception_id|. Must be called on the IO thread. |
| 140 DevToolsURLInterceptorRequestJob* GetJob( |
| 141 const std::string& interception_id) const; |
| 142 |
| 143 base::flat_map<int, base::WeakPtr<protocol::NetworkHandler>> |
| 144 intercepted_render_frames_; |
| 145 |
| 146 base::flat_map<int, base::WeakPtr<protocol::NetworkHandler>> |
| 147 intercepted_frame_tree_nodes_; |
| 148 |
| 149 base::flat_map<std::string, DevToolsURLInterceptorRequestJob*> |
| 150 interception_id_to_job_map_; |
| 151 |
| 152 base::flat_set<const net::URLRequest*> sub_requests_; |
| 153 base::flat_map<const net::URLRequest*, std::string> expected_redirects_; |
| 154 size_t next_id_; |
| 155 |
| 156 base::WeakPtrFactory<State> weak_ptr_factory_; |
| 157 }; |
| 158 |
| 159 State& state() const { return *state_; } |
| 160 |
| 161 private: |
| 162 void CreateStateOnIoThread(); |
| 163 |
| 164 BrowserContext* const browser_context_; |
| 165 |
| 166 std::unique_ptr<State> state_; |
| 167 base::WeakPtrFactory<DevToolsURLRequestInterceptor> weak_ptr_factory_; |
| 168 |
| 169 DISALLOW_COPY_AND_ASSIGN(DevToolsURLRequestInterceptor); |
| 170 }; |
| 171 |
| 172 } // namespace content |
| 173 |
| 174 #endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_URL_REQUEST_INTERCEPTOR_H_ |
OLD | NEW |