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 #include "content/browser/devtools/devtools_url_request_interceptor.h" | |
6 | |
7 #include "base/strings/stringprintf.h" | |
8 #include "content/browser/devtools/devtools_agent_host_impl.h" | |
9 #include "content/browser/devtools/devtools_url_interceptor_request_job.h" | |
10 #include "content/browser/devtools/protocol/network_handler.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 #include "content/public/browser/devtools_agent_host.h" | |
13 #include "net/http/http_request_headers.h" | |
14 #include "net/url_request/url_request.h" | |
15 | |
16 namespace content { | |
17 namespace { | |
18 const char kDevToolsEmulateNetworkConditionsClientId[] = | |
19 "X-DevTools-Emulate-Network-Conditions-Client-Id"; | |
20 } // namespace | |
21 | |
22 DevToolsURLRequestInterceptor::DevToolsURLRequestInterceptor() | |
23 : state_(new State()) {} | |
24 | |
25 DevToolsURLRequestInterceptor::~DevToolsURLRequestInterceptor() {} | |
26 | |
27 net::URLRequestJob* DevToolsURLRequestInterceptor::MaybeInterceptRequest( | |
28 net::URLRequest* request, | |
29 net::NetworkDelegate* network_delegate) const { | |
30 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
31 std::string client_id; | |
32 if (!request->extra_request_headers().GetHeader( | |
33 kDevToolsEmulateNetworkConditionsClientId, &client_id)) { | |
34 return nullptr; | |
35 } | |
36 | |
37 // We don't want to intercept our own sub requests! | |
38 if (state_->IsSubRequest(request)) | |
39 return nullptr; | |
40 | |
41 DevToolsAgentHostImpl* devtools_agent_host_impl = | |
42 static_cast<DevToolsAgentHostImpl*>( | |
43 DevToolsAgentHost::GetForId(client_id).get()); | |
44 | |
45 if (!devtools_agent_host_impl) | |
46 return nullptr; | |
47 | |
48 protocol::NetworkHandler* network_handler = | |
49 devtools_agent_host_impl->GetHandler<protocol::NetworkHandler>(); | |
50 | |
51 if (!network_handler || !network_handler->interception_enabled()) | |
52 return nullptr; | |
53 | |
54 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
| |
55 if (parent_id.empty()) { | |
56 return new DevToolsURLInterceptorRequestJob( | |
57 state_->GetWeakPtr(), state_->GetNewId(), request, network_delegate, | |
58 network_handler, false); | |
59 } else { | |
60 return new DevToolsURLInterceptorRequestJob( | |
61 state_->GetWeakPtr(), std::move(parent_id), request, network_delegate, | |
62 network_handler, true); | |
63 } | |
64 } | |
65 | |
66 net::URLRequestJob* DevToolsURLRequestInterceptor::MaybeInterceptRedirect( | |
67 net::URLRequest* request, | |
68 net::NetworkDelegate* network_delegate, | |
69 const GURL& location) const { | |
70 return nullptr; | |
71 } | |
72 | |
73 net::URLRequestJob* DevToolsURLRequestInterceptor::MaybeInterceptResponse( | |
74 net::URLRequest* request, | |
75 net::NetworkDelegate* network_delegate) const { | |
76 return nullptr; | |
77 } | |
78 | |
79 DevToolsURLRequestInterceptor::State::State() | |
80 : next_id_(0), weak_ptr_factory_(this) {} | |
81 | |
82 DevToolsURLRequestInterceptor::State::~State() {} | |
83 | |
84 void DevToolsURLRequestInterceptor::State::RegisterSubRequest( | |
85 const net::URLRequest* request) { | |
86 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
87 DCHECK(sub_requests_.find(request) == sub_requests_.end()); | |
88 sub_requests_.insert(request); | |
89 } | |
90 | |
91 void DevToolsURLRequestInterceptor::State::UnregisterSubRequest( | |
92 const net::URLRequest* request) { | |
93 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
94 DCHECK(sub_requests_.find(request) != sub_requests_.end()); | |
95 sub_requests_.erase(request); | |
96 } | |
97 | |
98 bool DevToolsURLRequestInterceptor::State::IsSubRequest( | |
99 const net::URLRequest* request) const { | |
100 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
101 return sub_requests_.find(request) != sub_requests_.end(); | |
102 } | |
103 | |
104 void DevToolsURLRequestInterceptor::State::ExpectRequestAfterRedirect( | |
105 const net::URLRequest* request, | |
106 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.
| |
107 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
108 expected_redirects_[request] = id; | |
109 } | |
110 | |
111 std::string DevToolsURLRequestInterceptor::State::GetNewId() { | |
112 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
113 return base::StringPrintf("id-%zu", ++next_id_); | |
114 } | |
115 | |
116 std::string DevToolsURLRequestInterceptor::State::GetRedirectChainParentId( | |
117 const net::URLRequest* request) { | |
118 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
119 auto find_it = expected_redirects_.find(request); | |
120 if (find_it == expected_redirects_.end()) { | |
121 return ""; | |
122 } | |
123 std::string id = std::move(find_it->second); | |
124 expected_redirects_.erase(find_it); | |
125 return id; | |
126 } | |
127 | |
128 } // namespace content | |
OLD | NEW |