1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
// Copyright 2014 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/protocol/network_handler.h"
#include "base/containers/hash_tables.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "content/browser/frame_host/frame_tree_node.h"
#include "content/browser/frame_host/render_frame_host_impl.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/cert_store.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/resource_context.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/common/content_client.h"
#include "net/cert/x509_cert_types.h"
#include "net/cert/x509_certificate.h"
#include "net/cookies/cookie_store.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
namespace content {
namespace devtools {
namespace network {
using CookieListCallback = net::CookieStore::GetCookieListCallback;
namespace {
net::URLRequestContext* GetRequestContextOnIO(
ResourceContext* resource_context,
net::URLRequestContextGetter* context_getter,
const GURL& url) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
net::URLRequestContext* context =
GetContentClient()->browser()->OverrideRequestContextForURL(
url, resource_context);
if (!context)
context = context_getter->GetURLRequestContext();
return context;
}
void GotCookiesForURLOnIO(
const CookieListCallback& callback,
const net::CookieList& cookie_list) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(callback, cookie_list));
}
void GetCookiesForURLOnIO(
ResourceContext* resource_context,
net::URLRequestContextGetter* context_getter,
const GURL& url,
const CookieListCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
net::URLRequestContext* request_context =
GetRequestContextOnIO(resource_context, context_getter, url);
request_context->cookie_store()->GetAllCookiesForURLAsync(
url, base::Bind(&GotCookiesForURLOnIO, callback));
}
void GetCookiesForURLOnUI(
ResourceContext* resource_context,
net::URLRequestContextGetter* context_getter,
const GURL& url,
const CookieListCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
base::Bind(&GetCookiesForURLOnIO,
base::Unretained(resource_context),
base::Unretained(context_getter),
url,
callback));
}
void DeletedCookieOnIO(const base::Closure& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
callback);
}
void DeleteCookieOnIO(
ResourceContext* resource_context,
net::URLRequestContextGetter* context_getter,
const GURL& url,
const std::string& cookie_name,
const base::Closure& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
net::URLRequestContext* request_context =
GetRequestContextOnIO(resource_context, context_getter, url);
request_context->cookie_store()->DeleteCookieAsync(
url, cookie_name, base::Bind(&DeletedCookieOnIO, callback));
}
void DeleteCookieOnUI(
ResourceContext* resource_context,
net::URLRequestContextGetter* context_getter,
const GURL& url,
const std::string& cookie_name,
const base::Closure& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
base::Bind(&DeleteCookieOnIO,
base::Unretained(resource_context),
base::Unretained(context_getter),
url,
cookie_name,
callback));
}
class GetCookiesCommand {
public:
explicit GetCookiesCommand(
RenderFrameHostImpl* frame_host,
const CookieListCallback& callback)
: callback_(callback),
request_count_(0) {
CookieListCallback got_cookies_callback = base::Bind(
&GetCookiesCommand::GotCookiesForURL, base::Unretained(this));
BrowserContext* browser_context =
frame_host->GetSiteInstance()->GetBrowserContext();
std::queue<FrameTreeNode*> queue;
queue.push(frame_host->frame_tree_node());
while (!queue.empty()) {
FrameTreeNode* node = queue.front();
queue.pop();
// Only traverse nodes with the same local root.
if (node->current_frame_host()->IsCrossProcessSubframe())
continue;
int process_id = node->current_frame_host()->GetProcess()->GetID();
++request_count_;
GetCookiesForURLOnUI(
browser_context->GetResourceContext(),
browser_context->GetRequestContextForRenderProcess(process_id),
node->current_url(),
got_cookies_callback);
for (size_t i = 0; i < node->child_count(); ++i)
queue.push(node->child_at(i));
}
}
private:
void GotCookiesForURL(const net::CookieList& cookie_list) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
for (const net::CanonicalCookie& cookie : cookie_list) {
std::string key = base::StringPrintf(
"%s::%s::%s::%d",
cookie.Name().c_str(),
cookie.Domain().c_str(),
cookie.Path().c_str(),
cookie.IsSecure());
cookies_[key] = cookie;
}
--request_count_;
if (!request_count_) {
net::CookieList list;
list.reserve(cookies_.size());
for (const auto& pair : cookies_)
list.push_back(pair.second);
callback_.Run(list);
delete this;
}
}
CookieListCallback callback_;
int request_count_;
base::hash_map<std::string, net::CanonicalCookie> cookies_;
};
} // namespace
typedef DevToolsProtocolClient::Response Response;
NetworkHandler::NetworkHandler() : host_(nullptr), weak_factory_(this) {
}
NetworkHandler::~NetworkHandler() {
}
void NetworkHandler::SetRenderFrameHost(RenderFrameHostImpl* host) {
host_ = host;
}
void NetworkHandler::SetClient(scoped_ptr<Client> client) {
client_.swap(client);
}
Response NetworkHandler::ClearBrowserCache() {
if (host_)
GetContentClient()->browser()->ClearCache(host_);
return Response::OK();
}
Response NetworkHandler::ClearBrowserCookies() {
if (host_)
GetContentClient()->browser()->ClearCookies(host_);
return Response::OK();
}
Response NetworkHandler::GetCookies(DevToolsCommandId command_id) {
if (!host_)
return Response::InternalError("Could not connect to view");
new GetCookiesCommand(
host_,
base::Bind(&NetworkHandler::SendGetCookiesResponse,
weak_factory_.GetWeakPtr(),
command_id));
return Response::OK();
}
void NetworkHandler::SendGetCookiesResponse(
DevToolsCommandId command_id,
const net::CookieList& cookie_list) {
std::vector<scoped_refptr<Cookie>> cookies;
for (size_t i = 0; i < cookie_list.size(); ++i) {
const net::CanonicalCookie& cookie = cookie_list[i];
cookies.push_back(Cookie::Create()
->set_name(cookie.Name())
->set_value(cookie.Value())
->set_domain(cookie.Domain())
->set_path(cookie.Path())
->set_expires(cookie.ExpiryDate().ToDoubleT() * 1000)
->set_size(cookie.Name().length() + cookie.Value().length())
->set_http_only(cookie.IsHttpOnly())
->set_secure(cookie.IsSecure())
->set_session(!cookie.IsPersistent()));
}
client_->SendGetCookiesResponse(command_id,
GetCookiesResponse::Create()->set_cookies(cookies));
}
Response NetworkHandler::DeleteCookie(
DevToolsCommandId command_id,
const std::string& cookie_name,
const std::string& url) {
if (!host_)
return Response::InternalError("Could not connect to view");
BrowserContext* browser_context =
host_->GetSiteInstance()->GetBrowserContext();
int process_id = host_->GetProcess()->GetID();
DeleteCookieOnUI(
browser_context->GetResourceContext(),
browser_context->GetRequestContextForRenderProcess(process_id),
GURL(url),
cookie_name,
base::Bind(&NetworkHandler::SendDeleteCookieResponse,
weak_factory_.GetWeakPtr(),
command_id));
return Response::OK();
}
void NetworkHandler::SendDeleteCookieResponse(DevToolsCommandId command_id) {
client_->SendDeleteCookieResponse(command_id,
DeleteCookieResponse::Create());
}
Response NetworkHandler::CanEmulateNetworkConditions(bool* result) {
*result = false;
return Response::OK();
}
Response NetworkHandler::EmulateNetworkConditions(bool offline,
double latency,
double download_throughput,
double upload_throughput) {
return Response::FallThrough();
}
Response NetworkHandler::GetCertificateDetails(
int certificate_id,
scoped_refptr<CertificateDetails>* result) {
scoped_refptr<net::X509Certificate> cert;
content::CertStore* cert_store = CertStore::GetInstance();
cert_store->RetrieveCert(certificate_id, &cert);
if (!cert.get())
return Response::InvalidParams("certificateId");
std::string name(cert->subject().GetDisplayName());
std::string issuer(cert->issuer().GetDisplayName());
base::Time valid_from = cert->valid_start();
base::Time valid_to = cert->valid_expiry();
std::vector<std::string> dns_names;
std::vector<std::string> ip_addrs;
cert->GetSubjectAltName(&dns_names, &ip_addrs);
*result = CertificateDetails::Create()
->set_subject(CertificateSubject::Create()
->set_name(name)
->set_san_dns_names(dns_names)
->set_san_ip_addresses(ip_addrs))
->set_issuer(issuer)
->set_valid_from(valid_from.ToDoubleT())
->set_valid_to(valid_to.ToDoubleT());
return Response::OK();
}
Response NetworkHandler::ShowCertificateViewer(int certificate_id) {
if (!host_)
return Response::InternalError("Could not connect to view");
WebContents* web_contents = WebContents::FromRenderFrameHost(host_);
web_contents->GetDelegate()->ShowCertificateViewerInDevTools(
web_contents, certificate_id);
return Response::OK();
}
} // namespace dom
} // namespace devtools
} // namespace content
|