summaryrefslogtreecommitdiffstats
path: root/comm/mail/base/content/widgets/browserPopups.js
blob: f6d2a2139f67afc4622ad78dd09aa6a4c4cf25c7 (plain)
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
/* 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-globals-from ../utilityOverlay.js */

/* globals saveURL */ // From contentAreaUtils.js
/* globals goUpdateCommand */ // From globalOverlay.js

var { AppConstants } = ChromeUtils.importESModule(
  "resource://gre/modules/AppConstants.sys.mjs"
);
var { InlineSpellChecker, SpellCheckHelper } = ChromeUtils.importESModule(
  "resource://gre/modules/InlineSpellChecker.sys.mjs"
);
var { PlacesUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/PlacesUtils.sys.mjs"
);
var { ShortcutUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/ShortcutUtils.sys.mjs"
);
var { XPCOMUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/XPCOMUtils.sys.mjs"
);
ChromeUtils.defineModuleGetter(
  this,
  "MailUtils",
  "resource:///modules/MailUtils.jsm"
);
var { E10SUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/E10SUtils.sys.mjs"
);

var gContextMenu;
var gSpellChecker = new InlineSpellChecker();

/** Called by ContextMenuParent.sys.mjs */
function openContextMenu({ data }, browser, actor) {
  if (!browser.hasAttribute("context")) {
    return;
  }

  let wgp = actor.manager;

  if (!wgp.isCurrentGlobal) {
    // Don't display context menus for unloaded documents
    return;
  }

  // NOTE: We don't use `wgp.documentURI` here as we want to use the failed
  // channel URI in the case we have loaded an error page.
  let documentURIObject = wgp.browsingContext.currentURI;

  let frameReferrerInfo = data.frameReferrerInfo;
  if (frameReferrerInfo) {
    frameReferrerInfo = E10SUtils.deserializeReferrerInfo(frameReferrerInfo);
  }

  let linkReferrerInfo = data.linkReferrerInfo;
  if (linkReferrerInfo) {
    linkReferrerInfo = E10SUtils.deserializeReferrerInfo(linkReferrerInfo);
  }

  let frameID = nsContextMenu.WebNavigationFrames.getFrameId(
    wgp.browsingContext
  );

  nsContextMenu.contentData = {
    context: data.context,
    browser,
    actor,
    editFlags: data.editFlags,
    spellInfo: data.spellInfo,
    principal: wgp.documentPrincipal,
    storagePrincipal: wgp.documentStoragePrincipal,
    documentURIObject,
    docLocation: data.docLocation,
    charSet: data.charSet,
    referrerInfo: E10SUtils.deserializeReferrerInfo(data.referrerInfo),
    frameReferrerInfo,
    linkReferrerInfo,
    contentType: data.contentType,
    contentDisposition: data.contentDisposition,
    frameID,
    frameOuterWindowID: frameID,
    frameBrowsingContext: wgp.browsingContext,
    selectionInfo: data.selectionInfo,
    disableSetDesktopBackground: data.disableSetDesktopBackground,
    loginFillInfo: data.loginFillInfo,
    parentAllowsMixedContent: data.parentAllowsMixedContent,
    userContextId: wgp.browsingContext.originAttributes.userContextId,
    webExtContextData: data.webExtContextData,
    cookieJarSettings: wgp.cookieJarSettings,
  };

  // Note: `popup` must be in `document`, but `browser` might be in a
  // different document, such as about:3pane.
  let popup = document.getElementById(browser.getAttribute("context"));
  let context = nsContextMenu.contentData.context;

  // Fill in some values in the context from the WindowGlobalParent actor.
  context.principal = wgp.documentPrincipal;
  context.storagePrincipal = wgp.documentStoragePrincipal;
  context.frameID = frameID;
  context.frameOuterWindowID = wgp.outerWindowId;
  context.frameBrowsingContextID = wgp.browsingContext.id;

  // We don't have access to the original event here, as that happened in
  // another process. Therefore we synthesize a new MouseEvent to propagate the
  // inputSource to the subsequently triggered popupshowing event.
  let newEvent = document.createEvent("MouseEvent");
  let screenX = context.screenXDevPx / window.devicePixelRatio;
  let screenY = context.screenYDevPx / window.devicePixelRatio;
  newEvent.initNSMouseEvent(
    "contextmenu",
    true,
    true,
    null,
    0,
    screenX,
    screenY,
    0,
    0,
    false,
    false,
    false,
    false,
    2,
    null,
    0,
    context.mozInputSource
  );
  popup.openPopupAtScreen(newEvent.screenX, newEvent.screenY, true, newEvent);
}

