summaryrefslogtreecommitdiffstats
path: root/ios/web/active_state_manager_impl.mm
blob: 0553038c54d75843dc20fa1d8276dbc78e54b4ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ios/web/active_state_manager_impl.h"

#include "base/logging.h"
#include "ios/web/public/browser_state.h"
#include "ios/web/public/web_thread.h"

namespace web {

namespace {
// The number of ActiveStateManagers that are currently in active state.
// At most one ActiveStateManager can be active at any given time.
int g_active_state_manager_active_count = 0;
}  // namespace

ActiveStateManagerImpl::ActiveStateManagerImpl(BrowserState* browser_state)
    : browser_state_(browser_state), active_(false) {
  DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI);
  DCHECK(browser_state_);
}

ActiveStateManagerImpl::~ActiveStateManagerImpl() {
  FOR_EACH_OBSERVER(Observer, observer_list_, WillBeDestroyed());
  DCHECK(!IsActive());
}

void ActiveStateManagerImpl::SetActive(bool active) {
  DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI);

  if (active == active_) {
    return;
  }
  if (active) {
    ++g_active_state_manager_active_count;
  } else {
    --g_active_state_manager_active_count;
  }
  DCHECK_GE(1, g_active_state_manager_active_count);
  active_ = active;

  if (active) {
    FOR_EACH_OBSERVER(Observer, observer_list_, OnActive());
  } else {
    FOR_EACH_OBSERVER(Observer, observer_list_, OnInactive());
  }
}

bool ActiveStateManagerImpl::IsActive() {
  DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI);
  return active_;
}

void ActiveStateManagerImpl::AddObserver(Observer* obs) {
  DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI);
  observer_list_.AddObserver(obs);
}

void ActiveStateManagerImpl::RemoveObserver(Observer* obs) {
  DCHECK_CURRENTLY_ON_WEB_THREAD(WebThread::UI);
  observer_list_.RemoveObserver(obs);
}

}  // namespace web