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
3 // found in the LICENSE file.
5 #include "ios/web/active_state_manager_impl.h"
7 #include "base/logging.h"
8 #include "ios/web/public/browser_state.h"
9 #include "ios/web/public/web_thread.h"
14 // The number of ActiveStateManagers that are currently in active state.
15 // At most one ActiveStateManager can be active at any given time.
16 int g_active_state_manager_active_count = 0;
19 ActiveStateManagerImpl::ActiveStateManagerImpl(BrowserState* browser_state)
20 : browser_state_(browser_state), active_(false) {
21 DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI);
22 DCHECK(browser_state_);
25 ActiveStateManagerImpl::~ActiveStateManagerImpl() {
26 FOR_EACH_OBSERVER(Observer, observer_list_, WillBeDestroyed());
30 void ActiveStateManagerImpl::SetActive(bool active) {
31 DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI);
33 if (active == active_) {
37 ++g_active_state_manager_active_count;
39 --g_active_state_manager_active_count;
41 DCHECK_GE(1, g_active_state_manager_active_count);
45 FOR_EACH_OBSERVER(Observer, observer_list_, OnActive());
47 FOR_EACH_OBSERVER(Observer, observer_list_, OnInactive());
51 bool ActiveStateManagerImpl::IsActive() {
52 DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI);
56 void ActiveStateManagerImpl::AddObserver(Observer* obs) {
57 DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI);
58 observer_list_.AddObserver(obs);
61 void ActiveStateManagerImpl::RemoveObserver(Observer* obs) {
62 DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI);
63 observer_list_.RemoveObserver(obs);