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
,
28 remoting
.ClientSession
.Capability
.DESKTOP_SHAPE
31 // Append the app-specific capabilities.
32 this.requiredCapabilities_
.push
.apply(this.requiredCapabilities_
,
39 * @param {remoting.ClientSession.EventHandler} listener
40 * @param {remoting.SessionLogger} logger
41 * @param {boolean=} opt_useApiaryForLogging
42 * @return {Promise<!remoting.ClientSession>} Resolves with the client session
43 * if succeeded or rejects with remoting.Error on failure.
45 remoting
.ClientSessionFactory
.prototype.createSession
=
46 function(listener
, logger
, opt_useApiaryForLogging
) {
50 /** @type {remoting.SignalStrategy} */
52 /** @type {remoting.ClientPlugin} */
55 function OnError(/** !remoting.Error */ error
) {
56 logger
.logSessionStateChange(
57 remoting
.ChromotingEvent
.SessionState
.CONNECTION_FAILED
,
58 remoting
.ChromotingEvent
.ConnectionError
.UNEXPECTED
);
59 base
.dispose(signalStrategy
);
60 base
.dispose(clientPlugin
);
64 var promise
= remoting
.identity
.getToken().then(
65 function(/** string */ authToken
) {
67 return remoting
.identity
.getUserInfo();
68 }).then(function(/** {email: string, name: string} */ userInfo
) {
69 logger
.logSessionStateChange(
70 remoting
.ChromotingEvent
.SessionState
.SIGNALING
,
71 remoting
.ChromotingEvent
.ConnectionError
.NONE
);
72 return connectSignaling(userInfo
.email
, token
);
73 }).then(function(/** remoting.SignalStrategy */ strategy
) {
74 signalStrategy
= strategy
;
75 logger
.logSessionStateChange(
76 remoting
.ChromotingEvent
.SessionState
.CREATING_PLUGIN
,
77 remoting
.ChromotingEvent
.ConnectionError
.NONE
);
78 return createPlugin(that
.container_
, that
.requiredCapabilities_
);
79 }).then(function(/** remoting.ClientPlugin */ plugin
) {
80 clientPlugin
= plugin
;
81 // TODO(kelvinp): Remove |opt_useApiaryForLogging| once we have migrated
82 // away from XMPP based logging (crbug.com/523423).
83 var sessionLogger
= Boolean(opt_useApiaryForLogging
) ?
85 new remoting
.LogToServer(signalStrategy
);
86 return new remoting
.ClientSession(
87 plugin
, signalStrategy
, sessionLogger
, listener
);
89 remoting
.Error
.handler(OnError
)
92 return /** @type {Promise<!remoting.ClientSession>} */ (promise
);
96 * @param {string} email
97 * @param {string} token
98 * @return {Promise<!remoting.SignalStrategy>}
100 function connectSignaling(email
, token
) {
101 var signalStrategy
= remoting
.SignalStrategy
.create();
102 var deferred
= new base
.Deferred();
103 function onSignalingState(/** remoting.SignalStrategy.State */ state
) {
105 case remoting
.SignalStrategy
.State
.CONNECTED
:
106 deferred
.resolve(signalStrategy
);
109 case remoting
.SignalStrategy
.State
.FAILED
:
110 var error
= signalStrategy
.getError();
111 signalStrategy
.dispose();
112 deferred
.reject(error
);
116 signalStrategy
.setStateChangedCallback(onSignalingState
);
117 signalStrategy
.connect(remoting
.settings
.XMPP_SERVER
, email
, token
);
118 return deferred
.promise();
122 * Creates the plugin.
123 * @param {HTMLElement} container parent element for the plugin.
124 * @param {Array<string>} capabilities capabilities required by this
126 * @return {Promise<!remoting.ClientPlugin>}
128 function createPlugin(container
, capabilities
) {
129 var plugin
= remoting
.ClientPlugin
.factory
.createPlugin(
130 container
, capabilities
);
131 var deferred
= new base
.Deferred();
133 function onInitialized(/** boolean */ initialized
) {
135 console
.error('ERROR: remoting plugin not loaded');
137 deferred
.reject(new remoting
.Error(remoting
.Error
.Tag
.MISSING_PLUGIN
));
141 deferred
.resolve(plugin
);
143 plugin
.initialize(onInitialized
);
144 return deferred
.promise();