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 httpOnly, | |
149 net::CookieSameSite sameSite, | |
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 httpOnly, | |
167 sameSite, | |
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 httpOnly, | |
183 net::CookieSameSite sameSite, | |
184 base::Time expires, | |
185 const SetCookieCallback& callback) { | |
186 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
187 BrowserThread::PostTask( | |
dgozman
2016/08/12 05:22:46
Just inline this into SetCookie method.
allada
2016/08/16 01:10:39
I am following the pattern of DeleteCookieOnUI(),
dgozman
2016/08/16 18:56:59
I see, thanks.
| |
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, httpOnly, | |
194 sameSite, 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* httpOnly, | |
307 const std::string* sameSite, | |
308 double* expires) { | |
309 if (!host_) | |
310 return Response::InternalError("Could not connect to view"); | |
311 | |
312 net::CookieSameSite sameSiteEnum = net::CookieSameSite::DEFAULT_MODE; | |
313 if (sameSite && *sameSite == kSameSiteLax) | |
314 sameSiteEnum = net::CookieSameSite::LAX_MODE; | |
315 else if (sameSite && *sameSite == kSameSiteStrict) | |
316 sameSiteEnum = net::CookieSameSite::STRICT_MODE; | |
317 | |
318 base::Time expirationDate; | |
319 if (expires) | |
320 expirationDate = (*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, httpOnly ? *httpOnly : false, | |
330 sameSiteEnum, expirationDate, | |
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() | |
341 ->set_success(success)); | |
dgozman
2016/08/12 05:22:46
nit: this fits previous line
allada
2016/08/16 01:10:39
Done.
| |
342 } | |
343 | |
231 void NetworkHandler::SendGetCookiesResponse( | 344 void NetworkHandler::SendGetCookiesResponse( |
232 DevToolsCommandId command_id, | 345 DevToolsCommandId command_id, |
233 const net::CookieList& cookie_list) { | 346 const net::CookieList& cookie_list) { |
234 std::vector<scoped_refptr<Cookie>> cookies; | 347 std::vector<scoped_refptr<Cookie>> cookies; |
235 for (size_t i = 0; i < cookie_list.size(); ++i) { | 348 for (size_t i = 0; i < cookie_list.size(); ++i) { |
236 const net::CanonicalCookie& cookie = cookie_list[i]; | 349 const net::CanonicalCookie& cookie = cookie_list[i]; |
237 scoped_refptr<Cookie> devtools_cookie = Cookie::Create() | 350 scoped_refptr<Cookie> devtools_cookie = Cookie::Create() |
238 ->set_name(cookie.Name()) | 351 ->set_name(cookie.Name()) |
239 ->set_value(cookie.Value()) | 352 ->set_value(cookie.Value()) |
240 ->set_domain(cookie.Domain()) | 353 ->set_domain(cookie.Domain()) |
241 ->set_path(cookie.Path()) | 354 ->set_path(cookie.Path()) |
242 ->set_expires(cookie.ExpiryDate().ToDoubleT() * 1000) | 355 ->set_expires(cookie.ExpiryDate().ToDoubleT() * 1000) |
243 ->set_size(cookie.Name().length() + cookie.Value().length()) | 356 ->set_size(cookie.Name().length() + cookie.Value().length()) |
244 ->set_http_only(cookie.IsHttpOnly()) | 357 ->set_http_only(cookie.IsHttpOnly()) |
245 ->set_secure(cookie.IsSecure()) | 358 ->set_secure(cookie.IsSecure()) |
246 ->set_session(!cookie.IsPersistent()); | 359 ->set_session(!cookie.IsPersistent()); |
247 | 360 |
248 switch (cookie.SameSite()) { | 361 switch (cookie.SameSite()) { |
249 case net::CookieSameSite::STRICT_MODE: | 362 case net::CookieSameSite::STRICT_MODE: |
250 devtools_cookie->set_same_site(cookie::kSameSiteStrict); | 363 devtools_cookie->set_same_site(kSameSiteStrict); |
251 break; | 364 break; |
252 case net::CookieSameSite::LAX_MODE: | 365 case net::CookieSameSite::LAX_MODE: |
253 devtools_cookie->set_same_site(cookie::kSameSiteLax); | 366 devtools_cookie->set_same_site(kSameSiteLax); |
254 break; | 367 break; |
255 case net::CookieSameSite::NO_RESTRICTION: | 368 case net::CookieSameSite::NO_RESTRICTION: |
256 break; | 369 break; |
257 } | 370 } |
258 cookies.push_back(devtools_cookie); | 371 cookies.push_back(devtools_cookie); |
259 } | 372 } |
260 client_->SendGetCookiesResponse(command_id, | 373 client_->SendGetCookiesResponse(command_id, |
261 GetCookiesResponse::Create()->set_cookies(cookies)); | 374 GetCookiesResponse::Create()->set_cookies(cookies)); |
262 } | 375 } |
263 | 376 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
340 return Response::InternalError("Could not connect to view"); | 453 return Response::InternalError("Could not connect to view"); |
341 WebContents* web_contents = WebContents::FromRenderFrameHost(host_); | 454 WebContents* web_contents = WebContents::FromRenderFrameHost(host_); |
342 web_contents->GetDelegate()->ShowCertificateViewerInDevTools( | 455 web_contents->GetDelegate()->ShowCertificateViewerInDevTools( |
343 web_contents, certificate_id); | 456 web_contents, certificate_id); |
344 return Response::OK(); | 457 return Response::OK(); |
345 } | 458 } |
346 | 459 |
347 } // namespace network | 460 } // namespace network |
348 } // namespace devtools | 461 } // namespace devtools |
349 } // namespace content | 462 } // namespace content |
OLD | NEW |