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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
import { ShortcutUtils } from "resource://gre/modules/ShortcutUtils.sys.mjs";
const lazy = {};
XPCOMUtils.defineLazyServiceGetters(lazy, {
WindowsUIUtils: ["@mozilla.org/windows-ui-utils;1", "nsIWindowsUIUtils"],
});
ChromeUtils.defineESModuleGetters(lazy, {
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.sys.mjs",
});
import { Rect, Point } from "resource://gre/modules/Geometry.sys.mjs";
const PLAYER_URI = "chrome://global/content/pictureinpicture/player.xhtml";
// Currently, we need titlebar="yes" on macOS in order for the player window
// to be resizable. See bug 1824171.
const TITLEBAR = AppConstants.platform == "macosx" ? "yes" : "no";
const PLAYER_FEATURES = `chrome,alwaysontop,lockaspectratio,resizable,dialog,titlebar=${TITLEBAR}`;
const WINDOW_TYPE = "Toolkit:PictureInPicture";
const TOGGLE_ENABLED_PREF =
"media.videocontrols.picture-in-picture.video-toggle.enabled";
const TOGGLE_FIRST_SEEN_PREF =
"media.videocontrols.picture-in-picture.video-toggle.first-seen-secs";
const TOGGLE_HAS_USED_PREF =
"media.videocontrols.picture-in-picture.video-toggle.has-used";
const TOGGLE_POSITION_PREF =
"media.videocontrols.picture-in-picture.video-toggle.position";
const TOGGLE_POSITION_RIGHT = "right";
const TOGGLE_POSITION_LEFT = "left";
const RESIZE_MARGIN_PX = 16;
const BACKGROUND_DURATION_HISTOGRAM_ID =
"FX_PICTURE_IN_PICTURE_BACKGROUND_TAB_PLAYING_DURATION";
const FOREGROUND_DURATION_HISTOGRAM_ID =
"FX_PICTURE_IN_PICTURE_FOREGROUND_TAB_PLAYING_DURATION";
XPCOMUtils.defineLazyPreferenceGetter(
lazy,
"PIP_ENABLED",
"media.videocontrols.picture-in-picture.enabled",
false
);
XPCOMUtils.defineLazyPreferenceGetter(
lazy,
"PIP_URLBAR_BUTTON",
"media.videocontrols.picture-in-picture.urlbar-button.enabled",
false
);
XPCOMUtils.defineLazyPreferenceGetter(
lazy,
"RESPECT_PIP_DISABLED",
"media.videocontrols.picture-in-picture.respect-disablePictureInPicture",
true
);
/**
* Tracks the number of currently open player windows for Telemetry tracking
*/
let gCurrentPlayerCount = 0;
/**
* To differentiate windows in the Telemetry Event Log, each Picture-in-Picture
* player window is given a unique ID.
*/
let gNextWindowID = 0;
export class PictureInPictureLauncherParent extends JSWindowActorParent {
receiveMessage(aMessage) {
switch (aMessage.name) {
case "PictureInPicture:Request": {
let videoData = aMessage.data;
PictureInPicture.handlePictureInPictureRequest(this.manager, videoData);
break;
}
}
}
}
export class PictureInPictureToggleParent extends JSWindowActorParent {
receiveMessage(aMessage) {
let browsingContext = aMessage.target.browsingContext;
let browser = browsingContext.top.embedderElement;
switch (aMessage.name) {
case "PictureInPicture:OpenToggleContextMenu": {
let win = browser.ownerGlobal;
PictureInPicture.openToggleContextMenu(win, aMessage.data);
break;
}
case "PictureInPicture:UpdateEligiblePipVideoCount": {
let { pipCount, pipDisabledCount } = aMessage.data;
PictureInPicture.updateEligiblePipVideoCount(browsingContext, {
pipCount,
pipDisabledCount,
});
PictureInPicture.updateUrlbarToggle(browser);
break;
}
case "PictureInPicture:SetFirstSeen": {
let { dateSeconds } = aMessage.data;
PictureInPicture.setFirstSeen(dateSeconds);
break;
}
case "PictureInPicture:SetHasUsed": {
let { hasUsed } = aMessage.data;
PictureInPicture.setHasUsed(hasUsed);
break;
}
}
}
}
/**
* This module is responsible for creating a Picture in Picture window to host
* a clone of a video element running in web content.
*/
export class PictureInPictureParent extends JSWindowActorParent {
receiveMessage(aMessage) {
switch (aMessage.name) {
case "PictureInPicture:Resize": {
let videoData = aMessage.data;
PictureInPicture.resizePictureInPictureWindow(videoData, this);
break;
}
case "PictureInPicture:Close": {
/**
* Content has requested that its Picture in Picture window go away.
*/
let reason = aMessage.data.reason;
PictureInPicture.closeSinglePipWindow({ reason, actorRef: this });
break;
}
case "PictureInPicture:Playing": {
let player = PictureInPicture.getWeakPipPlayer(this);
if (player) {
player.setIsPlayingState(true);
}
break;
}
case "PictureInPicture:Paused": {
let player = PictureInPicture.getWeakPipPlayer(this);
if (player) {
player.setIsPlayingState(false);
}
break;
}
case "PictureInPicture:Muting": {
let player = PictureInPicture.getWeakPipPlayer(this);
if (player) {
player.setIsMutedState(true);
}
break;
}
case "PictureInPicture:Unmuting": {
let player = PictureInPicture.getWeakPipPlayer(this);
if (player) {
player.setIsMutedState(false);
}
break;
}
case "PictureInPicture:EnableSubtitlesButton": {
let player = PictureInPicture.getWeakPipPlayer(this);
if (player) {
player.enableSubtitlesButton();
}
break;
}
case "PictureInPicture:DisableSubtitlesButton": {
let player = PictureInPicture.getWeakPipPlayer(this);
if (player) {
player.disableSubtitlesButton();
}
break;
}
case "PictureInPicture:SetTimestampAndScrubberPosition": {
let { timestamp, scrubberPosition } = aMessage.data;
let player = PictureInPicture.getWeakPipPlayer(this);
player.setTimestamp(timestamp);
player.setScrubberPosition(scrubberPosition);
break;
}
}
}
}
/**
* This module is responsible for creating a Picture in Picture window to host
* a clone of a video element running in web content.
*/
export var PictureInPicture = {
// Maps PictureInPictureParent actors to their corresponding PiP player windows
weakPipToWin: new WeakMap(),
// Maps PiP player windows to their originating content's browser
weakWinToBrowser: new WeakMap(),
// Maps a browser to the number of PiP windows it has
browserWeakMap: new WeakMap(),
// Maps an AppWindow to the number of PiP windows it has
originatingWinWeakMap: new WeakMap(),
// Maps a WindowGlobal to count of eligible PiP videos
weakGlobalToEligiblePipCount: new WeakMap(),
/**
* Returns the player window if one exists and if it hasn't yet been closed.
*
* @param {PictureInPictureParent} pipActorRef
* Reference to the calling PictureInPictureParent actor
*
* @returns {Window} the player window if it exists and is not in the
* process of being closed. Returns null otherwise.
*/
getWeakPipPlayer(pipActorRef) {
let playerWin = this.weakPipToWin.get(pipActorRef);
if (!playerWin || playerWin.closed) {
return null;
}
return playerWin;
},
/**
* Get the PiP panel for a browser. Create the panel if needed.
* @param {Browser} browser The current browser
* @returns panel The panel element
*/
getPanelForBrowser(browser) {
let panel = browser.ownerDocument.querySelector("#PictureInPicturePanel");
if (!panel) {
browser.ownerGlobal.ensureCustomElements("moz-toggle");
browser.ownerGlobal.ensureCustomElements("moz-support-link");
let template = browser.ownerDocument.querySelector(
"#PictureInPicturePanelTemplate"
);
let clone = template.content.cloneNode(true);
template.replaceWith(clone);
panel = this.getPanelForBrowser(browser);
}
return panel;
},
handleEvent(event) {
switch (event.type) {
case "TabSwapPictureInPicture": {
this.onPipSwappedBrowsers(event);
break;
}
case "TabSelect": {
this.updatePlayingDurationHistograms();
break;
}
}
},
/**
* Increase the count of PiP windows for a given browser
* @param browser The browser to increase PiP count in browserWeakMap
*/
addPiPBrowserToWeakMap(browser) {
let count = this.browserWeakMap.has(browser)
? this.browserWeakMap.get(browser)
: 0;
this.browserWeakMap.set(browser, count + 1);
// If a browser is being added to the browserWeakMap, that means its
// probably a good time to make sure the playing duration histograms
// are up-to-date, as it means that we've either opened a new PiP
// player window, or moved the originating tab to another window.
this.updatePlayingDurationHistograms();
},
/**
* Increase the count of PiP windows for a given AppWindow.
*
* @param {Browser} browser
* The content browser that the originating video lives in and from which
* we'll read its parent window to increase PiP window count in originatingWinWeakMap.
*/
addOriginatingWinToWeakMap(browser) {
let parentWin = browser.ownerGlobal;
let count = this.originatingWinWeakMap.get(parentWin);
if (!count || count == 0) {
this.setOriginatingWindowActive(parentWin.browsingContext, true);
this.originatingWinWeakMap.set(parentWin, 1);
let gBrowser = browser.getTabBrowser();
if (gBrowser) {
gBrowser.tabContainer.addEventListener("TabSelect", this);
}
} else {
this.originatingWinWeakMap.set(parentWin, count + 1);
}
},
/**
* Decrease the count of PiP windows for a given browser.
* If the count becomes 0, we will remove the browser from the WeakMap
* @param browser The browser to decrease PiP count in browserWeakMap
*/
removePiPBrowserFromWeakMap(browser) {
let count = this.browserWeakMap.get(browser);
if (count <= 1) {
this.browserWeakMap.delete(browser);
let tabbrowser = browser.getTabBrowser();
if (tabbrowser && !tabbrowser.shouldActivateDocShell(browser)) {
browser.docShellIsActive = false;
}
} else {
this.browserWeakMap.set(browser, count - 1);
}
},
/**
* Decrease the count of PiP windows for a given AppWindow.
* If the count becomes 0, we will remove the AppWindow from the WeakMap.
*
* @param {Browser} browser
* The content browser that the originating video lives in and from which
* we'll read its parent window to decrease PiP window count in originatingWinWeakMap.
*/
removeOriginatingWinFromWeakMap(browser) {
let parentWin = browser?.ownerGlobal;
if (!parentWin) {
return;
}
let count = this.originatingWinWeakMap.get(parentWin);
if (!count || count <= 1) {
this.originatingWinWeakMap.delete(parentWin, 0);
this.setOriginatingWindowActive(parentWin.browsingContext, false);
let gBrowser = browser.getTabBrowser();
if (gBrowser) {
gBrowser.tabContainer.removeEventListener("TabSelect", this);
}
} else {
this.originatingWinWeakMap.set(parentWin, count - 1);
}
},
onPipSwappedBrowsers(event) {
let otherTab = event.detail;
if (otherTab) {
for (let win of Services.wm.getEnumerator(WINDOW_TYPE)) {
if (this.weakWinToBrowser.get(win) === event.target.linkedBrowser) {
this.weakWinToBrowser.set(win, otherTab.linkedBrowser);
this.removePiPBrowserFromWeakMap(event.target.linkedBrowser);
this.removeOriginatingWinFromWeakMap(event.target.linkedBrowser);
this.addPiPBrowserToWeakMap(otherTab.linkedBrowser);
this.addOriginatingWinToWeakMap(otherTab.linkedBrowser);
}
}
otherTab.addEventListener("TabSwapPictureInPicture", this);
}
},
updatePlayingDurationHistograms() {
// A tab switch occurred in a browser window with one more tabs that have
// PiP player windows associated with them.
for (let win of Services.wm.getEnumerator(WINDOW_TYPE)) {
let browser = this.weakWinToBrowser.get(win);
let gBrowser = browser.getTabBrowser();
if (gBrowser?.selectedBrowser == browser) {
// If there are any background stopwatches running for this window, finish
// them and switch to foreground.
if (TelemetryStopwatch.running(BACKGROUND_DURATION_HISTOGRAM_ID, win)) {
TelemetryStopwatch.finish(BACKGROUND_DURATION_HISTOGRAM_ID, win);
}
if (
!TelemetryStopwatch.running(FOREGROUND_DURATION_HISTOGRAM_ID, win)
) {
TelemetryStopwatch.start(FOREGROUND_DURATION_HISTOGRAM_ID, win, {
inSeconds: true,
});
}
} else {
// If there are any foreground stopwatches running for this window, finish
// them and switch to background.
if (TelemetryStopwatch.running(FOREGROUND_DURATION_HISTOGRAM_ID, win)) {
TelemetryStopwatch.finish(FOREGROUND_DURATION_HISTOGRAM_ID, win);
}
if (
!TelemetryStopwatch.running(BACKGROUND_DURATION_HISTOGRAM_ID, win)
) {
TelemetryStopwatch.start(BACKGROUND_DURATION_HISTOGRAM_ID, win, {
inSeconds: true,
});
}
}
}
},
/**
* Called when the browser UI handles the View:PictureInPicture command via
* the keyboard.
*
* @param {Event} event
*/
onCommand(event) {
if (!lazy.PIP_ENABLED) {
return;
}
let win = event.target.ownerGlobal;
let bc = Services.focus.focusedContentBrowsingContext;
if (bc.top == win.gBrowser.selectedBrowser.browsingContext) {
let actor = bc.currentWindowGlobal.getActor("PictureInPictureLauncher");
actor.sendAsyncMessage("PictureInPicture:KeyToggle");
}
},
async focusTabAndClosePip(window, pipActor) {
let browser = this.weakWinToBrowser.get(window);
if (!browser) {
return;
}
let gBrowser = browser.getTabBrowser();
let tab = gBrowser.getTabForBrowser(browser);
// focus the tab's window
tab.ownerGlobal.focus();
gBrowser.selectedTab = tab;
await this.closeSinglePipWindow({ reason: "unpip", actorRef: pipActor });
},
/**
* Update the respect PiPDisabled pref value when the toggle is clicked.
* @param {Event} event The event from toggling the respect
* PiPDisabled in the PiP panel
*/
toggleRespectDisablePip(event) {
let toggle = event.target;
let respectPipDisabled = !toggle.pressed;
Services.prefs.setBoolPref(
"media.videocontrols.picture-in-picture.respect-disablePictureInPicture",
respectPipDisabled
);
Services.telemetry.recordEvent(
"pictureinpicture",
"disrespect_disable",
"urlBar"
);
},
/**
* Updates the PiP count and PiPDisabled count of eligible PiP videos for a
* respective WindowGlobal.
* @param {BrowsingContext} browsingContext The BrowsingContext with eligible videos
* @param {Object} object
* pipCount: The number of eligible videos for the respective WindowGlobal
* pipDisabledCount: The number of disablePiP videos for the respective WindowGlobal
*/
updateEligiblePipVideoCount(browsingContext, object) {
let windowGlobal = browsingContext.currentWindowGlobal;
if (windowGlobal) {
this.weakGlobalToEligiblePipCount.set(windowGlobal, object);
}
},
/**
* A generator function that yeilds a WindowGlobal, it's respective PiP
* count, and if any of the videos have PiPDisabled set.
* @param {Browser} browser The selected browser
*/
*windowGlobalPipCountGenerator(browser) {
let contextsToVisit = [browser.browsingContext];
while (contextsToVisit.length) {
let currentBC = contextsToVisit.pop();
let windowGlobal = currentBC.currentWindowGlobal;
if (!windowGlobal) {
continue;
}
let { pipCount, pipDisabledCount } =
this.weakGlobalToEligiblePipCount.get(windowGlobal) || {
pipCount: 0,
pipDisabledCount: 0,
};
contextsToVisit.push(...currentBC.children);
yield { windowGlobal, pipCount, pipDisabledCount };
}
},
/**
* Gets the total eligible video count and total PiPDisabled count for a
* given browser.
* @param {Browser} browser The selected browser
* @returns Total count of eligible PiP videos for the selected broser
*/
getEligiblePipVideoCount(browser) {
let totalPipCount = 0;
let totalPipDisabled = 0;
for (let {
pipCount,
pipDisabledCount,
} of this.windowGlobalPipCountGenerator(browser)) {
totalPipCount += pipCount;
totalPipDisabled += pipDisabledCount;
}
return { totalPipCount, totalPipDisabled };
},
/**
* This function updates the hover text on the urlbar PiP button when we enter or exit PiP
* @param {Document} document The window document
* @param {Element} pipToggle The urlbar PiP button
* @param {String} dataL10nId The data l10n id of the string we want to show
*/
updateUrlbarHoverText(document, pipToggle, dataL10nId) {
let shortcut = document.getElementById("key_togglePictureInPicture");
document.l10n.setAttributes(pipToggle, dataL10nId, {
shortcut: ShortcutUtils.prettifyShortcut(shortcut),
});
},
/**
* Toggles the visibility of the PiP urlbar button. If the total video count
* is 1, then we will show the button. If any eligible video has PiPDisabled,
* then the button will show. Otherwise the button is hidden.
* @param {Browser} browser The selected browser
*/
updateUrlbarToggle(browser) {
if (!lazy.PIP_ENABLED || !lazy.PIP_URLBAR_BUTTON) {
return;
}
let win = browser.ownerGlobal;
if (win.closed || win.gBrowser?.selectedBrowser !== browser) {
return;
}
let { totalPipCount, totalPipDisabled } =
this.getEligiblePipVideoCount(browser);
let pipToggle = win.document.getElementById("picture-in-picture-button");
pipToggle.hidden = !(
totalPipCount === 1 ||
(totalPipDisabled > 0 && lazy.RESPECT_PIP_DISABLED)
);
let browserHasPip = !!this.browserWeakMap.get(browser);
if (browserHasPip) {
this.setUrlbarPipIconActive(browser.ownerGlobal);
} else {
this.setUrlbarPipIconInactive(browser.ownerGlobal);
}
},
/**
* Open the PiP panel if any video has PiPDisabled, otherwise finds the
* correct WindowGlobal to open the eligible PiP video.
* @param {Event} event Event from clicking the PiP urlbar button
*/
toggleUrlbar(event) {
if (event.button !== 0) {
return;
}
let win = event.target.ownerGlobal;
let browser = win.gBrowser.selectedBrowser;
let pipPanel = this.getPanelForBrowser(browser);
for (let {
windowGlobal,
pipCount,
pipDisabledCount,
} of this.windowGlobalPipCountGenerator(browser)) {
if (
(pipDisabledCount > 0 && lazy.RESPECT_PIP_DISABLED) ||
(pipPanel && pipPanel.state !== "closed")
) {
this.togglePipPanel(browser);
return;
} else if (pipCount === 1) {
let actor = windowGlobal.getActor("PictureInPictureToggle");
actor.sendAsyncMessage("PictureInPicture:UrlbarToggle");
return;
}
}
},
/**
* Set the toggle for PiPDisabled when the panel is shown.
* If the pref is set from about:config, we need to update
* the toggle switch in the panel to match the pref.
* @param {Event} event The panel shown event
*/
onPipPanelShown(event) {
let toggle = event.target.querySelector("#respect-pipDisabled-switch");
toggle.pressed = !lazy.RESPECT_PIP_DISABLED;
},
/**
* Update the visibility of the urlbar PiP button when the panel is hidden.
* The button will show when there is more than 1 video and at least 1 video
* has PiPDisabled. If we no longer want to respect PiPDisabled then we
* need to check if the urlbar button should still be visible.
* @param {Event} event The panel hidden event
*/
onPipPanelHidden(event) {
this.updateUrlbarToggle(event.view.gBrowser.selectedBrowser);
},
/**
* Create the PiP panel if needed and toggle the display of the panel
* @param {Browser} browser The current browser
*/
togglePipPanel(browser) {
let pipPanel = this.getPanelForBrowser(browser);
if (pipPanel.state === "closed") {
let anchor = browser.ownerDocument.querySelector(
"#picture-in-picture-button"
);
pipPanel.openPopup(anchor, "bottomright topright");
Services.telemetry.recordEvent(
"pictureinpicture",
"opened_method",
"urlBar",
null,
{ disableDialog: "true" }
);
} else {
pipPanel.hidePopup();
}
},
/**
* Sets the PiP urlbar to an active state. This changes the icon in the
* urlbar button to the unpip icon.
* @param {Window} win The current Window
*/
setUrlbarPipIconActive(win) {
let pipToggle = win.document.getElementById("picture-in-picture-button");
pipToggle.toggleAttribute("pipactive", true);
this.updateUrlbarHoverText(
win.document,
pipToggle,
"picture-in-picture-urlbar-button-close"
);
},
/**
* Sets the PiP urlbar to an inactive state. This changes the icon in the
* urlbar button to the open pip icon.
* @param {Window} win The current window
*/
setUrlbarPipIconInactive(win) {
if (!win) {
return;
}
let pipToggle = win.document.getElementById("picture-in-picture-button");
pipToggle.toggleAttribute("pipactive", false);
this.updateUrlbarHoverText(
win.document,
pipToggle,
"picture-in-picture-urlbar-button-open"
);
},
/**
* Remove attribute which enables pip icon in tab
*
* @param {Window} window
* A PictureInPicture player's window, used to resolve the player's
* associated originating content browser
*/
clearPipTabIcon(window) {
const browser = this.weakWinToBrowser.get(window);
if (!browser) {
return;
}
// see if no other pip windows are open for this content browser
for (let win of Services.wm.getEnumerator(WINDOW_TYPE)) {
if (
win !== window &&
this.weakWinToBrowser.has(win) &&
this.weakWinToBrowser.get(win) === browser
) {
return;
}
}
let gBrowser = browser.getTabBrowser();
let tab = gBrowser?.getTabForBrowser(browser);
if (tab) {
tab.removeAttribute("pictureinpicture");
}
},
/**
* Closes and waits for passed PiP player window to finish closing.
*
* @param {Window} pipWin
* Player window to close
*/
async closePipWindow(pipWin) {
if (pipWin.closed) {
return;
}
let closedPromise = new Promise(resolve => {
pipWin.addEventListener("unload", resolve, { once: true });
});
pipWin.close();
await closedPromise;
},
/**
* Closes a single PiP window. Used exclusively in conjunction with support
* for multiple PiP windows
*
* @param {Object} closeData
* Additional data required to complete a close operation on a PiP window
* @param {PictureInPictureParent} closeData.actorRef
* The PictureInPictureParent actor associated with the PiP window being closed
* @param {string} closeData.reason
* The reason for closing this PiP window
*/
async closeSinglePipWindow(closeData) {
const { reason, actorRef } = closeData;
const win = this.getWeakPipPlayer(actorRef);
if (!win) {
return;
}
this.removePiPBrowserFromWeakMap(this.weakWinToBrowser.get(win));
Services.telemetry.recordEvent(
"pictureinpicture",
"closed_method",
reason,
null
);
await this.closePipWindow(win);
},
/**
* A request has come up from content to open a Picture in Picture
* window.
*
* @param {WindowGlobalParent} wgps
* The WindowGlobalParent that is requesting the Picture in Picture
* window.
*
* @param {object} videoData
* An object containing the following properties:
*
* videoHeight (int):
* The preferred height of the video.
*
* videoWidth (int):
* The preferred width of the video.
*
* @returns {Promise}
* Resolves once the Picture in Picture window has been created, and
* the player component inside it has finished loading.
*/
async handlePictureInPictureRequest(wgp, videoData) {
gCurrentPlayerCount += 1;
Services.telemetry.scalarSetMaximum(
"pictureinpicture.most_concurrent_players",
gCurrentPlayerCount
);
let browser = wgp.browsingContext.top.embedderElement;
let parentWin = browser.ownerGlobal;
let win = await this.openPipWindow(parentWin, videoData);
win.setIsPlayingState(videoData.playing);
win.setIsMutedState(videoData.isMuted);
// set attribute which shows pip icon in tab
let tab = parentWin.gBrowser.getTabForBrowser(browser);
tab.setAttribute("pictureinpicture", true);
this.setUrlbarPipIconActive(parentWin);
tab.addEventListener("TabSwapPictureInPicture", this);
let pipId = gNextWindowID.toString();
win.setupPlayer(pipId, wgp, videoData.videoRef);
gNextWindowID++;
this.weakWinToBrowser.set(win, browser);
this.addPiPBrowserToWeakMap(browser);
this.addOriginatingWinToWeakMap(browser);
win.setScrubberPosition(videoData.scrubberPosition);
win.setTimestamp(videoData.timestamp);
Services.prefs.setBoolPref(TOGGLE_HAS_USED_PREF, true);
let args = {
width: win.innerWidth.toString(),
height: win.innerHeight.toString(),
screenX: win.screenX.toString(),
screenY: win.screenY.toString(),
ccEnabled: videoData.ccEnabled.toString(),
webVTTSubtitles: videoData.webVTTSubtitles.toString(),
};
Services.telemetry.recordEvent(
"pictureinpicture",
"create",
"player",
pipId,
args
);
},
/**
* Calls the browsingContext's `forceAppWindowActive` flag to determine if the
* the top level chrome browsingContext should be forcefully set as active or not.
* When the originating window's browsing context is set to active, captions on the
* PiP window are properly updated. Forcing active while a PiP window is open ensures
* that captions are still updated when the originating window is occluded.
*
* @param {BrowsingContext} browsingContext
* The browsing context of the originating window
* @param {boolean} isActive
* True to force originating window as active, or false to not enforce it
* @see CanonicalBrowsingContext
*/
setOriginatingWindowActive(browsingContext, isActive) {
browsingContext.forceAppWindowActive = isActive;
},
/**
* unload event has been called in player.js, cleanup our preserved
* browser object.
*
* @param {Window} window
*/
unload(window) {
TelemetryStopwatch.finish(
"FX_PICTURE_IN_PICTURE_WINDOW_OPEN_DURATION",
window
);
if (TelemetryStopwatch.running(BACKGROUND_DURATION_HISTOGRAM_ID, window)) {
TelemetryStopwatch.finish(BACKGROUND_DURATION_HISTOGRAM_ID, window);
} else if (
TelemetryStopwatch.running(FOREGROUND_DURATION_HISTOGRAM_ID, window)
) {
TelemetryStopwatch.finish(FOREGROUND_DURATION_HISTOGRAM_ID, window);
}
let browser = this.weakWinToBrowser.get(window);
this.removeOriginatingWinFromWeakMap(browser);
gCurrentPlayerCount -= 1;
// Saves the location of the Picture in Picture window
this.savePosition(window);
this.clearPipTabIcon(window);
this.setUrlbarPipIconInactive(browser?.ownerGlobal);
},
/**
* Open a Picture in Picture window on the same screen as parentWin,
* sized based on the information in videoData.
*
* @param {ChromeWindow} parentWin
* The window hosting the browser that requested the Picture in
* Picture window.
*
* @param {object} videoData
* An object containing the following properties:
*
* videoHeight (int):
* The preferred height of the video.
*
* videoWidth (int):
* The preferred width of the video.
*
* @param {PictureInPictureParent} actorReference
* Reference to the calling PictureInPictureParent
*
* @returns {Promise}
* Resolves once the window has opened and loaded the player component.
*/
async openPipWindow(parentWin, videoData) {
let { top, left, width, height } = this.fitToScreen(parentWin, videoData);
let { left: resolvedLeft, top: resolvedTop } = this.resolveOverlapConflicts(
left,
top,
width,
height
);
top = Math.round(resolvedTop);
left = Math.round(resolvedLeft);
width = Math.round(width);
height = Math.round(height);
let features =
`${PLAYER_FEATURES},top=${top},left=${left},outerWidth=${width},` +
`outerHeight=${height}`;
let isPrivate = lazy.PrivateBrowsingUtils.isWindowPrivate(parentWin);
if (isPrivate) {
features += ",private";
}
let pipWindow = Services.ww.openWindow(
parentWin,
PLAYER_URI,
null,
features,
null
);
TelemetryStopwatch.start(
"FX_PICTURE_IN_PICTURE_WINDOW_OPEN_DURATION",
pipWindow,
{
inSeconds: true,
}
);
pipWindow.windowUtils.setResizeMargin(RESIZE_MARGIN_PX);
// If the window is Private the icon will have already been set when
// it was opened.
if (Services.appinfo.OS == "WINNT" && !isPrivate) {
lazy.WindowsUIUtils.setWindowIconNoData(pipWindow);
}
return new Promise(resolve => {
pipWindow.addEventListener(
"load",
() => {
resolve(pipWindow);
},
{ once: true }
);
});
},
/**
* This function tries to restore the last known Picture-in-Picture location
* and size. If those values are unknown or offscreen, then a default
* location and size is used.
*
* @param {ChromeWindow|PlayerWindow} requestingWin
* The window hosting the browser that requested the Picture in
* Picture window. If this is an existing player window then the returned
* player size and position will be determined based on the existing
* player window's size and position.
*
* @param {object} videoData
* An object containing the following properties:
*
* videoHeight (int):
* The preferred height of the video.
*
* videoWidth (int):
* The preferred width of the video.
*
* @returns {object}
* The size and position for the player window, in CSS pixels relative to
* requestingWin.
*
* top (int):
* The top position for the player window.
*
* left (int):
* The left position for the player window.
*
* width (int):
* The width of the player window.
*
* height (int):
* The height of the player window.
*/
fitToScreen(requestingWin, videoData) {
let { videoHeight, videoWidth } = videoData;
const isPlayer = requestingWin.document.location.href == PLAYER_URI;
let requestingCssToDesktopScale =
requestingWin.devicePixelRatio / requestingWin.desktopToDeviceScale;
let top, left, width, height;
if (!isPlayer) {
// requestingWin is a content window, load last PiP's dimensions
({ top, left, width, height } = this.loadPosition());
} else if (requestingWin.windowState === requestingWin.STATE_FULLSCREEN) {
// `requestingWin` is a PiP window and in fullscreen. We stored the size
// and position before entering fullscreen and we will use that to
// calculate the new position
({ top, left, width, height } = requestingWin.getDeferredResize());
left *= requestingCssToDesktopScale;
top *= requestingCssToDesktopScale;
} else {
// requestingWin is a PiP player, conserve its dimensions in this case
left = requestingWin.screenX * requestingCssToDesktopScale;
top = requestingWin.screenY * requestingCssToDesktopScale;
width = requestingWin.outerWidth;
height = requestingWin.outerHeight;
}
// Check that previous location and size were loaded.
// Note that at this point left and top are in desktop pixels, while width
// and height are in CSS pixels.
if (!isNaN(top) && !isNaN(left) && !isNaN(width) && !isNaN(height)) {
// Get the screen of the last PiP window. PiP screen will be the default
// screen if the point was not on a screen.
let PiPScreen = this.getWorkingScreen(left, top);
// Center position of PiP window.
let PipScreenCssToDesktopScale =
PiPScreen.defaultCSSScaleFactor / PiPScreen.contentsScaleFactor;
let centerX = left + (width * PipScreenCssToDesktopScale) / 2;
let centerY = top + (height * PipScreenCssToDesktopScale) / 2;
// We have the screen, now we will get the dimensions of the screen
let [PiPScreenLeft, PiPScreenTop, PiPScreenWidth, PiPScreenHeight] =
this.getAvailScreenSize(PiPScreen);
// Check that the center of the last PiP location is within the screen limits
// If it's not, then we will use the default size and position
if (
PiPScreenLeft <= centerX &&
centerX <= PiPScreenLeft + PiPScreenWidth &&
PiPScreenTop <= centerY &&
centerY <= PiPScreenTop + PiPScreenHeight
) {
let oldWidthDesktopPix = width * PipScreenCssToDesktopScale;
// The new PiP window will keep the height of the old
// PiP window and adjust the width to the correct ratio
width = Math.round((height * videoWidth) / videoHeight);
// Minimum window size on Windows is 136
if (AppConstants.platform == "win") {
width = 136 > width ? 136 : width;
}
let widthDesktopPix = width * PipScreenCssToDesktopScale;
let heightDesktopPix = height * PipScreenCssToDesktopScale;
// WIGGLE_ROOM allows the PiP window to be within 5 pixels of the right
// side of the screen to stay snapped to the right side
const WIGGLE_ROOM = 5;
// If the PiP window was right next to the right side of the screen
// then move the PiP window to the right the same distance that
// the width changes from previous width to current width
let rightScreen = PiPScreenLeft + PiPScreenWidth;
let distFromRight = rightScreen - (left + widthDesktopPix);
if (
0 < distFromRight &&
distFromRight <= WIGGLE_ROOM + (oldWidthDesktopPix - widthDesktopPix)
) {
left += distFromRight;
}
// Checks if some of the PiP window is off screen and
// if so it will adjust to move everything on screen
if (left < PiPScreenLeft) {
// off the left of the screen
// slide right
left = PiPScreenLeft;
}
if (top < PiPScreenTop) {
// off the top of the screen
// slide down
top = PiPScreenTop;
}
if (left + widthDesktopPix > PiPScreenLeft + PiPScreenWidth) {
// off the right of the screen
// slide left
left = PiPScreenLeft + PiPScreenWidth - widthDesktopPix;
}
if (top + heightDesktopPix > PiPScreenTop + PiPScreenHeight) {
// off the bottom of the screen
// slide up
top = PiPScreenTop + PiPScreenHeight - heightDesktopPix;
}
// Convert top / left from desktop to requestingWin-relative CSS pixels.
top /= requestingCssToDesktopScale;
left /= requestingCssToDesktopScale;
return { top, left, width, height };
}
}
// We don't have the size or position of the last PiP window, so fall
// back to calculating the default location.
let screen = this.getWorkingScreen(
requestingWin.screenX * requestingCssToDesktopScale,
requestingWin.screenY * requestingCssToDesktopScale,
requestingWin.outerWidth * requestingCssToDesktopScale,
requestingWin.outerHeight * requestingCssToDesktopScale
);
let [screenLeft, screenTop, screenWidth, screenHeight] =
this.getAvailScreenSize(screen);
let screenCssToDesktopScale =
screen.defaultCSSScaleFactor / screen.contentsScaleFactor;
// The Picture in Picture window will be a maximum of a quarter of
// the screen height, and a third of the screen width.
const MAX_HEIGHT = screenHeight / 4;
const MAX_WIDTH = screenWidth / 3;
width = videoWidth * screenCssToDesktopScale;
height = videoHeight * screenCssToDesktopScale;
let aspectRatio = videoWidth / videoHeight;
if (videoHeight > MAX_HEIGHT || videoWidth > MAX_WIDTH) {
// We're bigger than the max.
// Take the largest dimension and clamp it to the associated max.
// Recalculate the other dimension to maintain aspect ratio.
if (videoWidth >= videoHeight) {
// We're clamping the width, so the height must be adjusted to match
// the original aspect ratio. Since aspect ratio is width over height,
// that means we need to _divide_ the MAX_WIDTH by the aspect ratio to
// calculate the appropriate height.
width = MAX_WIDTH;
height = Math.round(MAX_WIDTH / aspectRatio);
} else {
// We're clamping the height, so the width must be adjusted to match
// the original aspect ratio. Since aspect ratio is width over height,
// this means we need to _multiply_ the MAX_HEIGHT by the aspect ratio
// to calculate the appropriate width.
height = MAX_HEIGHT;
width = Math.round(MAX_HEIGHT * aspectRatio);
}
}
// Now that we have the dimensions of the video, we need to figure out how
// to position it in the bottom right corner. Since we know the width of the
// available rect, we need to subtract the dimensions of the window we're
// opening to get the top left coordinates that openWindow expects.
//
// In event that the user has multiple displays connected, we have to
// calculate the top-left coordinate of the new window in absolute
// coordinates that span the entire display space, since this is what the
// openWindow expects for its top and left feature values.
//
// The screenWidth and screenHeight values only tell us the available
// dimensions on the screen that the parent window is on. We add these to
// the screenLeft and screenTop values, which tell us where this screen is
// located relative to the "origin" in absolute coordinates.
let isRTL = Services.locale.isAppLocaleRTL;
left = isRTL ? screenLeft : screenLeft + screenWidth - width;
top = screenTop + screenHeight - height;
// Convert top/left from desktop pixels to requestingWin-relative CSS
// pixels, and width / height to the target screen's CSS pixels, which is
// what we've made the size calculation against.
top /= requestingCssToDesktopScale;
left /= requestingCssToDesktopScale;
width /= screenCssToDesktopScale;
height /= screenCssToDesktopScale;
return { top, left, width, height };
},
/**
* This function will take the size and potential location of a new
* Picture-in-Picture player window, and try to return the location
* coordinates that will best ensure that the player window will not overlap
* with other pre-existing player windows.
*
* @param {int} left
* x position of left edge for Picture-in-Picture window that is being
* opened
* @param {int} top
* y position of top edge for Picture-in-Picture window that is being
* opened
* @param {int} width
* Width of Picture-in-Picture window that is being opened
* @param {int} height
* Height of Picture-in-Picture window that is being opened
*
* @returns {object}
* An object with the following properties:
*
* top (int):
* The recommended top position for the player window.
*
* left (int):
* The recommended left position for the player window.
*/
resolveOverlapConflicts(left, top, width, height) {
// This algorithm works by first identifying the possible candidate
// locations that the new player window could be placed without overlapping
// other player windows (assuming that a confict is discovered at all of
// course). The optimal candidate is then selected by its distance to the
// original conflict, shorter distances are better.
//
// Candidates are discovered by iterating over each of the sides of every
// pre-existing player window. One candidate is collected for each side.
// This is done to ensure that the new player window will be opened to
// tightly fit along the edge of another player window.
//
// These candidates are then pruned for candidates that will introduce
// further conflicts. Finally the ideal candidate is selected from this
// pool of remaining candidates, optimized for minimizing distance to
// the original conflict.
let playerRects = [];
for (let playerWin of Services.wm.getEnumerator(WINDOW_TYPE)) {
playerRects.push(
new Rect(
playerWin.screenX,
playerWin.screenY,
playerWin.outerWidth,
playerWin.outerHeight
)
);
}
const newPlayerRect = new Rect(left, top, width, height);
let conflictingPipRect = playerRects.find(rect =>
rect.intersects(newPlayerRect)
);
if (!conflictingPipRect) {
// no conflicts found
return { left, top };
}
const conflictLoc = conflictingPipRect.center();
// Will try to resolve a better placement only on the screen where
// the conflict occurred
const conflictScreen = this.getWorkingScreen(conflictLoc.x, conflictLoc.y);
const [screenTop, screenLeft, screenWidth, screenHeight] =
this.getAvailScreenSize(conflictScreen);
const screenRect = new Rect(
screenTop,
screenLeft,
screenWidth,
screenHeight
);
const getEdgeCandidates = rect => {
return [
// left edge's candidate
new Point(rect.left - newPlayerRect.width, rect.top),
// top edge's candidate
new Point(rect.left, rect.top - newPlayerRect.height),
// right edge's candidate
new Point(rect.right + newPlayerRect.width, rect.top),
// bottom edge's candidate
new Point(rect.left, rect.bottom),
];
};
let candidateLocations = [];
for (const playerRect of playerRects) {
for (let candidateLoc of getEdgeCandidates(playerRect)) {
const candidateRect = new Rect(
candidateLoc.x,
candidateLoc.y,
width,
height
);
if (!screenRect.contains(candidateRect)) {
continue;
}
// test that no PiPs conflict with this candidate box
if (playerRects.some(rect => rect.intersects(candidateRect))) {
continue;
}
const candidateCenter = candidateRect.center();
const candidateDistanceToConflict =
Math.abs(conflictLoc.x - candidateCenter.x) +
Math.abs(conflictLoc.y - candidateCenter.y);
candidateLocations.push({
distanceToConflict: candidateDistanceToConflict,
location: candidateLoc,
});
}
}
if (!candidateLocations.length) {
// if no suitable candidates can be found, return the original location
return { left, top };
}
// sort candidates by distance to the conflict, select the closest
const closestCandidate = candidateLocations.sort(
(firstCand, secondCand) =>
firstCand.distanceToConflict - secondCand.distanceToConflict
)[0];
if (!closestCandidate) {
// can occur if there were no valid candidates, return original location
return { left, top };
}
const resolvedX = closestCandidate.location.x;
const resolvedY = closestCandidate.location.y;
return { left: resolvedX, top: resolvedY };
},
/**
* Resizes the the PictureInPicture player window.
*
* @param {object} videoData
* The source video's data.
* @param {PictureInPictureParent} actorRef
* Reference to the PictureInPicture parent actor.
*/
resizePictureInPictureWindow(videoData, actorRef) {
let win = this.getWeakPipPlayer(actorRef);
if (!win) {
return;
}
win.resizeToVideo(this.fitToScreen(win, videoData));
},
/**
* Opens the context menu for toggling PictureInPicture.
*
* @param {Window} window
* @param {object} data
* Message data from the PictureInPictureToggleParent
*/
openToggleContextMenu(window, data) {
let document = window.document;
let popup = document.getElementById("pictureInPictureToggleContextMenu");
let contextMoveToggle = document.getElementById(
"context_MovePictureInPictureToggle"
);
// Set directional string for toggle position
let position = Services.prefs.getStringPref(
TOGGLE_POSITION_PREF,
TOGGLE_POSITION_RIGHT
);
switch (position) {
case TOGGLE_POSITION_RIGHT:
document.l10n.setAttributes(
contextMoveToggle,
"picture-in-picture-move-toggle-left"
);
break;
case TOGGLE_POSITION_LEFT:
document.l10n.setAttributes(
contextMoveToggle,
"picture-in-picture-move-toggle-right"
);
break;
}
// We synthesize a new MouseEvent to propagate the inputSource to the
// subsequently triggered popupshowing event.
let newEvent = document.createEvent("MouseEvent");
let screenX = data.screenXDevPx / window.devicePixelRatio;
let screenY = data.screenYDevPx / window.devicePixelRatio;
newEvent.initNSMouseEvent(
"contextmenu",
true,
true,
null,
0,
screenX,
screenY,
0,
0,
false,
false,
false,
false,
0,
null,
0,
data.mozInputSource
);
popup.openPopupAtScreen(newEvent.screenX, newEvent.screenY, true, newEvent);
},
hideToggle() {
Services.prefs.setBoolPref(TOGGLE_ENABLED_PREF, false);
Services.telemetry.recordEvent(
"pictureinpicture.settings",
"disable",
"player"
);
},
/**
* This is used in AsyncTabSwitcher.jsm and tabbrowser.js to check if the browser
* currently has a PiP window.
* If the browser has a PiP window we want to keep the browser in an active state because
* the browser is still partially visible.
* @param browser The browser to check if it has a PiP window
* @returns true if browser has PiP window else false
*/
isOriginatingBrowser(browser) {
return this.browserWeakMap.has(browser);
},
moveToggle() {
// Get the current position
let position = Services.prefs.getStringPref(
TOGGLE_POSITION_PREF,
TOGGLE_POSITION_RIGHT
);
let newPosition = "";
// Determine what the opposite position would be for that preference
switch (position) {
case TOGGLE_POSITION_RIGHT:
newPosition = TOGGLE_POSITION_LEFT;
break;
case TOGGLE_POSITION_LEFT:
newPosition = TOGGLE_POSITION_RIGHT;
break;
}
if (newPosition) {
Services.prefs.setStringPref(TOGGLE_POSITION_PREF, newPosition);
}
},
/**
* This function takes a screen and will return the left, top, width and
* height of the screen
* @param {Screen} screen
* The screen we need to get the size and coordinates of
*
* @returns {array}
* Size and location of screen in desktop pixels.
*
* screenLeft.value (int):
* The left position for the screen.
*
* screenTop.value (int):
* The top position for the screen.
*
* screenWidth.value (int):
* The width of the screen.
*
* screenHeight.value (int):
* The height of the screen.
*/
getAvailScreenSize(screen) {
let screenLeft = {},
screenTop = {},
screenWidth = {},
screenHeight = {};
screen.GetAvailRectDisplayPix(
screenLeft,
screenTop,
screenWidth,
screenHeight
);
return [
screenLeft.value,
screenTop.value,
screenWidth.value,
screenHeight.value,
];
},
/**
* This function takes in a rect in desktop pixels, and returns the screen it
* is located on.
*
* If the left and top are not on any screen, it will return the default
* screen.
*
* @param {int} left
* left or x coordinate
*
* @param {int} top
* top or y coordinate
*
* @param {int} width
* top or y coordinate
*
* @param {int} height
* top or y coordinate
*
* @returns {Screen} screen
* the screen the left and top are on otherwise, default screen
*/
getWorkingScreen(left, top, width = 1, height = 1) {
// Get the screen manager
let screenManager = Cc["@mozilla.org/gfx/screenmanager;1"].getService(
Ci.nsIScreenManager
);
// use screenForRect to get screen
// this returns the default screen if left and top are not
// on any screen
return screenManager.screenForRect(left, top, width, height);
},
/**
* Saves position and size of Picture-in-Picture window
* @param {Window} win The Picture-in-Picture window
*/
savePosition(win) {
let xulStore = Services.xulStore;
// We store left / top position in desktop pixels, like SessionStore does,
// so that we can restore them properly (as CSS pixels need to be relative
// to a screen, and we won't have a target screen to restore).
let cssToDesktopScale = win.devicePixelRatio / win.desktopToDeviceScale;
let left = win.screenX * cssToDesktopScale;
let top = win.screenY * cssToDesktopScale;
let width = win.outerWidth;
let height = win.outerHeight;
xulStore.setValue(PLAYER_URI, "picture-in-picture", "left", left);
xulStore.setValue(PLAYER_URI, "picture-in-picture", "top", top);
xulStore.setValue(PLAYER_URI, "picture-in-picture", "width", width);
xulStore.setValue(PLAYER_URI, "picture-in-picture", "height", height);
},
/**
* Load last Picture in Picture location and size
* @returns {object}
* The size and position of the last Picture in Picture window.
*
* top (int):
* The top position for the last player window.
* Otherwise NaN
*
* left (int):
* The left position for the last player window.
* Otherwise NaN
*
* width (int):
* The width of the player last window.
* Otherwise NaN
*
* height (int):
* The height of the player last window.
* Otherwise NaN
*/
loadPosition() {
let xulStore = Services.xulStore;
let left = parseInt(
xulStore.getValue(PLAYER_URI, "picture-in-picture", "left")
);
let top = parseInt(
xulStore.getValue(PLAYER_URI, "picture-in-picture", "top")
);
let width = parseInt(
xulStore.getValue(PLAYER_URI, "picture-in-picture", "width")
);
let height = parseInt(
xulStore.getValue(PLAYER_URI, "picture-in-picture", "height")
);
return { top, left, width, height };
},
setFirstSeen(dateSeconds) {
if (!dateSeconds) {
return;
}
Services.prefs.setIntPref(TOGGLE_FIRST_SEEN_PREF, dateSeconds);
},
setHasUsed(hasUsed) {
Services.prefs.setBoolPref(TOGGLE_HAS_USED_PREF, !!hasUsed);
},
};
|