summaryrefslogtreecommitdiffstats
path: root/chrome/browser/power_save_blocker_common.cc
blob: 9fcfd7ec723e1a887ce9def160ec6ffefdd324d7 (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
// Copyright (c) 2009 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 "chrome/browser/power_save_blocker.h"
#include "content/browser/browser_thread.h"

// Accessed only from the UI thread.
int PowerSaveBlocker::blocker_count_ = 0;

PowerSaveBlocker::PowerSaveBlocker(bool enable) : enabled_(false) {
  if (enable)
    Enable();
}

PowerSaveBlocker::~PowerSaveBlocker(void) {
  Disable();
}

void PowerSaveBlocker::Enable() {
  if (enabled_)
    return;

  enabled_ = true;
  PostAdjustBlockCount(1);
}

void PowerSaveBlocker::Disable() {
  if (!enabled_)
    return;

  enabled_ = false;
  PostAdjustBlockCount(-1);
}


void PowerSaveBlocker::PostAdjustBlockCount(int delta) {
  BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      NewRunnableFunction(&PowerSaveBlocker::AdjustBlockCount, delta));
}

// Called only from UI thread.
void PowerSaveBlocker::AdjustBlockCount(int delta) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  bool was_blocking = (blocker_count_ != 0);

  blocker_count_ += delta;

  bool is_blocking = (blocker_count_ != 0);

  DCHECK_GE(blocker_count_, 0);

  if (is_blocking != was_blocking)
    ApplyBlock(is_blocking);
}