| 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..35bf22e4b47bcff9fae0ea9f3c0cfb6a71940d2c
|
| --- /dev/null
|
| +++ b/content/browser/devtools/devtools_url_request_interceptor.h
|
| @@ -0,0 +1,184 @@
|
| +// 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 "base/optional.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(base::Optional<net::Error> error_reason,
|
| + base::Optional<std::string> raw_response,
|
| + protocol::Maybe<std::string> modified_url,
|
| + protocol::Maybe<std::string> modified_method,
|
| + protocol::Maybe<std::string> modified_post_data,
|
| + protocol::Maybe<protocol::Network::Headers> modified_headers);
|
| + ~Modifications();
|
| +
|
| + bool RequestShouldContineUnchanged() const;
|
| +
|
| + // If none of the following are set then the request will be allowed to
|
| + // continue unchanged.
|
| + base::Optional<net::Error> error_reason; // Finish with error.
|
| + base::Optional<std::string> raw_response; // Finish with mock response.
|
| +
|
| + // Optionally modify before sending to network.
|
| + protocol::Maybe<std::string> modified_url;
|
| + protocol::Maybe<std::string> modified_method;
|
| + protocol::Maybe<std::string> modified_post_data;
|
| + protocol::Maybe<protocol::Network::Headers> modified_headers;
|
| + };
|
| +
|
| + // Unfortunately the interface methods are const so we need a mutable inner
|
| + // class to hold our state.
|
| + class State {
|
| + public:
|
| + State();
|
| + ~State();
|
| +
|
| + // Must be called on the IO thread.
|
| + void ContinueRequest(std::string interception_id,
|
| + std::unique_ptr<Modifications> modifications,
|
| + 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,
|
| + int process_id,
|
| + base::WeakPtr<protocol::NetworkHandler> network_handler);
|
| +
|
| + void StopInterceptingRequestsInternal(int render_frame_id,
|
| + int frame_tree_node_id,
|
| + int process_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<std::pair<int, 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_
|
|
|