Bug 1535487 - determine rootUrl directly in buglist creator r=tomprince
[gecko.git] / toolkit / actors / KeyPressEventModelCheckerChild.jsm
blob77563c6710697d074813e9eb01b3eef063cae692
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 "use strict";
9 var EXPORTED_SYMBOLS = ["KeyPressEventModelCheckerChild"];
11 const {ActorChild} = ChromeUtils.import("resource://gre/modules/ActorChild.jsm");
12 const {AppConstants} = ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
13 const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
15 class KeyPressEventModelCheckerChild extends ActorChild {
16   // Currently, the event is dispatched only when the document becomes editable
17   // because of contenteditable.  If you need to add new editor which is in
18   // designMode, you need to change MaybeDispatchCheckKeyPressEventModelEvent()
19   // of nsHTMLDocument.
20   handleEvent(aEvent) {
21     if (!AppConstants.DEBUG) {
22       // Stop propagation in opt build to save the propagation cost.
23       // However, the event is necessary for running test_bug1514940.html.
24       // Therefore, we need to keep propagating it at least on debug build.
25       aEvent.stopImmediatePropagation();
26     }
28     // Currently, even if we set HTMLDocument.KEYPRESS_EVENT_MODEL_CONFLATED
29     // here, conflated model isn't used forcibly.  If you need it, you need
30     // to change WidgetKeyboardEvent, dom::KeyboardEvent and PresShell.
31     let model = HTMLDocument.KEYPRESS_EVENT_MODEL_DEFAULT;
32     if (this._isOldConfluence(aEvent.target.ownerGlobal)) {
33       model = HTMLDocument.KEYPRESS_EVENT_MODEL_SPLIT;
34     }
35     aEvent.target.setKeyPressEventModel(model);
36   }
38   _isOldConfluence(aWindow) {
39     if (!aWindow) {
40       return false;
41     }
42     // aWindow should be an editor window in <iframe>.  However, we don't know
43     // whether it can be without <iframe>.  Anyway, there should be tinyMCE
44     // object in the parent window or in the window.
45     let tinyMCEObject;
46     // First, try to retrieve tinyMCE object from parent window.
47     try {
48       tinyMCEObject = ChromeUtils.waiveXrays(aWindow.parent).tinyMCE;
49     } catch (e) {
50       // Ignore the exception for now.
51     }
52     // Next, if there is no tinyMCE object in the parent window, let's check
53     // the window.
54     if (!tinyMCEObject) {
55       try {
56         tinyMCEObject = ChromeUtils.waiveXrays(aWindow).tinyMCE;
57       } catch (e) {
58         // Fallthrough to return false below.
59       }
60       // If we couldn't find tinyMCE object, let's assume that it's not
61       // Confluence instance.
62       if (!tinyMCEObject) {
63         return false;
64       }
65     }
66     // If there is tinyMCE object, we can assume that we loaded Confluence
67     // instance.  So, let's check the version whether it allows conflated
68     // keypress event model.
69     try {
70       let {author, version} =
71           new tinyMCEObject.plugins.CursorTargetPlugin().getInfo();
72       // If it's not Confluence, don't include it into the telemetry because
73       // we just need to collect percentage of old version in all loaded
74       // Confluence instances.
75       if (author !== "Atlassian") {
76         return false;
77       }
78       let isOldVersion = version === "1.0";
79       Services.telemetry.keyedScalarAdd("dom.event.confluence_load_count",
80                                         isOldVersion ? "old" : "new", 1);
81       return isOldVersion;
82     } catch (e) {
83       return false;
84     }
85   }