This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
/
web.js
58 lines (47 loc) · 1.57 KB
/
web.js
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
/* 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/. */
// A channel that completes the OAuth flow using Firefox WebChannel events
// https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/WebChannel.jsm
// https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/FxAccountsOAuthClient.jsm
'use strict';
define([
'underscore',
'lib/channels/duplex',
'lib/channels/senders/web-channel',
'lib/channels/receivers/web-channel'
], function (_, DuplexChannel, WebChannelSender, WebChannelReceiver) {
function WebChannel(id) {
if (! id) {
throw new Error('WebChannel must have an id');
}
this._id = id;
}
_.extend(WebChannel.prototype, new DuplexChannel(), {
initialize: function (options) {
options = options || {};
var win = options.window || window;
var webChannelId = this._id;
var sender = this._sender = new WebChannelSender();
sender.initialize({
window: win,
webChannelId: webChannelId
});
var receiver = this._receiver = new WebChannelReceiver();
receiver.initialize({
window: win,
webChannelId: webChannelId
});
DuplexChannel.prototype.initialize.call(this, {
window: win,
sender: sender,
receiver: receiver
});
},
teardown: function () {
this._sender.teardown();
this._receiver.teardown();
}
});
return WebChannel;
});