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