/**
 * Function to set the global nsContextMenu. Called by popupshowing on browserContext.
 *
 * @param {Event} event - The onpopupshowing event.
 * @returns {boolean}
 */
function browserContextOnShowing(event) {
  if (event.target.id != "browserContext") {
    return true;
  }

  gContextMenu = new nsContextMenu(event.target, event.shiftKey);
  return gContextMenu.shouldDisplay;
}

/**
 * Function to clear out the global nsContextMenu.
 *
 * @param {Event} event - The onpopuphiding event.
 */
function browserContextOnHiding(event) {
  if (event.target.id != "browserContext") {
    return;
  }

  gContextMenu.hiding();
  gContextMenu = null;
}

class nsContextMenu {
  constructor(aXulMenu, aIsShift) {
    this.xulMenu = aXulMenu;

    // Get contextual info.
    this.setContext();

    if (!this.shouldDisplay) {
      return;
    }

    this.isContentSelected =
      !this.selectionInfo || !this.selectionInfo.docSelectionIsCollapsed;

    if (!aIsShift) {
      // The rest of this block sends menu information to WebExtensions.
      let subject = {
        menu: aXulMenu,
        tab: document.getElementById("tabmail")
          ? document.getElementById("tabmail").currentTabInfo
          : window,
        timeStamp: this.timeStamp,
        isContentSelected: this.isContentSelected,
        inFrame: this.inFrame,
        isTextSelected: this.isTextSelected,
        onTextInput: this.onTextInput,
        onLink: this.onLink,
        onImage: this.onImage,
        onVideo: this.onVideo,
        onAudio: this.onAudio,
        onCanvas: this.onCanvas,
        onEditable: this.onEditable,
        onSpellcheckable: this.onSpellcheckable,
        onPassword: this.onPassword,
        srcUrl: this.mediaURL,
        frameUrl: this.contentData ? this.contentData.docLocation : undefined,
        pageUrl: this.browser ? this.browser.currentURI.spec : undefined,
        linkText: this.linkTextStr,
        linkUrl: this.linkURL,
        selectionText: this.isTextSelected
          ? this.selectionInfo.fullText
          : undefined,
        frameId: this.frameID,
        webExtBrowserType: this.webExtBrowserType,
        webExtContextData: this.contentData
          ? this.contentData.webExtContextData
          : undefined,
      };

      subject.wrappedJSObject = subject;
      Services.obs.notifyObservers(subject, "on-build-contextmenu");
    }

    // Reset after "on-build-contextmenu" notification in case selection was
    // changed during the notification.
    this.isContentSelected =
      !this.selectionInfo || !this.selectionInfo.docSelectionIsCollapsed;
    this.initItems();

    // If all items in the menu are hidden, set this.shouldDisplay to false
    // so that the callers know to not even display the empty menu.
    let contextPopup = document.getElementById("browserContext");
    for (let item of contextPopup.children) {
      if (!item.hidden) {
        return;
      }
    }

    // All items must have been hidden.
    this.shouldDisplay = false;
  }

