summaryrefslogtreecommitdiffstats
path: root/browser/actors/AboutNewTabParent.jsm
blob: 659522458eaf280428bee7cedef3220170515a91 (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
/* 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 EXPORTED_SYMBOLS = ["AboutNewTabParent", "DefaultBrowserNotification"];

const { AppConstants } = ChromeUtils.import(
  "resource://gre/modules/AppConstants.jsm"
);
const { XPCOMUtils } = ChromeUtils.import(
  "resource://gre/modules/XPCOMUtils.jsm"
);

XPCOMUtils.defineLazyModuleGetters(this, {
  Services: "resource://gre/modules/Services.jsm",
  ASRouter: "resource://activity-stream/lib/ASRouter.jsm",
});

XPCOMUtils.defineLazyPreferenceGetter(
  this,
  "DEFAULT_BROWSER_NOTIFICATION_ENABLED",
  "browser.defaultbrowser.notificationbar",
  false
);

XPCOMUtils.defineLazyPreferenceGetter(
  this,
  "SHOULD_CHECK_DEFAULT_BROWSER",
  "browser.shell.checkDefaultBrowser",
  false
);

class AboutNewTabParent extends JSWindowActorParent {
  async receiveMessage(message) {
    switch (message.name) {
      case "DefaultBrowserNotification":
        ASRouter.waitForInitialized.then(() =>
          ASRouter.sendTriggerMessage({
            browser: this.browsingContext.top.embedderElement,
            // triggerId and triggerContext
            id: "defaultBrowserCheck",
            context: { source: "newtab" },
          })
        );
        await DefaultBrowserNotification.maybeShow(
          this.browsingContext.top.embedderElement,
          this.browsingContext.topChromeWindow?.getShellService()
        );
    }
  }
}

// This is a singleton so we can limit display of the notification to
// once per session.
var DefaultBrowserNotification = {
  _notificationDisplayed: false,
  _modalPromptDisplayed: false,
  _notification: null,

  // Used by tests to reset the internal state
  reset() {
    this._notificationDisplayed = false;
    this._modalPromptDisplayed = false;
    this._notification = null;
  },

  notifyModalDisplayed() {
    this._modalPromptDisplayed = true;
  },

  // ShellService is injected here so tests can fake it
  async maybeShow(browser, shellService) {
    if (
      !this._notificationDisplayed &&
      !this._modalPromptDisplayed &&
      !this._notification
    ) {
      let shouldPrompt = await this.willCheckDefaultBrowser(shellService);
      if (shouldPrompt) {
        this.prompt(browser);
        this._notificationDisplayed = true;
      }
    }
  },

  /**
   * Checks if the default browser check prompt will be shown.
   * @returns {boolean} True if the default browser check prompt will be shown.
   */
  async willCheckDefaultBrowser(shellService) {
    if (
      !DEFAULT_BROWSER_NOTIFICATION_ENABLED ||
      !SHOULD_CHECK_DEFAULT_BROWSER ||
      AppConstants.DEBUG ||
      !shellService ||
      shellService.isDefaultBrowserOptOut()
    ) {
      return false;
    }

    // Don't show the prompt if we're already the default browser.
    let isDefault = false;
    try {
      isDefault = shellService.isDefaultBrowser(false, false);
    } catch (ex) {}

    if (!isDefault) {
      let promptCount = Services.prefs.getIntPref(
        "browser.defaultbrowser.notificationbar.checkcount",
        0
      );

      promptCount++;
      Services.prefs.setIntPref(
        "browser.defaultbrowser.notificationbar.checkcount",
        promptCount
      );
      if (
        promptCount >
        Services.prefs.getIntPref(
          "browser.defaultbrowser.notificationbar.checklimit"
        )
      ) {
        return false;
      }
    }

    return !isDefault;
  },

  prompt(browser) {
    let win = browser.ownerGlobal;
    let { gBrowser } = win;

    let { MozXULElement } = win;
    MozXULElement.insertFTLIfNeeded("browser/defaultBrowserNotification.ftl");
    let doc = browser.ownerDocument;
    let messageFragment = doc.createDocumentFragment();
    let message = doc.createElement("span");
    doc.l10n.setAttributes(message, "default-browser-notification-message");
    messageFragment.appendChild(message);

    let buttons = [
      {
        "l10n-id": "default-browser-notification-button",
        primary: true,
        callback: () => {
          win.getShellService().setAsDefault();
          this.closeNotification();
        },
      },
    ];

    let iconPixels = win.devicePixelRatio > 1 ? "64" : "32";
    let iconURL = "chrome://branding/content/icon" + iconPixels + ".png";
    const priority = win.gNotificationBox.PRIORITY_INFO_MEDIUM;
    let callback = this._onNotificationEvent.bind(this);
    this._notification = gBrowser
      .getNotificationBox(browser)
      .appendNotification(
        messageFragment,
        "default-browser",
        iconURL,
        priority,
        buttons,
        callback
      );
  },

  _onNotificationEvent(eventType) {
    if (eventType == "removed") {
      this._notification = null;
    } else if (eventType == "dismissed") {
      Services.prefs.setBoolPref("browser.shell.checkDefaultBrowser", false);
      Services.prefs.setBoolPref(
        "browser.shell.checkDefaultBrowser.disabledByDismissingNotification",
        true
      );
    }
  },

  closeNotification() {
    if (this._notification) {
      this._notification.close();
      this._notification = null;
    }
  },
};