File: power_api.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (159 lines) | stat: -rw-r--r-- 5,685 bytes parent folder | download | duplicates (5)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_BROWSER_API_POWER_POWER_API_H_
#define EXTENSIONS_BROWSER_API_POWER_POWER_API_H_

#include <map>
#include <memory>
#include <string>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "build/chromeos_buildflags.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
#include "extensions/browser/extension_function.h"
#include "extensions/browser/extension_registry_observer.h"
#include "extensions/common/api/power.h"
#include "extensions/common/extension_id.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/device/public/mojom/wake_lock.mojom.h"

namespace content {
class BrowserContext;
}

namespace extensions {

// Implementation of the chrome.power.requestKeepAwake API.
class PowerRequestKeepAwakeFunction : public ExtensionFunction {
 public:
  DECLARE_EXTENSION_FUNCTION("power.requestKeepAwake", POWER_REQUESTKEEPAWAKE)

 protected:
  ~PowerRequestKeepAwakeFunction() override = default;

  // ExtensionFunction:
  ResponseAction Run() override;
};

// Implementation of the chrome.power.releaseKeepAwake API.
class PowerReleaseKeepAwakeFunction : public ExtensionFunction {
 public:
  DECLARE_EXTENSION_FUNCTION("power.releaseKeepAwake", POWER_RELEASEKEEPAWAKE)

 protected:
  ~PowerReleaseKeepAwakeFunction() override = default;

  // ExtensionFunction:
  ResponseAction Run() override;
};

#if BUILDFLAG(IS_CHROMEOS)
// Implementation of the chrome.power.reportActivity API.
class PowerReportActivityFunction : public ExtensionFunction {
 public:
  DECLARE_EXTENSION_FUNCTION("power.reportActivity", POWER_REPORTACTIVITY)

 protected:
  ~PowerReportActivityFunction() override = default;

  // ExtensionFunction:
  ResponseAction Run() override;
};
#endif  // BUILDFLAG(IS_CHROMEOS)

// Handles calls made via the chrome.power API. There is a separate instance of
// this class for each profile, as requests are tracked by extension ID, but a
// regular and incognito profile will share the same instance.
class PowerAPI : public BrowserContextKeyedAPI,
                 public extensions::ExtensionRegistryObserver {
 public:
  using ActivateWakeLockFunction =
      base::RepeatingCallback<void(device::mojom::WakeLockType)>;
  using CancelWakeLockFunction = base::RepeatingCallback<void()>;

  explicit PowerAPI(content::BrowserContext* context);
  ~PowerAPI() override;
  PowerAPI(const PowerAPI&) = delete;
  PowerAPI& operator=(const PowerAPI&) = delete;

  static PowerAPI* Get(content::BrowserContext* context);

  // BrowserContextKeyedAPI implementation.
  static BrowserContextKeyedAPIFactory<PowerAPI>* GetFactoryInstance();

  // Map from extension ID to the corresponding level for each extension
  // that has an outstanding request.
  using ExtensionLevelMap = std::map<std::string, api::power::Level>;
  const ExtensionLevelMap& extension_levels() const {
    return extension_levels_;
  }

  // Adds an extension lock at `level` for `extension_id`, replacing the
  // extension's existing lock, if any.
  void AddRequest(const ExtensionId& extension_id, api::power::Level level);

  // Removes an extension lock for an extension. Calling this for an
  // extension id without a lock will do nothing.
  void RemoveRequest(const ExtensionId& extension_id);

  // Replaces the functions that will be called to activate and cancel the wake
  // lock. Passing empty callbacks will revert to the default.
  void SetWakeLockFunctionsForTesting(
      ActivateWakeLockFunction activate_function,
      CancelWakeLockFunction cancel_function);

  // Overridden from extensions::ExtensionRegistryObserver.
  void OnExtensionUnloaded(content::BrowserContext* browser_context,
                           const Extension* extension,
                           UnloadedExtensionReason reason) override;

 private:
  friend class BrowserContextKeyedAPIFactory<PowerAPI>;

  // Updates wake lock status and `current_level_` after iterating
  // over `extension_levels_`.
  void UpdateWakeLock();

  // BrowserContextKeyedAPI implementation.
  static const char* service_name() { return "PowerAPI"; }
  static const bool kServiceRedirectedInIncognito = true;
  static const bool kServiceIsCreatedWithBrowserContext = false;
  void Shutdown() override;

  // Activates the wake lock with the type. `is_wake_lock_active_` is set true.
  void ActivateWakeLock(device::mojom::WakeLockType type);

  // Cancels the current wake lock if it is in active state.
  // `is_wake_lock_active_` is set false.
  void CancelWakeLock();

  // Returns the raw pointer of the bound `wake_lock_`. This function is used
  // only inside ActivateWakeLock() and CancelWakeLock() to perform the wake
  // lock mojo calls. The `wake_lock_` is bound and the wake lock mojo pipe is
  // created only once at the first time the GetWakeLock() is called.
  device::mojom::WakeLock* GetWakeLock();

  raw_ptr<content::BrowserContext> browser_context_;

  // Functions that should be called to activate and cancel the wake lock.
  // Tests can change this to record what would've been done instead of
  // actually changing the system power-saving settings.
  ActivateWakeLockFunction activate_wake_lock_function_;
  CancelWakeLockFunction cancel_wake_lock_function_;

  mojo::Remote<device::mojom::WakeLock> wake_lock_;
  bool is_wake_lock_active_;

  // Current level used by wake lock.
  api::power::Level current_level_;

  // Outstanding requests.
  ExtensionLevelMap extension_levels_;
};

}  // namespace extensions

#endif  // EXTENSIONS_BROWSER_API_POWER_POWER_API_H_