| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #import "ios/web/browsing_data_partition_impl.h" | 5 #import "ios/web/browsing_data_partition_impl.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "ios/web/public/browser_state.h" | 8 #include "ios/web/public/browser_state.h" |
| 9 #import "ios/web/public/crw_browsing_data_store.h" | 9 #import "ios/web/public/crw_browsing_data_store.h" |
| 10 #include "ios/web/public/web_thread.h" | 10 #include "ios/web/public/web_thread.h" |
| 11 | 11 |
| 12 // A class that observes the |mode| changes in a CRWBrowsingDataStore using KVO. | 12 // A class that observes the |mode| changes in a CRWBrowsingDataStore using KVO. |
| 13 // Maintains a count of all the CRWBrowsingDataStores whose mode is out of sync | 13 // Maintains a count of all the CRWBrowsingDataStores whose mode is out of sync |
| 14 // with their corresponding ActiveStateManager's active state. | 14 // with their corresponding ActiveStateManager's active state. |
| 15 @interface CRWBrowsingDataStoreModeObserver : NSObject | 15 @interface CRWBrowsingDataStoreModeObserver : NSObject |
| 16 | 16 |
| 17 // The total count of the CRWBrowsingDataStores -- that are being observed -- | 17 // The total count of the CRWBrowsingDataStores -- that are being observed -- |
| 18 // whose mode is out of sync with their associated ActiveStateManager's active | 18 // whose mode is out of sync with their associated ActiveStateManager's active |
| 19 // state. | 19 // state. |
| 20 @property(nonatomic, assign) NSUInteger outOfSyncStoreCount; | 20 @property(nonatomic, assign) NSUInteger outOfSyncStoreCount; |
| 21 | 21 |
| 22 // Adds |browsingDataStore| to the list of the CRWBrowsingDataStores that are | 22 // Adds |browsingDataStore| to the list of the CRWBrowsingDataStores that are |
| 23 // being observed for KVO changes in the |mode| value. |browserState| cannot be | 23 // being observed for KVO changes in the |mode| value. |browserState| cannot be |
| 24 // null and the |browserState|'s associated CRWBrowsingDataStore must be | 24 // null and the |browserState|'s associated CRWBrowsingDataStore must be |
| 25 // |browsingDataStore|. | 25 // |browsingDataStore|. |
| 26 // The |browsingDataStore|'s mode must not already be SYNCHRONIZING. | 26 // The |browsingDataStore|'s mode must not already be |CHANGING|. |
| 27 - (void)startObservingBrowsingDataStore:(CRWBrowsingDataStore*)browsingDataStore | 27 - (void)startObservingBrowsingDataStore:(CRWBrowsingDataStore*)browsingDataStore |
| 28 browserState:(web::BrowserState*)browserState; | 28 browserState:(web::BrowserState*)browserState; |
| 29 | 29 |
| 30 // Stops observing |browsingDataStore| for its |mode| change. | 30 // Stops observing |browsingDataStore| for its |mode| change. |
| 31 // The |browsingDataStore|'s mode must not be SYNCHRONIZING. | 31 // The |browsingDataStore|'s mode must not be |CHANGING|. |
| 32 - (void)stopObservingBrowsingDataStore:(CRWBrowsingDataStore*)browsingDataStore; | 32 - (void)stopObservingBrowsingDataStore:(CRWBrowsingDataStore*)browsingDataStore; |
| 33 @end | 33 @end |
| 34 | 34 |
| 35 @implementation CRWBrowsingDataStoreModeObserver | 35 @implementation CRWBrowsingDataStoreModeObserver |
| 36 | 36 |
| 37 @synthesize outOfSyncStoreCount = _outOfSyncStoreCount; | 37 @synthesize outOfSyncStoreCount = _outOfSyncStoreCount; |
| 38 | 38 |
| 39 - (void)startObservingBrowsingDataStore:(CRWBrowsingDataStore*)browsingDataStore | 39 - (void)startObservingBrowsingDataStore:(CRWBrowsingDataStore*)browsingDataStore |
| 40 browserState:(web::BrowserState*)browserState { | 40 browserState:(web::BrowserState*)browserState { |
| 41 web::BrowsingDataPartition* browsing_data_partition = | 41 web::BrowsingDataPartition* browsing_data_partition = |
| 42 web::BrowserState::GetBrowsingDataPartition(browserState); | 42 web::BrowserState::GetBrowsingDataPartition(browserState); |
| 43 DCHECK(browsing_data_partition); | 43 DCHECK(browsing_data_partition); |
| 44 DCHECK_EQ(browsing_data_partition->GetBrowsingDataStore(), browsingDataStore); | 44 DCHECK_EQ(browsing_data_partition->GetBrowsingDataStore(), browsingDataStore); |
| 45 DCHECK_NE(SYNCHRONIZING, browsingDataStore.mode); | 45 DCHECK_NE(web::CHANGING, browsingDataStore.mode); |
| 46 | 46 |
| 47 [browsingDataStore addObserver:self | 47 [browsingDataStore addObserver:self |
| 48 forKeyPath:@"mode" | 48 forKeyPath:@"mode" |
| 49 options:0 | 49 options:0 |
| 50 context:browserState]; | 50 context:browserState]; |
| 51 } | 51 } |
| 52 | 52 |
| 53 - (void)stopObservingBrowsingDataStore: | 53 - (void)stopObservingBrowsingDataStore: |
| 54 (CRWBrowsingDataStore*)browsingDataStore { | 54 (CRWBrowsingDataStore*)browsingDataStore { |
| 55 DCHECK_NE(SYNCHRONIZING, browsingDataStore.mode); | 55 DCHECK_NE(web::CHANGING, browsingDataStore.mode); |
| 56 | 56 |
| 57 [browsingDataStore removeObserver:self forKeyPath:@"mode"]; | 57 [browsingDataStore removeObserver:self forKeyPath:@"mode"]; |
| 58 } | 58 } |
| 59 | 59 |
| 60 - (void)observeValueForKeyPath:(NSString*)keyPath | 60 - (void)observeValueForKeyPath:(NSString*)keyPath |
| 61 ofObject:(CRWBrowsingDataStore*)browsingDataStore | 61 ofObject:(CRWBrowsingDataStore*)browsingDataStore |
| 62 change:(NSDictionary*)change | 62 change:(NSDictionary*)change |
| 63 context:(void*)context { | 63 context:(void*)context { |
| 64 DCHECK([keyPath isEqual:@"mode"]); | 64 DCHECK([keyPath isEqual:@"mode"]); |
| 65 DCHECK([browsingDataStore isKindOfClass:[CRWBrowsingDataStore class]]); | 65 DCHECK([browsingDataStore isKindOfClass:[CRWBrowsingDataStore class]]); |
| 66 DCHECK(context); | 66 DCHECK(context); |
| 67 | 67 |
| 68 if (browsingDataStore.mode == SYNCHRONIZING) { | 68 if (browsingDataStore.mode == web::CHANGING) { |
| 69 ++self.outOfSyncStoreCount; | 69 ++self.outOfSyncStoreCount; |
| 70 return; | 70 return; |
| 71 } | 71 } |
| 72 web::BrowserState* browserState = static_cast<web::BrowserState*>(context); | 72 web::BrowserState* browserState = static_cast<web::BrowserState*>(context); |
| 73 web::ActiveStateManager* activeStateManager = | 73 web::ActiveStateManager* activeStateManager = |
| 74 web::BrowserState::GetActiveStateManager(browserState); | 74 web::BrowserState::GetActiveStateManager(browserState); |
| 75 DCHECK(activeStateManager); | 75 DCHECK(activeStateManager); |
| 76 bool activeState = activeStateManager->IsActive(); | 76 bool activeState = activeStateManager->IsActive(); |
| 77 // Check if the |browsingDataStore|'s associated ActiveStateManager's active | 77 // Check if the |browsingDataStore|'s associated ActiveStateManager's active |
| 78 // state is still out of sync. | 78 // state is still out of sync. |
| 79 if (activeState && browsingDataStore.mode == INACTIVE) { | 79 if (activeState && browsingDataStore.mode == web::INACTIVE) { |
| 80 [browsingDataStore makeActiveWithCompletionHandler:nil]; | 80 [browsingDataStore makeActiveWithCompletionHandler:nil]; |
| 81 } else if (!activeState && browsingDataStore.mode == ACTIVE) { | 81 } else if (!activeState && browsingDataStore.mode == web::ACTIVE) { |
| 82 [browsingDataStore makeInactiveWithCompletionHandler:nil]; | 82 [browsingDataStore makeInactiveWithCompletionHandler:nil]; |
| 83 } | 83 } |
| 84 | 84 |
| 85 DCHECK_GE(self.outOfSyncStoreCount, 1U); | 85 DCHECK_GE(self.outOfSyncStoreCount, 1U); |
| 86 --self.outOfSyncStoreCount; | 86 --self.outOfSyncStoreCount; |
| 87 // TODO(shreyasv): Have a BrowsingDataPartitionClient be informed when | 87 // TODO(shreyasv): Have a BrowsingDataPartitionClient be informed when |
| 88 // |self.outOfSyncStoreCount| goes down to 0. crbug.com/480654. | 88 // |self.outOfSyncStoreCount| goes down to 0. crbug.com/480654. |
| 89 } | 89 } |
| 90 | 90 |
| 91 @end | 91 @end |
| (...skipping 16 matching lines...) Expand all Loading... |
| 108 active_state_manager_ = static_cast<ActiveStateManagerImpl*>( | 108 active_state_manager_ = static_cast<ActiveStateManagerImpl*>( |
| 109 BrowserState::GetActiveStateManager(browser_state)); | 109 BrowserState::GetActiveStateManager(browser_state)); |
| 110 DCHECK(active_state_manager_); | 110 DCHECK(active_state_manager_); |
| 111 active_state_manager_->AddObserver(this); | 111 active_state_manager_->AddObserver(this); |
| 112 } | 112 } |
| 113 | 113 |
| 114 BrowsingDataPartitionImpl::~BrowsingDataPartitionImpl() { | 114 BrowsingDataPartitionImpl::~BrowsingDataPartitionImpl() { |
| 115 if (active_state_manager_) { | 115 if (active_state_manager_) { |
| 116 active_state_manager_->RemoveObserver(this); | 116 active_state_manager_->RemoveObserver(this); |
| 117 } | 117 } |
| 118 DCHECK_NE(SYNCHRONIZING, [browsing_data_store_ mode]); | 118 DCHECK_NE(CHANGING, [browsing_data_store_ mode]); |
| 119 [g_browsing_data_store_mode_observer | 119 [g_browsing_data_store_mode_observer |
| 120 stopObservingBrowsingDataStore:browsing_data_store_]; | 120 stopObservingBrowsingDataStore:browsing_data_store_]; |
| 121 } | 121 } |
| 122 | 122 |
| 123 // static | 123 // static |
| 124 bool BrowsingDataPartition::IsSynchronized() { | 124 bool BrowsingDataPartition::IsSynchronized() { |
| 125 return [g_browsing_data_store_mode_observer outOfSyncStoreCount] == 0U; | 125 return [g_browsing_data_store_mode_observer outOfSyncStoreCount] == 0U; |
| 126 } | 126 } |
| 127 | 127 |
| 128 CRWBrowsingDataStore* BrowsingDataPartitionImpl::GetBrowsingDataStore() { | 128 CRWBrowsingDataStore* BrowsingDataPartitionImpl::GetBrowsingDataStore() { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 155 } | 155 } |
| 156 | 156 |
| 157 void BrowsingDataPartitionImpl::WillBeDestroyed() { | 157 void BrowsingDataPartitionImpl::WillBeDestroyed() { |
| 158 DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI); | 158 DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI); |
| 159 | 159 |
| 160 active_state_manager_->RemoveObserver(this); | 160 active_state_manager_->RemoveObserver(this); |
| 161 active_state_manager_ = nullptr; | 161 active_state_manager_ = nullptr; |
| 162 } | 162 } |
| 163 | 163 |
| 164 } // namespace web | 164 } // namespace web |
| OLD | NEW |