Bug 1869043 allow a device to be specified with MediaTrackGraph::NotifyWhenDeviceStar...
[gecko.git] / toolkit / actors / KeyPressEventModelCheckerChild.sys.mjs
blob8b4fe82f1782a2e8962478cd758f222ac0813242
1 /* -*- mode: js; indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* vim: set ts=2 sw=2 sts=2 et tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
9 export class KeyPressEventModelCheckerChild extends JSWindowActorChild {
10   // Currently, the event is dispatched only when the document becomes editable
11   // because of contenteditable.  If you need to add new editor which is in
12   // designMode, you need to change MaybeDispatchCheckKeyPressEventModelEvent()
13   // of Document.
14   handleEvent(aEvent) {
15     if (!AppConstants.DEBUG) {
16       // Stop propagation in opt build to save the propagation cost.
17       // However, the event is necessary for running test_bug1514940.html.
18       // Therefore, we need to keep propagating it at least on debug build.
19       aEvent.stopImmediatePropagation();
20     }
22     // Currently, even if we set Document.KEYPRESS_EVENT_MODEL_CONFLATED
23     // here, conflated model isn't used forcibly.  If you need it, you need
24     // to change WidgetKeyboardEvent, dom::KeyboardEvent and PresShell.
25     let model = Document.KEYPRESS_EVENT_MODEL_DEFAULT;
26     if (
27       this._isOldOfficeOnlineServer(aEvent.target) ||
28       this._isOldConfluence(aEvent.target.ownerGlobal)
29     ) {
30       model = Document.KEYPRESS_EVENT_MODEL_SPLIT;
31     }
32     aEvent.target.setKeyPressEventModel(model);
33   }
35   _isOldOfficeOnlineServer(aDocument) {
36     let editingElement = aDocument.getElementById(
37       "WACViewPanel_EditingElement"
38     );
39     // If it's not Office Online Server, don't include it into the telemetry
40     // because we just need to collect percentage of old version in all loaded
41     // Office Online Server instances.
42     if (!editingElement) {
43       return false;
44     }
45     let isOldVersion = !editingElement.classList.contains(
46       "WACViewPanel_DisableLegacyKeyCodeAndCharCode"
47     );
48     Services.telemetry.keyedScalarAdd(
49       "dom.event.office_online_load_count",
50       isOldVersion ? "old" : "new",
51       1
52     );
53     return isOldVersion;
54   }
56   _isOldConfluence(aWindow) {
57     if (!aWindow) {
58       return false;
59     }
60     // aWindow should be an editor window in <iframe>.  However, we don't know
61     // whether it can be without <iframe>.  Anyway, there should be tinyMCE
62     // object in the parent window or in the window.
63     let tinyMCEObject;
64     // First, try to retrieve tinyMCE object from parent window.
65     try {
66       tinyMCEObject = ChromeUtils.waiveXrays(aWindow.parent).tinyMCE;
67     } catch (e) {
68       // Ignore the exception for now.
69     }
70     // Next, if there is no tinyMCE object in the parent window, let's check
71     // the window.
72     if (!tinyMCEObject) {
73       try {
74         tinyMCEObject = ChromeUtils.waiveXrays(aWindow).tinyMCE;
75       } catch (e) {
76         // Fallthrough to return false below.
77       }
78       // If we couldn't find tinyMCE object, let's assume that it's not
79       // Confluence instance.
80       if (!tinyMCEObject) {
81         return false;
82       }
83     }
84     // If there is tinyMCE object, we can assume that we loaded Confluence
85     // instance.  So, let's check the version whether it allows conflated
86     // keypress event model.
87     try {
88       let { author, version } =
89         new tinyMCEObject.plugins.CursorTargetPlugin().getInfo();
90       // If it's not Confluence, don't include it into the telemetry because
91       // we just need to collect percentage of old version in all loaded
92       // Confluence instances.
93       if (author !== "Atlassian") {
94         return false;
95       }
96       let isOldVersion = version === "1.0";
97       Services.telemetry.keyedScalarAdd(
98         "dom.event.confluence_load_count",
99         isOldVersion ? "old" : "new",
100         1
101       );
102       return isOldVersion;
103     } catch (e) {
104       return false;
105     }
106   }