| 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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 // CookieStoreIOS. | 54 // CookieStoreIOS. |
| 55 class NotificationTrampoline { | 55 class NotificationTrampoline { |
| 56 public: | 56 public: |
| 57 static NotificationTrampoline* GetInstance(); | 57 static NotificationTrampoline* GetInstance(); |
| 58 | 58 |
| 59 void AddObserver(CookieNotificationObserver* obs); | 59 void AddObserver(CookieNotificationObserver* obs); |
| 60 void RemoveObserver(CookieNotificationObserver* obs); | 60 void RemoveObserver(CookieNotificationObserver* obs); |
| 61 | 61 |
| 62 // Notify the observers. | 62 // Notify the observers. |
| 63 void NotifyCookiesChanged(); | 63 void NotifyCookiesChanged(); |
| 64 void NotifyCookiePolicyChanged(); | |
| 65 | 64 |
| 66 private: | 65 private: |
| 67 NotificationTrampoline(); | 66 NotificationTrampoline(); |
| 68 ~NotificationTrampoline(); | 67 ~NotificationTrampoline(); |
| 69 | 68 |
| 70 base::ObserverList<CookieNotificationObserver> observer_list_; | 69 base::ObserverList<CookieNotificationObserver> observer_list_; |
| 71 | 70 |
| 72 DISALLOW_COPY_AND_ASSIGN(NotificationTrampoline); | 71 DISALLOW_COPY_AND_ASSIGN(NotificationTrampoline); |
| 73 | 72 |
| 74 static NotificationTrampoline* g_notification_trampoline; | 73 static NotificationTrampoline* g_notification_trampoline; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 88 | 87 |
| 89 void NotificationTrampoline::RemoveObserver(CookieNotificationObserver* obs) { | 88 void NotificationTrampoline::RemoveObserver(CookieNotificationObserver* obs) { |
| 90 observer_list_.RemoveObserver(obs); | 89 observer_list_.RemoveObserver(obs); |
| 91 } | 90 } |
| 92 | 91 |
| 93 void NotificationTrampoline::NotifyCookiesChanged() { | 92 void NotificationTrampoline::NotifyCookiesChanged() { |
| 94 for (auto& observer : observer_list_) | 93 for (auto& observer : observer_list_) |
| 95 observer.OnSystemCookiesChanged(); | 94 observer.OnSystemCookiesChanged(); |
| 96 } | 95 } |
| 97 | 96 |
| 98 void NotificationTrampoline::NotifyCookiePolicyChanged() { | |
| 99 for (auto& observer : observer_list_) | |
| 100 observer.OnSystemCookiePolicyChanged(); | |
| 101 } | |
| 102 | |
| 103 NotificationTrampoline::NotificationTrampoline() { | 97 NotificationTrampoline::NotificationTrampoline() { |
| 104 } | 98 } |
| 105 | 99 |
| 106 NotificationTrampoline::~NotificationTrampoline() { | 100 NotificationTrampoline::~NotificationTrampoline() { |
| 107 } | 101 } |
| 108 | 102 |
| 109 // Global instance of NotificationTrampoline. | 103 // Global instance of NotificationTrampoline. |
| 110 NotificationTrampoline* NotificationTrampoline::g_notification_trampoline = | 104 NotificationTrampoline* NotificationTrampoline::g_notification_trampoline = |
| 111 nullptr; | 105 nullptr; |
| 112 | 106 |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 | 304 |
| 311 cookie_monster_->SetPersistSessionCookies(true); | 305 cookie_monster_->SetPersistSessionCookies(true); |
| 312 cookie_monster_->SetForceKeepSessionState(); | 306 cookie_monster_->SetForceKeepSessionState(); |
| 313 } | 307 } |
| 314 | 308 |
| 315 CookieStoreIOS::~CookieStoreIOS() { | 309 CookieStoreIOS::~CookieStoreIOS() { |
| 316 NotificationTrampoline::GetInstance()->RemoveObserver(this); | 310 NotificationTrampoline::GetInstance()->RemoveObserver(this); |
| 317 } | 311 } |
| 318 | 312 |
| 319 // static | 313 // static |
| 320 void CookieStoreIOS::SetCookiePolicy(CookiePolicy setting) { | |
| 321 NSHTTPCookieAcceptPolicy policy = (setting == ALLOW) | |
| 322 ? NSHTTPCookieAcceptPolicyAlways | |
| 323 : NSHTTPCookieAcceptPolicyNever; | |
| 324 NSHTTPCookieStorage* store = [NSHTTPCookieStorage sharedHTTPCookieStorage]; | |
| 325 NSHTTPCookieAcceptPolicy current_policy = [store cookieAcceptPolicy]; | |
| 326 if (current_policy == policy) | |
| 327 return; | |
| 328 [store setCookieAcceptPolicy:policy]; | |
| 329 NotificationTrampoline::GetInstance()->NotifyCookiePolicyChanged(); | |
| 330 } | |
| 331 | |
| 332 // static | |
| 333 std::unique_ptr<CookieStoreIOS> CookieStoreIOS::CreateCookieStore( | 314 std::unique_ptr<CookieStoreIOS> CookieStoreIOS::CreateCookieStore( |
| 334 NSHTTPCookieStorage* cookie_storage) { | 315 NSHTTPCookieStorage* cookie_storage) { |
| 335 DCHECK(cookie_storage); | 316 DCHECK(cookie_storage); |
| 336 // TODO(huey): Update this when CrNet supports multiple cookie jars. | 317 // TODO(huey): Update this when CrNet supports multiple cookie jars. |
| 337 [cookie_storage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; | 318 [cookie_storage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; |
| 338 | 319 |
| 339 // Create a cookie store with no persistent store backing. Then, populate | 320 // Create a cookie store with no persistent store backing. Then, populate |
| 340 // it from the system's cookie jar. | 321 // it from the system's cookie jar. |
| 341 std::unique_ptr<CookieStoreIOS> cookie_store( | 322 std::unique_ptr<CookieStoreIOS> cookie_store( |
| 342 new CookieStoreIOS(nullptr, cookie_storage)); | 323 new CookieStoreIOS(nullptr, cookie_storage)); |
| (...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 760 void CookieStoreIOS::ClearSystemStore() { | 741 void CookieStoreIOS::ClearSystemStore() { |
| 761 DCHECK(thread_checker_.CalledOnValidThread()); | 742 DCHECK(thread_checker_.CalledOnValidThread()); |
| 762 base::scoped_nsobject<NSArray> copy( | 743 base::scoped_nsobject<NSArray> copy( |
| 763 [[NSArray alloc] initWithArray:[system_store_ cookies]]); | 744 [[NSArray alloc] initWithArray:[system_store_ cookies]]); |
| 764 for (NSHTTPCookie* cookie in copy.get()) | 745 for (NSHTTPCookie* cookie in copy.get()) |
| 765 [system_store_ deleteCookie:cookie]; | 746 [system_store_ deleteCookie:cookie]; |
| 766 DCHECK_EQ(0u, [[system_store_ cookies] count]); | 747 DCHECK_EQ(0u, [[system_store_ cookies] count]); |
| 767 creation_time_manager_->Clear(); | 748 creation_time_manager_->Clear(); |
| 768 } | 749 } |
| 769 | 750 |
| 770 void CookieStoreIOS::OnSystemCookiePolicyChanged() { | |
| 771 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 772 | |
| 773 // If the CookieStoreIOS is not synchronized or is not backed by | |
| 774 // |NSHTTPCookieStorage sharedHTTPCookieStorage| this callback is irrelevant. | |
| 775 if (synchronization_state_ == NOT_SYNCHRONIZED || | |
| 776 system_store_ != [NSHTTPCookieStorage sharedHTTPCookieStorage]) { | |
| 777 return; | |
| 778 } | |
| 779 | |
| 780 NSHTTPCookieAcceptPolicy policy = | |
| 781 [system_store_ cookieAcceptPolicy]; | |
| 782 if (policy == NSHTTPCookieAcceptPolicyAlways) { | |
| 783 // If cookies are disabled, the system cookie store should be empty. | |
| 784 DCHECK(![[system_store_ cookies] count]); | |
| 785 DCHECK(synchronization_state_ != SYNCHRONIZING); | |
| 786 synchronization_state_ = SYNCHRONIZING; | |
| 787 cookie_monster_->GetAllCookiesAsync(base::Bind( | |
| 788 &CookieStoreIOS::AddCookiesToSystemStore, weak_factory_.GetWeakPtr())); | |
| 789 } else { | |
| 790 DCHECK_EQ(NSHTTPCookieAcceptPolicyNever, policy); | |
| 791 // FlushStore() does not write the cookies to disk when they are disabled. | |
| 792 // Explicitly copy them. | |
| 793 WriteToCookieMonster([system_store_ cookies]); | |
| 794 FlushStore(base::Closure()); | |
| 795 ClearSystemStore(); | |
| 796 if (synchronization_state_ == SYNCHRONIZING) { | |
| 797 // If synchronization was in progress, abort it and leave the cookie store | |
| 798 // empty. | |
| 799 // Temporarily toggle the synchronization state so that pending tasks are | |
| 800 // redirected to cookie_monster_ and can complete normally. | |
| 801 synchronization_state_ = NOT_SYNCHRONIZED; | |
| 802 RunAllPendingTasks(); | |
| 803 synchronization_state_ = SYNCHRONIZED; | |
| 804 } | |
| 805 } | |
| 806 } | |
| 807 | |
| 808 void CookieStoreIOS::SetSynchronizedWithSystemStore(bool synchronized) { | 751 void CookieStoreIOS::SetSynchronizedWithSystemStore(bool synchronized) { |
| 809 DCHECK(thread_checker_.CalledOnValidThread()); | 752 DCHECK(thread_checker_.CalledOnValidThread()); |
| 810 | 753 |
| 811 if (synchronized == (synchronization_state_ != NOT_SYNCHRONIZED)) | 754 if (synchronized == (synchronization_state_ != NOT_SYNCHRONIZED)) |
| 812 return; // The cookie store is already in the desired state. | 755 return; // The cookie store is already in the desired state. |
| 813 | 756 |
| 814 #if !defined(NDEBUG) | 757 #if !defined(NDEBUG) |
| 815 if (!synchronized) { | 758 if (!synchronized) { |
| 816 DCHECK_EQ(this, g_current_synchronized_store) | 759 DCHECK_EQ(this, g_current_synchronized_store) |
| 817 << "This cookie store was not synchronized"; | 760 << "This cookie store was not synchronized"; |
| (...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1159 weak_factory_.GetWeakPtr(), callback); | 1102 weak_factory_.GetWeakPtr(), callback); |
| 1160 } | 1103 } |
| 1161 | 1104 |
| 1162 base::Closure CookieStoreIOS::WrapClosure(const base::Closure& callback) { | 1105 base::Closure CookieStoreIOS::WrapClosure(const base::Closure& callback) { |
| 1163 DCHECK(thread_checker_.CalledOnValidThread()); | 1106 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1164 return base::Bind(&CookieStoreIOS::UpdateCachesAfterClosure, | 1107 return base::Bind(&CookieStoreIOS::UpdateCachesAfterClosure, |
| 1165 weak_factory_.GetWeakPtr(), callback); | 1108 weak_factory_.GetWeakPtr(), callback); |
| 1166 } | 1109 } |
| 1167 | 1110 |
| 1168 } // namespace net | 1111 } // namespace net |
| OLD | NEW |