File: wake_lock_manager.cc

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 (93 lines) | stat: -rw-r--r-- 3,736 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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/wake_lock/wake_lock_manager.h"

#include "base/check_op.h"
#include "third_party/blink/public/mojom/wake_lock/wake_lock.mojom-blink.h"
#include "third_party/blink/public/platform/browser_interface_broker_proxy.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/modules/wake_lock/wake_lock_sentinel.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"

namespace blink {

WakeLockManager::WakeLockManager(ExecutionContext* execution_context,
                                 V8WakeLockType::Enum type)
    : wake_lock_(execution_context),
      wake_lock_type_(type),
      execution_context_(execution_context) {
  DCHECK_NE(execution_context, nullptr);
}

void WakeLockManager::AcquireWakeLock(
    ScriptPromiseResolver<WakeLockSentinel>* resolver) {
  // https://w3c.github.io/screen-wake-lock/#the-request-method
  if (!wake_lock_.is_bound()) {
    // 8.3.2. If document.[[ActiveLocks]]["screen"] is empty, then invoke the
    //        following steps in parallel:
    // 8.3.2.1. Invoke acquire a wake lock with "screen".
    mojo::Remote<mojom::blink::WakeLockService> wake_lock_service;
    execution_context_->GetBrowserInterfaceBroker().GetInterface(
        wake_lock_service.BindNewPipeAndPassReceiver());

    wake_lock_service->GetWakeLock(
        ToMojomWakeLockType(wake_lock_type_),
        device::mojom::blink::WakeLockReason::kOther, "Blink Wake Lock",
        wake_lock_.BindNewPipeAndPassReceiver(
            execution_context_->GetTaskRunner(TaskType::kWakeLock)));
    wake_lock_.set_disconnect_handler(WTF::BindOnce(
        &WakeLockManager::OnWakeLockConnectionError, WrapWeakPersistent(this)));
    wake_lock_->RequestWakeLock();
  }
  // 8.3.3. Let lock be a new WakeLockSentinel object with its type attribute
  //        set to type.
  // 8.3.4. Append lock to document.[[ActiveLocks]]["screen"].
  // 8.3.5. Resolve promise with lock.
  auto* sentinel = MakeGarbageCollected<WakeLockSentinel>(
      resolver->GetScriptState(), wake_lock_type_, this);
  wake_lock_sentinels_.insert(sentinel);
  resolver->Resolve(sentinel);
}

void WakeLockManager::UnregisterSentinel(WakeLockSentinel* sentinel) {
  // https://w3c.github.io/screen-wake-lock/#release-wake-lock-algorithm
  // 1. If document.[[ActiveLocks]][type] does not contain lock, abort these
  //    steps.
  auto iterator = wake_lock_sentinels_.find(sentinel);
  CHECK(iterator != wake_lock_sentinels_.end());

  // 2. Remove lock from document.[[ActiveLocks]][type].
  wake_lock_sentinels_.erase(iterator);

  // 3. If document.[[ActiveLocks]][type] is empty, then run the following steps
  //    in parallel:
  // 3.1. Ask the underlying operating system to release the wake lock of type
  //      type and let success be true if the operation succeeded, or else
  //      false.
  if (wake_lock_sentinels_.empty() && wake_lock_.is_bound()) {
    wake_lock_->CancelWakeLock();
    wake_lock_.reset();
  }
}

void WakeLockManager::ClearWakeLocks() {
  while (!wake_lock_sentinels_.empty())
    (*wake_lock_sentinels_.begin())->DoRelease();
}

void WakeLockManager::OnWakeLockConnectionError() {
  wake_lock_.reset();
  ClearWakeLocks();
}

void WakeLockManager::Trace(Visitor* visitor) const {
  visitor->Trace(execution_context_);
  visitor->Trace(wake_lock_sentinels_);
  visitor->Trace(wake_lock_);
}

}  // namespace blink