Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 "ios/net/cookies/cookie_store_ios.h" | 5 #include "ios/net/cookies/cookie_store_ios.h" |
| 6 | 6 |
| 7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 267 bool HasExplicitDomain(const std::string& cookie_line) { | 267 bool HasExplicitDomain(const std::string& cookie_line) { |
| 268 ParsedCookie cookie(cookie_line); | 268 ParsedCookie cookie(cookie_line); |
| 269 return cookie.HasDomain(); | 269 return cookie.HasDomain(); |
| 270 } | 270 } |
| 271 | 271 |
| 272 } // namespace | 272 } // namespace |
| 273 | 273 |
| 274 #pragma mark - | 274 #pragma mark - |
| 275 #pragma mark CookieStoreIOS | 275 #pragma mark CookieStoreIOS |
| 276 | 276 |
| 277 CookieStoreIOS::CookieStoreIOS( | 277 CookieStoreIOS::CookieStoreIOS(NSHTTPCookieStorage* cookie_storage) |
| 278 net::CookieMonster::PersistentCookieStore* persistent_store) | 278 : CookieStoreIOS(nullptr, cookie_storage) {} |
| 279 : CookieStoreIOS(persistent_store, | |
| 280 [NSHTTPCookieStorage sharedHTTPCookieStorage]) { | |
| 281 } | |
| 282 | 279 |
| 283 CookieStoreIOS::~CookieStoreIOS() { | 280 CookieStoreIOS::~CookieStoreIOS() { |
| 284 NotificationTrampoline::GetInstance()->RemoveObserver(this); | 281 NotificationTrampoline::GetInstance()->RemoveObserver(this); |
| 285 } | 282 } |
| 286 | 283 |
| 287 // static | 284 // static |
| 288 std::unique_ptr<CookieStoreIOS> CookieStoreIOS::CreateCookieStore( | 285 std::unique_ptr<CookieStoreIOS> CookieStoreIOS::CreateCookieStore( |
| 289 NSHTTPCookieStorage* cookie_storage) { | 286 NSHTTPCookieStorage* cookie_storage) { |
| 290 DCHECK(cookie_storage); | 287 DCHECK(cookie_storage); |
| 291 // TODO(huey): Update this when CrNet supports multiple cookie jars. | 288 // TODO(huey): Update this when CrNet supports multiple cookie jars. |
| 292 [cookie_storage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; | 289 [cookie_storage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; |
| 293 | 290 |
| 294 // Create a cookie store with no persistent store backing. Then, populate | 291 // Create a cookie store with no persistent store backing. Then, populate |
| 295 // it from the system's cookie jar. | 292 // it from the system's cookie jar. |
| 296 std::unique_ptr<CookieStoreIOS> cookie_store( | 293 std::unique_ptr<CookieStoreIOS> cookie_store( |
| 297 new CookieStoreIOS(nullptr, cookie_storage)); | 294 new CookieStoreIOS(cookie_storage)); |
| 298 cookie_store->synchronization_state_ = SYNCHRONIZED; | |
| 299 cookie_store->FlushStore(base::Closure()); | 295 cookie_store->FlushStore(base::Closure()); |
| 300 return cookie_store; | 296 return cookie_store; |
| 301 } | 297 } |
| 302 | 298 |
| 303 // static | 299 // static |
| 304 void CookieStoreIOS::NotifySystemCookiesChanged() { | 300 void CookieStoreIOS::NotifySystemCookiesChanged() { |
| 305 NotificationTrampoline::GetInstance()->NotifyCookiesChanged(); | 301 NotificationTrampoline::GetInstance()->NotifyCookiesChanged(); |
| 306 } | 302 } |
| 307 | 303 |
| 308 void CookieStoreIOS::SetMetricsEnabled() { | 304 void CookieStoreIOS::SetMetricsEnabled() { |
| 309 static CookieStoreIOS* g_cookie_store_with_metrics = nullptr; | 305 static CookieStoreIOS* g_cookie_store_with_metrics = nullptr; |
| 310 DCHECK(!g_cookie_store_with_metrics || g_cookie_store_with_metrics == this) | 306 DCHECK(!g_cookie_store_with_metrics || g_cookie_store_with_metrics == this) |
| 311 << "Only one cookie store may use metrics."; | 307 << "Only one cookie store may use metrics."; |
| 312 g_cookie_store_with_metrics = this; | 308 g_cookie_store_with_metrics = this; |
| 313 metrics_enabled_ = true; | 309 metrics_enabled_ = true; |
| 314 } | 310 } |
| 315 | 311 |
| 316 #pragma mark - | 312 #pragma mark - |
| 317 #pragma mark CookieStore methods | 313 #pragma mark CookieStore methods |
| 318 | 314 |
| 319 void CookieStoreIOS::SetCookieWithOptionsAsync( | 315 void CookieStoreIOS::SetCookieWithOptionsAsync( |
| 320 const GURL& url, | 316 const GURL& url, |
| 321 const std::string& cookie_line, | 317 const std::string& cookie_line, |
| 322 const net::CookieOptions& options, | 318 const net::CookieOptions& options, |
| 323 const SetCookiesCallback& callback) { | 319 const SetCookiesCallback& callback) { |
| 324 DCHECK(thread_checker_.CalledOnValidThread()); | 320 DCHECK(thread_checker_.CalledOnValidThread()); |
| 325 | 321 |
| 326 switch (synchronization_state_) { | 322 // The exclude_httponly() option would only be used by a javascript |
| 327 case NOT_SYNCHRONIZED: | 323 // engine. |
| 328 cookie_monster_->SetCookieWithOptionsAsync(url, cookie_line, options, | 324 DCHECK(!options.exclude_httponly()); |
| 329 WrapSetCallback(callback)); | |
| 330 break; | |
| 331 case SYNCHRONIZED: | |
| 332 // The exclude_httponly() option would only be used by a javascript | |
| 333 // engine. | |
| 334 DCHECK(!options.exclude_httponly()); | |
| 335 // If cookies are not allowed, they are stashed in the CookieMonster, and | |
| 336 // should be written there instead. | |
| 337 DCHECK(SystemCookiesAllowed()); | |
| 338 | 325 |
| 339 base::Time server_time = | 326 // If cookies are not allowed, they are stashed in the CookieMonster, and |
| 340 options.has_server_time() ? options.server_time() : base::Time(); | 327 // should be written there instead. |
| 341 NSHTTPCookie* cookie = | 328 DCHECK(SystemCookiesAllowed()); |
| 342 GetNSHTTPCookieFromCookieLine(cookie_line, url, server_time); | |
| 343 DLOG_IF(WARNING, !cookie) | |
| 344 << "Could not create cookie for line: " << cookie_line; | |
| 345 | 329 |
| 346 // On iOS, [cookie domain] is not empty when the cookie domain is not | 330 base::Time server_time = |
| 347 // specified: it is inferred from the URL instead. The only case when it | 331 options.has_server_time() ? options.server_time() : base::Time(); |
| 348 // is empty is when the domain attribute is incorrectly formatted. | 332 NSHTTPCookie* cookie = |
| 349 std::string domain_string(base::SysNSStringToUTF8([cookie domain])); | 333 GetNSHTTPCookieFromCookieLine(cookie_line, url, server_time); |
| 350 std::string dummy; | 334 DLOG_IF(WARNING, !cookie) << "Could not create cookie for line: " |
| 351 bool has_explicit_domain = HasExplicitDomain(cookie_line); | 335 << cookie_line; |
| 352 bool has_valid_domain = | |
| 353 net::cookie_util::GetCookieDomainWithString( | |
| 354 url, domain_string, &dummy); | |
| 355 // A cookie can be set if all of: | |
| 356 // a) The cookie line is well-formed | |
| 357 // b) The Domain attribute, if present, was not malformed | |
| 358 // c) At least one of: | |
| 359 // 1) The cookie had no explicit Domain, so the Domain was inferred | |
| 360 // from the URL, or | |
| 361 // 2) The cookie had an explicit Domain for which the URL is allowed | |
| 362 // to set cookies. | |
| 363 bool success = (cookie != nil) && !domain_string.empty() && | |
| 364 (!has_explicit_domain || has_valid_domain); | |
| 365 | 336 |
| 366 if (success) { | 337 // On iOS, [cookie domain] is not empty when the cookie domain is not |
| 367 [system_store_ setCookie:cookie]; | 338 // specified: it is inferred from the URL instead. The only case when it |
| 368 creation_time_manager_->SetCreationTime( | 339 // is empty is when the domain attribute is incorrectly formatted. |
| 369 cookie, | 340 std::string domain_string(base::SysNSStringToUTF8([cookie domain])); |
| 370 creation_time_manager_->MakeUniqueCreationTime(base::Time::Now())); | 341 std::string dummy; |
| 371 } | 342 bool has_explicit_domain = HasExplicitDomain(cookie_line); |
| 343 bool has_valid_domain = | |
| 344 net::cookie_util::GetCookieDomainWithString(url, domain_string, &dummy); | |
| 345 // A cookie can be set if all of: | |
| 346 // a) The cookie line is well-formed | |
| 347 // b) The Domain attribute, if present, was not malformed | |
| 348 // c) At least one of: | |
| 349 // 1) The cookie had no explicit Domain, so the Domain was inferred | |
| 350 // from the URL, or | |
| 351 // 2) The cookie had an explicit Domain for which the URL is allowed | |
| 352 // to set cookies. | |
| 353 bool success = (cookie != nil) && !domain_string.empty() && | |
| 354 (!has_explicit_domain || has_valid_domain); | |
| 372 | 355 |
| 373 if (!callback.is_null()) | 356 if (success) { |
| 374 callback.Run(success); | 357 [system_store_ setCookie:cookie]; |
| 375 break; | 358 creation_time_manager_->SetCreationTime( |
| 359 cookie, | |
| 360 creation_time_manager_->MakeUniqueCreationTime(base::Time::Now())); | |
| 376 } | 361 } |
| 362 | |
| 363 if (!callback.is_null()) | |
| 364 callback.Run(success); | |
| 377 } | 365 } |
| 378 | 366 |
| 379 void CookieStoreIOS::SetCookieWithDetailsAsync( | 367 void CookieStoreIOS::SetCookieWithDetailsAsync( |
| 380 const GURL& url, | 368 const GURL& url, |
| 381 const std::string& name, | 369 const std::string& name, |
| 382 const std::string& value, | 370 const std::string& value, |
| 383 const std::string& domain, | 371 const std::string& domain, |
| 384 const std::string& path, | 372 const std::string& path, |
| 385 base::Time creation_time, | 373 base::Time creation_time, |
| 386 base::Time expiration_time, | 374 base::Time expiration_time, |
| 387 base::Time last_access_time, | 375 base::Time last_access_time, |
| 388 bool secure, | 376 bool secure, |
| 389 bool http_only, | 377 bool http_only, |
| 390 CookieSameSite same_site, | 378 CookieSameSite same_site, |
| 391 bool enforce_strict_secure, | 379 bool enforce_strict_secure, |
| 392 CookiePriority priority, | 380 CookiePriority priority, |
| 393 const SetCookiesCallback& callback) { | 381 const SetCookiesCallback& callback) { |
| 394 DCHECK(thread_checker_.CalledOnValidThread()); | 382 DCHECK(thread_checker_.CalledOnValidThread()); |
| 383 // If cookies are not allowed, they are stashed in the CookieMonster, and | |
| 384 // should be written there instead. | |
| 385 DCHECK(SystemCookiesAllowed()); | |
| 395 | 386 |
| 396 switch (synchronization_state_) { | 387 bool success = false; |
| 397 case NOT_SYNCHRONIZED: | 388 |
| 398 cookie_monster_->SetCookieWithDetailsAsync( | 389 if (creation_time.is_null()) |
| 390 creation_time = base::Time::Now(); | |
| 391 | |
| 392 // First create a CanonicalCookie, to normalize the arguments, | |
| 393 // particularly domain and path, and perform validation. | |
| 394 std::unique_ptr<net::CanonicalCookie> canonical_cookie = | |
| 395 net::CanonicalCookie::Create( | |
| 399 url, name, value, domain, path, creation_time, expiration_time, | 396 url, name, value, domain, path, creation_time, expiration_time, |
| 400 last_access_time, secure, http_only, same_site, enforce_strict_secure, | 397 secure, http_only, same_site, enforce_strict_secure, priority); |
| 401 priority, WrapSetCallback(callback)); | |
| 402 break; | |
| 403 case SYNCHRONIZED: | |
| 404 // If cookies are not allowed, they are stashed in the CookieMonster, and | |
| 405 // should be written there instead. | |
| 406 DCHECK(SystemCookiesAllowed()); | |
| 407 | 398 |
| 408 bool success = false; | 399 if (canonical_cookie) { |
| 400 NSHTTPCookie* cookie = SystemCookieFromCanonicalCookie(*canonical_cookie); | |
| 409 | 401 |
| 410 if (creation_time.is_null()) | 402 if (cookie != nil) { |
| 411 creation_time = base::Time::Now(); | 403 [system_store_ setCookie:cookie]; |
| 404 creation_time_manager_->SetCreationTime( | |
| 405 cookie, creation_time_manager_->MakeUniqueCreationTime( | |
| 406 canonical_cookie->CreationDate())); | |
| 407 success = true; | |
| 408 } | |
| 409 } | |
| 412 | 410 |
| 413 // First create a CanonicalCookie, to normalize the arguments, | 411 if (!callback.is_null()) |
| 414 // particularly domain and path, and perform validation. | 412 callback.Run(success); |
| 415 std::unique_ptr<net::CanonicalCookie> canonical_cookie = | |
| 416 net::CanonicalCookie::Create( | |
| 417 url, name, value, domain, path, creation_time, expiration_time, | |
| 418 secure, http_only, same_site, enforce_strict_secure, priority); | |
| 419 | |
| 420 if (canonical_cookie) { | |
| 421 NSHTTPCookie* cookie = | |
| 422 SystemCookieFromCanonicalCookie(*canonical_cookie); | |
| 423 | |
| 424 if (cookie != nil) { | |
| 425 [system_store_ setCookie:cookie]; | |
| 426 creation_time_manager_->SetCreationTime( | |
| 427 cookie, creation_time_manager_->MakeUniqueCreationTime( | |
| 428 canonical_cookie->CreationDate())); | |
| 429 success = true; | |
| 430 } | |
| 431 } | |
| 432 | |
| 433 if (!callback.is_null()) | |
| 434 callback.Run(success); | |
| 435 break; | |
| 436 } | |
| 437 } | 413 } |
| 438 | 414 |
| 439 void CookieStoreIOS::GetCookiesWithOptionsAsync( | 415 void CookieStoreIOS::GetCookiesWithOptionsAsync( |
| 440 const GURL& url, | 416 const GURL& url, |
| 441 const net::CookieOptions& options, | 417 const net::CookieOptions& options, |
| 442 const GetCookiesCallback& callback) { | 418 const GetCookiesCallback& callback) { |
| 443 DCHECK(thread_checker_.CalledOnValidThread()); | 419 DCHECK(thread_checker_.CalledOnValidThread()); |
| 444 | 420 |
| 445 switch (synchronization_state_) { | 421 // If cookies are not allowed, they are stashed in the CookieMonster, and |
| 446 case NOT_SYNCHRONIZED: | 422 // should be read from there instead. |
| 447 cookie_monster_->GetCookiesWithOptionsAsync(url, options, callback); | 423 DCHECK(SystemCookiesAllowed()); |
| 448 break; | 424 // The exclude_httponly() option would only be used by a javascript |
| 449 case SYNCHRONIZED: | 425 // engine. |
| 450 // If cookies are not allowed, they are stashed in the CookieMonster, and | 426 DCHECK(!options.exclude_httponly()); |
| 451 // should be read from there instead. | |
| 452 DCHECK(SystemCookiesAllowed()); | |
| 453 // The exclude_httponly() option would only be used by a javascript | |
| 454 // engine. | |
| 455 DCHECK(!options.exclude_httponly()); | |
| 456 | 427 |
| 457 // TODO(mkwst): If/when iOS supports Same-Site cookies, we'll need to pass | 428 // TODO(mkwst): If/when iOS supports Same-Site cookies, we'll need to pass |
| 458 // options in here as well. https://crbug.com/459154 | 429 // options in here as well. https://crbug.com/459154 |
| 459 NSArray* cookies = GetCookiesForURL(system_store_, | 430 NSArray* cookies = |
| 460 url, creation_time_manager_.get()); | 431 GetCookiesForURL(system_store_, url, creation_time_manager_.get()); |
| 461 if (!callback.is_null()) | 432 if (!callback.is_null()) |
| 462 callback.Run(BuildCookieLineWithOptions(cookies, options)); | 433 callback.Run(BuildCookieLineWithOptions(cookies, options)); |
| 463 break; | |
| 464 } | |
| 465 } | 434 } |
| 466 | 435 |
| 467 void CookieStoreIOS::GetCookieListWithOptionsAsync( | 436 void CookieStoreIOS::GetCookieListWithOptionsAsync( |
| 468 const GURL& url, | 437 const GURL& url, |
| 469 const net::CookieOptions& options, | 438 const net::CookieOptions& options, |
| 470 const GetCookieListCallback& callback) { | 439 const GetCookieListCallback& callback) { |
| 471 DCHECK(thread_checker_.CalledOnValidThread()); | 440 DCHECK(thread_checker_.CalledOnValidThread()); |
| 441 if (!SystemCookiesAllowed()) { | |
| 442 // If cookies are not allowed, the cookies are stashed in the | |
| 443 // CookieMonster, so get them from there. | |
| 444 cookie_monster_->GetCookieListWithOptionsAsync(url, options, callback); | |
| 445 return; | |
| 446 } | |
| 472 | 447 |
| 473 switch (synchronization_state_) { | 448 // TODO(mkwst): If/when iOS supports Same-Site cookies, we'll need to pass |
| 474 case NOT_SYNCHRONIZED: | 449 // options in here as well. https://crbug.com/459154 |
| 475 cookie_monster_->GetCookieListWithOptionsAsync(url, options, callback); | 450 NSArray* cookies = |
| 476 break; | 451 GetCookiesForURL(system_store_, url, creation_time_manager_.get()); |
| 477 case SYNCHRONIZED: | 452 net::CookieList cookie_list = CanonicalCookieListFromSystemCookies(cookies); |
| 478 if (!SystemCookiesAllowed()) { | 453 if (!callback.is_null()) |
| 479 // If cookies are not allowed, the cookies are stashed in the | 454 callback.Run(cookie_list); |
| 480 // CookieMonster, so get them from there. | |
| 481 cookie_monster_->GetCookieListWithOptionsAsync(url, options, callback); | |
| 482 return; | |
| 483 } | |
| 484 | |
| 485 // TODO(mkwst): If/when iOS supports Same-Site cookies, we'll need to pass | |
| 486 // options in here as well. https://crbug.com/459154 | |
| 487 NSArray* cookies = GetCookiesForURL(system_store_, | |
| 488 url, creation_time_manager_.get()); | |
| 489 net::CookieList cookie_list = CanonicalCookieListFromSystemCookies( | |
| 490 cookies); | |
| 491 if (!callback.is_null()) | |
| 492 callback.Run(cookie_list); | |
| 493 break; | |
| 494 } | |
| 495 } | 455 } |
| 496 | 456 |
| 497 void CookieStoreIOS::GetAllCookiesAsync(const GetCookieListCallback& callback) { | 457 void CookieStoreIOS::GetAllCookiesAsync(const GetCookieListCallback& callback) { |
| 498 DCHECK(thread_checker_.CalledOnValidThread()); | 458 DCHECK(thread_checker_.CalledOnValidThread()); |
| 499 | 459 |
| 500 switch (synchronization_state_) { | 460 if (!SystemCookiesAllowed()) { |
| 501 case NOT_SYNCHRONIZED: | 461 // If cookies are not allowed, the cookies are stashed in the |
| 502 cookie_monster_->GetAllCookiesAsync(callback); | 462 // CookieMonster, so get them from there. |
| 503 break; | 463 cookie_monster_->GetAllCookiesAsync(callback); |
| 504 case SYNCHRONIZED: | 464 return; |
| 505 if (!SystemCookiesAllowed()) { | 465 } |
| 506 // If cookies are not allowed, the cookies are stashed in the | |
| 507 // CookieMonster, so get them from there. | |
| 508 cookie_monster_->GetAllCookiesAsync(callback); | |
| 509 return; | |
| 510 } | |
| 511 | 466 |
| 512 NSArray* cookies = GetAllCookies(system_store_, | 467 NSArray* cookies = GetAllCookies(system_store_, creation_time_manager_.get()); |
| 513 creation_time_manager_.get()); | 468 net::CookieList cookie_list = CanonicalCookieListFromSystemCookies(cookies); |
| 514 net::CookieList cookie_list = CanonicalCookieListFromSystemCookies( | 469 if (!callback.is_null()) { |
| 515 cookies); | 470 callback.Run(cookie_list); |
| 516 if (!callback.is_null()) { | |
| 517 callback.Run(cookie_list); | |
| 518 } | |
| 519 break; | |
| 520 } | 471 } |
| 521 } | 472 } |
| 522 | 473 |
| 523 void CookieStoreIOS::DeleteCookieAsync(const GURL& url, | 474 void CookieStoreIOS::DeleteCookieAsync(const GURL& url, |
| 524 const std::string& cookie_name, | 475 const std::string& cookie_name, |
| 525 const base::Closure& callback) { | 476 const base::Closure& callback) { |
| 526 DCHECK(thread_checker_.CalledOnValidThread()); | 477 DCHECK(thread_checker_.CalledOnValidThread()); |
| 527 | 478 |
| 528 switch (synchronization_state_) { | 479 NSArray* cookies = |
| 529 case NOT_SYNCHRONIZED: | 480 GetCookiesForURL(system_store_, url, creation_time_manager_.get()); |
| 530 cookie_monster_->DeleteCookieAsync(url, cookie_name, | 481 for (NSHTTPCookie* cookie in cookies) { |
| 531 WrapClosure(callback)); | 482 if ([[cookie name] isEqualToString:base::SysUTF8ToNSString(cookie_name)]) { |
| 532 break; | 483 [system_store_ deleteCookie:cookie]; |
| 533 case SYNCHRONIZED: | 484 creation_time_manager_->DeleteCreationTime(cookie); |
| 534 NSArray* cookies = GetCookiesForURL(system_store_, | 485 } |
| 535 url, creation_time_manager_.get()); | |
| 536 for (NSHTTPCookie* cookie in cookies) { | |
| 537 if ([[cookie name] | |
| 538 isEqualToString:base::SysUTF8ToNSString(cookie_name)]) { | |
| 539 [system_store_ deleteCookie:cookie]; | |
| 540 creation_time_manager_->DeleteCreationTime(cookie); | |
| 541 } | |
| 542 } | |
| 543 if (!callback.is_null()) | |
| 544 callback.Run(); | |
| 545 break; | |
| 546 } | 486 } |
| 487 | |
| 488 if (!callback.is_null()) | |
| 489 callback.Run(); | |
| 547 } | 490 } |
| 548 | 491 |
| 549 void CookieStoreIOS::DeleteCanonicalCookieAsync( | 492 void CookieStoreIOS::DeleteCanonicalCookieAsync( |
| 550 const CanonicalCookie& cookie, | 493 const CanonicalCookie& cookie, |
| 551 const DeleteCallback& callback) { | 494 const DeleteCallback& callback) { |
| 552 DCHECK(thread_checker_.CalledOnValidThread()); | 495 DCHECK(thread_checker_.CalledOnValidThread()); |
| 553 | 496 |
| 554 switch (synchronization_state_) { | 497 // This relies on the fact cookies are given unique creation dates. |
| 555 case NOT_SYNCHRONIZED: | 498 CookieFilterFunction filter = base::Bind( |
| 556 cookie_monster_->DeleteCanonicalCookieAsync(cookie, | 499 IsCookieCreatedBetween, cookie.CreationDate(), cookie.CreationDate()); |
| 557 WrapDeleteCallback(callback)); | 500 DeleteCookiesWithFilter(filter, callback); |
| 558 break; | |
| 559 case SYNCHRONIZED: | |
| 560 // This relies on the fact cookies are given unique creation dates. | |
| 561 CookieFilterFunction filter = base::Bind( | |
| 562 IsCookieCreatedBetween, cookie.CreationDate(), cookie.CreationDate()); | |
| 563 DeleteCookiesWithFilter(filter, callback); | |
| 564 } | |
| 565 } | 501 } |
| 566 | 502 |
| 567 void CookieStoreIOS::DeleteAllCreatedBetweenAsync( | 503 void CookieStoreIOS::DeleteAllCreatedBetweenAsync( |
| 568 const base::Time& delete_begin, | 504 const base::Time& delete_begin, |
| 569 const base::Time& delete_end, | 505 const base::Time& delete_end, |
| 570 const DeleteCallback& callback) { | 506 const DeleteCallback& callback) { |
| 571 DCHECK(thread_checker_.CalledOnValidThread()); | 507 DCHECK(thread_checker_.CalledOnValidThread()); |
| 572 | 508 |
| 573 if (metrics_enabled_) | 509 if (metrics_enabled_) |
| 574 ResetCookieCountMetrics(); | 510 ResetCookieCountMetrics(); |
| 575 | 511 |
| 576 switch (synchronization_state_) { | 512 CookieFilterFunction filter = |
| 577 case NOT_SYNCHRONIZED: | 513 base::Bind(&IsCookieCreatedBetween, delete_begin, delete_end); |
| 578 cookie_monster_->DeleteAllCreatedBetweenAsync( | 514 DeleteCookiesWithFilter(filter, callback); |
| 579 delete_begin, delete_end, WrapDeleteCallback(callback)); | |
| 580 break; | |
| 581 case SYNCHRONIZED: | |
| 582 CookieFilterFunction filter = | |
| 583 base::Bind(&IsCookieCreatedBetween, delete_begin, delete_end); | |
| 584 DeleteCookiesWithFilter(filter, callback); | |
| 585 break; | |
| 586 } | |
| 587 } | 515 } |
| 588 | 516 |
| 589 void CookieStoreIOS::DeleteAllCreatedBetweenWithPredicateAsync( | 517 void CookieStoreIOS::DeleteAllCreatedBetweenWithPredicateAsync( |
| 590 const base::Time& delete_begin, | 518 const base::Time& delete_begin, |
| 591 const base::Time& delete_end, | 519 const base::Time& delete_end, |
| 592 const CookiePredicate& predicate, | 520 const CookiePredicate& predicate, |
| 593 const DeleteCallback& callback) { | 521 const DeleteCallback& callback) { |
| 594 DCHECK(thread_checker_.CalledOnValidThread()); | 522 DCHECK(thread_checker_.CalledOnValidThread()); |
| 595 | 523 |
| 596 if (metrics_enabled_) | 524 if (metrics_enabled_) |
| 597 ResetCookieCountMetrics(); | 525 ResetCookieCountMetrics(); |
| 598 | 526 |
| 599 switch (synchronization_state_) { | 527 CookieFilterFunction filter = base::Bind(IsCookieCreatedBetweenWithPredicate, |
| 600 case NOT_SYNCHRONIZED: | 528 delete_begin, delete_end, predicate); |
| 601 cookie_monster_->DeleteAllCreatedBetweenWithPredicateAsync( | 529 DeleteCookiesWithFilter(filter, callback); |
| 602 delete_begin, delete_end, predicate, WrapDeleteCallback(callback)); | |
| 603 break; | |
| 604 case SYNCHRONIZED: | |
| 605 CookieFilterFunction filter = | |
| 606 base::Bind(IsCookieCreatedBetweenWithPredicate, delete_begin, | |
| 607 delete_end, predicate); | |
| 608 DeleteCookiesWithFilter(filter, callback); | |
| 609 break; | |
| 610 } | |
| 611 } | 530 } |
| 612 | 531 |
| 613 void CookieStoreIOS::DeleteSessionCookiesAsync(const DeleteCallback& callback) { | 532 void CookieStoreIOS::DeleteSessionCookiesAsync(const DeleteCallback& callback) { |
| 614 DCHECK(thread_checker_.CalledOnValidThread()); | 533 DCHECK(thread_checker_.CalledOnValidThread()); |
| 615 | 534 |
| 616 if (metrics_enabled_) | 535 if (metrics_enabled_) |
| 617 ResetCookieCountMetrics(); | 536 ResetCookieCountMetrics(); |
| 618 | 537 |
| 619 switch (synchronization_state_) { | 538 CookieFilterFunction filter = base::Bind(&IsCookieSessionCookie); |
| 620 case NOT_SYNCHRONIZED: | 539 DeleteCookiesWithFilter(filter, callback); |
| 621 cookie_monster_->DeleteSessionCookiesAsync(WrapDeleteCallback(callback)); | |
| 622 break; | |
| 623 case SYNCHRONIZED: | |
| 624 CookieFilterFunction filter = base::Bind(&IsCookieSessionCookie); | |
| 625 DeleteCookiesWithFilter(filter, callback); | |
| 626 break; | |
| 627 } | |
| 628 } | 540 } |
| 629 | 541 |
| 630 void CookieStoreIOS::FlushStore(const base::Closure& closure) { | 542 void CookieStoreIOS::FlushStore(const base::Closure& closure) { |
| 631 DCHECK(thread_checker_.CalledOnValidThread()); | 543 DCHECK(thread_checker_.CalledOnValidThread()); |
| 632 | 544 |
| 633 if (SystemCookiesAllowed()) { | 545 if (SystemCookiesAllowed()) { |
| 634 // If cookies are disabled, the system store is empty, and the cookies are | 546 // If cookies are disabled, the system store is empty, and the cookies are |
| 635 // stashed on disk. Do not delete the cookies on the disk in this case. | 547 // stashed on disk. Do not delete the cookies on the disk in this case. |
| 636 WriteToCookieMonster([system_store_ cookies]); | 548 WriteToCookieMonster([system_store_ cookies]); |
| 637 } | 549 } |
| 638 cookie_monster_->FlushStore(closure); | 550 cookie_monster_->FlushStore(closure); |
| 639 flush_closure_.Cancel(); | 551 flush_closure_.Cancel(); |
| 640 } | 552 } |
| 641 | 553 |
| 642 #pragma mark - | 554 #pragma mark - |
| 643 #pragma mark Private methods | 555 #pragma mark Protected methods |
| 644 | 556 |
| 645 CookieStoreIOS::CookieStoreIOS( | 557 CookieStoreIOS::CookieStoreIOS( |
| 646 net::CookieMonster::PersistentCookieStore* persistent_store, | 558 net::CookieMonster::PersistentCookieStore* persistent_store, |
| 647 NSHTTPCookieStorage* system_store) | 559 NSHTTPCookieStorage* system_store) |
| 648 : cookie_monster_(new net::CookieMonster(persistent_store, nullptr)), | 560 : cookie_monster_(new net::CookieMonster(persistent_store, nullptr)), |
| 649 system_store_(system_store), | 561 system_store_(system_store), |
| 650 creation_time_manager_(new CookieCreationTimeManager), | 562 creation_time_manager_(new CookieCreationTimeManager), |
| 651 metrics_enabled_(false), | 563 metrics_enabled_(false), |
| 652 synchronization_state_(NOT_SYNCHRONIZED), | |
| 653 cookie_cache_(new CookieCache()), | 564 cookie_cache_(new CookieCache()), |
| 654 weak_factory_(this) { | 565 weak_factory_(this) { |
| 655 DCHECK(system_store); | 566 DCHECK(system_store); |
| 656 | 567 |
| 657 NotificationTrampoline::GetInstance()->AddObserver(this); | 568 NotificationTrampoline::GetInstance()->AddObserver(this); |
| 658 | 569 |
| 659 cookie_monster_->SetPersistSessionCookies(true); | 570 cookie_monster_->SetPersistSessionCookies(true); |
| 660 cookie_monster_->SetForceKeepSessionState(); | 571 cookie_monster_->SetForceKeepSessionState(); |
| 661 } | 572 } |
| 662 | 573 |
| 574 CookieStoreIOS::SetCookiesCallback CookieStoreIOS::WrapSetCallback( | |
| 575 const SetCookiesCallback& callback) { | |
| 576 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 577 return base::Bind(&CookieStoreIOS::UpdateCachesAfterSet, | |
| 578 weak_factory_.GetWeakPtr(), callback); | |
| 579 } | |
| 580 | |
| 581 CookieStoreIOS::DeleteCallback CookieStoreIOS::WrapDeleteCallback( | |
| 582 const DeleteCallback& callback) { | |
| 583 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 584 return base::Bind(&CookieStoreIOS::UpdateCachesAfterDelete, | |
| 585 weak_factory_.GetWeakPtr(), callback); | |
| 586 } | |
| 587 | |
| 588 base::Closure CookieStoreIOS::WrapClosure(const base::Closure& callback) { | |
| 589 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 590 return base::Bind(&CookieStoreIOS::UpdateCachesAfterClosure, | |
| 591 weak_factory_.GetWeakPtr(), callback); | |
| 592 } | |
| 593 | |
| 594 #pragma mark - | |
| 595 #pragma mark Private methods | |
| 596 | |
| 663 void CookieStoreIOS::ClearSystemStore() { | 597 void CookieStoreIOS::ClearSystemStore() { |
| 664 DCHECK(thread_checker_.CalledOnValidThread()); | 598 DCHECK(thread_checker_.CalledOnValidThread()); |
| 665 base::scoped_nsobject<NSArray> copy( | 599 base::scoped_nsobject<NSArray> copy( |
| 666 [[NSArray alloc] initWithArray:[system_store_ cookies]]); | 600 [[NSArray alloc] initWithArray:[system_store_ cookies]]); |
| 667 for (NSHTTPCookie* cookie in copy.get()) | 601 for (NSHTTPCookie* cookie in copy.get()) |
| 668 [system_store_ deleteCookie:cookie]; | 602 [system_store_ deleteCookie:cookie]; |
| 669 DCHECK_EQ(0u, [[system_store_ cookies] count]); | 603 DCHECK_EQ(0u, [[system_store_ cookies] count]); |
| 670 creation_time_manager_->Clear(); | 604 creation_time_manager_->Clear(); |
| 671 } | 605 } |
| 672 | 606 |
| 673 bool CookieStoreIOS::SystemCookiesAllowed() { | 607 bool CookieStoreIOS::SystemCookiesAllowed() { |
| 674 DCHECK(thread_checker_.CalledOnValidThread()); | 608 DCHECK(thread_checker_.CalledOnValidThread()); |
| 675 return [system_store_ cookieAcceptPolicy] == | 609 return [system_store_ cookieAcceptPolicy] == |
| 676 NSHTTPCookieAcceptPolicyAlways; | 610 NSHTTPCookieAcceptPolicyAlways; |
| 677 } | 611 } |
| 678 | 612 |
| 679 void CookieStoreIOS::WriteToCookieMonster(NSArray* system_cookies) { | 613 void CookieStoreIOS::WriteToCookieMonster(NSArray* system_cookies) { |
| 680 DCHECK(thread_checker_.CalledOnValidThread()); | 614 DCHECK(thread_checker_.CalledOnValidThread()); |
| 681 if (synchronization_state_ != SYNCHRONIZED) | 615 if (!cookie_monster_->IsEphemeral()) |
|
Eugene But (OOO till 7-30)
2017/01/24 22:31:30
Conceptually it seems strange to have no-op behavi
maksims (do not use this acc)
2017/01/27 12:46:36
Done.
| |
| 682 return; | 616 return; |
| 683 | 617 |
| 684 // Copy the cookies from the global cookie store to |cookie_monster_|. | 618 // Copy the cookies from the global cookie store to |cookie_monster_|. |
| 685 // Unlike the system store, CookieMonster requires unique creation times. | 619 // Unlike the system store, CookieMonster requires unique creation times. |
| 686 net::CookieList cookie_list; | 620 net::CookieList cookie_list; |
| 687 NSUInteger cookie_count = [system_cookies count]; | 621 NSUInteger cookie_count = [system_cookies count]; |
| 688 cookie_list.reserve(cookie_count); | 622 cookie_list.reserve(cookie_count); |
| 689 for (NSHTTPCookie* cookie in system_cookies) { | 623 for (NSHTTPCookie* cookie in system_cookies) { |
| 690 cookie_list.push_back(CanonicalCookieFromSystemCookie( | 624 cookie_list.push_back(CanonicalCookieFromSystemCookie( |
| 691 cookie, creation_time_manager_->GetCreationTime(cookie))); | 625 cookie, creation_time_manager_->GetCreationTime(cookie))); |
| 692 } | 626 } |
| 693 cookie_monster_->SetAllCookiesAsync(cookie_list, SetCookiesCallback()); | 627 cookie_monster_->SetAllCookiesAsync(cookie_list, SetCookiesCallback()); |
| 694 | 628 |
| 695 // Update metrics. | 629 // Update metrics. |
| 696 if (metrics_enabled_) | 630 if (metrics_enabled_) |
| 697 UMA_HISTOGRAM_COUNTS_10000("CookieIOS.CookieWrittenCount", cookie_count); | 631 UMA_HISTOGRAM_COUNTS_10000("CookieIOS.CookieWrittenCount", cookie_count); |
| 698 } | 632 } |
| 699 | 633 |
| 700 void CookieStoreIOS::DeleteCookiesWithFilter(const CookieFilterFunction& filter, | 634 void CookieStoreIOS::DeleteCookiesWithFilter(const CookieFilterFunction& filter, |
| 701 const DeleteCallback& callback) { | 635 const DeleteCallback& callback) { |
| 702 DCHECK(thread_checker_.CalledOnValidThread()); | 636 DCHECK(thread_checker_.CalledOnValidThread()); |
| 703 DCHECK_EQ(SYNCHRONIZED, synchronization_state_); | |
| 704 NSArray* cookies = [system_store_ cookies]; | 637 NSArray* cookies = [system_store_ cookies]; |
| 705 | 638 |
| 706 // Collect the cookies to delete. | 639 // Collect the cookies to delete. |
| 707 base::scoped_nsobject<NSMutableArray> to_delete( | 640 base::scoped_nsobject<NSMutableArray> to_delete( |
| 708 [[NSMutableArray alloc] init]); | 641 [[NSMutableArray alloc] init]); |
| 709 for (NSHTTPCookie* cookie in cookies) { | 642 for (NSHTTPCookie* cookie in cookies) { |
| 710 base::Time creation_time = creation_time_manager_->GetCreationTime(cookie); | 643 base::Time creation_time = creation_time_manager_->GetCreationTime(cookie); |
| 711 if (filter.Run(cookie, creation_time)) | 644 if (filter.Run(cookie, creation_time)) |
| 712 [to_delete addObject:cookie]; | 645 [to_delete addObject:cookie]; |
| 713 } | 646 } |
| 714 | 647 |
| 715 // Delete them. | 648 // Delete them. |
| 716 for (NSHTTPCookie* cookie in to_delete.get()) { | 649 for (NSHTTPCookie* cookie in to_delete.get()) { |
| 717 [system_store_ deleteCookie:cookie]; | 650 [system_store_ deleteCookie:cookie]; |
| 718 creation_time_manager_->DeleteCreationTime(cookie); | 651 creation_time_manager_->DeleteCreationTime(cookie); |
| 719 } | 652 } |
| 720 | 653 |
| 721 if (!callback.is_null()) | 654 if (!callback.is_null()) |
| 722 callback.Run([to_delete count]); | 655 callback.Run([to_delete count]); |
| 723 } | 656 } |
| 724 | 657 |
| 725 void CookieStoreIOS::OnSystemCookiesChanged() { | 658 void CookieStoreIOS::OnSystemCookiesChanged() { |
| 726 DCHECK(thread_checker_.CalledOnValidThread()); | 659 DCHECK(thread_checker_.CalledOnValidThread()); |
| 727 | 660 |
| 728 // If the CookieStoreIOS is not synchronized or is not backed by | 661 // If the CookieStoreIOS is not synchronized or is not backed by |
| 729 // |NSHTTPCookieStorage sharedHTTPCookieStorage| this callback is irrelevant. | 662 // |NSHTTPCookieStorage sharedHTTPCookieStorage| this callback is irrelevant. |
| 730 if (synchronization_state_ != SYNCHRONIZED || | 663 if (!cookie_monster_->IsEphemeral() || |
|
Eugene But (OOO till 7-30)
2017/01/24 22:31:30
Same question here? Should we make CookieStoreIOSP
maksims (do not use this acc)
2017/01/27 12:46:36
Done.
| |
| 731 system_store_ != [NSHTTPCookieStorage sharedHTTPCookieStorage]) { | 664 system_store_ != [NSHTTPCookieStorage sharedHTTPCookieStorage]) { |
| 732 return; | 665 return; |
| 733 } | 666 } |
| 734 | 667 |
| 735 for (const auto& hook_map_entry : hook_map_) { | 668 for (const auto& hook_map_entry : hook_map_) { |
| 736 std::pair<GURL, std::string> key = hook_map_entry.first; | 669 std::pair<GURL, std::string> key = hook_map_entry.first; |
| 737 std::vector<net::CanonicalCookie> removed_cookies; | 670 std::vector<net::CanonicalCookie> removed_cookies; |
| 738 std::vector<net::CanonicalCookie> added_cookies; | 671 std::vector<net::CanonicalCookie> added_cookies; |
| 739 if (UpdateCacheForCookieFromSystem(key.first, key.second, &removed_cookies, | 672 if (UpdateCacheForCookieFromSystem(key.first, key.second, &removed_cookies, |
| 740 &added_cookies)) { | 673 &added_cookies)) { |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 908 CookieStoreIOS::CanonicalCookieListFromSystemCookies(NSArray* cookies) { | 841 CookieStoreIOS::CanonicalCookieListFromSystemCookies(NSArray* cookies) { |
| 909 net::CookieList cookie_list; | 842 net::CookieList cookie_list; |
| 910 cookie_list.reserve([cookies count]); | 843 cookie_list.reserve([cookies count]); |
| 911 for (NSHTTPCookie* cookie in cookies) { | 844 for (NSHTTPCookie* cookie in cookies) { |
| 912 base::Time created = creation_time_manager_->GetCreationTime(cookie); | 845 base::Time created = creation_time_manager_->GetCreationTime(cookie); |
| 913 cookie_list.push_back(CanonicalCookieFromSystemCookie(cookie, created)); | 846 cookie_list.push_back(CanonicalCookieFromSystemCookie(cookie, created)); |
| 914 } | 847 } |
| 915 return cookie_list; | 848 return cookie_list; |
| 916 } | 849 } |
| 917 | 850 |
| 918 CookieStoreIOS::SetCookiesCallback CookieStoreIOS::WrapSetCallback( | |
| 919 const SetCookiesCallback& callback) { | |
| 920 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 921 return base::Bind(&CookieStoreIOS::UpdateCachesAfterSet, | |
| 922 weak_factory_.GetWeakPtr(), callback); | |
| 923 } | |
| 924 | |
| 925 CookieStoreIOS::DeleteCallback CookieStoreIOS::WrapDeleteCallback( | |
| 926 const DeleteCallback& callback) { | |
| 927 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 928 return base::Bind(&CookieStoreIOS::UpdateCachesAfterDelete, | |
| 929 weak_factory_.GetWeakPtr(), callback); | |
| 930 } | |
| 931 | |
| 932 base::Closure CookieStoreIOS::WrapClosure(const base::Closure& callback) { | |
| 933 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 934 return base::Bind(&CookieStoreIOS::UpdateCachesAfterClosure, | |
| 935 weak_factory_.GetWeakPtr(), callback); | |
| 936 } | |
| 937 | |
| 938 } // namespace net | 851 } // namespace net |
| OLD | NEW |