| 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 #include "ios/web/active_state_manager_impl.h" | 5 #include "ios/web/active_state_manager_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 #include "ios/web/public/web_thread.h" | 9 #include "ios/web/public/web_thread.h" |
| 10 | 10 |
| 11 #if !defined(__has_feature) || !__has_feature(objc_arc) | 11 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 12 #error "This file requires ARC support." | 12 #error "This file requires ARC support." |
| 13 #endif | 13 #endif |
| 14 | 14 |
| 15 namespace web { | 15 namespace web { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 // The number of ActiveStateManagers that are currently in active state. | 18 // The number of ActiveStateManagers that are currently in active state. |
| 19 // At most one ActiveStateManager can be active at any given time. | 19 // At most one ActiveStateManager can be active at any given time. |
| 20 int g_active_state_manager_active_count = 0; | 20 int g_active_state_manager_active_count = 0; |
| 21 } // namespace | 21 } // namespace |
| 22 | 22 |
| 23 ActiveStateManagerImpl::ActiveStateManagerImpl(BrowserState* browser_state) | 23 ActiveStateManagerImpl::ActiveStateManagerImpl(BrowserState* browser_state) |
| 24 : browser_state_(browser_state), active_(false) { | 24 : browser_state_(browser_state), active_(false) { |
| 25 DCHECK_CURRENTLY_ON(WebThread::UI); | 25 DCHECK_CURRENTLY_ON(WebThread::UI); |
| 26 DCHECK(browser_state_); | 26 DCHECK(browser_state_); |
| 27 } | 27 } |
| 28 | 28 |
| 29 ActiveStateManagerImpl::~ActiveStateManagerImpl() { | 29 ActiveStateManagerImpl::~ActiveStateManagerImpl() { |
| 30 FOR_EACH_OBSERVER(Observer, observer_list_, WillBeDestroyed()); | 30 for (auto& observer : observer_list_) |
| 31 observer.WillBeDestroyed(); |
| 31 DCHECK(!IsActive()); | 32 DCHECK(!IsActive()); |
| 32 } | 33 } |
| 33 | 34 |
| 34 void ActiveStateManagerImpl::SetActive(bool active) { | 35 void ActiveStateManagerImpl::SetActive(bool active) { |
| 35 DCHECK_CURRENTLY_ON(WebThread::UI); | 36 DCHECK_CURRENTLY_ON(WebThread::UI); |
| 36 | 37 |
| 37 if (active == active_) { | 38 if (active == active_) { |
| 38 return; | 39 return; |
| 39 } | 40 } |
| 40 if (active) { | 41 if (active) { |
| 41 ++g_active_state_manager_active_count; | 42 ++g_active_state_manager_active_count; |
| 42 } else { | 43 } else { |
| 43 --g_active_state_manager_active_count; | 44 --g_active_state_manager_active_count; |
| 44 } | 45 } |
| 45 DCHECK_GE(1, g_active_state_manager_active_count); | 46 DCHECK_GE(1, g_active_state_manager_active_count); |
| 46 active_ = active; | 47 active_ = active; |
| 47 | 48 |
| 48 if (active) { | 49 if (active) { |
| 49 FOR_EACH_OBSERVER(Observer, observer_list_, OnActive()); | 50 for (auto& observer : observer_list_) |
| 51 observer.OnActive(); |
| 50 } else { | 52 } else { |
| 51 FOR_EACH_OBSERVER(Observer, observer_list_, OnInactive()); | 53 for (auto& observer : observer_list_) |
| 54 observer.OnInactive(); |
| 52 } | 55 } |
| 53 } | 56 } |
| 54 | 57 |
| 55 bool ActiveStateManagerImpl::IsActive() { | 58 bool ActiveStateManagerImpl::IsActive() { |
| 56 DCHECK_CURRENTLY_ON(WebThread::UI); | 59 DCHECK_CURRENTLY_ON(WebThread::UI); |
| 57 return active_; | 60 return active_; |
| 58 } | 61 } |
| 59 | 62 |
| 60 void ActiveStateManagerImpl::AddObserver(ActiveStateManager::Observer* obs) { | 63 void ActiveStateManagerImpl::AddObserver(ActiveStateManager::Observer* obs) { |
| 61 DCHECK_CURRENTLY_ON(WebThread::UI); | 64 DCHECK_CURRENTLY_ON(WebThread::UI); |
| 62 observer_list_.AddObserver(obs); | 65 observer_list_.AddObserver(obs); |
| 63 } | 66 } |
| 64 | 67 |
| 65 void ActiveStateManagerImpl::RemoveObserver(ActiveStateManager::Observer* obs) { | 68 void ActiveStateManagerImpl::RemoveObserver(ActiveStateManager::Observer* obs) { |
| 66 DCHECK_CURRENTLY_ON(WebThread::UI); | 69 DCHECK_CURRENTLY_ON(WebThread::UI); |
| 67 observer_list_.RemoveObserver(obs); | 70 observer_list_.RemoveObserver(obs); |
| 68 } | 71 } |
| 69 | 72 |
| 70 } // namespace web | 73 } // namespace web |
| OLD | NEW |