1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 /** @suppress {duplicate} */
6 var remoting = remoting || {};
13 * @param {Element} container parent element for the plugin to be created.
14 * @param {Array<string>} capabilities capabilities required by this
18 remoting.ClientSessionFactory = function(container, capabilities) {
20 this.container_ = /** @type {HTMLElement} */ (container);
22 /** @private {Array<string>} */
23 this.requiredCapabilities_ = [
24 remoting.ClientSession.Capability.SEND_INITIAL_RESOLUTION,
25 remoting.ClientSession.Capability.RATE_LIMIT_RESIZE_REQUESTS,
26 remoting.ClientSession.Capability.VIDEO_RECORDER
29 // Append the app-specific capabilities.
30 this.requiredCapabilities_.push.apply(this.requiredCapabilities_,
37 * @param {remoting.ClientSession.EventHandler} listener
38 * @return {Promise<!remoting.ClientSession>} Resolves with the client session
39 * if succeeded or rejects with remoting.Error on failure.
41 remoting.ClientSessionFactory.prototype.createSession = function(listener) {
45 /** @type {remoting.SignalStrategy} */
47 /** @type {remoting.ClientPlugin} */
50 function OnError(/** remoting.Error */ error) {
51 base.dispose(signalStrategy);
52 base.dispose(clientPlugin);
56 var promise = remoting.identity.getToken().then(
57 function(/** string */ authToken) {
59 return remoting.identity.getUserInfo();
60 }).then(function(/** {email: string, name: string} */ userInfo) {
61 return connectSignaling(userInfo.email, token);
62 }).then(function(/** remoting.SignalStrategy */ strategy) {
63 signalStrategy = strategy;
64 return createPlugin(that.container_, that.requiredCapabilities_);
65 }).then(function(/** remoting.ClientPlugin */ plugin) {
66 clientPlugin = plugin;
67 return new remoting.ClientSession(plugin, signalStrategy, listener);
69 remoting.Error.handler(OnError)
72 return /** @type {Promise<!remoting.ClientSession>} */ (promise);
76 * @param {string} email
77 * @param {string} token
78 * @return {Promise<!remoting.SignalStrategy>}
80 function connectSignaling(email, token) {
81 var signalStrategy = remoting.SignalStrategy.create();
82 var deferred = new base.Deferred();
83 function onSignalingState(/** remoting.SignalStrategy.State */ state) {
85 case remoting.SignalStrategy.State.CONNECTED:
86 deferred.resolve(signalStrategy);
89 case remoting.SignalStrategy.State.FAILED:
90 var error = signalStrategy.getError();
91 signalStrategy.dispose();
92 deferred.reject(error);
96 signalStrategy.setStateChangedCallback(onSignalingState);
97 signalStrategy.connect(remoting.settings.XMPP_SERVER, email, token);
98 return deferred.promise();
102 * Creates the plugin.
103 * @param {HTMLElement} container parent element for the plugin.
104 * @param {Array<string>} capabilities capabilities required by this
106 * @return {Promise<!remoting.ClientPlugin>}
108 function createPlugin(container, capabilities) {
109 var plugin = remoting.ClientPlugin.factory.createPlugin(
110 container, capabilities);
111 var deferred = new base.Deferred();
113 function onInitialized(/** boolean */ initialized) {
115 console.error('ERROR: remoting plugin not loaded');
117 deferred.reject(new remoting.Error(remoting.Error.Tag.MISSING_PLUGIN));
121 if (!plugin.isSupportedVersion()) {
122 console.error('ERROR: bad plugin version');
125 new remoting.Error(remoting.Error.Tag.BAD_PLUGIN_VERSION));
128 deferred.resolve(plugin);
130 plugin.initialize(onInitialized);
131 return deferred.promise();