  setContext() {
    let context = Object.create(null);

    if (nsContextMenu.contentData) {
      this.contentData = nsContextMenu.contentData;
      context = this.contentData.context;
      nsContextMenu.contentData = null;
    }

    this.shouldDisplay = !this.contentData || context.shouldDisplay;
    this.timeStamp = context.timeStamp;

    // Assign what's _possibly_ needed from `context` sent by ContextMenuChild.sys.mjs
    // Keep this consistent with the similar code in ContextMenu's _setContext
    this.bgImageURL = context.bgImageURL;
    this.imageDescURL = context.imageDescURL;
    this.imageInfo = context.imageInfo;
    this.mediaURL = context.mediaURL;

    this.canSpellCheck = context.canSpellCheck;
    this.hasBGImage = context.hasBGImage;
    this.hasMultipleBGImages = context.hasMultipleBGImages;
    this.isDesignMode = context.isDesignMode;
    this.inFrame = context.inFrame;
    this.inPDFViewer = context.inPDFViewer;
    this.inSrcdocFrame = context.inSrcdocFrame;
    this.inSyntheticDoc = context.inSyntheticDoc;

    this.link = context.link;
    this.linkDownload = context.linkDownload;
    this.linkProtocol = context.linkProtocol;
    this.linkTextStr = context.linkTextStr;
    this.linkURL = context.linkURL;
    this.linkURI = this.getLinkURI(); // can't send; regenerate

    this.onAudio = context.onAudio;
    this.onCanvas = context.onCanvas;
    this.onCompletedImage = context.onCompletedImage;
    this.onDRMMedia = context.onDRMMedia;
    this.onPiPVideo = context.onPiPVideo;
    this.onEditable = context.onEditable;
    this.onImage = context.onImage;
    this.onKeywordField = context.onKeywordField;
    this.onLink = context.onLink;
    this.onLoadedImage = context.onLoadedImage;
    this.onMailtoLink = context.onMailtoLink;
    this.onMozExtLink = context.onMozExtLink;
    this.onNumeric = context.onNumeric;
    this.onPassword = context.onPassword;
    this.onSaveableLink = context.onSaveableLink;
    this.onSpellcheckable = context.onSpellcheckable;
    this.onTextInput = context.onTextInput;
    this.onVideo = context.onVideo;

    this.target = context.target;
    this.targetIdentifier = context.targetIdentifier;

    this.principal = context.principal;
    this.storagePrincipal = context.storagePrincipal;
    this.frameID = context.frameID;
    this.frameOuterWindowID = context.frameOuterWindowID;
    this.frameBrowsingContext = BrowsingContext.get(
      context.frameBrowsingContextID
    );

    this.inSyntheticDoc = context.inSyntheticDoc;
    this.inAboutDevtoolsToolbox = context.inAboutDevtoolsToolbox;

    // Everything after this isn't sent directly from ContextMenu
    if (this.target) {
      this.ownerDoc = this.target.ownerDocument;
    }

    this.csp = E10SUtils.deserializeCSP(context.csp);

    if (!this.contentData) {
      return;
    }

    this.browser = this.contentData.browser;
    if (this.browser && this.browser.currentURI.spec == "about:blank") {
      this.shouldDisplay = false;
      return;
    }
    this.selectionInfo = this.contentData.selectionInfo;
    this.actor = this.contentData.actor;

    this.textSelected = this.selectionInfo?.text;
    this.isTextSelected = !!this.textSelected?.length;

    this.webExtBrowserType = this.browser.getAttribute(
      "webextension-view-type"
    );

    if (context.shouldInitInlineSpellCheckerUINoChildren) {
      gSpellChecker.initFromRemote(
        this.contentData.spellInfo,
        this.actor.manager
      );
    }

    if (this.contentData.spellInfo) {
      this.spellSuggestions = this.contentData.spellInfo.spellSuggestions;
    }

    if (context.shouldInitInlineSpellCheckerUIWithChildren) {
      gSpellChecker.initFromRemote(
        this.contentData.spellInfo,
        this.actor.manager
      );
      let canSpell = gSpellChecker.canSpellCheck && this.canSpellCheck;
      this.showItem("browserContext-spell-check-enabled", canSpell);
      this.showItem("browserContext-spell-separator", canSpell);
    }
  }

  hiding() {
    if (this.actor) {
      this.actor.hiding();
    }

    this.contentData = null;
    gSpellChecker.clearSuggestionsFromMenu();
    gSpellChecker.clearDictionaryListFromMenu();
    gSpellChecker.uninit();
  }

