summaryrefslogtreecommitdiffstats
path: root/content/browser/power_save_blocker_common.cc
blob: 8115b9cde9b2d2fe5e64e767eff7cc73078ce91b (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
// Copyright (c) 2011 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 "content/browser/power_save_blocker.h"

#include "base/bind.h"
#include "content/public/browser/browser_thread.h"

using content::BrowserThread;

// Accessed only from the UI thread.
int PowerSaveBlocker::blocker_count_[kPowerSaveBlockPreventStateCount];

PowerSaveBlocker::PowerSaveBlocker(PowerSaveBlockerType type) : type_(type) {
  DCHECK_LT(kPowerSaveBlockPreventNone, type);
  DCHECK_GT(kPowerSaveBlockPreventStateCount, type);
  std::vector<int> change(kPowerSaveBlockPreventStateCount);
  ++change[type_];
  PostAdjustBlockCount(change);
}

PowerSaveBlocker::~PowerSaveBlocker(void) {
  std::vector<int> change(kPowerSaveBlockPreventStateCount);
  --change[type_];
  PostAdjustBlockCount(change);
}

// static
void PowerSaveBlocker::PostAdjustBlockCount(const std::vector<int>& deltas) {
  BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      base::Bind(&PowerSaveBlocker::AdjustBlockCount, deltas));
}

// Called only from UI thread.
// static
void PowerSaveBlocker::AdjustBlockCount(const std::vector<int>& deltas) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  PowerSaveBlockerType old_block = HighestBlockType();

  for (size_t i = 0; i < deltas.size(); ++i)
    blocker_count_[i] += deltas[i];

  PowerSaveBlockerType new_block = HighestBlockType();

  if (new_block != old_block)
    ApplyBlock(new_block);
}

// Called only from UI thread.
PowerSaveBlocker::PowerSaveBlockerType PowerSaveBlocker::HighestBlockType() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  for (PowerSaveBlockerType t = kPowerSaveBlockPreventDisplaySleep;
       t >= kPowerSaveBlockPreventSystemSleep;
       t = static_cast<PowerSaveBlockerType>(t - 1)) {
    if (blocker_count_[t])
      return t;
  }

  return kPowerSaveBlockPreventNone;
}