OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/devtools/protocol/network_handler.h" | 5 #include "content/browser/devtools/protocol/network_handler.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/containers/hash_tables.h" | 9 #include "base/containers/hash_tables.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 #include "net/cert/x509_cert_types.h" | 25 #include "net/cert/x509_cert_types.h" |
26 #include "net/cert/x509_certificate.h" | 26 #include "net/cert/x509_certificate.h" |
27 #include "net/cookies/cookie_store.h" | 27 #include "net/cookies/cookie_store.h" |
28 #include "net/url_request/url_request_context.h" | 28 #include "net/url_request/url_request_context.h" |
29 #include "net/url_request/url_request_context_getter.h" | 29 #include "net/url_request/url_request_context_getter.h" |
30 | 30 |
31 namespace content { | 31 namespace content { |
32 namespace devtools { | 32 namespace devtools { |
33 namespace network { | 33 namespace network { |
34 | 34 |
| 35 using Response = DevToolsProtocolClient::Response; |
35 using CookieListCallback = net::CookieStore::GetCookieListCallback; | 36 using CookieListCallback = net::CookieStore::GetCookieListCallback; |
| 37 using SetCookieCallback = net::CookieStore::SetCookiesCallback; |
36 | 38 |
37 namespace { | 39 namespace { |
38 | 40 |
39 net::URLRequestContext* GetRequestContextOnIO( | 41 net::URLRequestContext* GetRequestContextOnIO( |
40 ResourceContext* resource_context, | 42 ResourceContext* resource_context, |
41 net::URLRequestContextGetter* context_getter, | 43 net::URLRequestContextGetter* context_getter, |
42 const GURL& url) { | 44 const GURL& url) { |
43 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 45 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
44 net::URLRequestContext* context = | 46 net::URLRequestContext* context = |
45 GetContentClient()->browser()->OverrideRequestContextForURL( | 47 GetContentClient()->browser()->OverrideRequestContextForURL( |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 BrowserThread::IO, | 121 BrowserThread::IO, |
120 FROM_HERE, | 122 FROM_HERE, |
121 base::Bind(&DeleteCookieOnIO, | 123 base::Bind(&DeleteCookieOnIO, |
122 base::Unretained(resource_context), | 124 base::Unretained(resource_context), |
123 base::Unretained(context_getter), | 125 base::Unretained(context_getter), |
124 url, | 126 url, |
125 cookie_name, | 127 cookie_name, |
126 callback)); | 128 callback)); |
127 } | 129 } |
128 | 130 |
| 131 void CookieSetOnIO(const SetCookieCallback& callback, bool success) { |
| 132 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 133 BrowserThread::PostTask( |
| 134 BrowserThread::UI, |
| 135 FROM_HERE, |
| 136 base::Bind(callback, success)); |
| 137 } |
| 138 |
| 139 void SetCookieOnIO( |
| 140 ResourceContext* resource_context, |
| 141 net::URLRequestContextGetter* context_getter, |
| 142 const GURL& url, |
| 143 const std::string& name, |
| 144 const std::string& value, |
| 145 const std::string& domain, |
| 146 const std::string& path, |
| 147 bool secure, |
| 148 bool http_only, |
| 149 net::CookieSameSite same_site, |
| 150 base::Time expires, |
| 151 const SetCookieCallback& callback) { |
| 152 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 153 net::URLRequestContext* request_context = |
| 154 GetRequestContextOnIO(resource_context, context_getter, url); |
| 155 |
| 156 bool are_experimental_cookie_features_enabled = |
| 157 request_context->network_delegate() |
| 158 ->AreExperimentalCookieFeaturesEnabled(); |
| 159 |
| 160 request_context->cookie_store()->SetCookieWithDetailsAsync( |
| 161 url, name, value, domain, path, |
| 162 base::Time(), |
| 163 expires, |
| 164 base::Time(), |
| 165 secure, |
| 166 http_only, |
| 167 same_site, |
| 168 are_experimental_cookie_features_enabled, |
| 169 net::COOKIE_PRIORITY_DEFAULT, |
| 170 base::Bind(&CookieSetOnIO, callback)); |
| 171 } |
| 172 |
| 173 void SetCookieOnUI( |
| 174 ResourceContext* resource_context, |
| 175 net::URLRequestContextGetter* context_getter, |
| 176 const GURL& url, |
| 177 const std::string& name, |
| 178 const std::string& value, |
| 179 const std::string& domain, |
| 180 const std::string& path, |
| 181 bool secure, |
| 182 bool http_only, |
| 183 net::CookieSameSite same_site, |
| 184 base::Time expires, |
| 185 const SetCookieCallback& callback) { |
| 186 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 187 BrowserThread::PostTask( |
| 188 BrowserThread::IO, |
| 189 FROM_HERE, |
| 190 base::Bind(&SetCookieOnIO, |
| 191 base::Unretained(resource_context), |
| 192 base::Unretained(context_getter), |
| 193 url, name, value, domain, path, secure, http_only, |
| 194 same_site, expires, callback)); |
| 195 } |
| 196 |
129 class GetCookiesCommand { | 197 class GetCookiesCommand { |
130 public: | 198 public: |
131 explicit GetCookiesCommand( | 199 explicit GetCookiesCommand( |
132 RenderFrameHostImpl* frame_host, | 200 RenderFrameHostImpl* frame_host, |
133 const CookieListCallback& callback) | 201 const CookieListCallback& callback) |
134 : callback_(callback), | 202 : callback_(callback), |
135 request_count_(0) { | 203 request_count_(0) { |
136 CookieListCallback got_cookies_callback = base::Bind( | 204 CookieListCallback got_cookies_callback = base::Bind( |
137 &GetCookiesCommand::GotCookiesForURL, base::Unretained(this)); | 205 &GetCookiesCommand::GotCookiesForURL, base::Unretained(this)); |
138 | 206 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 if (!host_) | 289 if (!host_) |
222 return Response::InternalError("Could not connect to view"); | 290 return Response::InternalError("Could not connect to view"); |
223 new GetCookiesCommand( | 291 new GetCookiesCommand( |
224 host_, | 292 host_, |
225 base::Bind(&NetworkHandler::SendGetCookiesResponse, | 293 base::Bind(&NetworkHandler::SendGetCookiesResponse, |
226 weak_factory_.GetWeakPtr(), | 294 weak_factory_.GetWeakPtr(), |
227 command_id)); | 295 command_id)); |
228 return Response::OK(); | 296 return Response::OK(); |
229 } | 297 } |
230 | 298 |
| 299 Response NetworkHandler::SetCookie(DevToolsCommandId command_id, |
| 300 const std::string& url, |
| 301 const std::string& name, |
| 302 const std::string& value, |
| 303 const std::string* domain, |
| 304 const std::string* path, |
| 305 bool* secure, |
| 306 bool* http_only, |
| 307 const std::string* same_site, |
| 308 double* expires) { |
| 309 if (!host_) |
| 310 return Response::InternalError("Could not connect to view"); |
| 311 |
| 312 net::CookieSameSite same_site_enum = net::CookieSameSite::DEFAULT_MODE; |
| 313 if (same_site && *same_site == kCookieSameSiteLax) |
| 314 same_site_enum = net::CookieSameSite::LAX_MODE; |
| 315 else if (same_site && *same_site == kCookieSameSiteStrict) |
| 316 same_site_enum = net::CookieSameSite::STRICT_MODE; |
| 317 |
| 318 base::Time expiration_date; |
| 319 if (expires) |
| 320 expiration_date = (*expires == 0) |
| 321 ? base::Time::UnixEpoch() |
| 322 : base::Time::FromDoubleT(*expires); |
| 323 |
| 324 SetCookieOnUI( |
| 325 host_->GetSiteInstance()->GetBrowserContext()->GetResourceContext(), |
| 326 host_->GetProcess()->GetStoragePartition()->GetURLRequestContext(), |
| 327 GURL(url), name, value, |
| 328 domain ? *domain : std::string(), path ? *path : std::string(), |
| 329 secure ? *secure : false, http_only ? *http_only : false, |
| 330 same_site_enum, expiration_date, |
| 331 base::Bind(&NetworkHandler::SendSetCookieResponse, |
| 332 weak_factory_.GetWeakPtr(), |
| 333 command_id)); |
| 334 return Response::OK(); |
| 335 } |
| 336 |
| 337 void NetworkHandler::SendSetCookieResponse(DevToolsCommandId command_id, |
| 338 bool success) { |
| 339 client_->SendSetCookieResponse(command_id, |
| 340 SetCookieResponse::Create()->set_success(success)); |
| 341 } |
| 342 |
231 void NetworkHandler::SendGetCookiesResponse( | 343 void NetworkHandler::SendGetCookiesResponse( |
232 DevToolsCommandId command_id, | 344 DevToolsCommandId command_id, |
233 const net::CookieList& cookie_list) { | 345 const net::CookieList& cookie_list) { |
234 std::vector<scoped_refptr<Cookie>> cookies; | 346 std::vector<scoped_refptr<Cookie>> cookies; |
235 for (size_t i = 0; i < cookie_list.size(); ++i) { | 347 for (size_t i = 0; i < cookie_list.size(); ++i) { |
236 const net::CanonicalCookie& cookie = cookie_list[i]; | 348 const net::CanonicalCookie& cookie = cookie_list[i]; |
237 scoped_refptr<Cookie> devtools_cookie = Cookie::Create() | 349 scoped_refptr<Cookie> devtools_cookie = Cookie::Create() |
238 ->set_name(cookie.Name()) | 350 ->set_name(cookie.Name()) |
239 ->set_value(cookie.Value()) | 351 ->set_value(cookie.Value()) |
240 ->set_domain(cookie.Domain()) | 352 ->set_domain(cookie.Domain()) |
241 ->set_path(cookie.Path()) | 353 ->set_path(cookie.Path()) |
242 ->set_expires(cookie.ExpiryDate().ToDoubleT() * 1000) | 354 ->set_expires(cookie.ExpiryDate().ToDoubleT() * 1000) |
243 ->set_size(cookie.Name().length() + cookie.Value().length()) | 355 ->set_size(cookie.Name().length() + cookie.Value().length()) |
244 ->set_http_only(cookie.IsHttpOnly()) | 356 ->set_http_only(cookie.IsHttpOnly()) |
245 ->set_secure(cookie.IsSecure()) | 357 ->set_secure(cookie.IsSecure()) |
246 ->set_session(!cookie.IsPersistent()); | 358 ->set_session(!cookie.IsPersistent()); |
247 | 359 |
248 switch (cookie.SameSite()) { | 360 switch (cookie.SameSite()) { |
249 case net::CookieSameSite::STRICT_MODE: | 361 case net::CookieSameSite::STRICT_MODE: |
250 devtools_cookie->set_same_site(cookie::kSameSiteStrict); | 362 devtools_cookie->set_same_site(kCookieSameSiteStrict); |
251 break; | 363 break; |
252 case net::CookieSameSite::LAX_MODE: | 364 case net::CookieSameSite::LAX_MODE: |
253 devtools_cookie->set_same_site(cookie::kSameSiteLax); | 365 devtools_cookie->set_same_site(kCookieSameSiteLax); |
254 break; | 366 break; |
255 case net::CookieSameSite::NO_RESTRICTION: | 367 case net::CookieSameSite::NO_RESTRICTION: |
256 break; | 368 break; |
257 } | 369 } |
258 cookies.push_back(devtools_cookie); | 370 cookies.push_back(devtools_cookie); |
259 } | 371 } |
260 client_->SendGetCookiesResponse(command_id, | 372 client_->SendGetCookiesResponse(command_id, |
261 GetCookiesResponse::Create()->set_cookies(cookies)); | 373 GetCookiesResponse::Create()->set_cookies(cookies)); |
262 } | 374 } |
263 | 375 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 return Response::InternalError("Could not connect to view"); | 452 return Response::InternalError("Could not connect to view"); |
341 WebContents* web_contents = WebContents::FromRenderFrameHost(host_); | 453 WebContents* web_contents = WebContents::FromRenderFrameHost(host_); |
342 web_contents->GetDelegate()->ShowCertificateViewerInDevTools( | 454 web_contents->GetDelegate()->ShowCertificateViewerInDevTools( |
343 web_contents, certificate_id); | 455 web_contents, certificate_id); |
344 return Response::OK(); | 456 return Response::OK(); |
345 } | 457 } |
346 | 458 |
347 } // namespace network | 459 } // namespace network |
348 } // namespace devtools | 460 } // namespace devtools |
349 } // namespace content | 461 } // namespace content |
OLD | NEW |