  initItems() {
    this.initSaveItems();
    this.initClipboardItems();
    this.initMediaPlayerItems();
    this.initBrowserItems();
    this.initSpellingItems();
    this.initSeparators();
  }
  addDictionaries() {
    openDictionaryList();
  }
  initSpellingItems() {
    let canSpell =
      gSpellChecker.canSpellCheck &&
      !gSpellChecker.initialSpellCheckPending &&
      this.canSpellCheck;
    let showDictionaries = canSpell && gSpellChecker.enabled;
    let onMisspelling = gSpellChecker.overMisspelling;
    let showUndo = canSpell && gSpellChecker.canUndo();
    this.showItem("browserContext-spell-check-enabled", canSpell);
    this.showItem("browserContext-spell-separator", canSpell);
    document
      .getElementById("browserContext-spell-check-enabled")
      .setAttribute("checked", canSpell && gSpellChecker.enabled);

    this.showItem("browserContext-spell-add-to-dictionary", onMisspelling);
    this.showItem("browserContext-spell-undo-add-to-dictionary", showUndo);

    // suggestion list
    this.showItem(
      "browserContext-spell-suggestions-separator",
      onMisspelling || showUndo
    );
    if (onMisspelling) {
      let addMenuItem = document.getElementById(
        "browserContext-spell-add-to-dictionary"
      );
      let suggestionCount = gSpellChecker.addSuggestionsToMenu(
        addMenuItem.parentNode,
        addMenuItem,
        this.spellSuggestions
      );
      this.showItem(
        "browserContext-spell-no-suggestions",
        suggestionCount == 0
      );
    } else {
      this.showItem("browserContext-spell-no-suggestions", false);
    }

    // dictionary list
    this.showItem("browserContext-spell-dictionaries", showDictionaries);
    if (canSpell) {
      let dictMenu = document.getElementById(
        "browserContext-spell-dictionaries-menu"
      );
      let dictSep = document.getElementById(
        "browserContext-spell-language-separator"
      );
      let count = gSpellChecker.addDictionaryListToMenu(dictMenu, dictSep);
      this.showItem(dictSep, count > 0);
      this.showItem("browserContext-spell-add-dictionaries-main", false);
    } else if (this.onSpellcheckable) {
      // when there is no spellchecker but we might be able to spellcheck
      // add the add to dictionaries item. This will ensure that people
      // with no dictionaries will be able to download them
      this.showItem(
        "browserContext-spell-language-separator",
        showDictionaries
      );
      this.showItem(
        "browserContext-spell-add-dictionaries-main",
        showDictionaries
      );
    } else {
      this.showItem("browserContext-spell-add-dictionaries-main", false);
    }
  }
  initSaveItems() {
    this.showItem("browserContext-savelink", this.onSaveableLink);
    this.showItem("browserContext-saveimage", this.onLoadedImage);
  }
  initClipboardItems() {
    // Copy depends on whether there is selected text.
    // Enabling this context menu item is now done through the global
    // command updating system.

    goUpdateGlobalEditMenuItems();

    this.showItem("browserContext-cut", this.onTextInput);
    this.showItem(
      "browserContext-copy",
      !this.onPlayableMedia && (this.isContentSelected || this.onTextInput)
    );
    this.showItem("browserContext-paste", this.onTextInput);

    this.showItem("browserContext-undo", this.onTextInput);
    // Select all not available in the thread pane or on playable media.
    this.showItem("browserContext-selectall", !this.onPlayableMedia);
    this.showItem("browserContext-copyemail", this.onMailtoLink);
    this.showItem("browserContext-copylink", this.onLink && !this.onMailtoLink);
    this.showItem("browserContext-copyimage", this.onImage);

    this.showItem("browserContext-composeemailto", this.onMailtoLink);
    this.showItem("browserContext-addemail", this.onMailtoLink);

    let searchTheWeb = document.getElementById("browserContext-searchTheWeb");
    this.showItem(
      searchTheWeb,
      !this.onPlayableMedia && this.isContentSelected
    );

    if (!searchTheWeb.hidden) {
      let selection = this.textSelected;

      let bundle = document.getElementById("bundle_messenger");
      let key = "openSearch.label";
      let abbrSelection;
      if (selection.length > 15) {
        key += ".truncated";
        abbrSelection = selection.slice(0, 15);
      } else {
        abbrSelection = selection;
      }

      searchTheWeb.label = bundle.getFormattedString(key, [
        Services.search.defaultEngine.name,
        abbrSelection,
      ]);
      searchTheWeb.value = selection;
    }
  }
  initMediaPlayerItems() {
    let onMedia = this.onVideo || this.onAudio;
    // Several mutually exclusive items.... play/pause, mute/unmute, show/hide
    this.showItem("browserContext-media-play", onMedia && this.target.paused);
    this.showItem("browserContext-media-pause", onMedia && !this.target.paused);
    this.showItem("browserContext-media-mute", onMedia && !this.target.muted);
    this.showItem("browserContext-media-unmute", onMedia && this.target.muted);
    if (onMedia) {
      let hasError =
        this.target.error != null ||
        this.target.networkState == this.target.NETWORK_NO_SOURCE;
      this.setItemAttr("browserContext-media-play", "disabled", hasError);
      this.setItemAttr("browserContext-media-pause", "disabled", hasError);
      this.setItemAttr("browserContext-media-mute", "disabled", hasError);
      this.setItemAttr("browserContext-media-unmute", "disabled", hasError);
    }
  }
  initBackForwardMenuItemTooltip(menuItemId, l10nId, shortcutId) {
    // On macOS regular menuitems are used and the shortcut isn't added.
    if (AppConstants.platform == "macosx") {
      return;
    }

    let shortcut = document.getElementById(shortcutId);
    if (shortcut) {
      shortcut = ShortcutUtils.prettifyShortcut(shortcut);
    } else {
      // Sidebar doesn't have navigation buttons or shortcuts, but we still
      // want to format the menu item tooltip to remove "$shortcut" string.
      shortcut = "";
    }
    let menuItem = document.getElementById(menuItemId);
    document.l10n.setAttributes(menuItem, l10nId, { shortcut });
  }
  initBrowserItems() {
    // Work out if we are a context menu on a special item e.g. an image, link
    // etc.
    let onSpecialItem =
      this.isContentSelected ||
      this.onCanvas ||
      this.onLink ||
      this.onImage ||
      this.onAudio ||
      this.onVideo ||
      this.onTextInput;

    // Internal about:* pages should not show nav items.
    let shouldShowNavItems =
      !onSpecialItem && this.browser.currentURI.scheme != "about";

    // Ensure these commands are updated with their current status.
    if (shouldShowNavItems) {
      goUpdateCommand("Browser:Back");
      goUpdateCommand("Browser:Forward");
      goUpdateCommand("cmd_stop");
      goUpdateCommand("cmd_reload");
    }

    let stopped = document.getElementById("cmd_stop").hasAttribute("disabled");
    this.showItem("browserContext-reload", shouldShowNavItems && stopped);
    this.showItem("browserContext-stop", shouldShowNavItems && !stopped);
    this.showItem("browserContext-sep-navigation", shouldShowNavItems);

    if (AppConstants.platform == "macosx") {
      this.showItem("browserContext-back", shouldShowNavItems);
      this.showItem("browserContext-forward", shouldShowNavItems);
    } else {
      this.showItem("context-navigation", shouldShowNavItems);

      this.initBackForwardMenuItemTooltip(
        "browserContext-back",
        "content-tab-menu-back",
        "key_goBackKb"
      );
      this.initBackForwardMenuItemTooltip(
        "browserContext-forward",
        "content-tab-menu-forward",
        "key_goForwardKb"
      );
    }

    // Only show open in browser if we're not on a special item and we're not
    // on an about: or chrome: protocol - for these protocols the browser is
    // unlikely to show the same thing as we do (if at all), so therefore don't
    // offer the option.
    this.showItem(
      "browserContext-openInBrowser",
      !onSpecialItem &&
        ["http", "https"].includes(this.contentData?.documentURIObject?.scheme)
    );

    // Only show browserContext-openLinkInBrowser if we're on a link and it isn't
    // a mailto link.
    this.showItem(
      "browserContext-openLinkInBrowser",
      this.onLink && ["http", "https"].includes(this.linkProtocol)
    );
  }
  initSeparators() {
    let separators = Array.from(
      this.xulMenu.querySelectorAll(":scope > menuseparator")
    );
    let lastShownSeparator = null;
    for (let separator of separators) {
      let shouldShow = this.shouldShowSeparator(separator);
      if (
        !shouldShow &&
        lastShownSeparator &&
        separator.classList.contains("webextension-group-separator")
      ) {
        // The separator for the WebExtension elements group must be shown, hide
        // the last shown menu separator instead.
        lastShownSeparator.hidden = true;
        shouldShow = true;
      }
      if (shouldShow) {
        lastShownSeparator = separator;
      }
      separator.hidden = !shouldShow;
    }
    this.checkLastSeparator(this.xulMenu);
  }

