/
index.html
841 lines (838 loc) · 31.8 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
<!DOCTYPE html>
<html>
<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: "Mozilla",
w3cid: "39125"
}, {
name: "Raphael Kubo da Costa",
company: "Intel Corporation",
w3cid: "95850",
},],
formerEditors: [{
name: "Ilya Bogdanovich",
url: "mailto:bogdanovichiy@yandex-team.ru",
company: "Yandex",
w3cid: "71741"
}, {
name: "Andrey Logvinov",
url: "mailto:alogvinov@yandex-team.ru",
company: "Yandex",
w3cid: "75989"
},],
github: "https://github.com/w3c/screen-wake-lock/",
group: "das",
testSuiteURI: "https://w3c-test.org/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: "web-platform"
};
</script>
</head>
<body data-cite="PERMISSIONS-POLICY PERMISSIONS">
<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-feature-policy.html, wakelock-enabled-on-self-origin-by-feature-policy.https.sub.html, wakelock-enabled-by-feature-policy-attribute-redirect-on-load.https.sub.html, wakelock-enabled-by-feature-policy-attribute.https.sub.html, wakelock-enabled-by-feature-policy.https.sub.html">
The Screen Wake Lock API defines a [=policy-controlled feature=]
identified by the string `"screen-wake-lock"`. Its [=default
allowlist=] is `["self"]`.
</p>
<aside class="note">
<p>
The <a>default allowlist</a> 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="Feature Policy over HTTP">
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 <a>block</a> the ongoing
operation, or simply dismiss the notification.
</p>
<section>
<h2>
The `"screen-wake-lock"` powerful feature
</h2>
<p>
The `"screen-wake-lock"` <a>powerful feature</a> enables the
capability defined by this specification.
</p>
</section>
<section>
<h2>
Permission algorithms
</h2>
<p data-tests="wakelockpermissiondescriptor.https.html">
To <dfn data-lt="block">block a permission</dfn>, run these steps:
</p>
<ol class="algorithm">
<li>Let |document:Document| be the [=environment settings object /
responsible document=] of the <a>current settings object</a>.
</li>
<li>Let |descriptor:PermissionDescriptor| be an instance of
{{PermissionDescriptor}}.
</li>
<li>Set |descriptor|'s <a>permission state</a> to
{{PermissionState/"denied"}}.
</li>
<li>Let |name| be |descriptor|'s {{PermissionDescriptor/name}}
member.
</li>
<li>Let |record| be the <a>platform wake lock</a>'s <a>state
record</a> associated with |document| and |name|.
</li>
<li>[=list/For each=] |lock:WakeLockSentinel| in
|record|.{{[[ActiveLocks]]}}:
<ol>
<li>Run <a>release a wake lock</a> with |lock| and |name|.
</li>
</ol>
</li>
</ol>
<p>
To <dfn>obtain permission</dfn> for <a>wake lock type</a> |name|, run
these steps <a>in parallel</a>. This async algorithm returns either
{{PermissionState/"granted"}} or {{PermissionState/"denied"}}.
</p>
<ol class="algorithm">
<li>Let |permissionDesc:PermissionDescriptor| be a newly created
{{PermissionDescriptor}}.
</li>
<li>Set |permissionDesc|'s {{PermissionDescriptor/name}} member to
"`screen-wake-lock`".
</li>
<li>Let |resultPromise:Promise| be the result of running <a>query a
permission</a> with |permissionDesc|.
</li>
<li>Await |resultPromise| to settle.
</li>
<li>If |resultPromise| rejects, return {{PermissionState/"denied"}}.
</li>
<li>Otherwise, let |status:PermissionStatus| be the result of
|resultPromise|.
</li>
<li>Let |state:PermissionState| be the value of
|status|.{{PermissionStatus/state}}.
</li>
<li>If |state| is not {{PermissionState/"prompt"}}, return |state|.
</li>
<li>If the <a>current global object</a> does not have [=transient
activation=], return {{PermissionState/"denied"}}.
</li>
<li>Otherwise, return the result of <a>requesting permission to
use</a> with |permissionDesc|.
</li>
</ol>
</section>
</section>
<section>
<h3>
Concepts and state record
</h3>
<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>
<p>
Each <a>platform wake lock</a> (one per <a>wake lock type</a>) has an
associated <dfn>state record</dfn> per [=environment settings object /
responsible document=] 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 (<em>non-normative</em>)
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<dfn>[[\ActiveLocks]]</dfn>
</td>
<td>
The empty <a>list</a>.
</td>
<td>
A <a>list</a> of {{WakeLockSentinel}} objects, representing
active wake locks associated with the [=environment settings
object / responsible document=].
</td>
</tr>
</tbody>
</table>
</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(WakeLockType type);
};
</pre>
<section>
<h3>
The <dfn>request()</dfn> method
</h3>
<p data-tests="wakelock-type.https.any.html">
The {{WakeLock/request()}} method, when invoked, MUST run the
following steps. The method takes one argument, the {{WakeLockType}}
|type:WakeLockType|:
</p>
<ol class="algorithm">
<li>Let |document:Document| be the [=environment settings object /
responsible document=] of the <a>current settings object</a>.
<ol>
<li data-tests=
"wakelock-disabled-by-feature-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=]
{{"NotAllowedError"}} {{DOMException}}.
</li>
</ol>
</li>
<li>If the |document|'s [=Document/browsing context=] is `null`,
return [=a promise rejected with=] {{"NotAllowedError"}}
{{DOMException}}.
</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-document-hidden-manual.https.html">If
|document| is <a>hidden</a>, return [=a promise rejected with=]
{{"NotAllowedError"}} {{DOMException}}.
</li>
<li>Let |promise:Promise| be [=a new promise=].
</li>
<li>Return |promise| and run the following steps <a>in parallel</a>:
<ol>
<li>
<a>Abort when</a> |document| is <a>hidden</a>.
</li>
<li>Let |state:PermissionState| be the result of awaiting
<a>obtain permission</a> steps with "`screen-wake-lock`":
<ol>
<li data-tests="wakelock-request-denied.https.html">If
|state| is {{PermissionState/"denied"}}, then reject
|promise| with a {{"NotAllowedError"}} {{DOMException}}, and
abort these steps.
</li>
</ol>
</li>
<li>Let |lock:WakeLockSentinel| be a new {{WakeLockSentinel}}
object with its {{WakeLockSentinel/type}} attribute set to
|type|.
</li>
<li>Let |success:boolean| be the result of awaiting <a>acquire a
wake lock</a> with |lock| and {{WakeLockType/"screen"}}:
<ol>
<li>If |success| is `false` then reject |promise| with a
{{"NotAllowedError"}} {{DOMException}}, and abort these
steps.
</li>
</ol>
</li>
<li>Resolve |promise| with |lock|.
</li>
</ol>
</li>
<li>
<a>If aborted</a>, reject |promise| with a {{"NotAllowedError"}}
{{DOMException}}.
</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 WakeLockType type;
Promise<void> 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>
<p>
The <dfn>type</dfn> attribute corresponds to the {{WakeLockSentinel}}'s
<a>wake lock type</a>.
</p>
<section>
<h3>
The <dfn>release()</dfn> method
</h3>
<p data-tests="wakelock-onrelease.https.html">
The {{WakeLockSentinel/release()}} method, when invoked, MUST run the
following steps:
</p>
<ol class="algorithm">
<li>Let |promise:Promise| be <a>a new promise</a>.
</li>
<li>Run the following steps <a>in parallel</a>:
<ol>
<li>Run <a>release wake lock</a> with |lock:WakeLockSentinel| set
to this object and |type:WakeLockType| set to the value of this
object's {{WakeLockSentinel/type}} attribute.
</li>
<li>Resolve |promise|.
</li>
</ol>
</li>
<li>Return |promise|.
</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</a> whose corresponding <a>event handler event type</a> is
<code>release</code>.
</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 {{[[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>
<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. The lock is considered acquired only when the request to the
operating system succeeds.
</p>
<p>
Conversely, the <a>user agent</a> <dfn data-lt=
"release 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 it:
</p>
<ul>
<li>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>
</ul>
</section>
<section>
<h3>
<dfn>Handling document loss of full activity</dfn>
</h3>
<p data-tests="wakelock-active-document.https.window.html">
When the user agent determines that a [=environment settings object /
responsible document=] of the <a>current settings object</a> is no
longer [=Document/fully active=], it must run these steps:
</p>
<ol class="algorithm">
<li>Let |document:Document| be the [=environment settings object /
responsible document=] of the <a>current settings object</a>.
</li>
<li>Let |screenRecord| be the <a>platform wake lock</a>'s <a>state
record</a> associated with |document| and <a>wake lock type</a>
[=screen wake lock=].
</li>
<li>[=list/For each=] |lock:WakeLockSentinel| in
|screenRecord|.{{[[ActiveLocks]]}}:
<ol>
<li>Run <a>release a wake lock</a> with |lock| and
{{WakeLockType/"screen"}}.
</li>
</ol>
</li>
</ol>
<aside class="note">
Only documents presented to the user, thus with an associated
[=Document/browsing context=] can be <a>active documents</a>, so the
above steps are only relevant for these.
</aside>
</section>
<section>
<h3>
<dfn>Handling document loss of visibility</dfn>
</h3>
<p data-tests="wakelock-document-hidden-manual.https.html">
When the user agent determines that the <a>visibility state</a> of
the [=environment settings object / responsible document=] of the
<a>current settings object</a> changes, it must run these steps:
</p>
<ol class="algorithm">
<li>Let |document:Document| be the [=environment settings object /
responsible document=] of the <a>current settings object</a>.
</li>
<li>If |document|'s <a>visibility state</a> is `"visible"`, abort
these steps.
</li>
<li>Let |screenRecord| be the <a>platform wake lock</a>'s <a>state
record</a> associated with <a>wake lock type</a> [=screen wake
lock=].
</li>
<li>[=list/For each=] |lock:WakeLockSentinel| in
|screenRecord|.{{[[ActiveLocks]]}}:
<ol>
<li>Run <a>release a wake lock</a> with |lock| and
{{WakeLockType/"screen"}}.
</li>
</ol>
</li>
</ol>
<aside class="note">
The <a>visibility state</a> is only exposed on the {{Window}} object.
</aside>
</section>
<section>
<h3>
Acquire wake lock algorithm
</h3>
<p>
To <dfn>acquire a wake lock</dfn> for a given |lock:WakeLockSentinel|
and |type:WakeLockType|, run these steps <a>in parallel</a>:
</p>
<ol class="algorithm">
<li>If the wake lock for type |type| is not <a>applicable</a>, return
`false`.
</li>
<li>Set |active:boolean| to `true` if the <a>platform wake lock</a>
has an active wake lock for |type|.
</li>
<li>Otherwise, ask the underlying operating system to <a>acquire the
wake lock</a> of type |type| and set |active| to `true` if the
operation succeeded, or else `false`.
</li>
<li>If |active| is `true`:
<ol>
<li>Let |document:Document| be the [=environment settings object
/ responsible document=] of the <a>current settings object</a>.
</li>
<li>Let |record| be the <a>platform wake lock</a>'s <a>state
record</a> associated with |document| and |type|.
</li>
<li>Add |lock| to |record|.{{[[ActiveLocks]]}}.
</li>
</ol>
</li>
<li>Return |active|.
</li>
</ol>
</section>
<section>
<h3>
Release wake lock algorithm
</h3>
<p>
To <dfn>release a wake lock</dfn> for a given |lock:WakeLockSentinel|
and |type:WakeLockType|, run these steps <a>in parallel</a>:
</p>
<ol class="algorithm">
<li>Let |document:Document| be the [=environment settings object /
responsible document=] of the <a>current settings object</a>.
</li>
<li>Let |record| be the <a>platform wake lock</a>'s <a>state
record</a> associated with |document| and |type|.
</li>
<li>If |record|.{{[[ActiveLocks]]}} does not contain |lock|, abort
these steps.
</li>
<li>Remove |lock| from |record|.{{[[ActiveLocks]]}}.
</li>
<li>If the internal slot {{[[ActiveLocks]]}} of all the <a>platform
wake lock</a>'s <a>state record</a>s are all empty, then run the
following steps <a>in parallel</a>:
<ol>
<li>Ask the underlying operating system to release the wake lock
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>
<a>Queue a task</a> to <a>fire an event</a> named "`release`" at
|lock|.
</li>
</ol>
</section>
</section>
<section>
<h2>
Security and privacy considerations
</h2>
<p>
Application of a wake lock causes various device components such as
display or CPU to operate at higher power levels than they otherwise
would. This can lead to undesirable and potentially dangerous effects
such as excessive heating and faster than normal battery charge
depletion. The latter is particularly relevant to mobile devices which
may not 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. Implementations should consider preventing wake
lock application if they determine that the remaining battery capacity
is low.
</p>
<p>
When the <a>user agent</a> does not <a>acquire wake lock</a> even
though a browsing context has requested it, this can be observed by the
browsing context and can possibly disclose sensitive information about
the state of the device such as that battery level is low.
</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 = true;
sentinel.onrelease = () => checkbox.checked = false;
</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>
<h2>
Dependencies
</h2>
<p>
Document's <code><dfn data-cite=
"PAGE-VISIBILITY#dom-document-hidden">hidden</dfn></code> attribute,
and <dfn data-cite="PAGE-VISIBILITY#dfn-visibility-states">visibility
state</dfn> are defined in [[PAGE-VISIBILITY]].
</p>
</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 id="idl-index" class="appendix"></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, Raphael Kubo da Costa 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>
</ul>
</section>
</section>
</body>
</html>