-
Notifications
You must be signed in to change notification settings - Fork 35
/
index.html
892 lines (889 loc) · 32.5 KB
/
index.html
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf-8'>
<title>
Screen Wake Lock API
</title>
<script src='https://www.w3.org/Tools/respec/respec-w3c' async class=
'remove'></script>
<script class='remove'>
var respecConfig = {
specStatus: "ED",
shortName: "screen-wake-lock",
editors: [{
name: "Kenneth Rohde Christiansen",
company: "Intel Corporation",
companyURL: "https://intel.com/",
w3cid: 57705
}, {
name: "Marcos Cáceres",
url: "https://marcosc.com",
company: "Apple Inc.",
companyURL: "https://www.apple.com/",
w3cid: "39125",
},],
formerEditors: [{
name: "Raphael Kubo da Costa",
company: "Intel Corporation",
w3cid: "95850",
}, {
name: "Ilya Bogdanovich",
url: "mailto:[email protected]",
company: "Yandex",
w3cid: "71741"
}, {
name: "Andrey Logvinov",
url: "mailto:[email protected]",
company: "Yandex",
w3cid: "75989"
},],
github: "https://github.com/w3c/screen-wake-lock/",
group: [ "das", "webapps" ],
testSuiteURI: "https://wpt.live/screen-wake-lock/",
implementationReportURI: "https://www.w3.org/wiki/DAS/Implementations",
otherLinks: [{
key: 'Quality Assurance Lead',
data: [{
value: 'Wanming Lin (Intel)',
href: 'https://github.com/Honry'
}]
}],
xref: {
profile: "web-platform",
specs: [
"permissions",
"permissions-policy",
]
}
};
</script>
</head>
<body>
<section id='abstract'>
<p>
This document specifies an API that allows web applications to request
a screen wake lock. Under the right conditions, and if allowed, the
screen wake lock prevents the system from turning off a device's
screen.
</p>
</section>
<section id='sotd'>
<p>
Implementors need to be aware that this specification is extremely
unstable. <strong>Implementors who are not taking part in the
discussions will find the specification changing out from under them in
incompatible ways.</strong> Vendors interested in implementing this
specification before it eventually reaches the Candidate Recommendation
phase should <a href=
"https://github.com/w3c/screen-wake-lock/issues">subscribe to the
repository on GitHub</a> and take part in the discussions.
</p>
</section>
<section class='informative'>
<h2>
Introduction
</h2>
<p>
Modern operating systems achieve longer battery life by implementing
aggressive power management, meaning that shortly after the lack of
user activity, a host device may lower the screen brightness, turn the
screen off and even let the CPU go into a deep power state, limiting
power usage as much as possible.
</p>
<p>
Though this is great for prolonged battery life, it can sometime hinder
some use cases such as scanning a barcode, reading an ebook, following
a recipe, presenting to an audience, and so on. See also
[[[wake-lock-use-cases]]].
</p>
<p>
A wake lock will generally prevent something from happening, but UAs
(and the underlying OS) may time limit a wake lock given the battery
status (wall power connected, discharging, low battery level), or even
disallow wake locks in the case a power saving mode is activated.
</p>
</section>
<section>
<h2>
Wake Locks
</h2>
<p>
This specification defines the following <dfn data-export="">wake lock
type</dfn>:
</p>
<ol>
<li>A <dfn data-export="">screen wake lock</dfn> prevents the screen
from turning off. Only visible documents can acquire the screen wake
lock.
</li>
</ol>
<p>
In the API, the [=wake lock types=] are represented by the
{{WakeLockType}} enum values.
</p>
<p class="note">
Other specifications might define different wake lock types.
</p>
</section>
<section>
<h3>
Policy control
</h3>
<p data-tests=
"wakelock-supported-by-permissions-policy.html, wakelock-enabled-on-self-origin-by-permissions-policy.https.sub.html, wakelock-enabled-by-permissions-policy-attribute-redirect-on-load.https.sub.html, wakelock-enabled-by-permissions-policy-attribute.https.sub.html, wakelock-enabled-by-permissions-policy.https.sub.html">
The Screen Wake Lock API defines a [=policy-controlled feature=]
identified by the string `"screen-wake-lock"`. Its [=policy-controlled
feature/default allowlist=] is `'self'`.
</p>
<aside class="note">
<p>
The [=policy-controlled feature/default allowlist=] of `'self'`
allows wake lock usage in same-origin nested frames but prevents
third-party content from using wake locks.
</p>
<p>
Third-party usage can be selectively enabled by adding
`allow="screen-wake-lock"` attribute to the frame container element:
</p>
<pre class="example html" title=
"Enabling screen wake lock on remote content">
<iframe src="https://third-party.com" allow="screen-wake-lock"/></iframe>
</pre>
<p>
Alternatively, the Screen Wake Lock API can be disabled completely by
specifying the permissions policy in a HTTP response header:
</p>
<pre class="example http" title="Permissions Policy header">
Permissions-Policy: screen-wake-lock=()
</pre>
<p>
See [[[PERMISSIONS-POLICY]]] for more details.
</p>
</aside>
</section>
<section>
<h3>
Permissions and user prompts
</h3>
<p>
The [[PERMISSIONS]] API provides a uniform way for websites to request
permissions from users and query which permissions they have.
</p>
<p>
A <a>user agent</a> can <dfn data-lt=
"deny wake lock|denies the wake lock">deny a wake lock</dfn> of a
particular <a>wake lock type</a> for a particular {{Document}} by any
implementation-specific reason, such as platform setting or user
preference.
</p>
<p>
It is RECOMMENDED that a user agent show some form of unobtrusive
notification that informs the user when a wake lock is active, as well
as provides the user with the means to [=screen wake lock permission
revocation algorithm|block=] the ongoing operation, or simply dismiss
the notification.
</p>
<section>
<h2>
The `"screen-wake-lock"` powerful feature
</h2>
<p data-tests="wakelockpermissiondescriptor.https.html">
The `"screen-wake-lock"` <a>powerful feature</a> enables the
capability defined by this specification.
</p>
</section>
<section>
<h2>
Permission algorithms
</h2>
<p>
The `"screen-wake-lock"` <a>powerful feature</a> defines a [=powerful
feature/permission revocation algorithm=]. To invoke the <dfn>Screen
Wake Lock permission revocation algorithm</dfn>, run these steps:
</p>
<ol class="algorithm">
<li>Let |document:Document| be the [=current global object=]'s
[=associated Document=].
</li>
<li>Let |lockList| be
|document|.{{Document/[[ActiveLocks]]}}["`screen`"].
</li>
<li>[=list/For each=] |lock:WakeLockSentinel| in |lockList|:
<ol>
<li>Run <a>release a wake lock</a> with |document|, |lock|, and
{{WakeLockType/"screen"}}.
</li>
</ol>
</li>
</ol>
</section>
</section>
<section>
<h3>
Concepts
</h3>
<p>
The <a>task source</a> for the <a>tasks</a> mentioned in this
specification is the <dfn>screen wake lock task source</dfn>.
</p>
<p>
The term <dfn>platform wake lock</dfn> refers to platform interfaces
with which the user agent interacts to query state and acquire and
release a wake lock.
</p>
<p>
A <a>platform wake lock</a> can be defined by the underlying platform
(e.g. in a native wake lock framework) or by the user agent, if it has
direct hardware control.
</p>
</section>
<section data-dfn-for="Document">
<h2>
Extensions to the `Document` interface
</h2>
<section>
<h3>
Internal slots
</h3>
<table class="simple">
<thead>
<tr>
<th>
Internal slot
</th>
<th>
Initial value
</th>
<th>
Description
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<dfn>[[\ActiveLocks]]</dfn>
</td>
<td>
An <a>ordered map</a> mapping <a>wake lock types</a> to empty
<a>lists</a>.
</td>
<td>
An <a>ordered map</a> of <a>wake lock types</a> to a
<a>list</a> of {{WakeLockSentinel}} objects associated with
this {{Document}}.
</td>
</tr>
</tbody>
</table>
</section>
</section>
<section data-dfn-for="Navigator">
<h2>
Extensions to the `Navigator` interface
</h2>
<pre class="idl">
[SecureContext]
partial interface Navigator {
[SameObject] readonly attribute WakeLock wakeLock;
};
</pre>
</section>
<section data-dfn-for="WakeLock">
<h2>
The <dfn>WakeLock</dfn> interface
</h2>
<p data-tests="wakelock-insecure-context.any.html">
The {{WakeLock}} interface allows a document to acquire a [=screen wake
lock=].
</p>
<pre class="idl">
[SecureContext, Exposed=(Window)]
interface WakeLock {
Promise<WakeLockSentinel> request(optional WakeLockType type = "screen");
};
</pre>
<section>
<h3>
The <dfn>request()</dfn> method
</h3>
<p data-tests="wakelock-type.https.any.html">
The <code>request(|type:WakeLockType|)</code> method steps are:
</p>
<ol class="algorithm">
<li>Let |document:Document| be [=this=]'s [=relevant global
object=]'s [=associated Document=].
</li>
<li data-tests="wakelock-active-document.https.window.html">If
|document| is not [=Document/fully active=], return [=a promise
rejected with=] with a {{"NotAllowedError"}} {{DOMException}}.
</li>
<li data-tests=
"wakelock-disabled-by-permissions-policy.https.sub.html">If
|document| is not [=allowed to use=] the [=policy-controlled
feature=] named "`screen-wake-lock`", return [=a promise rejected
with=] a {{"NotAllowedError"}} {{DOMException}}.
</li>
<li data-tests="wakelock-screen-type-on-worker.https.worker.html">If
the <a>user agent</a> <a>denies the wake lock</a> of this |type| for
|document|, return [=a promise rejected with=] a
{{"NotAllowedError"}} {{DOMException}}.
</li>
<li data-tests="wakelock-document-hidden-manual.https.html">If
|document|'s [=Document/visibility state=] is "`hidden`", return [=a
promise rejected with=] {{"NotAllowedError"}} {{DOMException}}.
</li>
<li>Let |promise:Promise| be [=a new promise=].
</li>
<li>Run the following steps <a>in parallel</a>:
<ol>
<li>Let |state:PermissionState| be the result of <a>requesting
permission to use</a> "`screen-wake-lock`".
</li>
<li data-tests="wakelock-request-denied.https.html">If |state| is
{{PermissionState/"denied"}}, then:
<ol>
<li>
<a>Queue a global task</a> on the <a>screen wake lock task
source</a> given |document|'s <a>relevant global object</a>
to reject |promise| with a {{"NotAllowedError"}}
{{DOMException}}.
</li>
<li>Abort these steps.
</li>
</ol>
</li>
<li>
<a>Queue a global task</a> on the <a>screen wake lock task
source</a> given |document|'s <a>relevant global object</a> to
run these steps:
<ol>
<li>If |document| is not [=Document/fully active=], then:
<ol>
<li>Reject |promise| with a {{"NotAllowedError"}}
{{DOMException}}.
</li>
<li>Abort these steps.
</li>
</ol>
</li>
<li>If |document|'s [=Document/visibility state=] is
"`hidden`", then:
<ol>
<li>Reject |promise| with a {{"NotAllowedError"}}
{{DOMException}}.
</li>
<li>Abort these steps.
</li>
</ol>
</li>
<li>If |document|.{{Document/[[ActiveLocks]]}}["`screen`"]
[=list/is empty=], then invoke the following steps <a>in
parallel</a>:
<ol>
<li>Invoke <a>acquire a wake lock</a> with
{{WakeLockType/"screen"}}.
<aside class="note">
The <a>acquire a wake lock</a> algorithm may
ultimately be unable to acquire a lock from the
operating system, but this is indistinguishable from
a successful lock acquisition to avoid user
fingerprinting (failure to acquire a lock can
indicate low battery levels, for example).
</aside>
</li>
</ol>
</li>
<li>Let |lock:WakeLockSentinel| be a new {{WakeLockSentinel}}
object with its {{WakeLockSentinel/type}} attribute set to
|type|.
</li>
<li>[=List/Append=] |lock| to
|document|.{{Document/[[ActiveLocks]]}}["`screen`"].
</li>
<li>Resolve |promise| with |lock|.
</li>
</ol>
</li>
</ol>
</li>
<li>Return |promise|.
</li>
</ol>
</section>
</section>
<section data-dfn-for="WakeLockSentinel">
<h2>
The <dfn>WakeLockSentinel</dfn> interface
</h2>
<pre class="idl">
[SecureContext, Exposed=(Window)]
interface WakeLockSentinel : EventTarget {
readonly attribute boolean released;
readonly attribute WakeLockType type;
Promise<undefined> release();
attribute EventHandler onrelease;
};
</pre>
<p>
A {{WakeLockSentinel}} object provides a handle to a <a>platform wake
lock</a>, and it holds on to it until it is either manually released or
until the underlying <a>platform wake lock</a> is released. Its
existence keeps a <a>platform wake lock</a> for a given <a>wake lock
type</a> active, and releasing all {{WakeLockSentinel}} instances of a
given <a>wake lock type</a> will cause the underlying <a>platform wake
lock</a> to be released.
</p>
<aside class="note">
See <a>auto-releasing wake locks</a>, <a>handling document loss of full
activity</a> and <a>handling document loss of visibility</a> for
circumstances under which a given wake lock may be released by the
<a>user agent</a>.
</aside>
<section>
<h3>
Internal slots
</h3>
<p>
{{WakeLockSentinel}} instances are created with the following
<a data-cite=
"ECMASCRIPT#sec-object-internal-methods-and-internal-slots">internal
slots</a>:
</p>
<table class="simple">
<thead>
<tr>
<th>
Internal slot
</th>
<th>
Initial value
</th>
<th>
Description (non-normative)
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<dfn>[[\Released]]</dfn>
</td>
<td>
`false`
</td>
<td>
Whether the given {{WakeLockSentinel}} has been released.
</td>
</tr>
</tbody>
</table>
</section>
<section>
<h3>
The <dfn>released</dfn> attribute
</h3>
<p data-tests="wakelock-released.https.html">
The {{WakeLockSentinel/released}} getter steps are to return
[=this=].{{WakeLockSentinel/[[Released]]}}.
</p>
<aside class="note">
Once a {{WakeLockSentinel}} is released,
{{WakeLockSentinel/released}} becomes `true`, and the value never
changes again.
</aside>
</section>
<section>
<h3>
The <dfn>type</dfn> attribute
</h3>
<p>
The {{WakeLockSentinel/type}} getter steps are to return [=this=]'s
<a>wake lock type</a>.
</p>
</section>
<section>
<h3>
The <dfn>release()</dfn> method
</h3>
<p data-tests="wakelock-onrelease.https.html">
The {{WakeLockSentinel/release()}} method steps are:
</p>
<ol class="algorithm">
<li>If <a>this</a>'s {{WakeLockSentinel/[[Released]]}} is `false`,
then run <a>release a wake lock</a> with |lock:WakeLockSentinel| set
to <a>this</a> and |type:WakeLockType| set to the value of
<a>this</a>'s {{WakeLockSentinel/type}} attribute.
</li>
<li>Return <a>a promise resolved with</a> `undefined`.
</li>
</ol>
</section>
<section>
<h3 data-tests="wakelock-onrelease.https.html">
The <dfn>onrelease</dfn> attribute
</h3>
<p>
The {{WakeLockSentinel/onrelease}} attribute is an <a>event handler
IDL attribute</a> for the "<code>onrelease</code>" <a>event
handler</a>, whose <a>event handler event type</a> is
"<dfn><code>release</code></dfn>".
</p>
<p>
It is used to notify scripts that a given {{WakeLockSentinel}}
object's handle has been released, either due to the
{{WakeLockSentinel/release()}} method being called or because the
wake lock was released by the <a>user agent</a>.
</p>
<aside class="note">
A {{WakeLockSentinel}} object's handle being released does not
necessarily mean the <a>platform wake lock</a> for a given <a>wake
lock type</a> was released. That depends on the <a>platform wake
lock</a>'s {{Document/[[ActiveLocks]]}} internal slot. See <a>release
a wake lock</a>.
</aside>
<aside class="note">
[[[#onrelease-example]]] contains an example of how to use the
{{WakeLockSentinel/onrelease}} event handler.
</aside>
</section>
<section>
<h3>
Garbage collection
</h3>
<p>
While a {{WakeLockSentinel}} object has one or more event listeners
registered for "<a><code>release</code></a>", and the
{{WakeLockSentinel}} object hasn't already been released, there MUST
be a strong reference from the {{Window}} object that the
{{WakeLockSentinel}} object's constructor was invoked from to the
{{WakeLockSentinel}} object itself.
</p>
<p>
While there is a task queued by an {{WakeLockSentinel}} object on the
<a>screen wake lock task source</a>, there MUST be a strong reference
from the {{Window}} object that the {{WakeLockSentinel}} object's
constructor was invoked from to that {{WakeLockSentinel}} object.
</p>
</section>
</section>
<section data-dfn-for="WakeLockType">
<h2>
The <dfn>WakeLockType</dfn> enum
</h2>
<p>
For the purpose of wake lock type description, this specification
defines the following enumeration to represent [=wake lock types=]:
</p>
<pre class="idl">
enum WakeLockType { "screen" };
</pre>
<dl>
<dt>
<dfn>screen</dfn>
</dt>
<dd>
<a>Screen wake lock</a> type.
</dd>
</dl>
</section>
<section>
<h2>
Managing Wake Locks
</h2>
<p>
This section applies to each <a>wake lock type</a> equally and
independently, unless a particular <a>wake lock type</a> is explicitly
mentioned.
</p>
<p>
The <a>user agent</a> <dfn data-lt=
"acquire wake lock|acquire the wake lock|acquired">acquires the wake
lock</dfn> by requesting the underlying operating system to apply the
lock. A possible return value of the request to the underlying
operating system is not checked. In other words, <a>user agents</a>
MUST treat wake lock acquisition as <strong>advisory-only</strong>.
</p>
<p>
Conversely, the <a>user agent</a> <dfn data-lt=
"release wake lock|release the wake lock">releases the wake lock</dfn>
by requesting the underlying operating system to no longer apply the
wake lock. The lock is considered released only when the request to the
operating system succeeds.
</p>
<p>
The wake lock is <dfn data-lt=
"applicable wake lock|applicability">applicable</dfn> if the state of
the operating system permits application of the lock (e.g. there is
sufficient battery charge).
</p>
<p>
The <a>screen wake lock</a> MUST NOT be <a>applicable</a> after the
screen is manually switched off by the user until it is switched on
again.
</p>
<aside class="note">
Whether the wake lock is applicable is a transient condition, e.g. when
the battery charge is low but then the battery is recharged. So like
the visibility requirement, this is part of automatic wake lock
management and not part of the decision process whether to allow or
deny the wake lock.
</aside>
<section>
<h3>
<dfn>Auto-releasing wake locks</dfn>
</h3>
<p>
A user agent may <a>release a wake lock</a> at any time. For example,
when:
</p>
<ul>
<li>It detects abnormal operation: such as infinite loops and tasks
exceeding imposed time limits (if any).
</li>
<li>Battery is considered low and discharging.
</li>
<li>The user turns on some kind of device power conservation mode.
</li>
<li>The operating system is configured to dim or turn off the screen
based on the user presence state.
</li>
</ul>
</section>
<section>
<h3>
<dfn>Handling document loss of full activity</dfn>
</h3>
<p data-tests="wakelock-active-document.https.window.html">
When a {{Document}} |document:Document| becomes no longer
[=Document/fully active=], the user agent must run these steps:
</p>
<ol class="algorithm">
<li>[=list/For each=] |lock:WakeLockSentinel| in
|document|.{{Document/[[ActiveLocks]]}}["`screen`"]:
<ol>
<li>Run <a>release a wake lock</a> with |document|, |lock|, and
{{WakeLockType/"screen"}}.
</li>
</ol>
</li>
</ol>
</section>
<section>
<h3>
<dfn>Handling document loss of visibility</dfn>
</h3>
<p data-tests="wakelock-document-hidden-manual.https.html">
This specification defines the following [=page visibility change
steps=] with [=Document/visibility state=] |state| and
|document:Document|:
</p>
<ol class="algorithm">
<li>If |state| is not "`hidden`", abort these steps.
</li>
<li>[=list/For each=] |lock:WakeLockSentinel| in
|document|.{{Document/[[ActiveLocks]]}}["`screen`"]:
<ol>
<li>Run <a>release a wake lock</a> with |document|, |lock|, and
{{WakeLockType/"screen"}}.
</li>
</ol>
</li>
</ol>
</section>
<section>
<h3>
Acquire wake lock algorithm
</h3>
<p>
To <dfn>acquire a wake lock</dfn> for a given |type:WakeLockType|,
run these steps:
</p>
<ol class="algorithm">
<li>If the wake lock for type |type| is not <a>applicable</a>, abort
these steps.
</li>
<li>Ask the underlying operating system to <a>acquire the wake
lock</a> of type |type|.
</li>
</ol>
</section>
<section>
<h3>
Release wake lock algorithm
</h3>
<p>
To <dfn>release a wake lock</dfn> for a given |document:Document|,
|lock:WakeLockSentinel|, and |type:WakeLockType|, run these steps:
</p>
<ol class="algorithm">
<li>If |document|.{{Document/[[ActiveLocks]]}}[|type|] does not
contain |lock|, abort these steps.
</li>
<li>Remove |lock| from
|document|.{{Document/[[ActiveLocks]]}}[|type|].
</li>
<li>If |document|.{{Document/[[ActiveLocks]]}}[|type|] [=list/is
empty=], then run the following steps <a>in parallel</a>:
<ol>
<li>Ask the underlying operating system to <a>release the wake
lock</a> of type |type| and let |success:boolean| be `true` if
the operation succeeded, or else `false`.
</li>
<li>If |success| is `true` and |type| is `"screen"` run the
following:
<ol>
<li>Reset the platform-specific inactivity timer after which
the screen is actually turned off.
</li>
</ol>
<aside class="note">
Resetting the inactivity timer prevents the screen from going
blank immediately after the wake lock is released.
</aside>
</li>
</ol>
</li>
<li>Set |lock|'s {{WakeLockSentinel/[[Released]]}} to `true`.
</li>
<li>
<a>Fire an event</a> named "<a><code>release</code></a>" at |lock|.
</li>
</ol>
</section>
</section>
<section>
<h2>
Security and privacy considerations
</h2>
<p>
Screen wake locks can cause various device components - particularly
the display - to operate at higher power levels than they otherwise
would. This can lead to undesirable effects, such as preventing the
device from automatically locking itself and faster battery depletion.
Faster battery depletion is of particular concern for mobile devices,
which often don't have a stationary power source readily available.
Complete battery depletion at an unexpected time can lead to inability
of the user to make or receive calls and use network services,
including the emergency call service.
</p>
<p>
Implementations MAY ignore requests for screen wake lock if, for
example, the battery capacity is low, or the user has put their device
in a power-saving mode.
</p>
<p>
It is RECOMMENDED that a user agent provide some UI or indicator that
allows the user to know when a screen wake lock is active. Providing
such a UI could help end users to identify if a particular web
application is having a negative energy impact on the device, and allow
them to take action if so desired.
</p>
</section>
<section id="examples" class="informative">
<h2>
Examples
</h2>
<pre class="example js" title="Acquires and releases a screen wake lock">
function tryKeepScreenAlive(minutes) {
navigator.wakeLock.request("screen").then(lock => {
setTimeout(() => lock.release(), minutes * 60 * 1000);
});
}
tryKeepScreenAlive(10);
</pre>
<p>
This example allows the user to request a screen wake lock by clicking
on a checkbox, but updates the checkbox checked state in case the wake
lock state changes:
</p>
<pre class="example js" id="onrelease-example">
const checkbox = document.createElement("input");
checkbox.setAttribute("type", "checkbox");
document.body.appendChild(checkbox);
const sentinel = await navigator.wakeLock.request("screen");
checkbox.checked = !sentinel.released;
sentinel.onrelease = () => checkbox.checked = !sentinel.released;
</pre>
<p>
In this example, two different wake lock requests are created and
released independently:
</p>
<pre class="example js">
let lock1 = await navigator.wakeLock.request("screen");
let lock2 = await navigator.wakeLock.request("screen");
lock1.release();
lock2.release();
</pre>
</section>
<section id="conformance">
<p>
This specification defines conformance criteria for a single product: a
<dfn>user agent</dfn> that implements the interfaces that it contains.
</p>
</section>
<section class="appendix informative" id="acknowledgments">
<h2>
Acknowledgments
</h2>
<p>
We would like to offer our sincere thanks to Mounir Lamouri, Sergey
Konstantinov, Matvey Larionov, Dominique Hazael-Massieux, Domenic
Denicola, Thomas Steiner, Anne van Kesteren for their contributions to
this work.
</p>
</section>
<section class="appendix informative" id="changes">
<h2>
Changes
</h2>
<p>
This section documents the changes since previous publications.
</p>
<section id="changes-20171214">
<h2>
Changes since the 14 December 2017 CR
</h2>
<ul>
<li>Convert the document to purely screen wake lock, and move system
lock to a new specification.
</li>
<li>Rewrite user-visible API.
</li>
<li>Add an <a>if aborted</a> step to <code>WakeLock.request()</code>
to deal with hidden documents.
</li>
<li>Add an IDL Index.
</li>
<li>Remove duplicate normative statements.
</li>
<li>Modernize the examples.
</li>
<li>Use internal slots instead of prose.
</li>
<li>Add info on when the user agent may <a>release a wake lock</a>.
</li>
<li>Handle document visibility.
</li>
<li>Make `ScreenWakeLock` constructable.
</li>
<li>Integrate optional permission prompting.
</li>
<li>Handle loss of full activity, as well as running in workers.
</li>
<li>Rewrite the introduction section.
</li>
<li>Rename Feature Policy to Permissions Policy.
</li>
<li>Add <code>WakeLockSentinel.released</code>.
</li>
<li>Define a task source for tasks in this specification.
</li>
</ul>
</section>
</section>
<section id="index" class="appendix"></section>
<section id="idl-index" class="appendix"></section>
</body>
</html>