summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/newmailaccount/content/uriListener.js
blob: c4d9177ebeb499eb69f704b7aa8e2baaf47f4690 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* 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/. */

/* globals openAccountSetupTabWithAccount, openAccountProvisionerTab */

/**
 * This object takes care of intercepting page loads and creating the
 * corresponding account if the page load turns out to be a text/xml file from
 * one of our account providers.
 */

var { XPCOMUtils } = ChromeUtils.importESModule(
  "resource://gre/modules/XPCOMUtils.sys.mjs"
);
var { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
var { JXON } = ChromeUtils.import("resource:///modules/JXON.jsm");

/**
 * This is an observer that watches all HTTP requests for one where the
 * response contentType contains text/xml.  Once that observation is
 * made, we ensure that the associated window for that request matches
 * the window belonging to the content tab for the account order form.
 * If so, we attach an nsITraceableListener to read the contents of the
 * request response, and react accordingly if the contents can be turned
 * into an email account.
 *
 * @param aBrowser The XUL <browser> the request lives in.
 * @param aParams An object containing various bits of information.
 * @param aParams.realName The real name of the person
 * @param aParams.email The email address the person picked.
 * @param aParams.searchEngine The search engine associated to that provider.
 */
function httpRequestObserver(aBrowser, aParams) {
  this.browser = aBrowser;
  this.params = aParams;
}

httpRequestObserver.prototype = {
  observe(aSubject, aTopic, aData) {
    if (
      aTopic != "http-on-examine-response" &&
      aTopic != "http-on-examine-cached-response"
    ) {
      return;
    }

    if (!(aSubject instanceof Ci.nsIHttpChannel)) {
      console.error(
        "Failed to get a nsIHttpChannel when " +
          "observing http-on-examine-response"
      );
      return;
    }
    // Helper function to get header values.
    let getHttpHeader = (httpChannel, header) => {
      // getResponseHeader throws when header is not set.
      try {
        return httpChannel.getResponseHeader(header);
      } catch (e) {
        return null;
      }
    };

    let contentType = getHttpHeader(aSubject, "Content-Type");
    if (!contentType || !contentType.toLowerCase().startsWith("text/xml")) {
      return;
    }

    // It's possible the account information changed during the setup at the
    // provider. Check some headers and set them if needed.
    let name = getHttpHeader(aSubject, "x-thunderbird-account-name");
    if (name) {
      this.params.realName = name;
    }
    let email = getHttpHeader(aSubject, "x-thunderbird-account-email");
    if (email) {
      this.params.email = email;
    }

    let requestWindow = this._getWindowForRequest(aSubject);
    if (!requestWindow || requestWindow !== this.browser.innerWindowID) {
      return;
    }

    // Ok, we've got a request that looks like a decent candidate.
    // Let's attach our TracingListener.
    if (aSubject instanceof Ci.nsITraceableChannel) {
      let newListener = new TracingListener(this.browser, this.params);
      newListener.oldListener = aSubject.setNewListener(newListener);
    }
  },

  /**
   * _getWindowForRequest is an internal function that takes an nsIRequest,
   * and returns the associated window for that request.  If it cannot find
   * an associated window, the function returns null. On exception, the
   * exception message is logged to the Error Console and null is returned.
   *
   * @param aRequest the nsIRequest to analyze
   */
  _getWindowForRequest(aRequest) {
    try {
      if (aRequest && aRequest.notificationCallbacks) {
        return aRequest.notificationCallbacks.getInterface(Ci.nsILoadContext)
          .currentWindowContext.innerWindowId;
      }
      if (
        aRequest &&
        aRequest.loadGroup &&
        aRequest.loadGroup.notificationCallbacks
      ) {
        return aRequest.loadGroup.notificationCallbacks.getInterface(
          Ci.nsILoadContext
        ).currentWindowContext.innerWindowId;
      }
    } catch (e) {
      console.error(
        "Could not find an associated window " +
          "for an HTTP request. Error: " +
          e
      );
    }
    return null;
  },

  QueryInterface: ChromeUtils.generateQI(["nsIObserver"]),
};

/**
 * TracingListener is an nsITracableChannel implementation that copies
 * an incoming stream of data from a request.  The data flows through this
 * nsITracableChannel transparently to the original listener. Once the
 * response data is fully downloaded, an attempt is made to parse it
 * as XML, and derive email account data from it.
 *
 * @param aBrowser The XUL <browser> the request lives in.
 * @param aParams An object containing various bits of information.
 * @param aParams.realName The real name of the person
 * @param aParams.email The email address the person picked.
 * @param aParams.searchEngine The search engine associated to that provider.
 */
function TracingListener(aBrowser, aParams) {
  this.chunks = [];
  this.browser = aBrowser;
  this.params = aParams;
  this.oldListener = null;
}

TracingListener.prototype = {
  onStartRequest(/* nsIRequest */ aRequest) {
    this.oldListener.onStartRequest(aRequest);
  },

  onStopRequest(/* nsIRequest */ aRequest, /* int */ aStatusCode) {
    const { CreateInBackend } = ChromeUtils.import(
      "resource:///modules/accountcreation/CreateInBackend.jsm"
    );
    const { readFromXML } = ChromeUtils.import(
      "resource:///modules/accountcreation/readFromXML.jsm"
    );
    const { AccountConfig } = ChromeUtils.import(
      "resource:///modules/accountcreation/AccountConfig.jsm"
    );

    let newAccount;
    try {
      // Construct the downloaded data (we'll assume UTF-8 bytes) into XML.
      let xml = this.chunks.join("");
      let bytes = new Uint8Array(xml.length);
      for (let i = 0; i < xml.length; i++) {
        bytes[i] = xml.charCodeAt(i);
      }
      xml = new TextDecoder().decode(bytes);

      // Attempt to derive email account information.
      let domParser = new DOMParser();
      let accountConfig = readFromXML(
        JXON.build(domParser.parseFromString(xml, "text/xml"))
      );
      AccountConfig.replaceVariables(
        accountConfig,
        this.params.realName,
        this.params.email
      );

      let host = aRequest.getRequestHeader("Host");
      let providerHostname = new URL("http://" + host).hostname;
      // Collect telemetry on which provider the new address was purchased from.
      Services.telemetry.keyedScalarAdd(
        "tb.account.new_account_from_provisioner",
        providerHostname,
        1
      );

      // Create the new account in the back end.
      newAccount = CreateInBackend.createAccountInBackend(accountConfig);

      let tabmail = document.getElementById("tabmail");
      // Find the tab associated with this browser, and close it.
      let myTabInfo = tabmail.tabInfo.filter(
        function (x) {
          return "browser" in x && x.browser == this.browser;
        }.bind(this)
      )[0];
      tabmail.closeTab(myTabInfo);

      // Trigger the first login to download the folder structure and messages.
      newAccount.incomingServer.getNewMessages(
        newAccount.incomingServer.rootFolder,
        this._msgWindow,
        null
      );
    } catch (e) {
      // Something went wrong with account set up. Dump the error out to the
      // error console, reopen the account provisioner tab, and show an error
      // dialog to the user.
      console.error("Problem interpreting provider XML:" + e);
      openAccountProvisionerTab();
      Services.prompt.alert(window, null, e);

      this.oldListener.onStopRequest(aRequest, aStatusCode);
      return;
    }

    // Open the account setup tab and show the success view or an error if we
    // weren't able to create the new account.
    openAccountSetupTabWithAccount(
      newAccount,
      this.params.realName,
      this.params.email
    );

    this.oldListener.onStopRequest(aRequest, aStatusCode);
  },

  onDataAvailable(
    /* nsIRequest */ aRequest,
    /* nsIInputStream */ aStream,
    /* int */ aOffset,
    /* int */ aCount
  ) {
    // We want to read the stream of incoming data, but we also want
    // to make sure it gets passed to the original listener. We do this
    // by passing the input stream through an nsIStorageStream, writing
    // the data to that stream, and passing it along to the next listener.
    let binaryInputStream = Cc[
      "@mozilla.org/binaryinputstream;1"
    ].createInstance(Ci.nsIBinaryInputStream);
    let storageStream = Cc["@mozilla.org/storagestream;1"].createInstance(
      Ci.nsIStorageStream
    );
    let outStream = Cc["@mozilla.org/binaryoutputstream;1"].createInstance(
      Ci.nsIBinaryOutputStream
    );

    binaryInputStream.setInputStream(aStream);

    // The segment size of 8192 is a little magical - more or less
    // copied from nsITraceableChannel example code strewn about the
    // web.
    storageStream.init(8192, aCount, null);
    outStream.setOutputStream(storageStream.getOutputStream(0));

    let data = binaryInputStream.readBytes(aCount);
    this.chunks.push(data);

    outStream.writeBytes(data, aCount);
    this.oldListener.onDataAvailable(
      aRequest,
      storageStream.newInputStream(0),
      aOffset,
      aCount
    );
  },

  QueryInterface: ChromeUtils.generateQI([
    "nsIStreamListener",
    "nsIRequestObserver",
  ]),
};