summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/wake_lock/navigator_wake_lock.cc
blob: 43671330e6c6b5fc7cc6b046cf83bbeb1b9c046c (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 2018 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 "third_party/blink/renderer/modules/wake_lock/navigator_wake_lock.h"

#include "third_party/blink/renderer/core/dom/document.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.h"

namespace blink {

// static
const char NavigatorWakeLock::kSupplementName[] = "NavigatorWakeLock";

NavigatorWakeLock::NavigatorWakeLock(Navigator& navigator)
    : Supplement<Navigator>(navigator) {}

// static
ScriptPromise NavigatorWakeLock::getWakeLock(ScriptState* script_state,
                                             Navigator& navigator,
                                             String lock_type) {
  return NavigatorWakeLock::From(navigator).getWakeLock(script_state,
                                                        lock_type);
}

ScriptPromise NavigatorWakeLock::getWakeLock(ScriptState* script_state,
                                             String lock_type) {
  // TODO(crbug.com/873030): Handle 'system' Wake Lock
  if (lock_type == "screen") {
    if (!wake_lock_screen_)
      wake_lock_screen_ = WakeLock::CreateScreenWakeLock(script_state);
    return wake_lock_screen_->GetPromise(script_state);
  }

  return ScriptPromise::RejectWithDOMException(
      script_state, DOMException::Create(DOMExceptionCode::kNotSupportedError,
                                         "WakeLockType Not Supported"));
}

NavigatorWakeLock& NavigatorWakeLock::From(Navigator& navigator) {
  NavigatorWakeLock* supplement =
      Supplement<Navigator>::From<NavigatorWakeLock>(navigator);
  if (!supplement) {
    supplement = new NavigatorWakeLock(navigator);
    ProvideTo(navigator, supplement);
  }
  return *supplement;
}

void NavigatorWakeLock::Trace(blink::Visitor* visitor) {
  visitor->Trace(wake_lock_screen_);
  Supplement<Navigator>::Trace(visitor);
}

}  // namespace blink