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
|
/* -*- mode: js; indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ts=2 sw=2 sts=2 et tw=80: */
/* 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/. */
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
ContentDOMReference: "resource://gre/modules/ContentDOMReference.sys.mjs",
E10SUtils: "resource://gre/modules/E10SUtils.sys.mjs",
InlineSpellCheckerContent:
"resource://gre/modules/InlineSpellCheckerContent.sys.mjs",
LoginHelper: "resource://gre/modules/LoginHelper.sys.mjs",
LoginManagerChild: "resource://gre/modules/LoginManagerChild.sys.mjs",
SelectionUtils: "resource://gre/modules/SelectionUtils.sys.mjs",
SpellCheckHelper: "resource://gre/modules/InlineSpellChecker.sys.mjs",
});
let contextMenus = new WeakMap();
export class ContextMenuChild extends JSWindowActorChild {
// PUBLIC
constructor() {
super();
this.target = null;
this.context = null;
this.lastMenuTarget = null;
}
static getTarget(browsingContext, message, key) {
let actor = contextMenus.get(browsingContext);
if (!actor) {
throw new Error(
"Can't find ContextMenu actor for browsing context with " +
"ID: " +
browsingContext.id
);
}
return actor.getTarget(message, key);
}
static getLastTarget(browsingContext) {
let contextMenu = contextMenus.get(browsingContext);
return contextMenu && contextMenu.lastMenuTarget;
}
receiveMessage(message) {
switch (message.name) {
case "ContextMenu:GetFrameTitle": {
let target = lazy.ContentDOMReference.resolve(
message.data.targetIdentifier
);
return Promise.resolve(target.ownerDocument.title);
}
case "ContextMenu:Canvas:ToBlobURL": {
let target = lazy.ContentDOMReference.resolve(
message.data.targetIdentifier
);
return new Promise(resolve => {
target.toBlob(blob => {
let blobURL = URL.createObjectURL(blob);
resolve(blobURL);
});
});
}
case "ContextMenu:Hiding": {
this.context = null;
this.target = null;
break;
}
case "ContextMenu:MediaCommand": {
lazy.E10SUtils.wrapHandlingUserInput(
this.contentWindow,
message.data.handlingUserInput,
() => {
let media = lazy.ContentDOMReference.resolve(
message.data.targetIdentifier
);
switch (message.data.command) {
case "play":
media.play();
break;
case "pause":
media.pause();
break;
case "loop":
media.loop = !media.loop;
break;
case "mute":
media.muted = true;
break;
case "unmute":
media.muted = false;
break;
case "playbackRate":
media.playbackRate = message.data.data;
break;
case "hidecontrols":
media.removeAttribute("controls");
break;
case "showcontrols":
media.setAttribute("controls", "true");
break;
case "fullscreen":
if (this.document.fullscreenEnabled) {
media.requestFullscreen();
}
break;
case "pictureinpicture":
if (!media.isCloningElementVisually) {
Services.telemetry.keyedScalarAdd(
"pictureinpicture.opened_method",
"contextmenu",
1
);
}
let event = new this.contentWindow.CustomEvent(
"MozTogglePictureInPicture",
{
bubbles: true,
detail: { reason: "contextMenu" },
},
this.contentWindow
);
media.dispatchEvent(event);
break;
}
}
);
break;
}
case "ContextMenu:ReloadFrame": {
let target = lazy.ContentDOMReference.resolve(
message.data.targetIdentifier
);
target.ownerDocument.location.reload(message.data.forceReload);
break;
}
case "ContextMenu:GetImageText": {
let img = lazy.ContentDOMReference.resolve(
message.data.targetIdentifier
);
const { direction } = this.contentWindow.getComputedStyle(img);
return img.recognizeCurrentImageText().then(results => {
return { results, direction };
});
}
case "ContextMenu:ToggleRevealPassword": {
let target = lazy.ContentDOMReference.resolve(
message.data.targetIdentifier
);
target.revealPassword = !target.revealPassword;
break;
}
case "ContextMenu:UseRelayMask": {
const input = lazy.ContentDOMReference.resolve(
message.data.targetIdentifier
);
input.setUserInput(message.data.emailMask);
break;
}
case "ContextMenu:ReloadImage": {
let image = lazy.ContentDOMReference.resolve(
message.data.targetIdentifier
);
if (image instanceof Ci.nsIImageLoadingContent) {
image.forceReload();
}
break;
}
case "ContextMenu:SearchFieldBookmarkData": {
let node = lazy.ContentDOMReference.resolve(
message.data.targetIdentifier
);
let charset = node.ownerDocument.characterSet;
let formBaseURI = Services.io.newURI(node.form.baseURI, charset);
let formURI = Services.io.newURI(
node.form.getAttribute("action"),
charset,
formBaseURI
);
let spec = formURI.spec;
let isURLEncoded =
node.form.method.toUpperCase() == "POST" &&
(node.form.enctype == "application/x-www-form-urlencoded" ||
node.form.enctype == "");
let title = node.ownerDocument.title;
function escapeNameValuePair([aName, aValue]) {
if (isURLEncoded) {
return escape(aName + "=" + aValue);
}
return encodeURIComponent(aName) + "=" + encodeURIComponent(aValue);
}
let formData = new this.contentWindow.FormData(node.form);
formData.delete(node.name);
formData = Array.from(formData).map(escapeNameValuePair);
formData.push(
escape(node.name) + (isURLEncoded ? escape("=%s") : "=%s")
);
let postData;
if (isURLEncoded) {
postData = formData.join("&");
} else {
let separator = spec.includes("?") ? "&" : "?";
spec += separator + formData.join("&");
}
return Promise.resolve({ spec, title, postData, charset });
}
case "ContextMenu:SaveVideoFrameAsImage": {
let video = lazy.ContentDOMReference.resolve(
message.data.targetIdentifier
);
let canvas = this.document.createElementNS(
"http://www.w3.org/1999/xhtml",
"canvas"
);
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
let ctxDraw = canvas.getContext("2d");
ctxDraw.drawImage(video, 0, 0);
// Note: if changing the content type, don't forget to update
// consumers that also hardcode this content type.
return Promise.resolve(canvas.toDataURL("image/jpeg", ""));
}
case "ContextMenu:SetAsDesktopBackground": {
let target = lazy.ContentDOMReference.resolve(
message.data.targetIdentifier
);
// Paranoia: check disableSetDesktopBackground again, in case the
// image changed since the context menu was initiated.
let disable = this._disableSetDesktopBackground(target);
if (!disable) {
try {
Services.scriptSecurityManager.checkLoadURIWithPrincipal(
target.ownerDocument.nodePrincipal,
target.currentURI
);
let canvas = this.document.createElement("canvas");
canvas.width = target.naturalWidth;
canvas.height = target.naturalHeight;
let ctx = canvas.getContext("2d");
ctx.drawImage(target, 0, 0);
let dataURL = canvas.toDataURL();
let url = new URL(target.ownerDocument.location.href).pathname;
let imageName = url.substr(url.lastIndexOf("/") + 1);
return Promise.resolve({ failed: false, dataURL, imageName });
} catch (e) {
console.error(e);
}
}
return Promise.resolve({
failed: true,
dataURL: null,
imageName: null,
});
}
}
return undefined;
}
/**
* Returns the event target of the context menu, using a locally stored
* reference if possible. If not, and aMessage.objects is defined,
* aMessage.objects[aKey] is returned. Otherwise null.
* @param {Object} aMessage Message with a objects property
* @param {String} aKey Key for the target on aMessage.objects
* @return {Object} Context menu target
*/
getTarget(aMessage, aKey = "target") {
return this.target || (aMessage.objects && aMessage.objects[aKey]);
}
// PRIVATE
_isXULTextLinkLabel(aNode) {
const XUL_NS =
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
return (
aNode.namespaceURI == XUL_NS &&
aNode.tagName == "label" &&
aNode.classList.contains("text-link") &&
aNode.href
);
}
// Generate fully qualified URL for clicked-on link.
_getLinkURL() {
let href = this.context.link.href;
if (href) {
// Handle SVG links:
if (typeof href == "object" && href.animVal) {
return this._makeURLAbsolute(this.context.link.baseURI, href.animVal);
}
return href;
}
href =
this.context.link.getAttribute("href") ||
this.context.link.getAttributeNS("http://www.w3.org/1999/xlink", "href");
if (!href || !href.match(/\S/)) {
// Without this we try to save as the current doc,
// for example, HTML case also throws if empty
throw new Error("Empty href");
}
return this._makeURLAbsolute(this.context.link.baseURI, href);
}
_getLinkURI() {
try {
return Services.io.newURI(this.context.linkURL);
} catch (ex) {
// e.g. empty URL string
}
return null;
}
// Get text of link.
_getLinkText() {
let text = this._gatherTextUnder(this.context.link);
if (!text || !text.match(/\S/)) {
text = this.context.link.getAttribute("title");
if (!text || !text.match(/\S/)) {
text = this.context.link.getAttribute("alt");
if (!text || !text.match(/\S/)) {
text = this.context.linkURL;
}
}
}
return text;
}
_getLinkProtocol() {
if (this.context.linkURI) {
return this.context.linkURI.scheme; // can be |undefined|
}
return null;
}
// Returns true if clicked-on link targets a resource that can be saved.
_isLinkSaveable() {
// We don't do the Right Thing for news/snews yet, so turn them off
// until we do.
return (
this.context.linkProtocol &&
!(
this.context.linkProtocol == "mailto" ||
this.context.linkProtocol == "tel" ||
this.context.linkProtocol == "javascript" ||
this.context.linkProtocol == "news" ||
this.context.linkProtocol == "snews"
)
);
}
// Gather all descendent text under given document node.
_gatherTextUnder(root) {
let text = "";
let node = root.firstChild;
let depth = 1;
while (node && depth > 0) {
// See if this node is text.
if (node.nodeType == node.TEXT_NODE) {
// Add this text to our collection.
text += " " + node.data;
} else if (this.contentWindow.HTMLImageElement.isInstance(node)) {
// If it has an "alt" attribute, add that.
let altText = node.getAttribute("alt");
if (altText && altText != "") {
text += " " + altText;
}
}
// Find next node to test.
// First, see if this node has children.
if (node.hasChildNodes()) {
// Go to first child.
node = node.firstChild;
depth++;
} else {
// No children, try next sibling (or parent next sibling).
while (depth > 0 && !node.nextSibling) {
node = node.parentNode;
depth--;
}
if (node.nextSibling) {
node = node.nextSibling;
}
}
}
// Strip leading and tailing whitespace.
text = text.trim();
// Compress remaining whitespace.
text = text.replace(/\s+/g, " ");
return text;
}
// Returns a "url"-type computed style attribute value, with the url() stripped.
_getComputedURL(aElem, aProp) {
let urls = aElem.ownerGlobal.getComputedStyle(aElem).getCSSImageURLs(aProp);
if (!urls.length) {
return null;
}
if (urls.length != 1) {
throw new Error("found multiple URLs");
}
return urls[0];
}
_makeURLAbsolute(aBase, aUrl) {
return Services.io.newURI(aUrl, null, Services.io.newURI(aBase)).spec;
}
_isProprietaryDRM() {
return (
this.context.target.isEncrypted &&
this.context.target.mediaKeys &&
this.context.target.mediaKeys.keySystem != "org.w3.clearkey"
);
}
_isMediaURLReusable(aURL) {
if (aURL.startsWith("blob:")) {
return URL.isValidObjectURL(aURL);
}
return true;
}
_isTargetATextBox(node) {
if (this.contentWindow.HTMLInputElement.isInstance(node)) {
return node.mozIsTextField(false);
}
return this.contentWindow.HTMLTextAreaElement.isInstance(node);
}
_isSpellCheckEnabled(aNode) {
// We can always force-enable spellchecking on textboxes
if (this._isTargetATextBox(aNode)) {
return true;
}
// We can never spell check something which is not content editable
let editable = aNode.isContentEditable;
if (!editable && aNode.ownerDocument) {
editable = aNode.ownerDocument.designMode == "on";
}
if (!editable) {
return false;
}
// Otherwise make sure that nothing in the parent chain disables spellchecking
return aNode.spellcheck;
}
_disableSetDesktopBackground(aTarget) {
// Disable the Set as Desktop Background menu item if we're still trying
// to load the image or the load failed.
if (!(aTarget instanceof Ci.nsIImageLoadingContent)) {
return true;
}
if ("complete" in aTarget && !aTarget.complete) {
return true;
}
if (aTarget.currentURI.schemeIs("javascript")) {
return true;
}
let request = aTarget.getRequest(Ci.nsIImageLoadingContent.CURRENT_REQUEST);
if (!request) {
return true;
}
return false;
}
async handleEvent(aEvent) {
contextMenus.set(this.browsingContext, this);
let defaultPrevented = aEvent.defaultPrevented;
if (
// If the event is not from a chrome-privileged document, and if
// `dom.event.contextmenu.enabled` is false, force defaultPrevented=false.
!aEvent.composedTarget.nodePrincipal.isSystemPrincipal &&
!Services.prefs.getBoolPref("dom.event.contextmenu.enabled")
) {
defaultPrevented = false;
}
if (defaultPrevented) {
return;
}
let doc = aEvent.composedTarget.ownerDocument;
if (!doc && Cu.isInAutomation) {
// doc has been observed to be null for many years, causing intermittent
// test failures all over the place (bug 1478596). The rate of failures
// is too low to debug locally, but frequent enough to be a nuisance.
// TODO bug 1478596: use these diagnostic logs to resolve the bug.
dump(
`doc is unexpectedly null (bug 1478596), composedTarget=${aEvent.composedTarget}\n`
);
// A potential fix is to fall back to aEvent.target.ownerDocument, per
// https://bugzilla.mozilla.org/show_bug.cgi?id=1478596#c1
// Let's print potentially viable alternatives to see what we should use.
for (let k of ["target", "originalTarget", "explicitOriginalTarget"]) {
dump(
` Alternative: ${k}=${aEvent[k]} and its doc=${aEvent[k]?.ownerDocument}\n`
);
}
}
let {
mozDocumentURIIfNotForErrorPages: docLocation,
characterSet: charSet,
baseURI,
} = doc;
docLocation = docLocation && docLocation.spec;
const loginManagerChild = lazy.LoginManagerChild.forWindow(doc.defaultView);
const docState = loginManagerChild.stateForDocument(doc);
const loginFillInfo = docState.getFieldContext(aEvent.composedTarget);
let disableSetDesktopBackground = null;
// Media related cache info parent needs for saving
let contentType = null;
let contentDisposition = null;
if (
aEvent.composedTarget.nodeType == aEvent.composedTarget.ELEMENT_NODE &&
aEvent.composedTarget instanceof Ci.nsIImageLoadingContent &&
aEvent.composedTarget.currentURI
) {
disableSetDesktopBackground = this._disableSetDesktopBackground(
aEvent.composedTarget
);
try {
let imageCache = Cc["@mozilla.org/image/tools;1"]
.getService(Ci.imgITools)
.getImgCacheForDocument(doc);
// The image cache's notion of where this image is located is
// the currentURI of the image loading content.
let props = imageCache.findEntryProperties(
aEvent.composedTarget.currentURI,
doc
);
try {
contentType = props.get("type", Ci.nsISupportsCString).data;
} catch (e) {}
try {
contentDisposition = props.get(
"content-disposition",
Ci.nsISupportsCString
).data;
} catch (e) {}
} catch (e) {}
}
let selectionInfo = lazy.SelectionUtils.getSelectionDetails(
this.contentWindow
);
this._setContext(aEvent);
let context = this.context;
this.target = context.target;
let spellInfo = null;
let editFlags = null;
let referrerInfo = Cc["@mozilla.org/referrer-info;1"].createInstance(
Ci.nsIReferrerInfo
);
referrerInfo.initWithElement(aEvent.composedTarget);
referrerInfo = lazy.E10SUtils.serializeReferrerInfo(referrerInfo);
// In the case "onLink" we may have to send link referrerInfo to use in
// _openLinkInParameters
let linkReferrerInfo = null;
if (context.onLink) {
linkReferrerInfo = Cc["@mozilla.org/referrer-info;1"].createInstance(
Ci.nsIReferrerInfo
);
linkReferrerInfo.initWithElement(context.link);
}
let target = context.target;
if (target) {
this._cleanContext();
}
editFlags = lazy.SpellCheckHelper.isEditable(
aEvent.composedTarget,
this.contentWindow
);
if (editFlags & lazy.SpellCheckHelper.SPELLCHECKABLE) {
spellInfo = lazy.InlineSpellCheckerContent.initContextMenu(
aEvent,
editFlags,
this
);
}
// Set the event target first as the copy image command needs it to
// determine what was context-clicked on. Then, update the state of the
// commands on the context menu.
this.docShell.docViewer
.QueryInterface(Ci.nsIDocumentViewerEdit)
.setCommandNode(aEvent.composedTarget);
aEvent.composedTarget.ownerGlobal.updateCommands("contentcontextmenu");
let data = {
context,
charSet,
baseURI,
referrerInfo,
editFlags,
contentType,
docLocation,
loginFillInfo,
selectionInfo,
contentDisposition,
disableSetDesktopBackground,
};
if (context.inFrame && !context.inSrcdocFrame) {
data.frameReferrerInfo = lazy.E10SUtils.serializeReferrerInfo(
doc.referrerInfo
);
}
if (linkReferrerInfo) {
data.linkReferrerInfo =
lazy.E10SUtils.serializeReferrerInfo(linkReferrerInfo);
}
// Notify observers (currently only webextensions) of the context menu being
// prepared, allowing them to set webExtContextData for us.
let prepareContextMenu = {
principal: doc.nodePrincipal,
setWebExtContextData(webExtContextData) {
data.webExtContextData = webExtContextData;
},
};
Services.obs.notifyObservers(prepareContextMenu, "on-prepare-contextmenu");
// In the event that the content is running in the parent process, we don't
// actually want the contextmenu events to reach the parent - we'll dispatch
// a new contextmenu event after the async message has reached the parent
// instead.
aEvent.stopPropagation();
data.spellInfo = null;
if (!spellInfo) {
this.sendAsyncMessage("contextmenu", data);
return;
}
try {
data.spellInfo = await spellInfo;
} catch (ex) {}
this.sendAsyncMessage("contextmenu", data);
}
/**
* Some things are not serializable, so we either have to only send
* their needed data or regenerate them in nsContextMenu.js
* - target and target.ownerDocument
* - link
* - linkURI
*/
_cleanContext() {
const context = this.context;
const cleanTarget = Object.create(null);
cleanTarget.ownerDocument = {
// used for nsContextMenu.initLeaveDOMFullScreenItems and
// nsContextMenu.initMediaPlayerItems
fullscreen: context.target.ownerDocument.fullscreen,
// used for nsContextMenu.initMiscItems
contentType: context.target.ownerDocument.contentType,
};
// used for nsContextMenu.initMediaPlayerItems
Object.assign(cleanTarget, {
ended: context.target.ended,
muted: context.target.muted,
paused: context.target.paused,
controls: context.target.controls,
duration: context.target.duration,
});
const onMedia = context.onVideo || context.onAudio;
if (onMedia) {
Object.assign(cleanTarget, {
loop: context.target.loop,
error: context.target.error,
networkState: context.target.networkState,
playbackRate: context.target.playbackRate,
NETWORK_NO_SOURCE: context.target.NETWORK_NO_SOURCE,
});
if (context.onVideo) {
Object.assign(cleanTarget, {
readyState: context.target.readyState,
HAVE_CURRENT_DATA: context.target.HAVE_CURRENT_DATA,
});
}
}
context.target = cleanTarget;
if (context.link) {
context.link = { href: context.linkURL };
}
delete context.linkURI;
}
_setContext(aEvent) {
this.context = Object.create(null);
const context = this.context;
context.timeStamp = aEvent.timeStamp;
context.screenXDevPx = aEvent.screenX * this.contentWindow.devicePixelRatio;
context.screenYDevPx = aEvent.screenY * this.contentWindow.devicePixelRatio;
context.inputSource = aEvent.inputSource;
let node = aEvent.composedTarget;
// Set the node to containing <video>/<audio>/<embed>/<object> if the node
// is in the videocontrols UA Widget.
if (node.containingShadowRoot?.isUAWidget()) {
const host = node.containingShadowRoot.host;
if (
this.contentWindow.HTMLMediaElement.isInstance(host) ||
this.contentWindow.HTMLEmbedElement.isInstance(host) ||
this.contentWindow.HTMLObjectElement.isInstance(host)
) {
node = host;
}
}
const XUL_NS =
"http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
context.shouldDisplay = true;
if (
node.nodeType == node.DOCUMENT_NODE ||
// Don't display for XUL element unless <label class="text-link">
(node.namespaceURI == XUL_NS && !this._isXULTextLinkLabel(node))
) {
context.shouldDisplay = false;
return;
}
const isAboutDevtoolsToolbox = this.document.documentURI.startsWith(
"about:devtools-toolbox"
);
const editFlags = lazy.SpellCheckHelper.isEditable(
node,
this.contentWindow
);
if (
isAboutDevtoolsToolbox &&
(editFlags & lazy.SpellCheckHelper.TEXTINPUT) === 0
) {
// Don't display for about:devtools-toolbox page unless the source was text input.
context.shouldDisplay = false;
return;
}
// Initialize context to be sent to nsContextMenu
// Keep this consistent with the similar code in nsContextMenu's setContext
context.bgImageURL = "";
context.imageDescURL = "";
context.imageInfo = null;
context.mediaURL = "";
context.webExtBrowserType = "";
context.canSpellCheck = false;
context.hasBGImage = false;
context.hasMultipleBGImages = false;
context.isDesignMode = false;
context.inFrame = false;
context.inPDFViewer = false;
context.inSrcdocFrame = false;
context.inSyntheticDoc = false;
context.inTabBrowser = true;
context.inWebExtBrowser = false;
context.link = null;
context.linkDownload = "";
context.linkProtocol = "";
context.linkTextStr = "";
context.linkURL = "";
context.linkURI = null;
context.onAudio = false;
context.onCanvas = false;
context.onCompletedImage = false;
context.onDRMMedia = false;
context.onPiPVideo = false;
context.onEditable = false;
context.onImage = false;
context.onKeywordField = false;
context.onLink = false;
context.onLoadedImage = false;
context.onMailtoLink = false;
context.onTelLink = false;
context.onMozExtLink = false;
context.onNumeric = false;
context.onPassword = false;
context.passwordRevealed = false;
context.onSaveableLink = false;
context.onSpellcheckable = false;
context.onTextInput = false;
context.onVideo = false;
context.inPDFEditor = false;
// Remember the node and its owner document that was clicked
// This may be modifed before sending to nsContextMenu
context.target = node;
context.targetIdentifier = lazy.ContentDOMReference.get(node);
context.csp = lazy.E10SUtils.serializeCSP(context.target.ownerDocument.csp);
// Check if we are in the PDF Viewer.
context.inPDFViewer =
context.target.ownerDocument.nodePrincipal.originNoSuffix ==
"resource://pdf.js";
if (context.inPDFViewer) {
context.pdfEditorStates = context.target.ownerDocument.editorStates;
context.inPDFEditor = !!context.pdfEditorStates?.isEditing;
}
// Check if we are in a synthetic document (stand alone image, video, etc.).
context.inSyntheticDoc = context.target.ownerDocument.mozSyntheticDocument;
context.shouldInitInlineSpellCheckerUINoChildren = false;
context.shouldInitInlineSpellCheckerUIWithChildren = false;
this._setContextForNodesNoChildren(editFlags);
this._setContextForNodesWithChildren(editFlags);
this.lastMenuTarget = {
// Remember the node for extensions.
targetRef: Cu.getWeakReference(node),
// The timestamp is used to verify that the target wasn't changed since the observed menu event.
timeStamp: context.timeStamp,
};
if (isAboutDevtoolsToolbox) {
// Setup the menu items on text input in about:devtools-toolbox.
context.inAboutDevtoolsToolbox = true;
context.canSpellCheck = false;
context.inTabBrowser = false;
context.inFrame = false;
context.inSrcdocFrame = false;
context.onSpellcheckable = false;
}
}
/**
* Sets up the parts of the context menu for when when nodes have no children.
*
* @param {Integer} editFlags The edit flags for the node. See SpellCheckHelper
* for the details.
*/
_setContextForNodesNoChildren(editFlags) {
const context = this.context;
if (context.target.nodeType == context.target.TEXT_NODE) {
// For text nodes, look at the parent node to determine the spellcheck attribute.
context.canSpellCheck =
context.target.parentNode && this._isSpellCheckEnabled(context.target);
return;
}
// We only deal with TEXT_NODE and ELEMENT_NODE in this function, so return
// early if we don't have one.
if (context.target.nodeType != context.target.ELEMENT_NODE) {
return;
}
// See if the user clicked on an image. This check mirrors
// nsDocumentViewer::GetInImage. Make sure to update both if this is
// changed.
if (
context.target instanceof Ci.nsIImageLoadingContent &&
(context.target.currentRequestFinalURI || context.target.currentURI)
) {
context.onImage = true;
context.imageInfo = {
currentSrc: context.target.currentSrc,
width: context.target.width,
height: context.target.height,
imageText: this.contentWindow.ImageDocument.isInstance(
context.target.ownerDocument
)
? undefined
: context.target.title || context.target.alt,
};
const { SVGAnimatedLength } = context.target.ownerGlobal;
if (SVGAnimatedLength.isInstance(context.imageInfo.height)) {
context.imageInfo.height = context.imageInfo.height.animVal.value;
}
if (SVGAnimatedLength.isInstance(context.imageInfo.width)) {
context.imageInfo.width = context.imageInfo.width.animVal.value;
}
const request = context.target.getRequest(
Ci.nsIImageLoadingContent.CURRENT_REQUEST
);
if (request && request.imageStatus & request.STATUS_SIZE_AVAILABLE) {
context.onLoadedImage = true;
}
if (
request &&
request.imageStatus & request.STATUS_LOAD_COMPLETE &&
!(request.imageStatus & request.STATUS_ERROR)
) {
context.onCompletedImage = true;
}
// The URL of the image before redirects is the currentURI. This is
// intended to be used for "Copy Image Link".
context.originalMediaURL = (() => {
let currentURI = context.target.currentURI?.spec;
if (currentURI && this._isMediaURLReusable(currentURI)) {
return currentURI;
}
return "";
})();
// The actual URL the image was loaded from (after redirects) is the
// currentRequestFinalURI. We should use that as the URL for purposes of
// deciding on the filename, if it is present. It might not be present
// if images are blocked.
//
// It is important to check both the final and the current URI, as they
// could be different blob URIs, see bug 1625786.
context.mediaURL = (() => {
let finalURI = context.target.currentRequestFinalURI?.spec;
if (finalURI && this._isMediaURLReusable(finalURI)) {
return finalURI;
}
let currentURI = context.target.currentURI?.spec;
if (currentURI && this._isMediaURLReusable(currentURI)) {
return currentURI;
}
return "";
})();
const descURL = context.target.getAttribute("longdesc");
if (descURL) {
context.imageDescURL = this._makeURLAbsolute(
context.target.ownerDocument.body.baseURI,
descURL
);
}
} else if (
this.contentWindow.HTMLCanvasElement.isInstance(context.target)
) {
context.onCanvas = true;
} else if (this.contentWindow.HTMLVideoElement.isInstance(context.target)) {
const mediaURL = context.target.currentSrc || context.target.src;
if (this._isMediaURLReusable(mediaURL)) {
context.mediaURL = mediaURL;
}
if (this._isProprietaryDRM()) {
context.onDRMMedia = true;
}
if (context.target.isCloningElementVisually) {
context.onPiPVideo = true;
}
// Firefox always creates a HTMLVideoElement when loading an ogg file
// directly. If the media is actually audio, be smarter and provide a
// context menu with audio operations.
if (
context.target.readyState >= context.target.HAVE_METADATA &&
(context.target.videoWidth == 0 || context.target.videoHeight == 0)
) {
context.onAudio = true;
} else {
context.onVideo = true;
}
} else if (this.contentWindow.HTMLAudioElement.isInstance(context.target)) {
context.onAudio = true;
const mediaURL = context.target.currentSrc || context.target.src;
if (this._isMediaURLReusable(mediaURL)) {
context.mediaURL = mediaURL;
}
if (this._isProprietaryDRM()) {
context.onDRMMedia = true;
}
} else if (
editFlags &
(lazy.SpellCheckHelper.INPUT | lazy.SpellCheckHelper.TEXTAREA)
) {
context.onTextInput = (editFlags & lazy.SpellCheckHelper.TEXTINPUT) !== 0;
context.onNumeric = (editFlags & lazy.SpellCheckHelper.NUMERIC) !== 0;
context.onEditable = (editFlags & lazy.SpellCheckHelper.EDITABLE) !== 0;
context.onPassword = (editFlags & lazy.SpellCheckHelper.PASSWORD) !== 0;
context.showRelay =
HTMLInputElement.isInstance(context.target) &&
!context.target.disabled &&
!context.target.readOnly &&
(lazy.LoginHelper.isInferredEmailField(context.target) ||
lazy.LoginHelper.isInferredUsernameField(context.target));
context.isDesignMode =
(editFlags & lazy.SpellCheckHelper.CONTENTEDITABLE) !== 0;
context.passwordRevealed =
context.onPassword && context.target.revealPassword;
context.onSpellcheckable =
(editFlags & lazy.SpellCheckHelper.SPELLCHECKABLE) !== 0;
// This is guaranteed to be an input or textarea because of the condition above,
// so the no-children flag is always correct. We deal with contenteditable elsewhere.
if (context.onSpellcheckable) {
context.shouldInitInlineSpellCheckerUINoChildren = true;
}
context.onKeywordField = editFlags & lazy.SpellCheckHelper.KEYWORD;
} else if (this.contentWindow.HTMLHtmlElement.isInstance(context.target)) {
const bodyElt = context.target.ownerDocument.body;
if (bodyElt) {
let computedURL;
try {
computedURL = this._getComputedURL(bodyElt, "background-image");
context.hasMultipleBGImages = false;
} catch (e) {
context.hasMultipleBGImages = true;
}
if (computedURL) {
context.hasBGImage = true;
context.bgImageURL = this._makeURLAbsolute(
bodyElt.baseURI,
computedURL
);
}
}
}
context.canSpellCheck = this._isSpellCheckEnabled(context.target);
}
/**
* Sets up the parts of the context menu for when when nodes have children.
*
* @param {Integer} editFlags The edit flags for the node. See SpellCheckHelper
* for the details.
*/
_setContextForNodesWithChildren(editFlags) {
const context = this.context;
// Second, bubble out, looking for items of interest that can have childen.
// Always pick the innermost link, background image, etc.
let elem = context.target;
while (elem) {
if (elem.nodeType == elem.ELEMENT_NODE) {
// Link?
const XLINK_NS = "http://www.w3.org/1999/xlink";
if (
!context.onLink &&
// Be consistent with what hrefAndLinkNodeForClickEvent
// does in browser.js
(this._isXULTextLinkLabel(elem) ||
(this.contentWindow.HTMLAnchorElement.isInstance(elem) &&
elem.href) ||
(this.contentWindow.SVGAElement.isInstance(elem) &&
(elem.href || elem.hasAttributeNS(XLINK_NS, "href"))) ||
(this.contentWindow.HTMLAreaElement.isInstance(elem) &&
elem.href) ||
this.contentWindow.HTMLLinkElement.isInstance(elem) ||
elem.getAttributeNS(XLINK_NS, "type") == "simple")
) {
// Target is a link or a descendant of a link.
context.onLink = true;
// Remember corresponding element.
context.link = elem;
context.linkURL = this._getLinkURL();
context.linkURI = this._getLinkURI();
context.linkTextStr = this._getLinkText();
context.linkProtocol = this._getLinkProtocol();
context.onMailtoLink = context.linkProtocol == "mailto";
context.onTelLink = context.linkProtocol == "tel";
context.onMozExtLink = context.linkProtocol == "moz-extension";
context.onSaveableLink = this._isLinkSaveable(context.link);
context.isSponsoredLink =
(elem.ownerDocument.URL === "about:newtab" ||
elem.ownerDocument.URL === "about:home") &&
elem.dataset.isSponsoredLink === "true";
try {
if (elem.download) {
// Ignore download attribute on cross-origin links
context.target.ownerDocument.nodePrincipal.checkMayLoad(
context.linkURI,
true
);
context.linkDownload = elem.download;
}
} catch (ex) {}
}
// Background image? Don't bother if we've already found a
// background image further down the hierarchy. Otherwise,
// we look for the computed background-image style.
if (!context.hasBGImage && !context.hasMultipleBGImages) {
let bgImgUrl = null;
try {
bgImgUrl = this._getComputedURL(elem, "background-image");
context.hasMultipleBGImages = false;
} catch (e) {
context.hasMultipleBGImages = true;
}
if (bgImgUrl) {
context.hasBGImage = true;
context.bgImageURL = this._makeURLAbsolute(elem.baseURI, bgImgUrl);
}
}
}
elem = elem.flattenedTreeParentNode;
}
// See if the user clicked in a frame.
const docDefaultView = context.target.ownerGlobal;
if (docDefaultView != docDefaultView.top) {
context.inFrame = true;
if (context.target.ownerDocument.isSrcdocDocument) {
context.inSrcdocFrame = true;
}
}
// if the document is editable, show context menu like in text inputs
if (!context.onEditable) {
if (editFlags & lazy.SpellCheckHelper.CONTENTEDITABLE) {
// If this.onEditable is false but editFlags is CONTENTEDITABLE, then
// the document itself must be editable.
context.onTextInput = true;
context.onKeywordField = false;
context.onImage = false;
context.onLoadedImage = false;
context.onCompletedImage = false;
context.inFrame = false;
context.inSrcdocFrame = false;
context.hasBGImage = false;
context.isDesignMode = true;
context.onEditable = true;
context.onSpellcheckable = true;
context.shouldInitInlineSpellCheckerUIWithChildren = true;
}
}
}
_destructionObservers = new Set();
registerDestructionObserver(obj) {
this._destructionObservers.add(obj);
}
unregisterDestructionObserver(obj) {
this._destructionObservers.delete(obj);
}
didDestroy() {
for (let obs of this._destructionObservers) {
obs.actorDestroyed(this);
}
this._destructionObservers = null;
}
}
|