summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/newmailaccount/content/provisionerCheckout.js
blob: 0fc38c87e557470a85f9c38b41238edcb30ecde7 (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 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/. */

// mail/base/content/contentAreaClick.js
/* globals hRefForClickEvent, openLinkExternally */
// mail/base/content/specialTabs.js
/* globals specialTabs */

var { ConsoleAPI } = ChromeUtils.importESModule(
  "resource://gre/modules/Console.sys.mjs"
);

/**
 * A content tab for the account provisioner.  We use Javascript-y magic to
 * "subclass" specialTabs.contentTabType, and then override the appropriate
 * members.
 *
 * Also note that provisionerCheckoutTab is a singleton (hence the maxTabs: 1).
 */
var provisionerCheckoutTabType = Object.create(specialTabs.contentTabType, {
  name: { value: "provisionerCheckoutTab" },
  modes: {
    value: {
      provisionerCheckoutTab: {
        type: "provisionerCheckoutTab",
        maxTabs: 1,
      },
    },
  },
  _log: {
    value: new ConsoleAPI({
      prefix: "mail.provider",
      maxLogLevel: "warn",
      maxLogLevelPref: "mail.provider.loglevel",
    }),
  },
});

/**
 * Here, we're overriding openTab - first we call the openTab of contentTab
 * (for the context of this provisionerCheckoutTab "aTab") and then passing
 * special arguments "realName", "email" and "searchEngine" from the caller
 * of openTab, and passing those to our _setMonitoring function.
 */
provisionerCheckoutTabType.openTab = function (aTab, aArgs) {
  specialTabs.contentTabType.openTab.call(this, aTab, aArgs);

  // Since there's only one tab of this type ever (see the mode definition),
  // we're OK to stash this stuff here.
  this._realName = aArgs.realName;
  this._email = aArgs.email;
  this._searchEngine = aArgs.searchEngine || "";

  this._setMonitoring(
    aTab.browser,
    aArgs.realName,
    aArgs.email,
    aArgs.searchEngine
  );
};

/**
 * We're overriding closeTab - first, we call the closeTab of contentTab,
 * (for the context of this provisionerCheckoutTab "aTab"), and then we
 * unregister our observer that was registered in _setMonitoring.
 */
provisionerCheckoutTabType.closeTab = function (aTab) {
  specialTabs.contentTabType.closeTab.call(this, aTab);
  this._log.info("Performing account provisioner cleanup");
  this._log.info("Removing httpRequestObserver");
  Services.obs.removeObserver(this._observer, "http-on-examine-response");
  Services.obs.removeObserver(
    this._observer,
    "http-on-examine-cached-response"
  );
  Services.obs.removeObserver(this.quitObserver, "mail-unloading-messenger");
  delete this._observer;
  this._log.info("Account provisioner cleanup is done.");
};

/**
 * Serialize our tab into something we can restore later.
 */
provisionerCheckoutTabType.persistTab = function (aTab) {
  return {
    tabURI: aTab.browser.currentURI.spec,
    realName: this._realName,
    email: this._email,
    searchEngine: this._searchEngine,
  };
};

/**
 * Re-open the provisionerCheckoutTab with all of the stuff we stashed in
 * persistTab. This will automatically hook up our monitoring again.
 */
provisionerCheckoutTabType.restoreTab = function (aTabmail, aPersistedState) {
  aTabmail.openTab("provisionerCheckoutTab", {
    url: aPersistedState.tabURI,
    realName: aPersistedState.realName,
    email: aPersistedState.email,
    searchEngine: aPersistedState.searchEngine,
    background: true,
  });
};

/**
 * This function registers an observer to watch for HTTP requests where the
 * contentType contains text/xml.
 */
provisionerCheckoutTabType._setMonitoring = function (
  aBrowser,
  aRealName,
  aEmail,
  aSearchEngine
) {
  let mail3Pane = Services.wm.getMostRecentWindow("mail:3pane");

  // We'll construct our special observer (defined in urlListener.js)
  // that will watch for requests where the contentType contains
  // text/xml.
  this._observer = new mail3Pane.httpRequestObserver(aBrowser, {
    realName: aRealName,
    email: aEmail,
    searchEngine: aSearchEngine,
  });

  // Register our observer
  Services.obs.addObserver(this._observer, "http-on-examine-response");
  Services.obs.addObserver(this._observer, "http-on-examine-cached-response");
  Services.obs.addObserver(this.quitObserver, "mail-unloading-messenger");

  this._log.info("httpRequestObserver wired up.");
};

/**
 * This observer listens for the mail-unloading-messenger event fired by each
 * mail window before they unload. If the mail window is the same window that
 * this provisionerCheckoutTab belongs to, then we stash a pref so that when
 * the session restarts, we go straight to the tab, as opposed to showing the
 * dialog again.
 */
provisionerCheckoutTabType.quitObserver = {
  observe(aSubject, aTopic, aData) {
    // Make sure we saw the right topic, and that the window that is closing
    // is the 3pane window that the provisionerCheckoutTab belongs to.
    if (aTopic == "mail-unloading-messenger" && aSubject === window) {
      // We quit while the provisionerCheckoutTab was opened. Set our sneaky
      // pref so that we suppress the dialog on startup.
      Services.prefs.setBoolPref(
        "mail.provider.suppress_dialog_on_startup",
        true
      );
    }
  },
};