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

Unified Diff: content/browser/devtools/devtools_url_request_interceptor.cc

Issue 2739323003: DevTools protocol interception, blocking & modification of requests (Closed)
Patch Set: Add missing expects plus tweak test output of Network.interceptedRedirect for clarity Created 3 years, 9 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.cc
diff --git a/content/browser/devtools/devtools_url_request_interceptor.cc b/content/browser/devtools/devtools_url_request_interceptor.cc
new file mode 100644
index 0000000000000000000000000000000000000000..6792961a1d5a425abdbb699bff2e2627ac1eaf0a
--- /dev/null
+++ b/content/browser/devtools/devtools_url_request_interceptor.cc
@@ -0,0 +1,128 @@
+// 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.
+
+#include "content/browser/devtools/devtools_url_request_interceptor.h"
+
+#include "base/strings/stringprintf.h"
+#include "content/browser/devtools/devtools_agent_host_impl.h"
+#include "content/browser/devtools/devtools_url_interceptor_request_job.h"
+#include "content/browser/devtools/protocol/network_handler.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/devtools_agent_host.h"
+#include "net/http/http_request_headers.h"
+#include "net/url_request/url_request.h"
+
+namespace content {
+namespace {
+const char kDevToolsEmulateNetworkConditionsClientId[] =
+ "X-DevTools-Emulate-Network-Conditions-Client-Id";
+} // namespace
+
+DevToolsURLRequestInterceptor::DevToolsURLRequestInterceptor()
+ : state_(new State()) {}
+
+DevToolsURLRequestInterceptor::~DevToolsURLRequestInterceptor() {}
+
+net::URLRequestJob* DevToolsURLRequestInterceptor::MaybeInterceptRequest(
+ net::URLRequest* request,
+ net::NetworkDelegate* network_delegate) const {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ std::string client_id;
+ if (!request->extra_request_headers().GetHeader(
+ kDevToolsEmulateNetworkConditionsClientId, &client_id)) {
+ return nullptr;
+ }
+
+ // We don't want to intercept our own sub requests!
+ if (state_->IsSubRequest(request))
+ return nullptr;
+
+ DevToolsAgentHostImpl* devtools_agent_host_impl =
+ static_cast<DevToolsAgentHostImpl*>(
+ DevToolsAgentHost::GetForId(client_id).get());
+
+ if (!devtools_agent_host_impl)
+ return nullptr;
+
+ protocol::NetworkHandler* network_handler =
+ devtools_agent_host_impl->GetHandler<protocol::NetworkHandler>();
+
+ if (!network_handler || !network_handler->interception_enabled())
+ return nullptr;
+
+ std::string parent_id = state_->GetRedirectChainParentId(request);
Sami 2017/03/22 17:00:46 Just to make sure I understand, does the same |req
alex clarke (OOO till 29th) 2017/03/24 13:57:24 Yes. If a 302 is served or mocked then it's sent t
+ if (parent_id.empty()) {
+ return new DevToolsURLInterceptorRequestJob(
+ state_->GetWeakPtr(), state_->GetNewId(), request, network_delegate,
+ network_handler, false);
+ } else {
+ return new DevToolsURLInterceptorRequestJob(
+ state_->GetWeakPtr(), std::move(parent_id), request, network_delegate,
+ network_handler, true);
+ }
+}
+
+net::URLRequestJob* DevToolsURLRequestInterceptor::MaybeInterceptRedirect(
+ net::URLRequest* request,
+ net::NetworkDelegate* network_delegate,
+ const GURL& location) const {
+ return nullptr;
+}
+
+net::URLRequestJob* DevToolsURLRequestInterceptor::MaybeInterceptResponse(
+ net::URLRequest* request,
+ net::NetworkDelegate* network_delegate) const {
+ return nullptr;
+}
+
+DevToolsURLRequestInterceptor::State::State()
+ : next_id_(0), weak_ptr_factory_(this) {}
+
+DevToolsURLRequestInterceptor::State::~State() {}
+
+void DevToolsURLRequestInterceptor::State::RegisterSubRequest(
+ const net::URLRequest* request) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ DCHECK(sub_requests_.find(request) == sub_requests_.end());
+ sub_requests_.insert(request);
+}
+
+void DevToolsURLRequestInterceptor::State::UnregisterSubRequest(
+ const net::URLRequest* request) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ DCHECK(sub_requests_.find(request) != sub_requests_.end());
+ sub_requests_.erase(request);
+}
+
+bool DevToolsURLRequestInterceptor::State::IsSubRequest(
+ const net::URLRequest* request) const {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ return sub_requests_.find(request) != sub_requests_.end();
+}
+
+void DevToolsURLRequestInterceptor::State::ExpectRequestAfterRedirect(
+ const net::URLRequest* request,
+ std::string id) {
Sami 2017/03/22 17:00:46 nit: const ref
alex clarke (OOO till 29th) 2017/03/24 13:57:23 Done.
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ expected_redirects_[request] = id;
+}
+
+std::string DevToolsURLRequestInterceptor::State::GetNewId() {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ return base::StringPrintf("id-%zu", ++next_id_);
+}
+
+std::string DevToolsURLRequestInterceptor::State::GetRedirectChainParentId(
+ const net::URLRequest* request) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ auto find_it = expected_redirects_.find(request);
+ if (find_it == expected_redirects_.end()) {
+ return "";
+ }
+ std::string id = std::move(find_it->second);
+ expected_redirects_.erase(find_it);
+ return id;
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698