summaryrefslogtreecommitdiffstats
path: root/mobile/android/chrome/content/Feedback.js
blob: 8727c46c39218b6eed76d7ab1ce43b3f093300b8 (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
/* 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/. */
"use strict";

var Feedback = {

  get _feedbackURL() {
    delete this._feedbackURL;
    return this._feedbackURL = Services.urlFormatter.formatURLPref("app.feedbackURL");
  },

  observe: function(aMessage, aTopic, aData) {
    if (aTopic !== "Feedback:Show") {
      return;
    }

    // Don't prompt for feedback in distribution builds.
    try {
      Services.prefs.getCharPref("distribution.id");
      return;
    } catch (e) {}

    let url = this._feedbackURL;
    let browser = BrowserApp.selectOrAddTab(url, { parentId: BrowserApp.selectedTab.id }).browser;

    browser.addEventListener("FeedbackClose", this, false, true);
    browser.addEventListener("FeedbackMaybeLater", this, false, true);

    // Dispatch a custom event to the page content when feedback is prompted by the browser.
    // This will be used by the page to determine it's being loaded directly by the browser,
    // instead of by the user visiting the page, e.g. through browser history.
    function loadListener(event) {
      browser.removeEventListener("DOMContentLoaded", loadListener, false);
      browser.contentDocument.dispatchEvent(new CustomEvent("FeedbackPrompted"));
    }
    browser.addEventListener("DOMContentLoaded", loadListener, false);
  },

  handleEvent: function(event) {
    if (!this._isAllowed(event.target)) {
      return;
    }

    switch (event.type) {
      case "FeedbackClose":
        // Do nothing.
        break;

      case "FeedbackMaybeLater":
        Messaging.sendRequest({ type: "Feedback:MaybeLater" });
        break;
    }

    let win = event.target.ownerDocument.defaultView.top;
    BrowserApp.closeTab(BrowserApp.getTabForWindow(win));
  },

  _isAllowed: function(node) {
    let uri = node.ownerDocument.documentURIObject;
    let feedbackURI = Services.io.newURI(this._feedbackURL, null, null);
    return uri.prePath === feedbackURI.prePath;
  }
};