Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(327)

Side by Side Diff: Source/modules/wake_lock/WakeLock.cpp

Issue 399313003: Initial implementation of API WakeLock. Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Implementation of WakeLock API on JavaScript side. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/modules/wake_lock/WakeLock.h ('k') | Source/modules/wake_lock/WakeLock.idl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "modules/wake_lock/WakeLock.h"
7
8 #include "core/dom/Document.h"
9 #include "modules/wake_lock/WakeLockController.h"
10 #include "modules/wake_lock/WakeLockPromiseResolver.h"
11 #include "public/platform/WebWakeLockType.h"
12
13 namespace blink {
14
15 static WebWakeLockType stringToWakeLockType(const AtomicString& wakeLockString)
16 {
17 WebWakeLockType type = WebWakeLockGuard;
18 if (wakeLockString == "screen")
19 type = WebWakeLockScreen;
20 else if (wakeLockString == "system")
21 type = WebWakeLockSystem;
22 else
23 ASSERT_NOT_REACHED();
24 return type;
25 }
26
27 PassRefPtrWillBeRawPtr<WakeLock> WakeLock::create(ExecutionContext* context)
28 {
29 RefPtrWillBeRawPtr<WakeLock> wakeLock = adoptRefWillBeNoop(new WakeLock(cont ext));
30 wakeLock->suspendIfNeeded();
31 return wakeLock.release();
32 }
33
34 WakeLock::WakeLock(ExecutionContext* context)
35 : ActiveDOMObject(context)
36 {
37 ScriptWrappable::init(this);
38 }
39
40 WakeLock::~WakeLock()
41 {
42 }
43
44 Document* WakeLock::document() const
45 {
46 return toDocument(executionContext());
47 }
48
49 Page* WakeLock::page() const
50 {
51 return document() ? document()->page() : 0;
52 }
53
54 ScriptPromise WakeLock::request(ScriptState* scriptState, const AtomicString& st ringType)
55 {
56 WebWakeLockType type = stringToWakeLockType(stringType);
57 if (WakeLockController* controller = WakeLockController::from(page()))
58 return controller->requestWakeLock(scriptState, type);
59
60 WakeLockPromiseResolverOwnPtr resolver = WakeLockPromiseResolver::create(scr iptState, type);
61 resolver->reject();
62 return resolver->promise();
63 }
64
65 ScriptPromise WakeLock::release(ScriptState* scriptState, const AtomicString& st ringType)
66 {
67 WebWakeLockType type = stringToWakeLockType(stringType);
68 if (WakeLockController* controller = WakeLockController::from(page()))
69 return controller->requestWakeUnlock(scriptState, type);
70
71 WakeLockPromiseResolverOwnPtr resolver = WakeLockPromiseResolver::create(scr iptState, type);
72 resolver->reject();
73 return resolver->promise();
74 }
75
76 bool WakeLock::isHeld(const AtomicString& type)
77 {
78 if (WakeLockController* controller = WakeLockController::from(page()))
79 return controller->isHeld(stringToWakeLockType(type));
80
81 return false;
82 }
83
84 } // namespace blink
OLDNEW
« no previous file with comments | « Source/modules/wake_lock/WakeLock.h ('k') | Source/modules/wake_lock/WakeLock.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698