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

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

Issue 2568503002: devtools: Split Network.getCookies into Network.get{All,}Cookies (Closed)
Patch Set: Rebased Created 4 years 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 11 matching lines...) Expand all
22 #include "content/public/common/content_client.h" 22 #include "content/public/common/content_client.h"
23 #include "net/cookies/cookie_store.h" 23 #include "net/cookies/cookie_store.h"
24 #include "net/url_request/url_request_context.h" 24 #include "net/url_request/url_request_context.h"
25 #include "net/url_request/url_request_context_getter.h" 25 #include "net/url_request/url_request_context_getter.h"
26 26
27 namespace content { 27 namespace content {
28 namespace protocol { 28 namespace protocol {
29 namespace { 29 namespace {
30 30
31 using GetCookiesCallback = protocol::Network::Backend::GetCookiesCallback; 31 using GetCookiesCallback = protocol::Network::Backend::GetCookiesCallback;
32 using GetAllCookiesCallback = protocol::Network::Backend::GetAllCookiesCallback;
32 using SetCookieCallback = protocol::Network::Backend::SetCookieCallback; 33 using SetCookieCallback = protocol::Network::Backend::SetCookieCallback;
33 using DeleteCookieCallback = protocol::Network::Backend::DeleteCookieCallback; 34 using DeleteCookieCallback = protocol::Network::Backend::DeleteCookieCallback;
34 35
35 net::URLRequestContext* GetRequestContextOnIO( 36 net::URLRequestContext* GetRequestContextOnIO(
36 ResourceContext* resource_context, 37 ResourceContext* resource_context,
37 net::URLRequestContextGetter* context_getter, 38 net::URLRequestContextGetter* context_getter,
38 const GURL& url) { 39 const GURL& url) {
39 DCHECK_CURRENTLY_ON(BrowserThread::IO); 40 DCHECK_CURRENTLY_ON(BrowserThread::IO);
40 net::URLRequestContext* context = 41 net::URLRequestContext* context =
41 GetContentClient()->browser()->OverrideRequestContextForURL( 42 GetContentClient()->browser()->OverrideRequestContextForURL(
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 expires, 139 expires,
139 base::Time(), 140 base::Time(),
140 secure, 141 secure,
141 http_only, 142 http_only,
142 same_site, 143 same_site,
143 are_experimental_cookie_features_enabled, 144 are_experimental_cookie_features_enabled,
144 net::COOKIE_PRIORITY_DEFAULT, 145 net::COOKIE_PRIORITY_DEFAULT,
145 base::Bind(&CookieSetOnIO, base::Passed(std::move(callback)))); 146 base::Bind(&CookieSetOnIO, base::Passed(std::move(callback))));
146 } 147 }
147 148
148 class GetCookiesCommand { 149 template <typename Callback>
150 class GetCookiesCommandBase {
149 public: 151 public:
150 GetCookiesCommand( 152 GetCookiesCommandBase(std::unique_ptr<Callback> callback)
151 RenderFrameHostImpl* frame_host, 153 : callback_(std::move(callback)), request_count_(0) {}
152 bool global,
153 std::unique_ptr<GetCookiesCallback> callback)
154 : callback_(std::move(callback)),
155 request_count_(0) {
156 net::CookieStore::GetCookieListCallback got_cookies_callback = base::Bind(
157 &GetCookiesCommand::GotCookiesForURL, base::Unretained(this));
158 154
159 if (global) { 155 protected:
160 request_count_ = 1;
161 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
162 &GetAllCookiesOnIO,
163 base::Unretained(frame_host->GetSiteInstance()->GetBrowserContext()->
164 GetResourceContext()),
165 base::Unretained(frame_host->GetProcess()->GetStoragePartition()->
166 GetURLRequestContext()),
167 got_cookies_callback));
168 return;
169 }
170
171 std::queue<FrameTreeNode*> queue;
172 queue.push(frame_host->frame_tree_node());
173 while (!queue.empty()) {
174 FrameTreeNode* node = queue.front();
175 queue.pop();
176
177 // Only traverse nodes with the same local root.
178 if (node->current_frame_host()->IsCrossProcessSubframe())
179 continue;
180 ++request_count_;
181 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind(
182 &GetCookiesForURLOnIO,
183 base::Unretained(frame_host->GetSiteInstance()->GetBrowserContext()->
184 GetResourceContext()),
185 base::Unretained(frame_host->GetProcess()->GetStoragePartition()->
186 GetURLRequestContext()),
187 node->current_url(),
188 got_cookies_callback));
189
190 for (size_t i = 0; i < node->child_count(); ++i)
191 queue.push(node->child_at(i));
192 }
193 }
194
195 private:
196 void GotCookiesForURL(const net::CookieList& cookie_list) { 156 void GotCookiesForURL(const net::CookieList& cookie_list) {
197 DCHECK_CURRENTLY_ON(BrowserThread::UI); 157 DCHECK_CURRENTLY_ON(BrowserThread::UI);
198 for (const net::CanonicalCookie& cookie : cookie_list) { 158 for (const net::CanonicalCookie& cookie : cookie_list) {
199 std::string key = base::StringPrintf( 159 std::string key = base::StringPrintf(
200 "%s::%s::%s::%d", 160 "%s::%s::%s::%d", cookie.Name().c_str(), cookie.Domain().c_str(),
201 cookie.Name().c_str(), 161 cookie.Path().c_str(), cookie.IsSecure());
202 cookie.Domain().c_str(),
203 cookie.Path().c_str(),
204 cookie.IsSecure());
205 cookies_[key] = cookie; 162 cookies_[key] = cookie;
206 } 163 }
207 --request_count_; 164 --request_count_;
208 if (!request_count_) { 165 if (!request_count_) {
209 SendResponse(); 166 SendResponse();
210 delete this; 167 delete this;
211 } 168 }
212 } 169 }
213 170
214 void SendResponse() { 171 void SendResponse() {
(...skipping 22 matching lines...) Expand all
237 devtools_cookie->SetSameSite(Network::CookieSameSiteEnum::Lax); 194 devtools_cookie->SetSameSite(Network::CookieSameSiteEnum::Lax);
238 break; 195 break;
239 case net::CookieSameSite::NO_RESTRICTION: 196 case net::CookieSameSite::NO_RESTRICTION:
240 break; 197 break;
241 } 198 }
242 cookies->addItem(std::move(devtools_cookie)); 199 cookies->addItem(std::move(devtools_cookie));
243 } 200 }
244 callback_->sendSuccess(std::move(cookies)); 201 callback_->sendSuccess(std::move(cookies));
245 } 202 }
246 203
247 std::unique_ptr<GetCookiesCallback> callback_; 204 std::unique_ptr<Callback> callback_;
248 int request_count_; 205 int request_count_;
249 base::hash_map<std::string, net::CanonicalCookie> cookies_; 206 base::hash_map<std::string, net::CanonicalCookie> cookies_;
250 }; 207 };
251 208
209 class GetCookiesCommand : public GetCookiesCommandBase<GetCookiesCallback> {
210 public:
211 GetCookiesCommand(RenderFrameHostImpl* frame_host,
212 std::unique_ptr<GetCookiesCallback> callback)
213 : GetCookiesCommandBase(std::move(callback)) {
214 net::CookieStore::GetCookieListCallback got_cookies_callback = base::Bind(
215 &GetCookiesCommand::GotCookiesForURL, base::Unretained(this));
216
217 std::queue<FrameTreeNode*> queue;
218 queue.push(frame_host->frame_tree_node());
219 while (!queue.empty()) {
220 FrameTreeNode* node = queue.front();
221 queue.pop();
222
223 // Only traverse nodes with the same local root.
224 if (node->current_frame_host()->IsCrossProcessSubframe())
225 continue;
226 ++request_count_;
227 BrowserThread::PostTask(
228 BrowserThread::IO, FROM_HERE,
229 base::Bind(&GetCookiesForURLOnIO,
230 base::Unretained(frame_host->GetSiteInstance()
231 ->GetBrowserContext()
232 ->GetResourceContext()),
233 base::Unretained(frame_host->GetProcess()
234 ->GetStoragePartition()
235 ->GetURLRequestContext()),
236 node->current_url(), got_cookies_callback));
237
238 for (size_t i = 0; i < node->child_count(); ++i)
239 queue.push(node->child_at(i));
240 }
241 }
242 };
243
244 class GetAllCookiesCommand
245 : public GetCookiesCommandBase<GetAllCookiesCallback> {
246 public:
247 GetAllCookiesCommand(RenderFrameHostImpl* frame_host,
248 std::unique_ptr<GetAllCookiesCallback> callback)
249 : GetCookiesCommandBase(std::move(callback)) {
250 net::CookieStore::GetCookieListCallback got_cookies_callback = base::Bind(
251 &GetAllCookiesCommand::GotCookiesForURL, base::Unretained(this));
252
253 request_count_ = 1;
254 BrowserThread::PostTask(
255 BrowserThread::IO, FROM_HERE,
256 base::Bind(&GetAllCookiesOnIO,
257 base::Unretained(frame_host->GetSiteInstance()
258 ->GetBrowserContext()
259 ->GetResourceContext()),
260 base::Unretained(frame_host->GetProcess()
261 ->GetStoragePartition()
262 ->GetURLRequestContext()),
263 got_cookies_callback));
264 }
265 };
266
252 } // namespace 267 } // namespace
253 268
254 NetworkHandler::NetworkHandler() 269 NetworkHandler::NetworkHandler()
255 : host_(nullptr), enabled_(false) { 270 : host_(nullptr), enabled_(false) {
256 } 271 }
257 272
258 NetworkHandler::~NetworkHandler() { 273 NetworkHandler::~NetworkHandler() {
259 } 274 }
260 275
261 void NetworkHandler::Wire(UberDispatcher* dispatcher) { 276 void NetworkHandler::Wire(UberDispatcher* dispatcher) {
(...skipping 21 matching lines...) Expand all
283 return Response::OK(); 298 return Response::OK();
284 } 299 }
285 300
286 Response NetworkHandler::ClearBrowserCookies() { 301 Response NetworkHandler::ClearBrowserCookies() {
287 if (host_) 302 if (host_)
288 GetContentClient()->browser()->ClearCookies(host_); 303 GetContentClient()->browser()->ClearCookies(host_);
289 return Response::OK(); 304 return Response::OK();
290 } 305 }
291 306
292 void NetworkHandler::GetCookies( 307 void NetworkHandler::GetCookies(
293 Maybe<bool> global,
294 std::unique_ptr<GetCookiesCallback> callback) { 308 std::unique_ptr<GetCookiesCallback> callback) {
295 if (!host_) 309 if (!host_)
296 callback->sendFailure(Response::InternalError()); 310 callback->sendFailure(Response::InternalError());
297 else 311 else
298 new GetCookiesCommand(host_, global.fromMaybe(false), std::move(callback)); 312 new GetCookiesCommand(host_, std::move(callback));
313 }
314
315 void NetworkHandler::GetAllCookies(
316 std::unique_ptr<GetAllCookiesCallback> callback) {
317 if (!host_)
318 callback->sendFailure(Response::InternalError());
319 else
320 new GetAllCookiesCommand(host_, std::move(callback));
299 } 321 }
300 322
301 void NetworkHandler::SetCookie( 323 void NetworkHandler::SetCookie(
302 const std::string& url, 324 const std::string& url,
303 const std::string& name, 325 const std::string& name,
304 const std::string& value, 326 const std::string& value,
305 Maybe<std::string> domain, 327 Maybe<std::string> domain,
306 Maybe<std::string> path, 328 Maybe<std::string> path,
307 Maybe<bool> secure, 329 Maybe<bool> secure,
308 Maybe<bool> http_only, 330 Maybe<bool> http_only,
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 base::Passed(std::move(callback)))); 381 base::Passed(std::move(callback))));
360 } 382 }
361 383
362 Response NetworkHandler::CanEmulateNetworkConditions(bool* result) { 384 Response NetworkHandler::CanEmulateNetworkConditions(bool* result) {
363 *result = false; 385 *result = false;
364 return Response::OK(); 386 return Response::OK();
365 } 387 }
366 388
367 } // namespace protocol 389 } // namespace protocol
368 } // namespace content 390 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/devtools/protocol/network_handler.h ('k') | content/browser/devtools/protocol_config.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698