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

Side by Side Diff: ios/net/cookies/cookie_store_ios.mm

Issue 1428673003: Injecting an NSHTTPCookieStorage dependency into CookieStoreIOS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: y Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ios/net/cookies/cookie_store_ios.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 // to cookies if one doesn't already exist. 193 // to cookies if one doesn't already exist.
194 DLOG_IF(ERROR, created_a.is_null() || created_b.is_null()) 194 DLOG_IF(ERROR, created_a.is_null() || created_b.is_null())
195 << "Cookie without creation date"; 195 << "Cookie without creation date";
196 #endif 196 #endif
197 if (created_a < created_b) 197 if (created_a < created_b)
198 return NSOrderedAscending; 198 return NSOrderedAscending;
199 return (created_a > created_b) ? NSOrderedDescending : NSOrderedSame; 199 return (created_a > created_b) ? NSOrderedDescending : NSOrderedSame;
200 } 200 }
201 201
202 // Gets the cookies for |url| from the system cookie store. 202 // Gets the cookies for |url| from the system cookie store.
203 NSArray* GetCookiesForURL(const GURL& url, CookieCreationTimeManager* manager) { 203 NSArray* GetCookiesForURL(NSHTTPCookieStorage* system_store,
204 NSArray* cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] 204 const GURL& url, CookieCreationTimeManager* manager) {
205 cookiesForURL:net::NSURLWithGURL(url)]; 205 NSArray* cookies = [system_store cookiesForURL:net::NSURLWithGURL(url)];
206 206
207 // Sort cookies by decreasing path length, then creation time, as per RFC6265. 207 // Sort cookies by decreasing path length, then creation time, as per RFC6265.
208 return [cookies sortedArrayUsingFunction:CompareCookies context:manager]; 208 return [cookies sortedArrayUsingFunction:CompareCookies context:manager];
209 } 209 }
210 210
211 // Builds a cookie line (such as "key1=value1; key2=value2") from an array of 211 // Builds a cookie line (such as "key1=value1; key2=value2") from an array of
212 // cookies. 212 // cookies.
213 std::string BuildCookieLine(NSArray* cookies, 213 std::string BuildCookieLine(NSArray* cookies,
214 const net::CookieOptions& options) { 214 const net::CookieOptions& options) {
215 // The exclude_httponly() option would only be used by a javascript engine. 215 // The exclude_httponly() option would only be used by a javascript engine.
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
267 return cookie.HasDomain(); 267 return cookie.HasDomain();
268 } 268 }
269 269
270 } // namespace 270 } // namespace
271 271
272 #pragma mark - 272 #pragma mark -
273 #pragma mark CookieStoreIOS 273 #pragma mark CookieStoreIOS
274 274
275 CookieStoreIOS::CookieStoreIOS( 275 CookieStoreIOS::CookieStoreIOS(
276 net::CookieMonster::PersistentCookieStore* persistent_store) 276 net::CookieMonster::PersistentCookieStore* persistent_store)
277 : creation_time_manager_(new CookieCreationTimeManager), 277 : CookieStoreIOS(persistent_store,
278 [NSHTTPCookieStorage sharedHTTPCookieStorage]) {
279 }
280
281 CookieStoreIOS::CookieStoreIOS(
282 net::CookieMonster::PersistentCookieStore* persistent_store,
283 NSHTTPCookieStorage* system_store)
284 : system_store_(system_store),
285 creation_time_manager_(new CookieCreationTimeManager),
278 metrics_enabled_(false), 286 metrics_enabled_(false),
279 flush_delay_(base::TimeDelta::FromSeconds(10)), 287 flush_delay_(base::TimeDelta::FromSeconds(10)),
280 synchronization_state_(NOT_SYNCHRONIZED), 288 synchronization_state_(NOT_SYNCHRONIZED),
281 cookie_cache_(new CookieCache()) { 289 cookie_cache_(new CookieCache()) {
290 DCHECK(system_store);
291
282 NotificationTrampoline::GetInstance()->AddObserver(this); 292 NotificationTrampoline::GetInstance()->AddObserver(this);
283 cookie_monster_ = new net::CookieMonster(persistent_store, nullptr); 293 cookie_monster_ = new net::CookieMonster(persistent_store, nullptr);
284 cookie_monster_->SetPersistSessionCookies(true); 294 cookie_monster_->SetPersistSessionCookies(true);
285 cookie_monster_->SetForceKeepSessionState(); 295 cookie_monster_->SetForceKeepSessionState();
286 } 296 }
287 297
288 // static 298 // static
289 void CookieStoreIOS::SetCookiePolicy(CookiePolicy setting) { 299 void CookieStoreIOS::SetCookiePolicy(CookiePolicy setting) {
290 NSHTTPCookieAcceptPolicy policy = (setting == ALLOW) 300 NSHTTPCookieAcceptPolicy policy = (setting == ALLOW)
291 ? NSHTTPCookieAcceptPolicyAlways 301 ? NSHTTPCookieAcceptPolicyAlways
292 : NSHTTPCookieAcceptPolicyNever; 302 : NSHTTPCookieAcceptPolicyNever;
293 NSHTTPCookieStorage* store = [NSHTTPCookieStorage sharedHTTPCookieStorage]; 303 NSHTTPCookieStorage* store = [NSHTTPCookieStorage sharedHTTPCookieStorage];
294 NSHTTPCookieAcceptPolicy current_policy = [store cookieAcceptPolicy]; 304 NSHTTPCookieAcceptPolicy current_policy = [store cookieAcceptPolicy];
295 if (current_policy == policy) 305 if (current_policy == policy)
296 return; 306 return;
297 [store setCookieAcceptPolicy:policy]; 307 [store setCookieAcceptPolicy:policy];
298 NotificationTrampoline::GetInstance()->NotifyCookiePolicyChanged(); 308 NotificationTrampoline::GetInstance()->NotifyCookiePolicyChanged();
299 } 309 }
300 310
301 CookieStoreIOS* CookieStoreIOS::CreateCookieStoreFromNSHTTPCookieStorage() { 311 // static
312 CookieStoreIOS* CookieStoreIOS::CreateCookieStore(
313 NSHTTPCookieStorage* cookie_storage) {
314 DCHECK(cookie_storage);
302 // TODO(huey): Update this when CrNet supports multiple cookie jars. 315 // TODO(huey): Update this when CrNet supports multiple cookie jars.
303 [[NSHTTPCookieStorage sharedHTTPCookieStorage] 316 [cookie_storage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
304 setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
305 317
306 // Create a cookie store with no persistent store backing. Then, populate 318 // Create a cookie store with no persistent store backing. Then, populate
307 // it from the system's cookie jar. 319 // it from the system's cookie jar.
308 CookieStoreIOS* cookie_store = new CookieStoreIOS(nullptr); 320 CookieStoreIOS* cookie_store = new CookieStoreIOS(nullptr, cookie_storage);
309 cookie_store->synchronization_state_ = SYNCHRONIZED; 321 cookie_store->synchronization_state_ = SYNCHRONIZED;
310 cookie_store->Flush(base::Closure()); 322 cookie_store->Flush(base::Closure());
311 return cookie_store; 323 return cookie_store;
312 } 324 }
313 325
314 // static 326 // static
315 void CookieStoreIOS::SwitchSynchronizedStore(CookieStoreIOS* old_store, 327 void CookieStoreIOS::SwitchSynchronizedStore(CookieStoreIOS* old_store,
316 CookieStoreIOS* new_store) { 328 CookieStoreIOS* new_store) {
317 DCHECK(new_store); 329 DCHECK(new_store);
318 DCHECK_NE(new_store, old_store); 330 DCHECK_NE(new_store, old_store);
319 if (old_store) 331 if (old_store)
320 old_store->SetSynchronizedWithSystemStore(false); 332 old_store->SetSynchronizedWithSystemStore(false);
321 new_store->SetSynchronizedWithSystemStore(true); 333 new_store->SetSynchronizedWithSystemStore(true);
322 } 334 }
323 335
324 // static 336 // static
325 void CookieStoreIOS::NotifySystemCookiesChanged() { 337 void CookieStoreIOS::NotifySystemCookiesChanged() {
326 NotificationTrampoline::GetInstance()->NotifyCookiesChanged(); 338 NotificationTrampoline::GetInstance()->NotifyCookiesChanged();
327 } 339 }
328 340
329 void CookieStoreIOS::Flush(const base::Closure& closure) { 341 void CookieStoreIOS::Flush(const base::Closure& closure) {
330 DCHECK(thread_checker_.CalledOnValidThread()); 342 DCHECK(thread_checker_.CalledOnValidThread());
331 343
332 if (SystemCookiesAllowed()) { 344 if (SystemCookiesAllowed()) {
333 // If cookies are disabled, the system store is empty, and the cookies are 345 // If cookies are disabled, the system store is empty, and the cookies are
334 // stashed on disk. Do not delete the cookies on the disk in this case. 346 // stashed on disk. Do not delete the cookies on the disk in this case.
335 WriteToCookieMonster( 347 WriteToCookieMonster([system_store_ cookies]);
336 [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]);
337 } 348 }
338 cookie_monster_->FlushStore(closure); 349 cookie_monster_->FlushStore(closure);
339 flush_closure_.Cancel(); 350 flush_closure_.Cancel();
340 } 351 }
341 352
342 void CookieStoreIOS::UnSynchronize() { 353 void CookieStoreIOS::UnSynchronize() {
343 SetSynchronizedWithSystemStore(false); 354 SetSynchronizedWithSystemStore(false);
344 } 355 }
345 356
346 void CookieStoreIOS::SetMetricsEnabled() { 357 void CookieStoreIOS::SetMetricsEnabled() {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 // b) The Domain attribute, if present, was not malformed 412 // b) The Domain attribute, if present, was not malformed
402 // c) At least one of: 413 // c) At least one of:
403 // 1) The cookie had no explicit Domain, so the Domain was inferred 414 // 1) The cookie had no explicit Domain, so the Domain was inferred
404 // from the URL, or 415 // from the URL, or
405 // 2) The cookie had an explicit Domain for which the URL is allowed 416 // 2) The cookie had an explicit Domain for which the URL is allowed
406 // to set cookies. 417 // to set cookies.
407 bool success = (cookie != nil) && !domain_string.empty() && 418 bool success = (cookie != nil) && !domain_string.empty() &&
408 (!has_explicit_domain || has_valid_domain); 419 (!has_explicit_domain || has_valid_domain);
409 420
410 if (success) { 421 if (success) {
411 [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie]; 422 [system_store_ setCookie:cookie];
412 creation_time_manager_->SetCreationTime( 423 creation_time_manager_->SetCreationTime(
413 cookie, 424 cookie,
414 creation_time_manager_->MakeUniqueCreationTime(base::Time::Now())); 425 creation_time_manager_->MakeUniqueCreationTime(base::Time::Now()));
415 } 426 }
416 427
417 if (!callback.is_null()) 428 if (!callback.is_null())
418 callback.Run(success); 429 callback.Run(success);
419 break; 430 break;
420 } 431 }
421 } 432 }
(...skipping 14 matching lines...) Expand all
436 options, callback)); 447 options, callback));
437 break; 448 break;
438 case SYNCHRONIZED: 449 case SYNCHRONIZED:
439 // If cookies are not allowed, they are stashed in the CookieMonster, and 450 // If cookies are not allowed, they are stashed in the CookieMonster, and
440 // should be read from there instead. 451 // should be read from there instead.
441 DCHECK(SystemCookiesAllowed()); 452 DCHECK(SystemCookiesAllowed());
442 // The exclude_httponly() option would only be used by a javascript 453 // The exclude_httponly() option would only be used by a javascript
443 // engine. 454 // engine.
444 DCHECK(!options.exclude_httponly()); 455 DCHECK(!options.exclude_httponly());
445 456
446 NSArray* cookies = GetCookiesForURL(url, creation_time_manager_.get()); 457 NSArray* cookies = GetCookiesForURL(system_store_,
458 url, creation_time_manager_.get());
447 if (!callback.is_null()) 459 if (!callback.is_null())
448 callback.Run(BuildCookieLine(cookies, options)); 460 callback.Run(BuildCookieLine(cookies, options));
449 break; 461 break;
450 } 462 }
451 } 463 }
452 464
453 void CookieStoreIOS::GetAllCookiesForURLAsync( 465 void CookieStoreIOS::GetAllCookiesForURLAsync(
454 const GURL& url, 466 const GURL& url,
455 const GetCookieListCallback& callback) { 467 const GetCookieListCallback& callback) {
456 DCHECK(thread_checker_.CalledOnValidThread()); 468 DCHECK(thread_checker_.CalledOnValidThread());
457 469
458 switch (synchronization_state_) { 470 switch (synchronization_state_) {
459 case NOT_SYNCHRONIZED: 471 case NOT_SYNCHRONIZED:
460 cookie_monster_->GetAllCookiesForURLAsync(url, callback); 472 cookie_monster_->GetAllCookiesForURLAsync(url, callback);
461 break; 473 break;
462 case SYNCHRONIZING: 474 case SYNCHRONIZING:
463 tasks_pending_synchronization_.push_back(base::Bind( 475 tasks_pending_synchronization_.push_back(base::Bind(
464 &CookieStoreIOS::GetAllCookiesForURLAsync, this, url, callback)); 476 &CookieStoreIOS::GetAllCookiesForURLAsync, this, url, callback));
465 break; 477 break;
466 case SYNCHRONIZED: 478 case SYNCHRONIZED:
467 if (!SystemCookiesAllowed()) { 479 if (!SystemCookiesAllowed()) {
468 // If cookies are not allowed, the cookies are stashed in the 480 // If cookies are not allowed, the cookies are stashed in the
469 // CookieMonster, so get them from there. 481 // CookieMonster, so get them from there.
470 cookie_monster_->GetAllCookiesForURLAsync(url, callback); 482 cookie_monster_->GetAllCookiesForURLAsync(url, callback);
471 return; 483 return;
472 } 484 }
473 485
474 NSArray* cookies = GetCookiesForURL(url, creation_time_manager_.get()); 486 NSArray* cookies = GetCookiesForURL(system_store_,
487 url, creation_time_manager_.get());
475 net::CookieList cookie_list; 488 net::CookieList cookie_list;
476 cookie_list.reserve([cookies count]); 489 cookie_list.reserve([cookies count]);
477 for (NSHTTPCookie* cookie in cookies) { 490 for (NSHTTPCookie* cookie in cookies) {
478 base::Time created = creation_time_manager_->GetCreationTime(cookie); 491 base::Time created = creation_time_manager_->GetCreationTime(cookie);
479 cookie_list.push_back(CanonicalCookieFromSystemCookie(cookie, created)); 492 cookie_list.push_back(CanonicalCookieFromSystemCookie(cookie, created));
480 } 493 }
481 if (!callback.is_null()) 494 if (!callback.is_null())
482 callback.Run(cookie_list); 495 callback.Run(cookie_list);
483 break; 496 break;
484 } 497 }
485 } 498 }
486 499
487 void CookieStoreIOS::DeleteCookieAsync(const GURL& url, 500 void CookieStoreIOS::DeleteCookieAsync(const GURL& url,
488 const std::string& cookie_name, 501 const std::string& cookie_name,
489 const base::Closure& callback) { 502 const base::Closure& callback) {
490 DCHECK(thread_checker_.CalledOnValidThread()); 503 DCHECK(thread_checker_.CalledOnValidThread());
491 504
492 switch (synchronization_state_) { 505 switch (synchronization_state_) {
493 case NOT_SYNCHRONIZED: 506 case NOT_SYNCHRONIZED:
494 cookie_monster_->DeleteCookieAsync(url, cookie_name, 507 cookie_monster_->DeleteCookieAsync(url, cookie_name,
495 WrapClosure(callback)); 508 WrapClosure(callback));
496 break; 509 break;
497 case SYNCHRONIZING: 510 case SYNCHRONIZING:
498 tasks_pending_synchronization_.push_back( 511 tasks_pending_synchronization_.push_back(
499 base::Bind(&CookieStoreIOS::DeleteCookieAsync, this, url, cookie_name, 512 base::Bind(&CookieStoreIOS::DeleteCookieAsync, this, url, cookie_name,
500 WrapClosure(callback))); 513 WrapClosure(callback)));
501 break; 514 break;
502 case SYNCHRONIZED: 515 case SYNCHRONIZED:
503 NSArray* cookies = GetCookiesForURL(url, creation_time_manager_.get()); 516 NSArray* cookies = GetCookiesForURL(system_store_,
517 url, creation_time_manager_.get());
504 for (NSHTTPCookie* cookie in cookies) { 518 for (NSHTTPCookie* cookie in cookies) {
505 if ([[cookie name] 519 if ([[cookie name]
506 isEqualToString:base::SysUTF8ToNSString(cookie_name)]) { 520 isEqualToString:base::SysUTF8ToNSString(cookie_name)]) {
507 [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie]; 521 [system_store_ deleteCookie:cookie];
508 creation_time_manager_->DeleteCreationTime(cookie); 522 creation_time_manager_->DeleteCreationTime(cookie);
509 } 523 }
510 } 524 }
511 if (!callback.is_null()) 525 if (!callback.is_null())
512 callback.Run(); 526 callback.Run();
513 break; 527 break;
514 } 528 }
515 } 529 }
516 530
517 // CookieStoreIOS is an implementation of CookieStore which is not a 531 // CookieStoreIOS is an implementation of CookieStore which is not a
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
606 CookieStoreIOS::~CookieStoreIOS() { 620 CookieStoreIOS::~CookieStoreIOS() {
607 NotificationTrampoline::GetInstance()->RemoveObserver(this); 621 NotificationTrampoline::GetInstance()->RemoveObserver(this);
608 STLDeleteContainerPairSecondPointers(hook_map_.begin(), hook_map_.end()); 622 STLDeleteContainerPairSecondPointers(hook_map_.begin(), hook_map_.end());
609 } 623 }
610 624
611 #pragma mark - 625 #pragma mark -
612 #pragma mark Private methods 626 #pragma mark Private methods
613 627
614 void CookieStoreIOS::ClearSystemStore() { 628 void CookieStoreIOS::ClearSystemStore() {
615 DCHECK(thread_checker_.CalledOnValidThread()); 629 DCHECK(thread_checker_.CalledOnValidThread());
616 NSHTTPCookieStorage* cookie_storage =
617 [NSHTTPCookieStorage sharedHTTPCookieStorage];
618 base::scoped_nsobject<NSArray> copy( 630 base::scoped_nsobject<NSArray> copy(
619 [[NSArray alloc] initWithArray:[cookie_storage cookies]]); 631 [[NSArray alloc] initWithArray:[system_store_ cookies]]);
620 for (NSHTTPCookie* cookie in copy.get()) 632 for (NSHTTPCookie* cookie in copy.get())
621 [cookie_storage deleteCookie:cookie]; 633 [system_store_ deleteCookie:cookie];
622 DCHECK_EQ(0u, [[cookie_storage cookies] count]); 634 DCHECK_EQ(0u, [[system_store_ cookies] count]);
623 creation_time_manager_->Clear(); 635 creation_time_manager_->Clear();
624 } 636 }
625 637
626 void CookieStoreIOS::OnSystemCookiePolicyChanged() { 638 void CookieStoreIOS::OnSystemCookiePolicyChanged() {
627 DCHECK(thread_checker_.CalledOnValidThread()); 639 DCHECK(thread_checker_.CalledOnValidThread());
628 640
629 if (synchronization_state_ == NOT_SYNCHRONIZED) 641 // If the CookieStoreIOS is not synchronized or is not backed by
642 // |NSHTTPCookieStorage sharedHTTPCookieStorage| this callback is irrelevant.
643 if (synchronization_state_ == NOT_SYNCHRONIZED ||
644 system_store_ != [NSHTTPCookieStorage sharedHTTPCookieStorage]) {
630 return; 645 return;
646 }
631 647
632 NSHTTPCookieAcceptPolicy policy = 648 NSHTTPCookieAcceptPolicy policy =
633 [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookieAcceptPolicy]; 649 [system_store_ cookieAcceptPolicy];
634 if (policy == NSHTTPCookieAcceptPolicyAlways) { 650 if (policy == NSHTTPCookieAcceptPolicyAlways) {
635 // If cookies are disabled, the system cookie store should be empty. 651 // If cookies are disabled, the system cookie store should be empty.
636 DCHECK(![[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] count]); 652 DCHECK(![[system_store_ cookies] count]);
637 DCHECK(synchronization_state_ != SYNCHRONIZING); 653 DCHECK(synchronization_state_ != SYNCHRONIZING);
638 synchronization_state_ = SYNCHRONIZING; 654 synchronization_state_ = SYNCHRONIZING;
639 cookie_monster_->GetAllCookiesAsync( 655 cookie_monster_->GetAllCookiesAsync(
640 base::Bind(&CookieStoreIOS::AddCookiesToSystemStore, this)); 656 base::Bind(&CookieStoreIOS::AddCookiesToSystemStore, this));
641 } else { 657 } else {
642 DCHECK_EQ(NSHTTPCookieAcceptPolicyNever, policy); 658 DCHECK_EQ(NSHTTPCookieAcceptPolicyNever, policy);
643 // Flush() does not write the cookies to disk when they are disabled. 659 // Flush() does not write the cookies to disk when they are disabled.
644 // Explicitly copy them. 660 // Explicitly copy them.
645 WriteToCookieMonster( 661 WriteToCookieMonster([system_store_ cookies]);
646 [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]);
647 Flush(base::Closure()); 662 Flush(base::Closure());
648 ClearSystemStore(); 663 ClearSystemStore();
649 if (synchronization_state_ == SYNCHRONIZING) { 664 if (synchronization_state_ == SYNCHRONIZING) {
650 // If synchronization was in progress, abort it and leave the cookie store 665 // If synchronization was in progress, abort it and leave the cookie store
651 // empty. 666 // empty.
652 // Temporarily toggle the synchronization state so that pending tasks are 667 // Temporarily toggle the synchronization state so that pending tasks are
653 // redirected to cookie_monster_ and can complete normally. 668 // redirected to cookie_monster_ and can complete normally.
654 synchronization_state_ = NOT_SYNCHRONIZED; 669 synchronization_state_ = NOT_SYNCHRONIZED;
655 RunAllPendingTasks(); 670 RunAllPendingTasks();
656 synchronization_state_ = SYNCHRONIZED; 671 synchronization_state_ = SYNCHRONIZED;
(...skipping 13 matching lines...) Expand all
670 << "This cookie store was not synchronized"; 685 << "This cookie store was not synchronized";
671 g_current_synchronized_store = nullptr; 686 g_current_synchronized_store = nullptr;
672 } else { 687 } else {
673 DCHECK_EQ((CookieStoreIOS*)nullptr, g_current_synchronized_store) 688 DCHECK_EQ((CookieStoreIOS*)nullptr, g_current_synchronized_store)
674 << "Un-synchronize the current cookie store first."; 689 << "Un-synchronize the current cookie store first.";
675 g_current_synchronized_store = this; 690 g_current_synchronized_store = this;
676 } 691 }
677 #endif 692 #endif
678 693
679 NSHTTPCookieAcceptPolicy policy = 694 NSHTTPCookieAcceptPolicy policy =
680 [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookieAcceptPolicy]; 695 [system_store_ cookieAcceptPolicy];
681 DCHECK(policy == NSHTTPCookieAcceptPolicyAlways || 696 DCHECK(policy == NSHTTPCookieAcceptPolicyAlways ||
682 policy == NSHTTPCookieAcceptPolicyNever); 697 policy == NSHTTPCookieAcceptPolicyNever);
683 698
684 // If cookies are disabled, the system cookie store should be empty. 699 // If cookies are disabled, the system cookie store should be empty.
685 DCHECK(policy == NSHTTPCookieAcceptPolicyAlways || 700 DCHECK(policy == NSHTTPCookieAcceptPolicyAlways ||
686 ![[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies] count]); 701 ![[system_store_ cookies] count]);
687 702
688 // If cookies are disabled, nothing is done now, the work will be done when 703 // If cookies are disabled, nothing is done now, the work will be done when
689 // cookies are re-enabled. 704 // cookies are re-enabled.
690 if (policy == NSHTTPCookieAcceptPolicyAlways) { 705 if (policy == NSHTTPCookieAcceptPolicyAlways) {
691 if (synchronized) { 706 if (synchronized) {
692 synchronization_state_ = SYNCHRONIZING; 707 synchronization_state_ = SYNCHRONIZING;
693 ClearSystemStore(); 708 ClearSystemStore();
694 cookie_monster_->GetAllCookiesAsync( 709 cookie_monster_->GetAllCookiesAsync(
695 base::Bind(&CookieStoreIOS::AddCookiesToSystemStore, this)); 710 base::Bind(&CookieStoreIOS::AddCookiesToSystemStore, this));
696 return; 711 return;
697 } else { 712 } else {
698 // Copy the cookies from the global store to |cookie_monster_|. 713 // Copy the cookies from the global store to |cookie_monster_|.
699 Flush(base::Closure()); 714 Flush(base::Closure());
700 } 715 }
701 } 716 }
702 synchronization_state_ = synchronized ? SYNCHRONIZED : NOT_SYNCHRONIZED; 717 synchronization_state_ = synchronized ? SYNCHRONIZED : NOT_SYNCHRONIZED;
703 718
704 if (synchronization_state_ == NOT_SYNCHRONIZED) { 719 if (synchronization_state_ == NOT_SYNCHRONIZED) {
705 // If there are pending tasks, then it means that the synchronization is 720 // If there are pending tasks, then it means that the synchronization is
706 // being canceled. All pending tasks can be sent to cookie_monster_. 721 // being canceled. All pending tasks can be sent to cookie_monster_.
707 RunAllPendingTasks(); 722 RunAllPendingTasks();
708 } 723 }
709 } 724 }
710 725
711 bool CookieStoreIOS::SystemCookiesAllowed() { 726 bool CookieStoreIOS::SystemCookiesAllowed() {
712 DCHECK(thread_checker_.CalledOnValidThread()); 727 DCHECK(thread_checker_.CalledOnValidThread());
713 return [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookieAcceptPolicy] == 728 return [system_store_ cookieAcceptPolicy] ==
714 NSHTTPCookieAcceptPolicyAlways; 729 NSHTTPCookieAcceptPolicyAlways;
715 } 730 }
716 731
717 void CookieStoreIOS::AddCookiesToSystemStore(const net::CookieList& cookies) { 732 void CookieStoreIOS::AddCookiesToSystemStore(const net::CookieList& cookies) {
718 DCHECK(thread_checker_.CalledOnValidThread()); 733 DCHECK(thread_checker_.CalledOnValidThread());
719 if (!SystemCookiesAllowed() || synchronization_state_ != SYNCHRONIZING) { 734 if (!SystemCookiesAllowed() || synchronization_state_ != SYNCHRONIZING) {
720 // If synchronization was aborted, the pending tasks have been processed at 735 // If synchronization was aborted, the pending tasks have been processed at
721 // that time. Now is too late. 736 // that time. Now is too late.
722 DCHECK(tasks_pending_synchronization_.empty()); 737 DCHECK(tasks_pending_synchronization_.empty());
723 return; 738 return;
724 } 739 }
725 740
726 // Report metrics. 741 // Report metrics.
727 if (metrics_enabled_) { 742 if (metrics_enabled_) {
728 size_t cookie_count = cookies.size(); 743 size_t cookie_count = cookies.size();
729 UMA_HISTOGRAM_COUNTS_10000("CookieIOS.CookieReadCount", cookie_count); 744 UMA_HISTOGRAM_COUNTS_10000("CookieIOS.CookieReadCount", cookie_count);
730 CheckForCookieLoss(cookie_count, COOKIES_READ); 745 CheckForCookieLoss(cookie_count, COOKIES_READ);
731 } 746 }
732 747
733 net::CookieList::const_iterator it; 748 net::CookieList::const_iterator it;
734 for (it = cookies.begin(); it != cookies.end(); ++it) { 749 for (it = cookies.begin(); it != cookies.end(); ++it) {
735 const net::CanonicalCookie& net_cookie = *it; 750 const net::CanonicalCookie& net_cookie = *it;
736 NSHTTPCookie* system_cookie = SystemCookieFromCanonicalCookie(net_cookie); 751 NSHTTPCookie* system_cookie = SystemCookieFromCanonicalCookie(net_cookie);
737 // Canonical cookie may not be convertable into system cookie if it contains 752 // Canonical cookie may not be convertable into system cookie if it contains
738 // invalid characters. 753 // invalid characters.
739 if (!system_cookie) 754 if (!system_cookie)
740 continue; 755 continue;
741 [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:system_cookie]; 756 [system_store_ setCookie:system_cookie];
742 creation_time_manager_->SetCreationTime(system_cookie, 757 creation_time_manager_->SetCreationTime(system_cookie,
743 net_cookie.CreationDate()); 758 net_cookie.CreationDate());
744 } 759 }
745 760
746 synchronization_state_ = SYNCHRONIZED; 761 synchronization_state_ = SYNCHRONIZED;
747 RunAllPendingTasks(); 762 RunAllPendingTasks();
748 } 763 }
749 764
750 void CookieStoreIOS::WriteToCookieMonster(NSArray* system_cookies) { 765 void CookieStoreIOS::WriteToCookieMonster(NSArray* system_cookies) {
751 DCHECK(thread_checker_.CalledOnValidThread()); 766 DCHECK(thread_checker_.CalledOnValidThread());
(...skipping 24 matching lines...) Expand all
776 for (const auto& task : tasks_pending_synchronization_) { 791 for (const auto& task : tasks_pending_synchronization_) {
777 task.Run(); 792 task.Run();
778 } 793 }
779 tasks_pending_synchronization_.clear(); 794 tasks_pending_synchronization_.clear();
780 } 795 }
781 796
782 void CookieStoreIOS::DeleteCookiesWithFilter(const CookieFilterFunction& filter, 797 void CookieStoreIOS::DeleteCookiesWithFilter(const CookieFilterFunction& filter,
783 const DeleteCallback& callback) { 798 const DeleteCallback& callback) {
784 DCHECK(thread_checker_.CalledOnValidThread()); 799 DCHECK(thread_checker_.CalledOnValidThread());
785 DCHECK_EQ(SYNCHRONIZED, synchronization_state_); 800 DCHECK_EQ(SYNCHRONIZED, synchronization_state_);
786 NSArray* cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]; 801 NSArray* cookies = [system_store_ cookies];
787 802
788 // Collect the cookies to delete. 803 // Collect the cookies to delete.
789 base::scoped_nsobject<NSMutableArray> to_delete( 804 base::scoped_nsobject<NSMutableArray> to_delete(
790 [[NSMutableArray alloc] init]); 805 [[NSMutableArray alloc] init]);
791 for (NSHTTPCookie* cookie in cookies) { 806 for (NSHTTPCookie* cookie in cookies) {
792 base::Time creation_time = creation_time_manager_->GetCreationTime(cookie); 807 base::Time creation_time = creation_time_manager_->GetCreationTime(cookie);
793 if (filter.Run(cookie, creation_time)) 808 if (filter.Run(cookie, creation_time))
794 [to_delete addObject:cookie]; 809 [to_delete addObject:cookie];
795 } 810 }
796 811
797 // Delete them. 812 // Delete them.
798 for (NSHTTPCookie* cookie in to_delete.get()) { 813 for (NSHTTPCookie* cookie in to_delete.get()) {
799 [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie]; 814 [system_store_ deleteCookie:cookie];
800 creation_time_manager_->DeleteCreationTime(cookie); 815 creation_time_manager_->DeleteCreationTime(cookie);
801 } 816 }
802 817
803 if (!callback.is_null()) 818 if (!callback.is_null())
804 callback.Run([to_delete count]); 819 callback.Run([to_delete count]);
805 } 820 }
806 821
807 void CookieStoreIOS::OnSystemCookiesChanged() { 822 void CookieStoreIOS::OnSystemCookiesChanged() {
808 DCHECK(thread_checker_.CalledOnValidThread()); 823 DCHECK(thread_checker_.CalledOnValidThread());
809 824
810 // If the CookieStoreIOS is not synchronized, system cookies are irrelevant. 825 // If the CookieStoreIOS is not synchronized or is not backed by
811 if (synchronization_state_ != SYNCHRONIZED) 826 // |NSHTTPCookieStorage sharedHTTPCookieStorage| this callback is irrelevant.
827 if (synchronization_state_ != SYNCHRONIZED ||
828 system_store_ != [NSHTTPCookieStorage sharedHTTPCookieStorage]) {
812 return; 829 return;
830 }
813 831
814 for (const auto& hook_map_entry : hook_map_) { 832 for (const auto& hook_map_entry : hook_map_) {
815 std::pair<GURL, std::string> key = hook_map_entry.first; 833 std::pair<GURL, std::string> key = hook_map_entry.first;
816 std::vector<net::CanonicalCookie> removed_cookies; 834 std::vector<net::CanonicalCookie> removed_cookies;
817 std::vector<net::CanonicalCookie> added_cookies; 835 std::vector<net::CanonicalCookie> added_cookies;
818 if (UpdateCacheForCookieFromSystem(key.first, key.second, &removed_cookies, 836 if (UpdateCacheForCookieFromSystem(key.first, key.second, &removed_cookies,
819 &added_cookies)) { 837 &added_cookies)) {
820 RunCallbacksForCookies(key.first, key.second, removed_cookies, true); 838 RunCallbacksForCookies(key.first, key.second, removed_cookies, true);
821 RunCallbacksForCookies(key.first, key.second, added_cookies, false); 839 RunCallbacksForCookies(key.first, key.second, added_cookies, false);
822 } 840 }
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
878 callbacks->Notify(cookie, removed); 896 callbacks->Notify(cookie, removed);
879 } 897 }
880 } 898 }
881 899
882 bool CookieStoreIOS::GetSystemCookies( 900 bool CookieStoreIOS::GetSystemCookies(
883 const GURL& gurl, 901 const GURL& gurl,
884 const std::string& name, 902 const std::string& name,
885 std::vector<net::CanonicalCookie>* cookies) { 903 std::vector<net::CanonicalCookie>* cookies) {
886 DCHECK(thread_checker_.CalledOnValidThread()); 904 DCHECK(thread_checker_.CalledOnValidThread());
887 NSURL* url = net::NSURLWithGURL(gurl); 905 NSURL* url = net::NSURLWithGURL(gurl);
888 NSHTTPCookieStorage* storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; 906 NSArray* nscookies = [system_store_ cookiesForURL:url];
889 NSArray* nscookies = [storage cookiesForURL:url];
890 bool found_cookies = false; 907 bool found_cookies = false;
891 for (NSHTTPCookie* nscookie in nscookies) { 908 for (NSHTTPCookie* nscookie in nscookies) {
892 if (nscookie.name.UTF8String == name) { 909 if (nscookie.name.UTF8String == name) {
893 net::CanonicalCookie canonical_cookie = CanonicalCookieFromSystemCookie( 910 net::CanonicalCookie canonical_cookie = CanonicalCookieFromSystemCookie(
894 nscookie, creation_time_manager_->GetCreationTime(nscookie)); 911 nscookie, creation_time_manager_->GetCreationTime(nscookie));
895 cookies->push_back(canonical_cookie); 912 cookies->push_back(canonical_cookie);
896 found_cookies = true; 913 found_cookies = true;
897 } 914 }
898 } 915 }
899 return found_cookies; 916 return found_cookies;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
982 DCHECK(thread_checker_.CalledOnValidThread()); 999 DCHECK(thread_checker_.CalledOnValidThread());
983 return base::Bind(&CookieStoreIOS::UpdateCachesAfterDelete, this, callback); 1000 return base::Bind(&CookieStoreIOS::UpdateCachesAfterDelete, this, callback);
984 } 1001 }
985 1002
986 base::Closure CookieStoreIOS::WrapClosure(const base::Closure& callback) { 1003 base::Closure CookieStoreIOS::WrapClosure(const base::Closure& callback) {
987 DCHECK(thread_checker_.CalledOnValidThread()); 1004 DCHECK(thread_checker_.CalledOnValidThread());
988 return base::Bind(&CookieStoreIOS::UpdateCachesAfterClosure, this, callback); 1005 return base::Bind(&CookieStoreIOS::UpdateCachesAfterClosure, this, callback);
989 } 1006 }
990 1007
991 } // namespace net 1008 } // namespace net
OLDNEW
« no previous file with comments | « ios/net/cookies/cookie_store_ios.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698