forked from w3c/compute-pressure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1799 lines (1751 loc) · 73.3 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
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<meta charset="utf-8" />
<title>Compute Pressure Level 1</title>
<script src="https://www.w3.org/Tools/respec/respec-w3c" class="remove" defer></script>
<script class="remove">
// All config options at https://respec.org/docs/
const respecConfig = {
shortName: "compute-pressure",
group: "das",
specStatus: "ED",
xref: "web-platform",
github: "https://github.com/w3c/compute-pressure",
formerEditors: [
{
name: "Olivier Yiptong",
company: "Google Inc.",
companyURL: "https://google.com",
w3cid: 81110,
},
{
name: "Victor Costan",
company: "Google Inc.",
companyURL: "https://google.com",
w3cid: 72312,
}
],
editors: [
{
name: "Kenneth Rohde Christiansen",
company: "Intel Corporation",
companyURL: "https://intel.com",
w3cid: 57705,
},
{
name: "Arnaud Mandy",
company: "Intel Corporation",
companyURL: "https://intel.com",
w3cid: 126342,
}
],
testSuiteURI: "https://github.com/web-platform-tests/wpt/labels/compute-pressure",
xref: {
profile: "web-platform",
specs: [
"permissions-policy", "hr-time", "picture-in-picture", "mediacapture-streams"
]
}
};
</script>
<style>
@counter-style pressure-states-emoji {
system: cyclic;
symbols: "\026AA" "\01F7E2" "\01F7E1" "\01F534";
suffix: " ";
}
.pressure-states {
list-style-type: pressure-states-emoji;
}
@counter-style pressure-source-emoji {
system: cyclic;
symbols: "\1F321" "\1F4A1";
suffix: " ";
}
.pressure-source {
list-style-type: pressure-source-emoji;
}
ul li::marker {
font-family: "Segoe UI Emoji", "Noto Color Emoji";
}
</style>
<section id="abstract">
<p>
The <cite>Compute Pressure API</cite> provides a way for websites to react to changes
in the CPU pressure of the target device, such that websites can trade off
resources for an improved user experience.
</p>
</section>
<section id="sotd"></section>
<section class="informative">
<h2>Introduction</h2>
<p>
Modern applications often need to balance the trade offs and advantages of fully utilizing
the system's computing resources, in order to provide a modern and delightful user experience.
</p>
<p>
As an example, many applications can render video effects with varying degrees of sophistication.
These applications aim to provide the best user experience, while avoiding driving the user's
device into a high pressure regime.
</p>
<p>
Utilization of [=processing units=] close to and often reaching 100% can lead to a bad
user experience, as different tasks are fighting for the processing time.
This can lead to slowless, which is especially noticeable with input delay.
Further, a prolonged utilization close 100% can cause the [=processing units=] to heat up due to prolonged
boosting, which can lead to throttling, resulting in an even worse user experience.
</p>
<p>
As a result of thermal limits, many smartphones, tablets and laptops can become uncomfortably hot
to the touch. The fans in laptops and desktops can become so loud that they disrupt conversations
or the users’ ability to focus.
</p>
<p>
In many cases, a device under high pressure appears to be unresponsive, as the operating
system may fail to schedule the threads advancing the task that the user is waiting for. See also
<a href="https://github.com/wicg/compute-pressure/#goals--motivating-use-cases">Use Cases</a>.
</p>
</section>
<section>
<h2>A Note on Feature Detection</h2>
<p><i>This section is non-normative.</i></p>
<p>
Feature detection is an established web development best practice. Resources on the topic are plentiful on- and
offline and the purpose of this section is not to discuss it further, but rather to put it in the context of
detecting hardware-dependent features.
</p>
<p>
Consider the below feature detection examples:
</p>
<aside class="example" title="Checking existence of PressureObserver interface">
<p>
This simple example illustrates how to check whether the [=User Agent=] exposes the
{{PressureObserver}} interface
</p>
<pre class="js">
if ("PressureObserver" in globalThis) {
// Use PressureObserver interface
}
</pre>
</aside>
<aside class="note">
<p>
Checking against {{globalThis/globalThis}} will work on any [=ECMAScript/agent=] as defined by
[[ECMAScript]], thus also in workers.
</p>
<p>
It does however not tell you whether that API is actually connected to a
real [=platform collector=], whether the [=platform collector=] is collecting real telemetry
readings, or whether the user is going to allow you to access it.
</p>
</aside>
</section>
<section>
<h2>Concepts</h2>
<p>
This specification defines the following concepts:
</p>
<section>
<h3>Processing Units</h3>
<p>
Computing devices consist of a multitude of different <dfn>processing units</dfn> such as the Central
Processing Unit (CPU), the Graphics Processing Unit (GPU) and many specialized
processing units. The latter are becoming popular such as ones designed to accelerate specific
tasks like machine learning or computer vision.
</p>
</section>
<section>
<h3>Pressure sources</h3>
<p>
The specification currently defines the <dfn>valid source types</dfn> as
<em>global system thermals</em> and the <em>central [=processing unit=]</em>, also known as the CPU.
Future levels of this specification MAY introduce additional [=source types=].
</p>
<pre class="idl">
enum PressureSource { "thermals", "cpu" };
</pre>
<p>
The <dfn>PressureSource</dfn> enum represents the [=valid source types=]:
</p>
<p>
<ul class="pressure-source">
<li>
{{PressureSource/"thermals"}} represents the global thermal state of the system.
</li>
<li>
{{PressureSource/"cpu"}} represents the average pressure of the central [=processing unit=]
across all its cores.
</li>
</ul>
</p>
<aside class="note">
<p>
If a user calls {{PressureObserver/observe()}} with a [=source type=] not part of
{{PressureSource}}, at the level of this specification the [=user agent=] supports,
the method will throw a {{TypeError}}.
</p>
<p>
If the [=source type=] is known by the [=user agent=] (part of {{PressureSource}}),
but not supported by it, the host OS or the underlying hardware, the method will instead
throw {{NotSupportedError}}.
</p>
<p>
To list the known [=source types=], a user can call the static attribute
{{PressureObserver/knownSources}}.
</p>
</aside>
</section>
<section>
<h3>Sampling and Reporting Rate</h3>
<p>
The <dfn>requested sampling interval</dfn> represents the desired
interval between samples to be obtained from the hardware,
expressed in milliseconds.
</p>
<p>
Interval and frequency are inverses of each other, so the
[=requested sampling interval=] can also be expressed as a
<dfn>requested sampling rate</dfn> in Hertz (cycles per second) by
dividing 1000 by the [=requested sampling interval=] value.
</p>
<p>
The <dfn>sampling rate</dfn> for a [=platform collector=] is defined as a rate
at which the [=user agent=] obtains telemetry readings from the underlying platform,
and it might differ from the pressure observers' [=requested sampling rates=].
The rate is measured in Hertz (cycles per second).
</p>
<p>
The <dfn>reporting rate</dfn> for a pressure observer is the rate at which it runs
the [=data delivery=] steps, and it will never exceed the [=sampling rate=].
</p>
<p>
The [=sampling rate=] differs from the [=requested sampling rate=] when the
[=requested sampling rate=] exceeds upper or lower sampling rate bounds
supported or accepted by the underlying platform and [=user agent=]<sup>†</sup>.
</p>
<p>
<sup>†</sup>It is recommended that the [=user agent=] limits the [=reporting rate=]
as outlined in [[[#rate-limiting-change-notifications]]].
</p>
<p>
In case the user didn't request a [=sampling rate=], the [=sampling rate=]
is [=implementation-defined=].
</p>
<aside class="note">
In case there are multiple instances of {{PressureObserver}} active with different
[=requested sampling intervals=], it is up to the [=user agent=] to set a
[=platform collector=] level [=sampling rate=] that best fulfills these requests,
while making sure that the [=reporting rate=] of all {{PressureObserver}}s does
not exceed their respective [=requested sampling rates=].
</aside>
</section>
</section>
<section> <h2>Platform primitives</h2>
<p>
The [=platform collector=] refers to a platform interface, with which the [=user agent=] interacts to
obtain the telemetry readings required by this specification.
</p>
<p>
A [=platform collector=] can be defined by the underlying platform (e.g. in a native telemetry
framework) or by the [=user agent=], if it has a direct access to hardware counters.
</p>
<p>
A [=platform collector=] can support telemetry for different <dfn>source types</dfn> of computing
devices defined by {{PressureSource}}, or there can be multiple [=platform collectors=].
</p>
<p>
From the implementation perspective [=platform collector=] can be treated as a software proxy for the
corresponding hardware counters. It is possible to have multiple [=platform collector=] simultaneously
interacting with the same underlying hardware if the underlying platform supports it.
</p>
<p>
In simple cases, a [=platform collector=] represents individual hardware counters, but if the provided
counter readings are a product of data fusion performed in software, the [=platform collector=]
represents the results of the data fusion process. This may happen in user space or in kernel space.
</p>
<p>
As collecting telemetry data often means polling hardware counters, it is not a free operation and thus,
it should not happen if there are no one observing the data. See [[[#life-cycle]]] for more information.
</p>
<p>
A [=platform collector=] samples data at a specific rate. A [=user agent=] may modify this rate
(if possible) for privacy reasons, or ignore and fuse certain readings.
</p>
</section>
<section>
<h3>
User notifications
</h3>
<p>
It is RECOMMENDED that a [=user agent=] show some form of user-visible
notification that informs the user when a pressure observer is active,
as well as provides the user with the means to block the ongoing operation,
or simply dismiss the notification.
</p>
</section>
<section>
<h3>
Policy control
</h3>
<p>
The Compute Pressure API defines a [=policy-controlled feature=]
identified by the token "compute-pressure".
Its [=policy-controlled feature/default allowlist=] is `'self'`.
</p>
<p>
Workers (dedicated and shared) adhere to the permission policy set by their
owning document(s).
</p>
<p>
Shared workers often have multiple owning documents as they can be obtained
by other documents with the [=same origin=].
In this case, all owning documents must be [=allowed to use=] the [=policy-controlled
feature=] defined by this specification.
</p>
<p>
Dedicated workers can be created from other workers,
in which case the permission policy of the first owning document
(or owning documents, in case of a shared worker) up the owner
chain will be used.
</p>
<aside class="note">
There has been discussion on allowing setting permission policy directly
on a worker on creation, in which case that would have to be consulted
as well.
</aside>
<aside class="note">
<p>
The [=policy-controlled feature/default allowlist=] of `'self'` allows usage in
same-origin nested frames but prevents third-party content from using
the feature.
</p>
<p>
Third-party usage can be selectively enabled by adding
`allow="compute-pressure"` attribute to the frame container element:
</p>
<pre class="example html" title=
"Enabling compute pressure on remote content">
<iframe src="https://third-party.com" allow="compute-pressure"/></iframe>
</pre>
<p>
Alternatively, the Compute Pressure 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: {"compute-pressure": []}
</pre>
<p>
See [[[PERMISSIONS-POLICY]]] for more details.
</p>
</aside>
</section>
<section>
<h3>
Internal Slot Definitions
</h3>
<p>
Each [=global object=] has:
<ul>
<li>
a <dfn>current pressure state</dfn> (a string), initialized to the empty string.
</li>
<li>
a <dfn>pressure observer task queued</dfn> (a boolean), which is initially false.
</li>
<li>
a <dfn>registered observer list</dfn> per supported [=source type=], which is initially empty.
</li>
<li>
a reference to an underlying <dfn>platform collector</dfn> as detailed in [[[#platform-primitives]]].
</li>
</ul>
A <dfn>registered observer</dfn> consists of an <dfn>observer</dfn> (a {{PressureObserver}} object).
</p>
<p>
The [=user agent=] has:
<ul>
<li>
a <dfn>max queued records</dfn> integer, which is set to an [=implementation-defined=] value, greater than 0.
</li>
<li>
a <dfn>supported source types</dfn> [=list=] of [=implementation-defined=] {{PressureSource}} values.
</li>
</ul>
</p>
<p>
A constructed {{PressureObserver}} object has the following internal slots:
</p>
<ul data-dfn-for="PressureObserver">
<li>
a <dfn>[[\Callback]]</dfn> of type {{PressureUpdateCallback}} set on creation.
</li>
<li>
a <dfn>[[\PendingObservePromises]]</dfn> [=list=] of zero or more source-promise [=tuples=], initially empty,
where source holds a {{PressureSource}} string and promise holds a {{Promise}} object.
</li>
<li>
a <dfn>[[\QueuedRecords]]</dfn> [=queue=] of zero or more {{PressureRecord}}
objects, which is initially empty.
</li>
<li>
a <dfn>[[\LastRecordMap]]</dfn> [=ordered map=] of {{PressureSource}} to
the latest {{PressureRecord}}.
</li>
<li>
a <dfn>[[\SampleIntervalMap]]</dfn> [=ordered map=] of {{PressureSource}} to
positive numbers. It represents the sample interval given source type.
</li>
</ul>
<p>
For the [=rate obfuscation=] mitigation the constructed {{PressureObserver}} object additionally
has the following internal slots:
</p>
<ul data-dfn-for="PressureObserver">
<li>
an <dfn>[[\ObservationWindow]]</dfn> integer set as part of the [=reset observation window=] steps.
</li>
<li>
a <dfn>[[\MaxChangesThreshold]]</dfn> integer set as part of the [=reset observation window=] steps.
</li>
<li>
a <dfn>[[\PenaltyDuration]]</dfn> integer set as part of the [=reset observation window=] steps.
</li>
<li>
a <dfn>[[\ChangesCountMap]]</dfn> [=ordered map=], [=map/keyed=] on a {{PressureSource}},
representing the [=source type=] that triggered transition to the [=current pressure state=].
The [=ordered map=]'s [=map/value=] is an integer representing the number of state changes in the
current observation window timeframe.
</li>
<li>
a <dfn>[[\AfterPenaltyRecordMap]]</dfn> [=ordered map=], [=map/keyed=] on a {{PressureSource}},
representing the [=source type=] of the last {{PressureRecord}}.
The [=ordered map=]'s [=map/value=] is a {{PressureRecord}}.
</li>
</ul>
</section>
<section> <h2>Pressure States</h2>
<p>
<dfn>Pressure states</dfn> represents the minimal set of useful states that allows websites
to react to changes in compute and system pressure with minimal degration in quality or service,
or user experience.
</p>
<pre class="idl">
enum PressureState { "nominal", "fair", "serious", "critical" };
</pre>
<p>
The <dfn>PressureState</dfn> enum represents the [=pressure state=] with the following states:
</p>
<p>
<ul class="pressure-states">
<li>
{{PressureState/"nominal"}}: The conditions of the target device are at an acceptable level with no noticeable
adverse effects on the user.
</li>
<li>
{{PressureState/"fair"}}: Target device pressure, temperature and/or energy usage are slightly elevated, potentially
resulting in reduced battery-life, as well as fans (or systems with fans) becoming active and audible.
Apart from that the target device is running flawlessly and can take on additional work.
</li>
<li>
{{PressureState/"serious"}}: Target device pressure, temperature and/or energy usage is consistently highly elevated.
The system may be throttling as a countermeasure to reduce thermals.
</li>
<li>
{{PressureState/"critical"}}: The temperature of the target device or system is significantly elevated and it requires
cooling down to avoid any potential issues.
</li>
</ul>
</p>
</section>
<section> <h2>Contributing Factors</h2>
<p>
<dfn>Contributing factors</dfn> represent the underlying hardware metrics contributing to the [=current pressure state=]
and can be [=implementation-defined=].
</p>
<p>
The <dfn>adjusted pressure state</dfn> is a [=pressure state=] determined by an [=implementation-defined=]
algorithm that takes as input [=source type=] and any other [=implementation-defined=] data from
[=contributing factors=]. This algorithm MUST not be deterministic to ensure [=break calibration=] mitigation effectiveness.
</p>
<p>
The <dfn>change in contributing factors is substantial</dfn> steps are as follows:
<ol>
<li>
If [=implementation-defined=] low-level hardware metrics that contribute to the
[=current pressure state=] drop below or exceed an, per metric,
[=implementation-defined=] threshold
for the [=current pressure state=], return true.
</li>
<li>
Return false.
</li>
</ol>
<aside class="note">
<p>
The [=change in contributing factors is substantial=] algorithm allows [=user agents=] to avoid flip-
flopping between states in certain circumstances. For example, a state might otherwise change too rapidly
in response to a certain system metric that fluctuates around a boundary condition that triggers a state
change.
</p>
<p>
This specification does not define the precise algorithm
to allow implementations optimize this algorithm to match the underlying hardware platform's behavior.
One possible implementation of this algorithm is to use a
<a href="https://en.wikipedia.org/wiki/Preisach_model_of_hysteresis#Nonideal_relay">nonideal relay</a>
as a model and identify appropriate lower threshold α and upper threshold β for each
[=pressure state=] taking special characteristics of each contributing factors into consideration.
</p>
</aside>
</p>
</section>
<section> <h2>Pressure Observer</h2>
The Compute Pressure API enables developers to understand the pressure
of system resources such as the CPU.
<section data-dfn-for="PressureObserverCallback">
<h3>The <a>PressureUpdateCallback</a> callback</h3>
<pre class="idl">
callback PressureUpdateCallback = undefined (
sequence<PressureRecord> changes,
PressureObserver observer
);
</pre>
This callback will be invoked when the [=pressure state=] changes.
</section>
<section data-dfn-for="PressureObserver"> <h2>The <a>PressureObserver</a> object</h2>
<p>
The {{PressureObserver}} can be used to observe changes in the [=pressure states=].
</p>
<pre class="idl">
[Exposed=(DedicatedWorker,SharedWorker,Window), SecureContext]
interface PressureObserver {
constructor(PressureUpdateCallback callback);
Promise<undefined> observe(PressureSource source, optional PressureObserverOptions options = {});
undefined unobserve(PressureSource source);
undefined disconnect();
sequence<PressureRecord> takeRecords();
[SameObject] static readonly attribute FrozenArray<PressureSource> knownSources;
};
</pre>
<p>The <dfn>PressureObserver</dfn> interface represents a {{PressureObserver}}.</p>
<section>
<h3>The <dfn>constructor()</dfn> method</h3>
<p>
The `new` {{PressureObserver(callback)}} constructor steps are:
<ol class="algorithm">
<li>
Set |this|.{{PressureObserver/[[Callback]]}} to |callback:PressureUpdateCallback|.
</li>
</ol>
</p>
</section>
<section>
<h3>The <dfn>observe()</dfn> method</h3>
<p>
The {{PressureObserver/observe(source, options)}} method steps are:
<ol class="algorithm">
<li>
Let |relevantGlobal| be [=this=]'s [=relevant global object=].
</li>
<li>
[=list/For each=] |document:Document| in |relevantGlobal|'s [=owning document set=]:
<ol>
<li>
If |document| is not [=allowed to use=] the [=policy-controlled
feature=] token "compute-pressure", return [=a promise rejected with=] {{NotAllowedError}}.
</li>
</ol>
<aside class="issue">
<a href="https://github.com/w3c/compute-pressure/issues/110">
Permission policy does not support workers directly yet #110</a>, so they cannot be set per
individual worker, so for now we consult the policy for all owning documents. For shared
workers this means that all owning documents have to have the policy enabled, which should
be easy to coordinate as shared workers require same origin.
</aside>
</li>
</li>
<li>
Set [=this=].{{PressureObserver/[[SampleIntervalMap]]}}[|source|] to |options:PressureObserverOptions|'s {{PressureObserverOptions/sampleInterval}}.
</li>
<li>
Let |promise:Promise| be [=a new promise=].
</li>
<li>
Let |pendingPromiseTuple| be (|source|, |promise|).
</li>
<li>
[=list/Append=] |pendingPromiseTuple| to [=this=].{{PressureObserver/[[PendingObservePromises]]}}.
</li>
<li>
[=promise/React=] to |promise|:
<ul>
<li>
If |promise| was [=resolved|fulfilled=] or [=rejected=], then:
<ol>
<li>
[=list/Remove=] |tuple| from [=this=].{{PressureObserver/[[PendingObservePromises]]}}.
</li>
</ol>
</li>
</ul>
</li>
<li>
Run the following steps [=in parallel=]:
<ol>
<li>
If |source:PressureSource| is not a [=valid source type=],
[=queue a global task=] on the [=PressureObserver task source=]
given |relevantGlobal|
to reject |promise| {{NotSupportedError}} and abort these steps.
</li>
<li>
Activate [=data delivery=] of |source| data to |relevantGlobal|.
</li>
<li>
[=Queue a global task=] on the [=PressureObserver task source=] given
|relevantGlobal| to run these steps:
<ol>
<li>
If |promise| was rejected, run the following substeps:
<ol>
<li>
If |relevantGlobal|'s [=registered observer list=] for |source| is [=list/empty=],
deactivate [=data delivery=] of |source| data to |relevantGlobal|.
</li>
<li>
Return.
</li>
</ol>
</li>
<li>
[=list/Append=] a new [=registered observer=] whose [=observer=] is [=this=]
to |relevantGlobal|'s [=registered observer list=] for |source|.
</li>
<li>
Resolve |promise|.
</li>
</ol>
</li>
</ol>
</li>
<li>
Return |promise|.
</li>
</ol>
</p>
</section>
<section>
<h3>The <dfn>unobserve()</dfn> method</h3>
<p>
The {{PressureObserver/unobserve(source)}} method steps are:
<ol class="algorithm">
<li>
If |source:PressureSource| is not a [=supported source type=], throw {{"NotSupportedError"}}.
</li>
<li>
[=list/Remove=] from |this|.{{PressureObserver/[[QueuedRecords]]}} all
|records| associated with |source|.
</li>
<li>
[=map/Remove=] |this|.{{PressureObserver/[[SampleIntervalMap]]}}[|source|].
</li>
<li>
[=map/Remove=] |this|.{{PressureObserver/[[LastRecordMap]]}}[|source|].
</li>
<li>
[=map/Remove=] |this|.{{PressureObserver/[[AfterPenaltyRecordMap]]}}[|source|].
</li>
<li>
[=list/For each=] (|promiseSource|, |pendingPromise|) of [=this=].{{PressureObserver/[[PendingObservePromises]]}},
if |source| is equal to |promiseSource|, [=reject=] |pendingPromise| with an {{AbortError}}.
</li>
<li>
Let |relevantGlobal| be [=this=]'s [=relevant global object=].
</li>
<li>
Remove any [=registered observer=] from |relevantGlobal|'s [=registered observer list=] for
|source| for which [=this=] is the [=registered observer=].
</li>
<li>
If the above [=registered observer list=] is [=list/empty=],
deactivate [=data delivery=] of |source| data to |relevantGlobal|.
</li>
</ol>
</p>
</section>
<section>
<h3>The <dfn>disconnect()</dfn> method</h3>
<p>
The {{PressureObserver/disconnect()}} method steps are:
<ol class="algorithm">
<li>
[=list/Empty=] |observer|.{{PressureObserver/[[QueuedRecords]]}}.
</li>
<li>
[=map/Clear=] |this|.{{PressureObserver/[[SampleIntervalMap]]}}.
</li>
<li>
[=map/Clear=] |this|.{{PressureObserver/[[LastRecordMap]]}}.
</li>
<li>
[=map/Clear=] |this|.{{PressureObserver/[[AfterPenaltyRecordMap]]}}.
</li>
<li>
[=list/For each=] (|promiseSource|, |pendingPromise|) of [=this=].{{PressureObserver/[[PendingObservePromises]]}},
[=reject=] |pendingPromise| with an {{AbortError}}.
</li>
<li>
Let |relevantGlobal| be [=this=]'s [=relevant global object=].
</li>
<li>
Remove any [=registered observer=] from |relevantGlobal|'s' [=registered observer list=] for
all supported [=source types=] for which [=this=] is the [=observer=].
</li>
<li>
If the above [=registered observer list=] is [=list/empty=],
deactivate [=data delivery=] of |source| data to |relevantGlobal|.
</li>
</ol>
</p>
</section>
<section>
<h3>The <dfn>takeRecords()</dfn> method</h3>
<aside class="note">
<p>
A common use case for this is to immediately fetch all pending state change records
immediately prior to disconnecting the observer, so that any pending state changes
can be processed when shutting down the observer.
</p>
<p>
Another use case is the ability to receive any pending, already-generated states
changes without waiting for the callbacks to be invoked. Callbacks are invoked from
a task queue as part of the event loop cycle, so this allows for conferring the
current state outside of the event loop cycle.
</p>
</aside>
<p>
The {{PressureObserver/takeRecords()}} method steps are:
<ol class="algorithm">
<li>
Let |records| be a [=list/clone=] of |observer|.{{PressureObserver/[[QueuedRecords]]}}.
</li>
<li>
[=list/Empty=] |observer|.{{PressureObserver/[[QueuedRecords]]}}.
</li>
<li>
Return |records|.
</li>
</ol>
</p>
</section>
<section>
<h3>The <dfn>knownSources</dfn> attribute</h3>
<p>
The {{PressureObserver/knownSources}} getter steps are:
<ol class="algorithm">
<li>
Return [=user agent=]'s [=supported source types=] in alphabetical order.
</li>
</ol>
</p>
<aside class="note">
<p>
The attribute property is a merely hint about the [=source types=] the [=user agent=] supports.
It is not guaranteed pressure observation will work on the underlying operating system or hardware.
Call {{PressureObserver/observe()}} and check for {{NotSupportedError}} if pressure observation is possible.
</p>
</aside>
</section>
</section>
<section data-dfn-for="PressureRecord">
<h3>The <dfn>PressureRecord</dfn> interface</h3>
<pre class="idl">
[Exposed=(DedicatedWorker,SharedWorker,Window), SecureContext]
interface PressureRecord {
readonly attribute PressureSource source;
readonly attribute PressureState state;
readonly attribute DOMHighResTimeStamp time;
[Default] object toJSON();
};
</pre>
<p>
A constructed {{PressureRecord}} object has the following internal slots:
</p>
<ul data-dfn-for="PressureRecord">
<li>
a <dfn>[[\Source]]</dfn> value of type {{PressureSource}}, which represents the current [=source type=].
</li>
<li>
a <dfn>[[\State]]</dfn> value of type {{PressureState}}, which represents the [=current pressure state=].
</li>
<li>
a <dfn>[[\Time]]</dfn> value of type {{DOMHighResTimeStamp}},
which corresponds to the
time the data was obtained from the system, relative to the [=environment settings object/time origin=] of the global object associated with
the {{PressureObserver}} instance that generated the notification.
</li>
</ul>
<section>
<h3>The <dfn>source</dfn> attribute</h3>
<p>
The {{PressureRecord/source}} [=getter steps=] are to return its {{PressureRecord/[[Source]]}} internal slot.
</p>
</section>
<section>
<h3>The <dfn>state</dfn> attribute</h3>
<p>
The {{PressureRecord/state}} [=getter steps=] are to return its {{PressureRecord/[[State]]}} internal slot.
</p>
</section>
<section>
<h3>The <dfn>time</dfn> attribute</h3>
<p>
The {{PressureRecord/time}} [=getter steps=] are to return its {{PressureRecord/[[Time]]}} internal slot.
</p>
</section>
<section>
<h3>The <dfn>toJSON</dfn> member</h3>
<p>
When {{PressureRecord.toJSON}} is called, run [[[WebIDL]]]'s [=default toJSON steps=].
</p>
</section>
</section>
<section data-dfn-for="PressureObserverOptions">
<h3>The <dfn>PressureObserverOptions</dfn> dictionary</h3>
<pre class="idl">
dictionary PressureObserverOptions {
[EnforceRange] unsigned long sampleInterval = 0;
};
</pre>
<section>
<h3>The <dfn>sampleInterval</dfn> member</h3>
<p>
The {{PressureObserverOptions/sampleInterval}} member represents the [=requested sampling
interval=] expressed in milliseconds.
</p>
<aside class="note">
<p>
A [=user agent=] might not be able to respect the requested sampling interval. For more information
consult [[[#sampling-and-reporting-rate]]].
</p>
</aside>
</section>
</section>
<section id="life-cycle">
<h3>Life-cycle and garbage collection</h3>
<p>
Each [=global object=] has a
strong reference to [=registered observers=] in their [=registered observer list=]
(one per source).
</p>
<aside class="note">
<p>
A {{PressureObserver}} is observing a |source:PressureSource| if it
exists in the [=registered observer list=], modified by invocations of
{{PressureObserver/observe()}},
{{PressureObserver/unobserve()}}
and {{PressureObserver/disconnect()}}.
</p>
<p>
This means that a {{PressureObserver}} object |observer:PressureObserver| will
remain alive (thus not be garbage collection) until both of these conditions hold:
<ul>
<li>
There are no scripting references to the |observer|.
</li>
<li>
The |observer| is not observing any |source|.
</li>
</ul>
</p>
<p>
[[[#cb-observer-example]]] contains an example of how to use the
{{PressureObserver/disconnect()}} (or any other {{PressureObserver}}
method) from the observer callback.
</p>
</aside>
</section>
<section id="processing-model">
<h3>Processing Model</h3>
<p>
This section outlines the steps the user agent must take when implementing the specification.
</p>
<section>
<h3>Supporting algorithms</h3>
<p>
The <dfn>reset observation window</dfn> steps given the argument |observer:PressureObserver|, are as follows:
<ul>
<li>
set |observer|.{{PressureObserver/[[ObservationWindow]]}} to an [=implementation-defined=] randomized integer value in
milliseconds within an [=implementation-defined=] range.
</li>
<li>
set |observer|.{{PressureObserver/[[MaxChangesThreshold]]}} to an [=implementation-defined=] randomized integer
value of maximum allowed changes within the |observationWindow| within an [=implementation-defined=] range.
</li>
<li>
set |observer|.{{PressureObserver/[[PenaltyDuration]]}} to an [=implementation-defined=] randomized integer value
in milliseconds, within an [=implementation-defined=] range.
</li>
<li>
[=list/Empty=] the observer.{{PressureObserver/[[ChangesCountMap]]}} map.
</li>
</ul>
Run the [=reset observation window=] steps and start a timer to re-run the steps when the observer.{{PressureObserver/[[ObservationWindow]]}}
time has passed, using different randomized values.
</p>
<p>
<aside class="note">
Readings are available for [=documents=] (incl. [=iframes=] and popup windows) and workers,
matching the following criteria:
<ul>
<li>
They are [=Document/fully active=] [=documents=], or
they are <a href="https://html.spec.whatwg.org/multipage/workers.html#active-needed-worker">active needed workers</a>.
</li>
<li>
Their [=origin=] is [=same origin=] with the [=Node/node document|document=] containing the
<a href="https://html.spec.whatwg.org/multipage/interaction.html#focused">focused</a> [=node=], or an
<a href="https://w3c.github.io/picture-in-picture/#initiators-of-active-picture-in-picture-sessions">
initiator of an active Picture-in-Picture session</a>, or the browsing [=context is capturing=],
including microphone, camera and display.
</li>
</ul>
</aside>
<p>
To determine the <dfn>owning document set</dfn> for a [=relevant global object=] |relevantGlobal|:
<ol>
<li>
Let |owningDocumentSet| be an empty [=set=].
</li>
<li>
If |relevantGlobal| is {{Window}}, then [=set/append=] |relevantGlobal|'s [=associated document=] to |owningDocumentSet|.
</li>
<li>
Otherwise, [=list/for each=] |owner| in {{WorkerGlobalScope}} |relevantGlobal|'s [=WorkerGlobalScope/owner set=]:
<ol>
<li>
If |owner| is a {{Document}}, then [=set/append=] |owner| to |owningDocumentSet|.
</li>
<li>
If |owner| is a {{WorkerGlobalScope}}, set |owningDocumentSet| to the [=set/union=] of
|owningDocumentSet| and |owner|'s [=owning document set=].
</li>
</ol>
<li>
Return |owningDocumentSet|.
</li>
</li>
</ol>
</p>
<p>
The <dfn>document has implicit focus</dfn> steps given the argument |document:Document|, are as follows:
<ol>
<li>
If |document| is not [=Document/fully active=], return false.
</li>
<li>
Let |relevantGlobal| be |document|'s [=relevant global object=].
</li>
<li>
[=list/For each=] |origin| in
<a href="https://w3c.github.io/picture-in-picture/#initiators-of-active-picture-in-picture-sessions">
initiators of active Picture-in-Picture sessions</a>:
<ol>
<li>
If |relevantGlobal|'s [=relevant settings object=]'s [=origin=] is [=same origin=] with |origin|, return true.
</li>
</ol>
</li>
<li>
If |relevantGlobal|'s [=browsing context=] is [=context is capturing|capturing=], return true.
</li>
<li>
Let |topLevelBC| be |relevantGlobal|'s [=browsing context=]'s [=top-level browsing context=].
</li>
<li>
If |topLevelBC| does not have [=top-level traversable/system focus=], return false.
</li>
<li>
Let |focusedDocument| be the |topLevelBC|'s
<a href="https://html.spec.whatwg.org/multipage/interaction.html#currently-focused-area-of-a-top-level-browsing-context">
currently focused area</a>'s [=Node/node document=].
</li>
<li>
If |relevantGlobal|'s [=relevant settings object=]'s [=origin=] is [=same origin=] with
|focusedDocument|'s [=origin=], return true.
</li>
<li>
Otherwise, return false.
</li>
</ol>
</p>
<p>