summaryrefslogtreecommitdiffstats
path: root/toolkit/components/prompts/src/SharedPromptUtils.jsm
blob: b27096ac28f840386f3c414088f93b25afe5b144 (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
this.EXPORTED_SYMBOLS = [ "PromptUtils", "EnableDelayHelper" ];

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const Cu = Components.utils;

Cu.import("resource://gre/modules/Services.jsm");

this.PromptUtils = {
    // Fire a dialog open/close event. Used by tabbrowser to focus the
    // tab which is triggering a prompt.
    // For remote dialogs, we pass in a different DOM window and a separate
    // target. If the caller doesn't pass in the target, then we'll simply use
    // the passed-in DOM window.
    // The detail may contain information about the principal on which the
    // prompt is triggered, as well as whether or not this is a tabprompt
    // (ie tabmodal alert/prompt/confirm and friends)
    fireDialogEvent : function (domWin, eventName, maybeTarget, detail) {
        let target = maybeTarget || domWin;
        let eventOptions = {cancelable: true, bubbles: true};
        if (detail) {
          eventOptions.detail = detail;
        }
        let event = new domWin.CustomEvent(eventName, eventOptions);
        let winUtils = domWin.QueryInterface(Ci.nsIInterfaceRequestor)
                             .getInterface(Ci.nsIDOMWindowUtils);
        winUtils.dispatchEventToChromeOnly(target, event);
    },

    objectToPropBag : function (obj) {
        let bag = Cc["@mozilla.org/hash-property-bag;1"].
                  createInstance(Ci.nsIWritablePropertyBag2);
        bag.QueryInterface(Ci.nsIWritablePropertyBag);

        for (let propName in obj)
            bag.setProperty(propName, obj[propName]);

        return bag;
    },

    propBagToObject : function (propBag, obj) {
        // Here we iterate over the object's original properties, not the bag
        // (ie, the prompt can't return more/different properties than were
        // passed in). This just helps ensure that the caller provides default
        // values, lest the prompt forget to set them.
        for (let propName in obj)
            obj[propName] = propBag.getProperty(propName);
    },
};

/**
 * This helper handles the enabling/disabling of dialogs that might
 * be subject to fast-clicking attacks. It handles the initial delayed
 * enabling of the dialog, as well as disabling it on blur and reapplying
 * the delay when the dialog regains focus.
 *
 * @param enableDialog   A custom function to be called when the dialog
 *                       is to be enabled.
 * @param diableDialog   A custom function to be called when the dialog
 *                       is to be disabled.
 * @param focusTarget    The window used to watch focus/blur events.
 */
this.EnableDelayHelper = function({enableDialog, disableDialog, focusTarget}) {
    this.enableDialog = makeSafe(enableDialog);
    this.disableDialog = makeSafe(disableDialog);
    this.focusTarget = focusTarget;

    this.disableDialog();

    this.focusTarget.addEventListener("blur", this, false);
    this.focusTarget.addEventListener("focus", this, false);
    this.focusTarget.document.addEventListener("unload", this, false);

    this.startOnFocusDelay();
};

this.EnableDelayHelper.prototype = {
    get delayTime() {
        return Services.prefs.getIntPref("security.dialog_enable_delay");
    },

    handleEvent : function(event) {
        if (event.target != this.focusTarget &&
            event.target != this.focusTarget.document)
            return;

        switch (event.type) {
            case "blur":
                this.onBlur();
                break;

            case "focus":
                this.onFocus();
                break;

            case "unload":
                this.onUnload();
                break;
        }
    },

    onBlur : function () {
        this.disableDialog();
        // If we blur while waiting to enable the buttons, just cancel the
        // timer to ensure the delay doesn't fire while not focused.
        if (this._focusTimer) {
            this._focusTimer.cancel();
            this._focusTimer = null;
        }
    },

    onFocus : function () {
        this.startOnFocusDelay();
    },

    onUnload: function() {
        this.focusTarget.removeEventListener("blur", this, false);
        this.focusTarget.removeEventListener("focus", this, false);
        this.focusTarget.document.removeEventListener("unload", this, false);

        if (this._focusTimer) {
            this._focusTimer.cancel();
            this._focusTimer = null;
        }

        this.focusTarget = this.enableDialog = this.disableDialog = null;
    },

    startOnFocusDelay : function() {
        if (this._focusTimer)
            return;

        this._focusTimer = Cc["@mozilla.org/timer;1"]
                             .createInstance(Ci.nsITimer);
        this._focusTimer.initWithCallback(
            () => { this.onFocusTimeout(); },
            this.delayTime,
            Ci.nsITimer.TYPE_ONE_SHOT
        );
    },

    onFocusTimeout : function() {
        this._focusTimer = null;
        this.enableDialog();
    },
};

function makeSafe(fn) {
    return function () {
        // The dialog could be gone by now (if the user closed it),
        // which makes it likely that the given fn might throw.
        try {
            fn();
        } catch (e) { }
    };
}