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

Side by Side Diff: content/browser/devtools/protocol/network_handler.cc

Issue 2221093003: [Devtools] Backend support for setCookie (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: backend changes Created 4 years, 4 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 unified diff | Download patch
OLDNEW
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
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;
36 37
37 namespace { 38 namespace {
38 39
39 net::URLRequestContext* GetRequestContextOnIO( 40 net::URLRequestContext* GetRequestContextOnIO(
40 ResourceContext* resource_context, 41 ResourceContext* resource_context,
41 net::URLRequestContextGetter* context_getter, 42 net::URLRequestContextGetter* context_getter,
42 const GURL& url) { 43 const GURL& url) {
43 DCHECK_CURRENTLY_ON(BrowserThread::IO); 44 DCHECK_CURRENTLY_ON(BrowserThread::IO);
44 net::URLRequestContext* context = 45 net::URLRequestContext* context =
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 BrowserThread::IO, 120 BrowserThread::IO,
120 FROM_HERE, 121 FROM_HERE,
121 base::Bind(&DeleteCookieOnIO, 122 base::Bind(&DeleteCookieOnIO,
122 base::Unretained(resource_context), 123 base::Unretained(resource_context),
123 base::Unretained(context_getter), 124 base::Unretained(context_getter),
124 url, 125 url,
125 cookie_name, 126 cookie_name,
126 callback)); 127 callback));
127 } 128 }
128 129
130 void CookieSetOnIO(const base::Closure& callback, bool success) {
dgozman 2016/08/09 01:07:25 Let's send an error over protocol if !success.
allada 2016/08/10 02:46:54 Done.
131 DCHECK_CURRENTLY_ON(BrowserThread::IO);
132 BrowserThread::PostTask(
133 BrowserThread::UI,
134 FROM_HERE,
135 callback);
136 }
137
138 void SetCookieOnIO(
139 ResourceContext* resource_context,
140 net::URLRequestContextGetter* context_getter,
141 const GURL& url, const std::string& name, const std::string& value,
142 const std::string& domain, const std::string& path, bool secure,
143 bool httpOnly, net::CookieSameSite sameSite, base::Time expires,
144 const base::Closure& callback) {
145 DCHECK_CURRENTLY_ON(BrowserThread::IO);
146 net::URLRequestContext* request_context =
147 GetRequestContextOnIO(resource_context, context_getter, url);
148
149 bool are_experimental_cookie_features_enabled =
150 request_context->network_delegate()
151 ->AreExperimentalCookieFeaturesEnabled();
152
153 request_context->cookie_store()->SetCookieWithDetailsAsync(
154 url, name, value, domain, path,
155 base::Time(),
156 expires,
157 base::Time(),
158 secure,
159 httpOnly,
160 sameSite,
161 are_experimental_cookie_features_enabled,
162 net::COOKIE_PRIORITY_DEFAULT,
163 base::Bind(&CookieSetOnIO, callback));
164 }
165
166 void SetCookieOnUI(
167 ResourceContext* resource_context,
168 net::URLRequestContextGetter* context_getter,
169 const GURL& url, const std::string& name, const std::string& value,
170 const std::string& domain, const std::string& path, bool secure,
171 bool httpOnly, net::CookieSameSite sameSite, base::Time expires,
172 const base::Closure& callback) {
173 DCHECK_CURRENTLY_ON(BrowserThread::UI);
174 BrowserThread::PostTask(
175 BrowserThread::IO,
176 FROM_HERE,
177 base::Bind(&SetCookieOnIO,
178 base::Unretained(resource_context),
179 base::Unretained(context_getter),
180 url, name, value, domain, path, secure, httpOnly,
181 sameSite, expires, callback));
182 }
183
129 class GetCookiesCommand { 184 class GetCookiesCommand {
130 public: 185 public:
131 explicit GetCookiesCommand( 186 explicit GetCookiesCommand(
132 RenderFrameHostImpl* frame_host, 187 RenderFrameHostImpl* frame_host,
133 const CookieListCallback& callback) 188 const CookieListCallback& callback)
134 : callback_(callback), 189 : callback_(callback),
135 request_count_(0) { 190 request_count_(0) {
136 CookieListCallback got_cookies_callback = base::Bind( 191 CookieListCallback got_cookies_callback = base::Bind(
137 &GetCookiesCommand::GotCookiesForURL, base::Unretained(this)); 192 &GetCookiesCommand::GotCookiesForURL, base::Unretained(this));
138 193
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 268
214 Response NetworkHandler::ClearBrowserCookies() { 269 Response NetworkHandler::ClearBrowserCookies() {
215 if (host_) 270 if (host_)
216 GetContentClient()->browser()->ClearCookies(host_); 271 GetContentClient()->browser()->ClearCookies(host_);
217 return Response::OK(); 272 return Response::OK();
218 } 273 }
219 274
220 Response NetworkHandler::GetCookies(DevToolsCommandId command_id) { 275 Response NetworkHandler::GetCookies(DevToolsCommandId command_id) {
221 if (!host_) 276 if (!host_)
222 return Response::InternalError("Could not connect to view"); 277 return Response::InternalError("Could not connect to view");
278
223 new GetCookiesCommand( 279 new GetCookiesCommand(
224 host_, 280 host_,
225 base::Bind(&NetworkHandler::SendGetCookiesResponse, 281 base::Bind(&NetworkHandler::SendGetCookiesResponse,
226 weak_factory_.GetWeakPtr(), 282 weak_factory_.GetWeakPtr(),
227 command_id)); 283 command_id));
228 return Response::OK(); 284 return Response::OK();
229 } 285 }
230 286
287 Response NetworkHandler::SetCookie(DevToolsCommandId command_id,
288 const std::string& url, const std::string& name,
289 const std::string& value, const std::string* domain,
290 const std::string* path, bool* secure, bool* httpOnly,
291 const std::string* sameSite, double* expires) {
292 if (!host_)
293 return Response::InternalError("Could not connect to view");
294
295 net::CookieSameSite sameSiteEnum = net::CookieSameSite::DEFAULT_MODE;
296 if (sameSite && *sameSite == "lax")
dgozman 2016/08/09 01:07:24 There are generated constants for all enums in pro
allada 2016/08/10 02:46:54 Done.
297 sameSiteEnum = net::CookieSameSite::LAX_MODE;
298 else if (sameSite && *sameSite == "strict")
299 sameSiteEnum = net::CookieSameSite::STRICT_MODE;
300
301 base::Time expirationDate;
302 if (expires)
303 expirationDate = (*expires == 0)
304 ? base::Time::UnixEpoch()
305 : base::Time::FromDoubleT(*expires);
306
307 SetCookieOnUI(
308 host_->GetSiteInstance()->GetBrowserContext()->GetResourceContext(),
309 host_->GetProcess()->GetStoragePartition()->GetURLRequestContext(),
310 GURL(url), name, value,
311 domain ? *domain : std::string(), path ? *path : std::string(),
312 secure ? *secure : false, httpOnly ? *httpOnly : false,
313 sameSiteEnum, expirationDate,
314 base::Bind(&NetworkHandler::SendSetCookieResponse,
315 weak_factory_.GetWeakPtr(),
316 command_id));
317 return Response::OK();
318 }
319
320 void NetworkHandler::SendSetCookieResponse(DevToolsCommandId command_id) {
321 client_->SendSetCookieResponse(command_id,
322 SetCookieResponse::Create());
323 }
324
231 void NetworkHandler::SendGetCookiesResponse( 325 void NetworkHandler::SendGetCookiesResponse(
232 DevToolsCommandId command_id, 326 DevToolsCommandId command_id,
233 const net::CookieList& cookie_list) { 327 const net::CookieList& cookie_list) {
234 std::vector<scoped_refptr<Cookie>> cookies; 328 std::vector<scoped_refptr<Cookie>> cookies;
235 for (size_t i = 0; i < cookie_list.size(); ++i) { 329 for (size_t i = 0; i < cookie_list.size(); ++i) {
236 const net::CanonicalCookie& cookie = cookie_list[i]; 330 const net::CanonicalCookie& cookie = cookie_list[i];
237 scoped_refptr<Cookie> devtools_cookie = Cookie::Create() 331 scoped_refptr<Cookie> devtools_cookie = Cookie::Create()
238 ->set_name(cookie.Name()) 332 ->set_name(cookie.Name())
239 ->set_value(cookie.Value()) 333 ->set_value(cookie.Value())
240 ->set_domain(cookie.Domain()) 334 ->set_domain(cookie.Domain())
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 return Response::InternalError("Could not connect to view"); 434 return Response::InternalError("Could not connect to view");
341 WebContents* web_contents = WebContents::FromRenderFrameHost(host_); 435 WebContents* web_contents = WebContents::FromRenderFrameHost(host_);
342 web_contents->GetDelegate()->ShowCertificateViewerInDevTools( 436 web_contents->GetDelegate()->ShowCertificateViewerInDevTools(
343 web_contents, certificate_id); 437 web_contents, certificate_id);
344 return Response::OK(); 438 return Response::OK();
345 } 439 }
346 440
347 } // namespace network 441 } // namespace network
348 } // namespace devtools 442 } // namespace devtools
349 } // namespace content 443 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698