  /**
   * Get a computed style property for an element.
   *
   * @param  aElem
   *         A DOM node
   * @param  aProp
   *         The desired CSS property
   * @returns the value of the property
   */
  getComputedStyle(aElem, aProp) {
    return aElem.ownerGlobal.getComputedStyle(aElem).getPropertyValue(aProp);
  }

  /**
   * Determine whether the clicked-on link can be saved, and whether it
   * may be saved according to the ScriptSecurityManager.
   *
   * @returns true if the protocol can be persisted and if the target has
   *         permission to link to the URL, false if not
   */
  isLinkSaveable() {
    try {
      Services.scriptSecurityManager.checkLoadURIWithPrincipal(
        this.target.nodePrincipal,
        this.linkURI,
        Ci.nsIScriptSecurityManager.STANDARD
      );
    } catch (e) {
      // Don't save things we can't link to.
      return false;
    }

    // We don't do the Right Thing for news/snews yet, so turn them off
    // until we do.
    return (
      this.linkProtocol &&
      !(
        this.linkProtocol == "mailto" ||
        this.linkProtocol == "javascript" ||
        this.linkProtocol == "news" ||
        this.linkProtocol == "snews"
      )
    );
  }

  /**
   * Save URL of clicked-on link.
   */
  saveLink() {
    saveURL(
      this.linkURL,
      null,
      this.linkTextStr,
      null,
      true,
      null,
      null,
      null,
      document
    );
  }

