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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: content/browser/devtools/devtools_url_request_interceptor.h
diff --git a/content/browser/devtools/devtools_url_request_interceptor.h b/content/browser/devtools/devtools_url_request_interceptor.h
new file mode 100644
index 0000000000000000000000000000000000000000..d1b31222bd3576491bf0954616657f6967f50815
--- /dev/null
+++ b/content/browser/devtools/devtools_url_request_interceptor.h
@@ -0,0 +1,183 @@
+// 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_REQUEST_INTERCEPTOR_H_
+#define CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_URL_REQUEST_INTERCEPTOR_H_
+
+#include "base/containers/flat_map.h"
+#include "base/containers/flat_set.h"
+#include "base/macros.h"
+#include "base/memory/weak_ptr.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_interceptor.h"
+
+namespace content {
+namespace protocol {
+class NetworkHandler;
+} // namespace
+
+class BrowserContext;
+class DevToolsURLInterceptorRequestJob;
+class WebContents;
+
+// An interceptor that creates DevToolsURLInterceptorRequestJobs for requests
+// from pages where interception has been enabled via
+// Network.enableFetchInterception.
+class DevToolsURLRequestInterceptor : public net::URLRequestInterceptor {
+ public:
+ explicit DevToolsURLRequestInterceptor(BrowserContext* browser_context);
+ ~DevToolsURLRequestInterceptor() override;
+
+ // Must be called on UI thread.
+ static DevToolsURLRequestInterceptor* FromBrowserContext(
+ BrowserContext* context);
+
+ // net::URLRequestInterceptor implementation:
+ net::URLRequestJob* MaybeInterceptRequest(
+ net::URLRequest* request,
+ net::NetworkDelegate* network_delegate) const override;
+
+ net::URLRequestJob* MaybeInterceptRedirect(
+ net::URLRequest* request,
+ net::NetworkDelegate* network_delegate,
+ const GURL& location) const override;
+
+ net::URLRequestJob* MaybeInterceptResponse(
+ net::URLRequest* request,
+ net::NetworkDelegate* network_delegate) const override;
+
+ enum class CommandStatus {
+ OK,
+ UnknownInterceptionId,
+ CommandAlreadyProcessed,
+ };
+
+ using CommandCallback = base::OnceCallback<void(const CommandStatus& status)>;
+
+ struct Modifications {
+ Modifications(protocol::Maybe<std::string> url,
+ protocol::Maybe<std::string> method,
+ protocol::Maybe<std::string> post_data,
+ protocol::Maybe<protocol::Network::Headers> headers);
+ ~Modifications();
+
+ protocol::Maybe<std::string> url;
+ protocol::Maybe<std::string> method;
+ protocol::Maybe<std::string> post_data;
+ protocol::Maybe<protocol::Network::Headers> headers;
+ };
+
+ // Unfortunately the interface methods are const so we need a mutable inner
+ // class to hold our state.
+ 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
+ public:
+ State();
+ ~State();
+
+ void AllowRequest(std::string interception_id, CommandCallback callback);
+
+ // Must be called on the IO thread.
+ void BlockRequest(std::string interception_id,
+ net::Error error_reason,
+ CommandCallback callback);
+
+ // Must be called on the IO thread.
+ void ModifyRequest(std::string interception_id,
+ std::unique_ptr<Modifications> modifications,
+ CommandCallback callback);
+
+ // Must be called on the IO thread.
+ void MockResponse(std::string interception_id,
+ std::string raw_response,
+ CommandCallback callback);
+
+ // Returns true if |request| should be intercepted and sets
+ // |network_handler|. Must be called on the IO thread.
+ bool ShouldInterceptRequest(
+ const net::URLRequest* request,
+ base::WeakPtr<protocol::NetworkHandler>* network_handler) const;
+
+ // Must be called on the IO thread.
+ void StartInterceptingRequests(
+ WebContents* web_contents,
+ base::WeakPtr<protocol::NetworkHandler> network_handler);
+
+ // Must be called on the IO thread.
+ void StopInterceptingRequests(WebContents* web_contents);
+
+ // Registers a |sub_request| that should not be intercepted.
+ void RegisterSubRequest(const net::URLRequest* sub_request);
+
+ // Unregisters a |sub_request|. Must be called on the IO thread.
+ void UnregisterSubRequest(const net::URLRequest* sub_request);
+
+ // To make the user's life easier we make sure requests in a redirect chain
+ // all have the same id. Must be called on the IO thread.
+ void ExpectRequestAfterRedirect(const net::URLRequest* request,
+ std::string id);
+
+ // Must be called on the IO thread.
+ std::string GetIdForRequest(const net::URLRequest* request,
+ bool* is_redirect);
+
+ // Must be called on the IO thread.
+ void RegisterJob(DevToolsURLInterceptorRequestJob* job,
+ const std::string& interception_id);
+
+ // Must be called on the IO thread.
+ void UnregisterJob(const std::string& interception_id);
+
+ base::WeakPtr<State> GetWeakPtr() { return weak_ptr_factory_.GetWeakPtr(); }
+
+ private:
+ class InterceptedWebContentsObserver;
+
+ void StartInterceptingRequestsInternal(
+ int render_frame_id,
+ int frame_tree_node_id,
+ base::WeakPtr<protocol::NetworkHandler> network_handler);
+
+ void StopInterceptingRequestsInternal(int render_frame_id,
+ int frame_tree_node_id);
+
+ // Returns a WeakPtr to the DevToolsURLInterceptorRequestJob corresponding
+ // to |interception_id|. Must be called on the IO thread.
+ DevToolsURLInterceptorRequestJob* GetJob(
+ const std::string& interception_id) const;
+
+ base::flat_map<int, base::WeakPtr<protocol::NetworkHandler>>
+ intercepted_render_frames_;
+
+ base::flat_map<int, base::WeakPtr<protocol::NetworkHandler>>
+ intercepted_frame_tree_nodes_;
+
+ base::flat_map<WebContents*,
+ std::unique_ptr<InterceptedWebContentsObserver>>
+ observers_;
+
+ base::flat_map<std::string, DevToolsURLInterceptorRequestJob*>
+ interception_id_to_job_map_;
+
+ base::flat_set<const net::URLRequest*> sub_requests_;
+ base::flat_map<const net::URLRequest*, std::string> expected_redirects_;
+ size_t next_id_;
+
+ base::WeakPtrFactory<State> weak_ptr_factory_;
+ };
+
+ State& state() const { return *state_; }
+
+ private:
+ BrowserContext* const browser_context_;
+
+ std::unique_ptr<State> state_;
+
+ DISALLOW_COPY_AND_ASSIGN(DevToolsURLRequestInterceptor);
+};
+
+} // namespace content
+
+#endif // CONTENT_BROWSER_DEVTOOLS_DEVTOOLS_URL_REQUEST_INTERCEPTOR_H_

Powered by Google App Engine
This is Rietveld 408576698