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 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 CookieStoreIOS* CookieStoreIOS::CreateCookieStore( | 314 CookieStoreIOS* CookieStoreIOS::CreateCookieStore( |
315 NSHTTPCookieStorage* cookie_storage) { | 315 NSHTTPCookieStorage* cookie_storage) { |
316 DCHECK(cookie_storage); | 316 DCHECK(cookie_storage); |
317 // TODO(huey): Update this when CrNet supports multiple cookie jars. | 317 // TODO(huey): Update this when CrNet supports multiple cookie jars. |
318 [cookie_storage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; | 318 [cookie_storage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; |
319 | 319 |
320 // Create a cookie store with no persistent store backing. Then, populate | 320 // Create a cookie store with no persistent store backing. Then, populate |
321 // it from the system's cookie jar. | 321 // it from the system's cookie jar. |
322 CookieStoreIOS* cookie_store = new CookieStoreIOS(nullptr, cookie_storage); | 322 CookieStoreIOS* cookie_store = new CookieStoreIOS(nullptr, cookie_storage); |
323 cookie_store->synchronization_state_ = SYNCHRONIZED; | 323 cookie_store->synchronization_state_ = SYNCHRONIZED; |
324 cookie_store->Flush(base::Closure()); | 324 cookie_store->FlushStore(base::Closure()); |
325 return cookie_store; | 325 return cookie_store; |
326 } | 326 } |
327 | 327 |
328 // static | 328 // static |
329 void CookieStoreIOS::SwitchSynchronizedStore(CookieStoreIOS* old_store, | 329 void CookieStoreIOS::SwitchSynchronizedStore(CookieStoreIOS* old_store, |
330 CookieStoreIOS* new_store) { | 330 CookieStoreIOS* new_store) { |
331 DCHECK(new_store); | 331 DCHECK(new_store); |
332 DCHECK_NE(new_store, old_store); | 332 DCHECK_NE(new_store, old_store); |
333 if (old_store) | 333 if (old_store) |
334 old_store->SetSynchronizedWithSystemStore(false); | 334 old_store->SetSynchronizedWithSystemStore(false); |
335 new_store->SetSynchronizedWithSystemStore(true); | 335 new_store->SetSynchronizedWithSystemStore(true); |
336 } | 336 } |
337 | 337 |
338 // static | 338 // static |
339 void CookieStoreIOS::NotifySystemCookiesChanged() { | 339 void CookieStoreIOS::NotifySystemCookiesChanged() { |
340 NotificationTrampoline::GetInstance()->NotifyCookiesChanged(); | 340 NotificationTrampoline::GetInstance()->NotifyCookiesChanged(); |
341 } | 341 } |
342 | 342 |
343 void CookieStoreIOS::Flush(const base::Closure& closure) { | |
344 DCHECK(thread_checker_.CalledOnValidThread()); | |
345 | |
346 if (SystemCookiesAllowed()) { | |
347 // If cookies are disabled, the system store is empty, and the cookies are | |
348 // stashed on disk. Do not delete the cookies on the disk in this case. | |
349 WriteToCookieMonster([system_store_ cookies]); | |
350 } | |
351 cookie_monster_->FlushStore(closure); | |
352 flush_closure_.Cancel(); | |
353 } | |
354 | |
355 void CookieStoreIOS::UnSynchronize() { | 343 void CookieStoreIOS::UnSynchronize() { |
356 SetSynchronizedWithSystemStore(false); | 344 SetSynchronizedWithSystemStore(false); |
357 } | 345 } |
358 | 346 |
359 void CookieStoreIOS::SetMetricsEnabled() { | 347 void CookieStoreIOS::SetMetricsEnabled() { |
360 static CookieStoreIOS* g_cookie_store_with_metrics = nullptr; | 348 static CookieStoreIOS* g_cookie_store_with_metrics = nullptr; |
361 DCHECK(!g_cookie_store_with_metrics || g_cookie_store_with_metrics == this) | 349 DCHECK(!g_cookie_store_with_metrics || g_cookie_store_with_metrics == this) |
362 << "Only one cookie store may use metrics."; | 350 << "Only one cookie store may use metrics."; |
363 g_cookie_store_with_metrics = this; | 351 g_cookie_store_with_metrics = this; |
364 metrics_enabled_ = true; | 352 metrics_enabled_ = true; |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 base::Bind(&CookieStoreIOS::DeleteSessionCookiesAsync, this, | 597 base::Bind(&CookieStoreIOS::DeleteSessionCookiesAsync, this, |
610 WrapDeleteCallback(callback))); | 598 WrapDeleteCallback(callback))); |
611 break; | 599 break; |
612 case SYNCHRONIZED: | 600 case SYNCHRONIZED: |
613 CookieFilterFunction filter = base::Bind(&IsCookieSessionCookie); | 601 CookieFilterFunction filter = base::Bind(&IsCookieSessionCookie); |
614 DeleteCookiesWithFilter(filter, callback); | 602 DeleteCookiesWithFilter(filter, callback); |
615 break; | 603 break; |
616 } | 604 } |
617 } | 605 } |
618 | 606 |
| 607 void CookieStoreIOS::FlushStore(const base::Closure& closure) { |
| 608 DCHECK(thread_checker_.CalledOnValidThread()); |
| 609 |
| 610 if (SystemCookiesAllowed()) { |
| 611 // If cookies are disabled, the system store is empty, and the cookies are |
| 612 // stashed on disk. Do not delete the cookies on the disk in this case. |
| 613 WriteToCookieMonster([system_store_ cookies]); |
| 614 } |
| 615 cookie_monster_->FlushStore(closure); |
| 616 flush_closure_.Cancel(); |
| 617 } |
| 618 |
619 #pragma mark - | 619 #pragma mark - |
620 #pragma mark Protected methods | 620 #pragma mark Protected methods |
621 | 621 |
622 CookieStoreIOS::~CookieStoreIOS() { | 622 CookieStoreIOS::~CookieStoreIOS() { |
623 NotificationTrampoline::GetInstance()->RemoveObserver(this); | 623 NotificationTrampoline::GetInstance()->RemoveObserver(this); |
624 STLDeleteContainerPairSecondPointers(hook_map_.begin(), hook_map_.end()); | 624 STLDeleteContainerPairSecondPointers(hook_map_.begin(), hook_map_.end()); |
625 } | 625 } |
626 | 626 |
627 #pragma mark - | 627 #pragma mark - |
628 #pragma mark Private methods | 628 #pragma mark Private methods |
(...skipping 22 matching lines...) Expand all Loading... |
651 [system_store_ cookieAcceptPolicy]; | 651 [system_store_ cookieAcceptPolicy]; |
652 if (policy == NSHTTPCookieAcceptPolicyAlways) { | 652 if (policy == NSHTTPCookieAcceptPolicyAlways) { |
653 // If cookies are disabled, the system cookie store should be empty. | 653 // If cookies are disabled, the system cookie store should be empty. |
654 DCHECK(![[system_store_ cookies] count]); | 654 DCHECK(![[system_store_ cookies] count]); |
655 DCHECK(synchronization_state_ != SYNCHRONIZING); | 655 DCHECK(synchronization_state_ != SYNCHRONIZING); |
656 synchronization_state_ = SYNCHRONIZING; | 656 synchronization_state_ = SYNCHRONIZING; |
657 cookie_monster_->GetAllCookiesAsync( | 657 cookie_monster_->GetAllCookiesAsync( |
658 base::Bind(&CookieStoreIOS::AddCookiesToSystemStore, this)); | 658 base::Bind(&CookieStoreIOS::AddCookiesToSystemStore, this)); |
659 } else { | 659 } else { |
660 DCHECK_EQ(NSHTTPCookieAcceptPolicyNever, policy); | 660 DCHECK_EQ(NSHTTPCookieAcceptPolicyNever, policy); |
661 // Flush() does not write the cookies to disk when they are disabled. | 661 // FlushStore() does not write the cookies to disk when they are disabled. |
662 // Explicitly copy them. | 662 // Explicitly copy them. |
663 WriteToCookieMonster([system_store_ cookies]); | 663 WriteToCookieMonster([system_store_ cookies]); |
664 Flush(base::Closure()); | 664 FlushStore(base::Closure()); |
665 ClearSystemStore(); | 665 ClearSystemStore(); |
666 if (synchronization_state_ == SYNCHRONIZING) { | 666 if (synchronization_state_ == SYNCHRONIZING) { |
667 // If synchronization was in progress, abort it and leave the cookie store | 667 // If synchronization was in progress, abort it and leave the cookie store |
668 // empty. | 668 // empty. |
669 // Temporarily toggle the synchronization state so that pending tasks are | 669 // Temporarily toggle the synchronization state so that pending tasks are |
670 // redirected to cookie_monster_ and can complete normally. | 670 // redirected to cookie_monster_ and can complete normally. |
671 synchronization_state_ = NOT_SYNCHRONIZED; | 671 synchronization_state_ = NOT_SYNCHRONIZED; |
672 RunAllPendingTasks(); | 672 RunAllPendingTasks(); |
673 synchronization_state_ = SYNCHRONIZED; | 673 synchronization_state_ = SYNCHRONIZED; |
674 } | 674 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
706 // cookies are re-enabled. | 706 // cookies are re-enabled. |
707 if (policy == NSHTTPCookieAcceptPolicyAlways) { | 707 if (policy == NSHTTPCookieAcceptPolicyAlways) { |
708 if (synchronized) { | 708 if (synchronized) { |
709 synchronization_state_ = SYNCHRONIZING; | 709 synchronization_state_ = SYNCHRONIZING; |
710 ClearSystemStore(); | 710 ClearSystemStore(); |
711 cookie_monster_->GetAllCookiesAsync( | 711 cookie_monster_->GetAllCookiesAsync( |
712 base::Bind(&CookieStoreIOS::AddCookiesToSystemStore, this)); | 712 base::Bind(&CookieStoreIOS::AddCookiesToSystemStore, this)); |
713 return; | 713 return; |
714 } else { | 714 } else { |
715 // Copy the cookies from the global store to |cookie_monster_|. | 715 // Copy the cookies from the global store to |cookie_monster_|. |
716 Flush(base::Closure()); | 716 FlushStore(base::Closure()); |
717 } | 717 } |
718 } | 718 } |
719 synchronization_state_ = synchronized ? SYNCHRONIZED : NOT_SYNCHRONIZED; | 719 synchronization_state_ = synchronized ? SYNCHRONIZED : NOT_SYNCHRONIZED; |
720 | 720 |
721 if (synchronization_state_ == NOT_SYNCHRONIZED) { | 721 if (synchronization_state_ == NOT_SYNCHRONIZED) { |
722 // If there are pending tasks, then it means that the synchronization is | 722 // If there are pending tasks, then it means that the synchronization is |
723 // being canceled. All pending tasks can be sent to cookie_monster_. | 723 // being canceled. All pending tasks can be sent to cookie_monster_. |
724 RunAllPendingTasks(); | 724 RunAllPendingTasks(); |
725 } | 725 } |
726 } | 726 } |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
839 &added_cookies)) { | 839 &added_cookies)) { |
840 RunCallbacksForCookies(key.first, key.second, removed_cookies, true); | 840 RunCallbacksForCookies(key.first, key.second, removed_cookies, true); |
841 RunCallbacksForCookies(key.first, key.second, added_cookies, false); | 841 RunCallbacksForCookies(key.first, key.second, added_cookies, false); |
842 } | 842 } |
843 } | 843 } |
844 | 844 |
845 // Do not schedule a flush if one is already scheduled. | 845 // Do not schedule a flush if one is already scheduled. |
846 if (!flush_closure_.IsCancelled()) | 846 if (!flush_closure_.IsCancelled()) |
847 return; | 847 return; |
848 | 848 |
849 flush_closure_.Reset(base::Bind(&CookieStoreIOS::Flush, | 849 flush_closure_.Reset(base::Bind(&CookieStoreIOS::FlushStore, |
850 base::Unretained(this), base::Closure())); | 850 base::Unretained(this), base::Closure())); |
851 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 851 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
852 FROM_HERE, flush_closure_.callback(), flush_delay_); | 852 FROM_HERE, flush_closure_.callback(), flush_delay_); |
853 } | 853 } |
854 | 854 |
855 scoped_ptr<net::CookieStore::CookieChangedSubscription> | 855 scoped_ptr<net::CookieStore::CookieChangedSubscription> |
856 CookieStoreIOS::AddCallbackForCookie(const GURL& gurl, | 856 CookieStoreIOS::AddCallbackForCookie(const GURL& gurl, |
857 const std::string& name, | 857 const std::string& name, |
858 const CookieChangedCallback& callback) { | 858 const CookieChangedCallback& callback) { |
859 DCHECK(thread_checker_.CalledOnValidThread()); | 859 DCHECK(thread_checker_.CalledOnValidThread()); |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1001 DCHECK(thread_checker_.CalledOnValidThread()); | 1001 DCHECK(thread_checker_.CalledOnValidThread()); |
1002 return base::Bind(&CookieStoreIOS::UpdateCachesAfterDelete, this, callback); | 1002 return base::Bind(&CookieStoreIOS::UpdateCachesAfterDelete, this, callback); |
1003 } | 1003 } |
1004 | 1004 |
1005 base::Closure CookieStoreIOS::WrapClosure(const base::Closure& callback) { | 1005 base::Closure CookieStoreIOS::WrapClosure(const base::Closure& callback) { |
1006 DCHECK(thread_checker_.CalledOnValidThread()); | 1006 DCHECK(thread_checker_.CalledOnValidThread()); |
1007 return base::Bind(&CookieStoreIOS::UpdateCachesAfterClosure, this, callback); | 1007 return base::Bind(&CookieStoreIOS::UpdateCachesAfterClosure, this, callback); |
1008 } | 1008 } |
1009 | 1009 |
1010 } // namespace net | 1010 } // namespace net |
OLD | NEW |