  /**
   * Save a clicked-on image.
   */
  saveImage() {
    saveURL(
      this.imageInfo.currentSrc,
      null,
      null,
      "SaveImageTitle",
      false,
      null,
      null,
      null,
      document
    );
  }

  /**
   * Extract email addresses from a mailto: link and put them on the
   * clipboard.
   */
  copyEmail() {
    // Copy the comma-separated list of email addresses only.
    // There are other ways of embedding email addresses in a mailto:
    // link, but such complex parsing is beyond us.

    const kMailToLength = 7; // length of "mailto:"

    var url = this.linkURL;
    var qmark = url.indexOf("?");
    var addresses;

    if (qmark > kMailToLength) {
      addresses = url.substring(kMailToLength, qmark);
    } else {
      addresses = url.substr(kMailToLength);
    }

    // Let's try to unescape it using a character set.
    try {
      addresses = Services.textToSubURI.unEscapeURIForUI(addresses);
    } catch (ex) {
      // Do nothing.
    }

    var clipboard = Cc["@mozilla.org/widget/clipboardhelper;1"].getService(
      Ci.nsIClipboardHelper
    );
    clipboard.copyString(addresses);
  }

  // ---------
  // Utilities

  /**
   * Set a DOM node's hidden property by passing in the node's id or the
   * element itself.
   *
   * @param aItemOrId
   *        a DOM node or the id of a DOM node
   * @param aShow
   *        true to show, false to hide
   */
  showItem(aItemOrId, aShow) {
    var item =
      aItemOrId.constructor == String
        ? document.getElementById(aItemOrId)
        : aItemOrId;
    if (item) {
      item.hidden = !aShow;
    }
  }

  /**
   * Set a DOM node's disabled property by passing in the node's id or the
   * element itself.
   *
   * @param aItemOrId  A DOM node or the id of a DOM node
   * @param aEnabled   True to enable the element, false to disable.
   */
  enableItem(aItemOrId, aEnabled) {
    var item =
      aItemOrId.constructor == String
        ? document.getElementById(aItemOrId)
        : aItemOrId;
    item.disabled = !aEnabled;
  }

  /**
   * Set given attribute of specified context-menu item. If the
   * value is null, then it removes the attribute (which works
   * nicely for the disabled attribute).
   *
   * @param  aId
   *         The id of an element
   * @param  aAttr
   *         The attribute name
   * @param  aVal
   *         The value to set the attribute to, or null to remove the attribute
   */
  setItemAttr(aId, aAttr, aVal) {
    var elem = document.getElementById(aId);
    if (elem) {
      if (aVal == null) {
        // null indicates attr should be removed.
        elem.removeAttribute(aAttr);
      } else {
        // Set attr=val.
        elem.setAttribute(aAttr, aVal);
      }
    }
  }

  /**
   * Get an absolute URL for clicked-on link, from the href property or by
   * resolving an XLink URL by hand.
   *
   * @returns the string absolute URL for the clicked-on link
   */
  getLinkURL() {
    if (this.link.href) {
      return this.link.href;
    }
    var href = this.link.getAttributeNS("http://www.w3.org/1999/xlink", "href");
    if (!href || href.trim() == "") {
      // Without this we try to save as the current doc,
      // for example, HTML case also throws if empty.
      throw new Error("Empty href");
    }
    href = this.makeURLAbsolute(this.link.baseURI, href);
    return href;
  }

