| 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" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "content/browser/frame_host/frame_tree_node.h" | 12 #include "content/browser/frame_host/frame_tree_node.h" |
| 13 #include "content/browser/frame_host/render_frame_host_impl.h" | 13 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 14 #include "content/public/browser/browser_context.h" | 14 #include "content/public/browser/browser_context.h" |
| 15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/content_browser_client.h" | 16 #include "content/public/browser/content_browser_client.h" |
| 17 #include "content/public/browser/render_process_host.h" | 17 #include "content/public/browser/render_process_host.h" |
| 18 #include "content/public/browser/resource_context.h" | 18 #include "content/public/browser/resource_context.h" |
| 19 #include "content/public/browser/site_instance.h" | 19 #include "content/public/browser/site_instance.h" |
| 20 #include "content/public/browser/storage_partition.h" | 20 #include "content/public/browser/storage_partition.h" |
| 21 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 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 devtools { | 28 namespace protocol { |
| 29 namespace network { | 29 namespace { |
| 30 | 30 |
| 31 using Response = DevToolsProtocolClient::Response; | 31 using GetCookiesCallback = protocol::Network::Backend::GetCookiesCallback; |
| 32 using CookieListCallback = net::CookieStore::GetCookieListCallback; | 32 using SetCookieCallback = protocol::Network::Backend::SetCookieCallback; |
| 33 using SetCookieCallback = net::CookieStore::SetCookiesCallback; | 33 using DeleteCookieCallback = protocol::Network::Backend::DeleteCookieCallback; |
| 34 | |
| 35 namespace { | |
| 36 | 34 |
| 37 net::URLRequestContext* GetRequestContextOnIO( | 35 net::URLRequestContext* GetRequestContextOnIO( |
| 38 ResourceContext* resource_context, | 36 ResourceContext* resource_context, |
| 39 net::URLRequestContextGetter* context_getter, | 37 net::URLRequestContextGetter* context_getter, |
| 40 const GURL& url) { | 38 const GURL& url) { |
| 41 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 39 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 42 net::URLRequestContext* context = | 40 net::URLRequestContext* context = |
| 43 GetContentClient()->browser()->OverrideRequestContextForURL( | 41 GetContentClient()->browser()->OverrideRequestContextForURL( |
| 44 url, resource_context); | 42 url, resource_context); |
| 45 if (!context) | 43 if (!context) |
| 46 context = context_getter->GetURLRequestContext(); | 44 context = context_getter->GetURLRequestContext(); |
| 47 return context; | 45 return context; |
| 48 } | 46 } |
| 49 | 47 |
| 50 void GotCookiesForURLOnIO( | 48 void GotCookiesOnIO( |
| 51 const CookieListCallback& callback, | 49 const net::CookieStore::GetCookieListCallback& callback, |
| 52 const net::CookieList& cookie_list) { | 50 const net::CookieList& cookie_list) { |
| 53 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 51 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 54 BrowserThread::PostTask( | 52 BrowserThread::PostTask( |
| 55 BrowserThread::UI, | 53 BrowserThread::UI, |
| 56 FROM_HERE, | 54 FROM_HERE, |
| 57 base::Bind(callback, cookie_list)); | 55 base::Bind(callback, cookie_list)); |
| 58 } | 56 } |
| 59 | 57 |
| 60 void GetCookiesForURLOnIO( | 58 void GetCookiesForURLOnIO( |
| 61 ResourceContext* resource_context, | 59 ResourceContext* resource_context, |
| 62 net::URLRequestContextGetter* context_getter, | 60 net::URLRequestContextGetter* context_getter, |
| 63 const GURL& url, | 61 const GURL& url, |
| 64 const CookieListCallback& callback) { | 62 const net::CookieStore::GetCookieListCallback& callback) { |
| 65 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 63 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 66 net::URLRequestContext* request_context = | 64 net::URLRequestContext* request_context = |
| 67 GetRequestContextOnIO(resource_context, context_getter, url); | 65 GetRequestContextOnIO(resource_context, context_getter, url); |
| 68 request_context->cookie_store()->GetAllCookiesForURLAsync( | 66 request_context->cookie_store()->GetAllCookiesForURLAsync( |
| 69 url, base::Bind(&GotCookiesForURLOnIO, callback)); | 67 url, base::Bind(&GotCookiesOnIO, callback)); |
| 70 } | 68 } |
| 71 | 69 |
| 72 void GetCookiesForURLOnUI( | 70 void GetAllCookiesOnIO( |
| 73 ResourceContext* resource_context, | 71 ResourceContext* resource_context, |
| 74 net::URLRequestContextGetter* context_getter, | 72 net::URLRequestContextGetter* context_getter, |
| 75 const GURL& url, | 73 const net::CookieStore::GetCookieListCallback& callback) { |
| 76 const CookieListCallback& callback) { | |
| 77 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 78 BrowserThread::PostTask( | |
| 79 BrowserThread::IO, | |
| 80 FROM_HERE, | |
| 81 base::Bind(&GetCookiesForURLOnIO, | |
| 82 base::Unretained(resource_context), | |
| 83 base::Unretained(context_getter), | |
| 84 url, | |
| 85 callback)); | |
| 86 } | |
| 87 | |
| 88 void GetCookiesOnIO(ResourceContext* resource_context, | |
| 89 net::URLRequestContextGetter* context_getter, | |
| 90 const CookieListCallback& callback) { | |
| 91 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 74 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 92 net::URLRequestContext* request_context = | 75 net::URLRequestContext* request_context = |
| 93 context_getter->GetURLRequestContext(); | 76 context_getter->GetURLRequestContext(); |
| 94 request_context->cookie_store()->GetAllCookiesAsync( | 77 request_context->cookie_store()->GetAllCookiesAsync( |
| 95 base::Bind(&GotCookiesForURLOnIO, callback)); | 78 base::Bind(&GotCookiesOnIO, callback)); |
| 96 } | 79 } |
| 97 | 80 |
| 98 void GetCookiesOnUI(ResourceContext* resource_context, | 81 void DeletedCookieOnIO(std::unique_ptr<DeleteCookieCallback> callback) { |
| 99 net::URLRequestContextGetter* context_getter, | |
| 100 const CookieListCallback& callback) { | |
| 101 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 102 BrowserThread::PostTask( | |
| 103 BrowserThread::IO, FROM_HERE, | |
| 104 base::Bind(&GetCookiesOnIO, base::Unretained(resource_context), | |
| 105 base::Unretained(context_getter), callback)); | |
| 106 } | |
| 107 | |
| 108 void DeletedCookieOnIO(const base::Closure& callback) { | |
| 109 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 82 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 110 BrowserThread::PostTask( | 83 BrowserThread::PostTask( |
| 111 BrowserThread::UI, | 84 BrowserThread::UI, |
| 112 FROM_HERE, | 85 FROM_HERE, |
| 113 callback); | 86 base::Bind(&DeleteCookieCallback::sendSuccess, |
| 87 base::Passed(std::move(callback)))); |
| 114 } | 88 } |
| 115 | 89 |
| 116 void DeleteCookieOnIO( | 90 void DeleteCookieOnIO( |
| 117 ResourceContext* resource_context, | 91 ResourceContext* resource_context, |
| 118 net::URLRequestContextGetter* context_getter, | 92 net::URLRequestContextGetter* context_getter, |
| 119 const GURL& url, | 93 const GURL& url, |
| 120 const std::string& cookie_name, | 94 const std::string& cookie_name, |
| 121 const base::Closure& callback) { | 95 std::unique_ptr<DeleteCookieCallback> callback) { |
| 122 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 96 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 123 net::URLRequestContext* request_context = | 97 net::URLRequestContext* request_context = |
| 124 GetRequestContextOnIO(resource_context, context_getter, url); | 98 GetRequestContextOnIO(resource_context, context_getter, url); |
| 125 request_context->cookie_store()->DeleteCookieAsync( | 99 request_context->cookie_store()->DeleteCookieAsync( |
| 126 url, cookie_name, base::Bind(&DeletedCookieOnIO, callback)); | 100 url, cookie_name, base::Bind(&DeletedCookieOnIO, |
| 101 base::Passed(std::move(callback)))); |
| 127 } | 102 } |
| 128 | 103 |
| 129 void DeleteCookieOnUI( | 104 void CookieSetOnIO(std::unique_ptr<SetCookieCallback> callback, bool success) { |
| 130 ResourceContext* resource_context, | |
| 131 net::URLRequestContextGetter* context_getter, | |
| 132 const GURL& url, | |
| 133 const std::string& cookie_name, | |
| 134 const base::Closure& callback) { | |
| 135 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 136 BrowserThread::PostTask( | |
| 137 BrowserThread::IO, | |
| 138 FROM_HERE, | |
| 139 base::Bind(&DeleteCookieOnIO, | |
| 140 base::Unretained(resource_context), | |
| 141 base::Unretained(context_getter), | |
| 142 url, | |
| 143 cookie_name, | |
| 144 callback)); | |
| 145 } | |
| 146 | |
| 147 void CookieSetOnIO(const SetCookieCallback& callback, bool success) { | |
| 148 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 105 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 149 BrowserThread::PostTask( | 106 BrowserThread::PostTask( |
| 150 BrowserThread::UI, | 107 BrowserThread::UI, |
| 151 FROM_HERE, | 108 FROM_HERE, |
| 152 base::Bind(callback, success)); | 109 base::Bind(&SetCookieCallback::sendSuccess, |
| 110 base::Passed(std::move(callback)), |
| 111 success)); |
| 153 } | 112 } |
| 154 | 113 |
| 155 void SetCookieOnIO( | 114 void SetCookieOnIO( |
| 156 ResourceContext* resource_context, | 115 ResourceContext* resource_context, |
| 157 net::URLRequestContextGetter* context_getter, | 116 net::URLRequestContextGetter* context_getter, |
| 158 const GURL& url, | 117 const GURL& url, |
| 159 const std::string& name, | 118 const std::string& name, |
| 160 const std::string& value, | 119 const std::string& value, |
| 161 const std::string& domain, | 120 const std::string& domain, |
| 162 const std::string& path, | 121 const std::string& path, |
| 163 bool secure, | 122 bool secure, |
| 164 bool http_only, | 123 bool http_only, |
| 165 net::CookieSameSite same_site, | 124 net::CookieSameSite same_site, |
| 166 base::Time expires, | 125 base::Time expires, |
| 167 const SetCookieCallback& callback) { | 126 std::unique_ptr<SetCookieCallback> callback) { |
| 168 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 127 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 169 net::URLRequestContext* request_context = | 128 net::URLRequestContext* request_context = |
| 170 GetRequestContextOnIO(resource_context, context_getter, url); | 129 GetRequestContextOnIO(resource_context, context_getter, url); |
| 171 | 130 |
| 172 bool are_experimental_cookie_features_enabled = | 131 bool are_experimental_cookie_features_enabled = |
| 173 request_context->network_delegate() | 132 request_context->network_delegate() |
| 174 ->AreExperimentalCookieFeaturesEnabled(); | 133 ->AreExperimentalCookieFeaturesEnabled(); |
| 175 | 134 |
| 176 request_context->cookie_store()->SetCookieWithDetailsAsync( | 135 request_context->cookie_store()->SetCookieWithDetailsAsync( |
| 177 url, name, value, domain, path, | 136 url, name, value, domain, path, |
| 178 base::Time(), | 137 base::Time(), |
| 179 expires, | 138 expires, |
| 180 base::Time(), | 139 base::Time(), |
| 181 secure, | 140 secure, |
| 182 http_only, | 141 http_only, |
| 183 same_site, | 142 same_site, |
| 184 are_experimental_cookie_features_enabled, | 143 are_experimental_cookie_features_enabled, |
| 185 net::COOKIE_PRIORITY_DEFAULT, | 144 net::COOKIE_PRIORITY_DEFAULT, |
| 186 base::Bind(&CookieSetOnIO, callback)); | 145 base::Bind(&CookieSetOnIO, base::Passed(std::move(callback)))); |
| 187 } | |
| 188 | |
| 189 void SetCookieOnUI( | |
| 190 ResourceContext* resource_context, | |
| 191 net::URLRequestContextGetter* context_getter, | |
| 192 const GURL& url, | |
| 193 const std::string& name, | |
| 194 const std::string& value, | |
| 195 const std::string& domain, | |
| 196 const std::string& path, | |
| 197 bool secure, | |
| 198 bool http_only, | |
| 199 net::CookieSameSite same_site, | |
| 200 base::Time expires, | |
| 201 const SetCookieCallback& callback) { | |
| 202 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 203 BrowserThread::PostTask( | |
| 204 BrowserThread::IO, | |
| 205 FROM_HERE, | |
| 206 base::Bind(&SetCookieOnIO, | |
| 207 base::Unretained(resource_context), | |
| 208 base::Unretained(context_getter), | |
| 209 url, name, value, domain, path, secure, http_only, | |
| 210 same_site, expires, callback)); | |
| 211 } | 146 } |
| 212 | 147 |
| 213 class GetCookiesCommand { | 148 class GetCookiesCommand { |
| 214 public: | 149 public: |
| 215 GetCookiesCommand(RenderFrameHostImpl* frame_host, | 150 GetCookiesCommand( |
| 216 bool global, | 151 RenderFrameHostImpl* frame_host, |
| 217 const CookieListCallback& callback) | 152 bool global, |
| 218 : callback_(callback), request_count_(0) { | 153 std::unique_ptr<GetCookiesCallback> callback) |
| 219 CookieListCallback got_cookies_callback = base::Bind( | 154 : callback_(std::move(callback)), |
| 155 request_count_(0) { |
| 156 net::CookieStore::GetCookieListCallback got_cookies_callback = base::Bind( |
| 220 &GetCookiesCommand::GotCookiesForURL, base::Unretained(this)); | 157 &GetCookiesCommand::GotCookiesForURL, base::Unretained(this)); |
| 221 | 158 |
| 222 if (global) { | 159 if (global) { |
| 223 request_count_ = 1; | 160 request_count_ = 1; |
| 224 GetCookiesOnUI(frame_host->GetSiteInstance() | 161 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( |
| 225 ->GetBrowserContext() | 162 &GetAllCookiesOnIO, |
| 226 ->GetResourceContext(), | 163 base::Unretained(frame_host->GetSiteInstance()->GetBrowserContext()-> |
| 227 frame_host->GetProcess() | 164 GetResourceContext()), |
| 228 ->GetStoragePartition() | 165 base::Unretained(frame_host->GetProcess()->GetStoragePartition()-> |
| 229 ->GetURLRequestContext(), | 166 GetURLRequestContext()), |
| 230 got_cookies_callback); | 167 got_cookies_callback)); |
| 231 return; | 168 return; |
| 232 } | 169 } |
| 233 | 170 |
| 234 std::queue<FrameTreeNode*> queue; | 171 std::queue<FrameTreeNode*> queue; |
| 235 queue.push(frame_host->frame_tree_node()); | 172 queue.push(frame_host->frame_tree_node()); |
| 236 while (!queue.empty()) { | 173 while (!queue.empty()) { |
| 237 FrameTreeNode* node = queue.front(); | 174 FrameTreeNode* node = queue.front(); |
| 238 queue.pop(); | 175 queue.pop(); |
| 239 | 176 |
| 240 // Only traverse nodes with the same local root. | 177 // Only traverse nodes with the same local root. |
| 241 if (node->current_frame_host()->IsCrossProcessSubframe()) | 178 if (node->current_frame_host()->IsCrossProcessSubframe()) |
| 242 continue; | 179 continue; |
| 243 ++request_count_; | 180 ++request_count_; |
| 244 GetCookiesForURLOnUI( | 181 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( |
| 245 frame_host->GetSiteInstance()->GetBrowserContext()-> | 182 &GetCookiesForURLOnIO, |
| 246 GetResourceContext(), | 183 base::Unretained(frame_host->GetSiteInstance()->GetBrowserContext()-> |
| 247 frame_host->GetProcess()->GetStoragePartition()-> | 184 GetResourceContext()), |
| 248 GetURLRequestContext(), | 185 base::Unretained(frame_host->GetProcess()->GetStoragePartition()-> |
| 186 GetURLRequestContext()), |
| 249 node->current_url(), | 187 node->current_url(), |
| 250 got_cookies_callback); | 188 got_cookies_callback)); |
| 251 | 189 |
| 252 for (size_t i = 0; i < node->child_count(); ++i) | 190 for (size_t i = 0; i < node->child_count(); ++i) |
| 253 queue.push(node->child_at(i)); | 191 queue.push(node->child_at(i)); |
| 254 } | 192 } |
| 255 } | 193 } |
| 256 | 194 |
| 257 private: | 195 private: |
| 258 void GotCookiesForURL(const net::CookieList& cookie_list) { | 196 void GotCookiesForURL(const net::CookieList& cookie_list) { |
| 259 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 197 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 260 for (const net::CanonicalCookie& cookie : cookie_list) { | 198 for (const net::CanonicalCookie& cookie : cookie_list) { |
| 261 std::string key = base::StringPrintf( | 199 std::string key = base::StringPrintf( |
| 262 "%s::%s::%s::%d", | 200 "%s::%s::%s::%d", |
| 263 cookie.Name().c_str(), | 201 cookie.Name().c_str(), |
| 264 cookie.Domain().c_str(), | 202 cookie.Domain().c_str(), |
| 265 cookie.Path().c_str(), | 203 cookie.Path().c_str(), |
| 266 cookie.IsSecure()); | 204 cookie.IsSecure()); |
| 267 cookies_[key] = cookie; | 205 cookies_[key] = cookie; |
| 268 } | 206 } |
| 269 --request_count_; | 207 --request_count_; |
| 270 if (!request_count_) { | 208 if (!request_count_) { |
| 271 net::CookieList list; | 209 SendResponse(); |
| 272 list.reserve(cookies_.size()); | |
| 273 for (const auto& pair : cookies_) | |
| 274 list.push_back(pair.second); | |
| 275 callback_.Run(list); | |
| 276 delete this; | 210 delete this; |
| 277 } | 211 } |
| 278 } | 212 } |
| 279 | 213 |
| 280 CookieListCallback callback_; | 214 void SendResponse() { |
| 215 std::unique_ptr<protocol::Array<Network::Cookie>> cookies = |
| 216 protocol::Array<Network::Cookie>::create(); |
| 217 for (const auto& pair : cookies_) { |
| 218 const net::CanonicalCookie& cookie = pair.second; |
| 219 std::unique_ptr<Network::Cookie> devtools_cookie = |
| 220 Network::Cookie::Create() |
| 221 .SetName(cookie.Name()) |
| 222 .SetValue(cookie.Value()) |
| 223 .SetDomain(cookie.Domain()) |
| 224 .SetPath(cookie.Path()) |
| 225 .SetExpires(cookie.ExpiryDate().ToDoubleT() * 1000) |
| 226 .SetSize(cookie.Name().length() + cookie.Value().length()) |
| 227 .SetHttpOnly(cookie.IsHttpOnly()) |
| 228 .SetSecure(cookie.IsSecure()) |
| 229 .SetSession(!cookie.IsPersistent()) |
| 230 .Build(); |
| 231 |
| 232 switch (cookie.SameSite()) { |
| 233 case net::CookieSameSite::STRICT_MODE: |
| 234 devtools_cookie->SetSameSite(Network::CookieSameSiteEnum::Strict); |
| 235 break; |
| 236 case net::CookieSameSite::LAX_MODE: |
| 237 devtools_cookie->SetSameSite(Network::CookieSameSiteEnum::Lax); |
| 238 break; |
| 239 case net::CookieSameSite::NO_RESTRICTION: |
| 240 break; |
| 241 } |
| 242 cookies->addItem(std::move(devtools_cookie)); |
| 243 } |
| 244 callback_->sendSuccess(std::move(cookies)); |
| 245 } |
| 246 |
| 247 std::unique_ptr<GetCookiesCallback> callback_; |
| 281 int request_count_; | 248 int request_count_; |
| 282 base::hash_map<std::string, net::CanonicalCookie> cookies_; | 249 base::hash_map<std::string, net::CanonicalCookie> cookies_; |
| 283 }; | 250 }; |
| 284 | 251 |
| 285 } // namespace | 252 } // namespace |
| 286 | 253 |
| 287 typedef DevToolsProtocolClient::Response Response; | |
| 288 | |
| 289 NetworkHandler::NetworkHandler() | 254 NetworkHandler::NetworkHandler() |
| 290 : host_(nullptr), enabled_(false), weak_factory_(this) { | 255 : host_(nullptr), enabled_(false) { |
| 291 } | 256 } |
| 292 | 257 |
| 293 NetworkHandler::~NetworkHandler() { | 258 NetworkHandler::~NetworkHandler() { |
| 294 } | 259 } |
| 295 | 260 |
| 261 void NetworkHandler::Wire(UberDispatcher* dispatcher) { |
| 262 Network::Dispatcher::wire(dispatcher, this); |
| 263 } |
| 264 |
| 296 void NetworkHandler::SetRenderFrameHost(RenderFrameHostImpl* host) { | 265 void NetworkHandler::SetRenderFrameHost(RenderFrameHostImpl* host) { |
| 297 host_ = host; | 266 host_ = host; |
| 298 } | 267 } |
| 299 | 268 |
| 300 void NetworkHandler::SetClient(std::unique_ptr<Client> client) { | 269 Response NetworkHandler::Enable(Maybe<int> max_total_size, |
| 301 client_.swap(client); | 270 Maybe<int> max_resource_size) { |
| 302 } | |
| 303 | |
| 304 Response NetworkHandler::Enable(const int* max_total_size, | |
| 305 const int* max_resource_size) { | |
| 306 enabled_ = true; | 271 enabled_ = true; |
| 307 return Response::FallThrough(); | 272 return Response::FallThrough(); |
| 308 } | 273 } |
| 309 | 274 |
| 310 Response NetworkHandler::Disable() { | 275 Response NetworkHandler::Disable() { |
| 311 enabled_ = false; | 276 enabled_ = false; |
| 312 return Response::FallThrough(); | 277 return Response::FallThrough(); |
| 313 } | 278 } |
| 314 | 279 |
| 315 Response NetworkHandler::ClearBrowserCache() { | 280 Response NetworkHandler::ClearBrowserCache() { |
| 316 if (host_) | 281 if (host_) |
| 317 GetContentClient()->browser()->ClearCache(host_); | 282 GetContentClient()->browser()->ClearCache(host_); |
| 318 return Response::OK(); | 283 return Response::OK(); |
| 319 } | 284 } |
| 320 | 285 |
| 321 Response NetworkHandler::ClearBrowserCookies() { | 286 Response NetworkHandler::ClearBrowserCookies() { |
| 322 if (host_) | 287 if (host_) |
| 323 GetContentClient()->browser()->ClearCookies(host_); | 288 GetContentClient()->browser()->ClearCookies(host_); |
| 324 return Response::OK(); | 289 return Response::OK(); |
| 325 } | 290 } |
| 326 | 291 |
| 327 Response NetworkHandler::GetCookies(DevToolsCommandId command_id, | 292 void NetworkHandler::GetCookies( |
| 328 const bool* global) { | 293 Maybe<bool> global, |
| 294 std::unique_ptr<GetCookiesCallback> callback) { |
| 329 if (!host_) | 295 if (!host_) |
| 330 return Response::InternalError("Could not connect to view"); | 296 callback->sendFailure(Response::InternalError()); |
| 331 new GetCookiesCommand(host_, global ? *global : false, | 297 else |
| 332 base::Bind(&NetworkHandler::SendGetCookiesResponse, | 298 new GetCookiesCommand(host_, global.fromMaybe(false), std::move(callback)); |
| 333 weak_factory_.GetWeakPtr(), command_id)); | |
| 334 return Response::OK(); | |
| 335 } | 299 } |
| 336 | 300 |
| 337 Response NetworkHandler::SetCookie(DevToolsCommandId command_id, | 301 void NetworkHandler::SetCookie( |
| 338 const std::string& url, | 302 const std::string& url, |
| 339 const std::string& name, | 303 const std::string& name, |
| 340 const std::string& value, | 304 const std::string& value, |
| 341 const std::string* domain, | 305 Maybe<std::string> domain, |
| 342 const std::string* path, | 306 Maybe<std::string> path, |
| 343 bool* secure, | 307 Maybe<bool> secure, |
| 344 bool* http_only, | 308 Maybe<bool> http_only, |
| 345 const std::string* same_site, | 309 Maybe<std::string> same_site, |
| 346 double* expires) { | 310 Maybe<double> expires, |
| 347 if (!host_) | 311 std::unique_ptr<SetCookieCallback> callback) { |
| 348 return Response::InternalError("Could not connect to view"); | 312 if (!host_) { |
| 313 callback->sendFailure(Response::InternalError()); |
| 314 return; |
| 315 } |
| 349 | 316 |
| 350 net::CookieSameSite same_site_enum = net::CookieSameSite::DEFAULT_MODE; | 317 net::CookieSameSite same_site_enum = net::CookieSameSite::DEFAULT_MODE; |
| 351 if (same_site && *same_site == kCookieSameSiteLax) | 318 if (same_site.isJust()) { |
| 352 same_site_enum = net::CookieSameSite::LAX_MODE; | 319 if (same_site.fromJust() == Network::CookieSameSiteEnum::Lax) |
| 353 else if (same_site && *same_site == kCookieSameSiteStrict) | 320 same_site_enum = net::CookieSameSite::LAX_MODE; |
| 354 same_site_enum = net::CookieSameSite::STRICT_MODE; | 321 else if (same_site.fromJust() == Network::CookieSameSiteEnum::Strict) |
| 322 same_site_enum = net::CookieSameSite::STRICT_MODE; |
| 323 } |
| 355 | 324 |
| 356 base::Time expiration_date; | 325 base::Time expiration_date; |
| 357 if (expires) | 326 if (expires.isJust()) { |
| 358 expiration_date = (*expires == 0) | 327 expiration_date = expires.fromJust() == 0 |
| 359 ? base::Time::UnixEpoch() | 328 ? base::Time::UnixEpoch() |
| 360 : base::Time::FromDoubleT(*expires); | 329 : base::Time::FromDoubleT(expires.fromJust()); |
| 330 } |
| 361 | 331 |
| 362 SetCookieOnUI( | 332 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( |
| 363 host_->GetSiteInstance()->GetBrowserContext()->GetResourceContext(), | 333 &SetCookieOnIO, |
| 364 host_->GetProcess()->GetStoragePartition()->GetURLRequestContext(), | 334 base::Unretained(host_->GetSiteInstance()->GetBrowserContext()-> |
| 365 GURL(url), name, value, | 335 GetResourceContext()), |
| 366 domain ? *domain : std::string(), path ? *path : std::string(), | 336 base::Unretained(host_->GetProcess()->GetStoragePartition()-> |
| 367 secure ? *secure : false, http_only ? *http_only : false, | 337 GetURLRequestContext()), |
| 368 same_site_enum, expiration_date, | 338 GURL(url), name, value, domain.fromMaybe(""), path.fromMaybe(""), |
| 369 base::Bind(&NetworkHandler::SendSetCookieResponse, | 339 secure.fromMaybe(false), http_only.fromMaybe(false), same_site_enum, |
| 370 weak_factory_.GetWeakPtr(), | 340 expiration_date, base::Passed(std::move(callback)))); |
| 371 command_id)); | |
| 372 return Response::OK(); | |
| 373 } | 341 } |
| 374 | 342 |
| 375 void NetworkHandler::SendSetCookieResponse(DevToolsCommandId command_id, | 343 void NetworkHandler::DeleteCookie( |
| 376 bool success) { | 344 const std::string& cookie_name, |
| 377 client_->SendSetCookieResponse(command_id, | 345 const std::string& url, |
| 378 SetCookieResponse::Create()->set_success(success)); | 346 std::unique_ptr<DeleteCookieCallback> callback) { |
| 379 } | 347 if (!host_) { |
| 380 | 348 callback->sendFailure(Response::InternalError()); |
| 381 void NetworkHandler::SendGetCookiesResponse( | 349 return; |
| 382 DevToolsCommandId command_id, | |
| 383 const net::CookieList& cookie_list) { | |
| 384 std::vector<scoped_refptr<Cookie>> cookies; | |
| 385 for (size_t i = 0; i < cookie_list.size(); ++i) { | |
| 386 const net::CanonicalCookie& cookie = cookie_list[i]; | |
| 387 scoped_refptr<Cookie> devtools_cookie = Cookie::Create() | |
| 388 ->set_name(cookie.Name()) | |
| 389 ->set_value(cookie.Value()) | |
| 390 ->set_domain(cookie.Domain()) | |
| 391 ->set_path(cookie.Path()) | |
| 392 ->set_expires(cookie.ExpiryDate().ToDoubleT() * 1000) | |
| 393 ->set_size(cookie.Name().length() + cookie.Value().length()) | |
| 394 ->set_http_only(cookie.IsHttpOnly()) | |
| 395 ->set_secure(cookie.IsSecure()) | |
| 396 ->set_session(!cookie.IsPersistent()); | |
| 397 | |
| 398 switch (cookie.SameSite()) { | |
| 399 case net::CookieSameSite::STRICT_MODE: | |
| 400 devtools_cookie->set_same_site(kCookieSameSiteStrict); | |
| 401 break; | |
| 402 case net::CookieSameSite::LAX_MODE: | |
| 403 devtools_cookie->set_same_site(kCookieSameSiteLax); | |
| 404 break; | |
| 405 case net::CookieSameSite::NO_RESTRICTION: | |
| 406 break; | |
| 407 } | |
| 408 cookies.push_back(devtools_cookie); | |
| 409 } | 350 } |
| 410 client_->SendGetCookiesResponse(command_id, | 351 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, base::Bind( |
| 411 GetCookiesResponse::Create()->set_cookies(cookies)); | 352 &DeleteCookieOnIO, |
| 412 } | 353 base::Unretained(host_->GetSiteInstance()->GetBrowserContext()-> |
| 413 | 354 GetResourceContext()), |
| 414 Response NetworkHandler::DeleteCookie( | 355 base::Unretained(host_->GetProcess()->GetStoragePartition()-> |
| 415 DevToolsCommandId command_id, | 356 GetURLRequestContext()), |
| 416 const std::string& cookie_name, | |
| 417 const std::string& url) { | |
| 418 if (!host_) | |
| 419 return Response::InternalError("Could not connect to view"); | |
| 420 DeleteCookieOnUI( | |
| 421 host_->GetSiteInstance()->GetBrowserContext()->GetResourceContext(), | |
| 422 host_->GetProcess()->GetStoragePartition()->GetURLRequestContext(), | |
| 423 GURL(url), | 357 GURL(url), |
| 424 cookie_name, | 358 cookie_name, |
| 425 base::Bind(&NetworkHandler::SendDeleteCookieResponse, | 359 base::Passed(std::move(callback)))); |
| 426 weak_factory_.GetWeakPtr(), | |
| 427 command_id)); | |
| 428 return Response::OK(); | |
| 429 } | 360 } |
| 430 | 361 |
| 431 void NetworkHandler::SendDeleteCookieResponse(DevToolsCommandId command_id) { | |
| 432 client_->SendDeleteCookieResponse(command_id, | |
| 433 DeleteCookieResponse::Create()); | |
| 434 } | |
| 435 | |
| 436 | |
| 437 Response NetworkHandler::CanEmulateNetworkConditions(bool* result) { | 362 Response NetworkHandler::CanEmulateNetworkConditions(bool* result) { |
| 438 *result = false; | 363 *result = false; |
| 439 return Response::OK(); | 364 return Response::OK(); |
| 440 } | 365 } |
| 441 | 366 |
| 442 Response NetworkHandler::EmulateNetworkConditions( | 367 } // namespace protocol |
| 443 bool offline, | |
| 444 double latency, | |
| 445 double download_throughput, | |
| 446 double upload_throughput, | |
| 447 const std::string* connection_type) { | |
| 448 return Response::FallThrough(); | |
| 449 } | |
| 450 | |
| 451 } // namespace network | |
| 452 } // namespace devtools | |
| 453 } // namespace content | 368 } // namespace content |
| OLD | NEW |