This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
261 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* 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/url', | ||
'lib/channels/base' | ||
], function (_, Url, BaseChannel) { | ||
|
||
function noOp() { | ||
// it's a noOp, nothing to do. | ||
} | ||
|
||
function WebChannel(id) { | ||
if (! id) { | ||
throw new Error('WebChannel must have an id'); | ||
} | ||
|
||
this.id = id; | ||
} | ||
|
||
_.extend(WebChannel.prototype, new BaseChannel(), { | ||
init: function (options) { | ||
options = options || {}; | ||
|
||
this._window = options.window || window; | ||
}, | ||
|
||
/** | ||
* Creates a new WebChannelMessageToChrome CustomEvent and dispatches it. | ||
* The event is received by a content script in Firefox | ||
* | ||
* @param {String} command | ||
* Command name | ||
* @param {Object} data | ||
* Message Object | ||
* @param {Function} [done] | ||
* Optional callback function | ||
*/ | ||
send: function (command, data, done) { | ||
done = done || noOp; | ||
|
||
try { | ||
// Browsers can blow up dispatching the event. | ||
// Ignore the blowups and return without retrying. | ||
var event = this.createEvent(command, data); | ||
this._window.dispatchEvent(event); | ||
} catch (e) { | ||
return done && done(e); | ||
} | ||
|
||
done(null); | ||
}, | ||
/** | ||
* Create a WebChannel compatible custom event | ||
* @param {String} command | ||
* Command name | ||
* @param {Object} data | ||
* Message object | ||
* @returns CustomEvent | ||
*/ | ||
createEvent: function(command, data) { | ||
return new this._window.CustomEvent('WebChannelMessageToChrome', { | ||
detail: { | ||
id: this.id, | ||
message: { | ||
command: command, | ||
data: data | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
return WebChannel; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* 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'; | ||
|
||
|
||
define([ | ||
'underscore', | ||
'chai', | ||
'router', | ||
'views/sign_in', | ||
'lib/channels/web', | ||
'/tests/mocks/window.js' | ||
], | ||
function (_, chai, Router, View, WebChannel, WindowMock) { | ||
var assert = chai.assert; | ||
|
||
describe('lib/channel/web', function () { | ||
it('requires an id', function (done) { | ||
try { | ||
new WebChannel(); | ||
} catch (e) { | ||
assert.equal(e.message, 'WebChannel must have an id'); | ||
done(); | ||
} | ||
}); | ||
|
||
describe('send', function () { | ||
var windowMock; | ||
var channel; | ||
|
||
beforeEach(function () { | ||
windowMock = new WindowMock(); | ||
}); | ||
|
||
it('sends an event with a callback', function (done) { | ||
channel = new WebChannel('MyChannel', windowMock); | ||
channel.init({ | ||
window: windowMock | ||
}); | ||
|
||
channel.send('after_render', {}, function (err, response) { | ||
assert.notOk(err); | ||
assert.ok(windowMock.dispatchedEvents['after_render']); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('throws an error if dispatchEvent fails', function (done) { | ||
windowMock.dispatchEvent = function () { | ||
throw new Error('Not supported'); | ||
}; | ||
|
||
channel = new WebChannel('MyChannel', windowMock); | ||
channel.init({ | ||
window: windowMock | ||
}); | ||
|
||
channel.send('after_render', {}, function (err, response) { | ||
assert.equal(err.message, 'Not supported'); | ||
assert.notOk(response); | ||
done(); | ||
} | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.