  /**
   * Generate a URI object from the linkURL spec
   *
   * @returns an nsIURI if possible, or null if not
   */
  getLinkURI() {
    try {
      return Services.io.newURI(this.linkURL);
    } catch (ex) {
      // e.g. empty URL string
    }
    return null;
  }

  /**
   * Get the scheme for the clicked-on linkURI, if present.
   *
   * @returns a scheme, possibly undefined, or null if there's no linkURI
   */
  getLinkProtocol() {
    if (this.linkURI) {
      return this.linkURI.scheme; // Can be |undefined|.
    }

    return null;
  }

  /**
   * Get the text of the clicked-on link.
   *
   * @returns {string}
   */
  linkText() {
    return this.linkTextStr;
  }

  /**
   * Determines whether the focused window has something selected.
   *
   * @returns true if there is a selection, false if not
   */
  isContentSelection() {
    return !document.commandDispatcher.focusedWindow.getSelection().isCollapsed;
  }

  /**
   * Convert relative URL to absolute, using a provided <base>.
   *
   * @param  aBase
   *         The URL string to use as the base
   * @param  aUrl
   *         The possibly-relative URL string
   * @returns The string absolute URL
   */
  makeURLAbsolute(aBase, aUrl) {
    // Construct nsIURL.
    var baseURI = Services.io.newURI(aBase);

    return Services.io.newURI(baseURI.resolve(aUrl)).spec;
  }

  /**
   * Determine whether a DOM node is a text or password input, or a textarea.
   *
   * @param  aNode
   *         The DOM node to check
   * @returns true for textboxes, false for other elements
   */
  isTargetATextBox(aNode) {
    if (HTMLInputElement.isInstance(aNode)) {
      return aNode.type == "text" || aNode.type == "password";
    }

    return HTMLTextAreaElement.isInstance(aNode);
  }

  /**
   * Determine whether a separator should be shown based on whether
   * there are any non-hidden items between it and the previous separator.
   *
   * @param {DomElement} element - The separator element.
   * @returns {boolean} True if the separator should be shown, false if not.
   */
  shouldShowSeparator(element) {
    if (element) {
      let sibling = element.previousElementSibling;
      while (sibling && sibling.localName != "menuseparator") {
        if (!sibling.hidden) {
          return true;
        }
        sibling = sibling.previousElementSibling;
      }
    }
    return false;
  }

  /**
   * Ensures that there isn't a separator shown at the bottom of the menu.
   *
   * @param aPopup  The menu to check.
   */
  checkLastSeparator(aPopup) {
    let sibling = aPopup.lastElementChild;
    while (sibling) {
      if (!sibling.hidden) {
        if (sibling.localName == "menuseparator") {
          // If we got here then the item is a menuseparator and everything
          // below it hidden.
          sibling.setAttribute("hidden", true);
          return;
        }
        return;
      }
      sibling = sibling.previousElementSibling;
    }
  }

  openInBrowser() {
    let url = this.contentData?.documentURIObject?.spec;
    if (!url) {
      return;
    }
    PlacesUtils.history
      .insert({
        url,
        visits: [
          {
            date: new Date(),
          },
        ],
      })
      .catch(console.error);
    Cc["@mozilla.org/uriloader/external-protocol-service;1"]
      .getService(Ci.nsIExternalProtocolService)
      .loadURI(Services.io.newURI(url));
  }

  openLinkInBrowser() {
    PlacesUtils.history
      .insert({
        url: this.linkURL,
        visits: [
          {
            date: new Date(),
          },
        ],
      })
      .catch(console.error);
    Cc["@mozilla.org/uriloader/external-protocol-service;1"]
      .getService(Ci.nsIExternalProtocolService)
      .loadURI(this.linkURI);
  }

  mediaCommand(command) {
    var media = this.target;

    switch (command) {
      case "play":
        media.play();
        break;
      case "pause":
        media.pause();
        break;
      case "mute":
        media.muted = true;
        break;
      case "unmute":
        media.muted = false;
        break;
      // XXX hide controls & show controls don't work in emails as Javascript is
      // disabled. May want to consider later for RSS feeds.
    }
  }
}

ChromeUtils.defineESModuleGetters(nsContextMenu, {
  WebNavigationFrames: "resource://gre/modules/WebNavigationFrames.sys.mjs",
});