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,
27 remoting.ClientSession.Capability.TOUCH_EVENTS
30 // Append the app-specific capabilities.
31 this.requiredCapabilities_.push.apply(this.requiredCapabilities_,
38 * @param {remoting.ClientSession.EventHandler} listener
39 * @param {boolean=} opt_useApiaryForLogging
40 * @return {Promise<!remoting.ClientSession>} Resolves with the client session
41 * if succeeded or rejects with remoting.Error on failure.
43 remoting.ClientSessionFactory.prototype.createSession =
44 function(listener, opt_useApiaryForLogging) {
48 /** @type {remoting.SignalStrategy} */
50 /** @type {remoting.ClientPlugin} */
52 var useApiaryForLogging = Boolean(opt_useApiaryForLogging);
54 function OnError(/** remoting.Error */ error) {
55 base.dispose(signalStrategy);
56 base.dispose(clientPlugin);
60 var promise = remoting.identity.getToken().then(
61 function(/** string */ authToken) {
63 return remoting.identity.getUserInfo();
64 }).then(function(/** {email: string, name: string} */ userInfo) {
65 return connectSignaling(userInfo.email, token);
66 }).then(function(/** remoting.SignalStrategy */ strategy) {
67 signalStrategy = strategy;
68 return createPlugin(that.container_, that.requiredCapabilities_);
69 }).then(function(/** remoting.ClientPlugin */ plugin) {
70 clientPlugin = plugin;
71 return new remoting.ClientSession(
72 plugin, signalStrategy, useApiaryForLogging, listener);
74 remoting.Error.handler(OnError)
77 return /** @type {Promise<!remoting.ClientSession>} */ (promise);
81 * @param {string} email
82 * @param {string} token
83 * @return {Promise<!remoting.SignalStrategy>}
85 function connectSignaling(email, token) {
86 var signalStrategy = remoting.SignalStrategy.create();
87 var deferred = new base.Deferred();
88 function onSignalingState(/** remoting.SignalStrategy.State */ state) {
90 case remoting.SignalStrategy.State.CONNECTED:
91 deferred.resolve(signalStrategy);
94 case remoting.SignalStrategy.State.FAILED:
95 var error = signalStrategy.getError();
96 signalStrategy.dispose();
97 deferred.reject(error);
101 signalStrategy.setStateChangedCallback(onSignalingState);
102 signalStrategy.connect(remoting.settings.XMPP_SERVER, email, token);
103 return deferred.promise();
107 * Creates the plugin.
108 * @param {HTMLElement} container parent element for the plugin.
109 * @param {Array<string>} capabilities capabilities required by this
111 * @return {Promise<!remoting.ClientPlugin>}
113 function createPlugin(container, capabilities) {
114 var plugin = remoting.ClientPlugin.factory.createPlugin(
115 container, capabilities);
116 var deferred = new base.Deferred();
118 function onInitialized(/** boolean */ initialized) {
120 console.error('ERROR: remoting plugin not loaded');
122 deferred.reject(new remoting.Error(remoting.Error.Tag.MISSING_PLUGIN));
126 if (!plugin.isSupportedVersion()) {
127 console.error('ERROR: bad plugin version');
130 new remoting.Error(remoting.Error.Tag.BAD_PLUGIN_VERSION));
133 deferred.resolve(plugin);
135 plugin.initialize(onInitialized);
136 return deferred.promise();