Roll src/third_party/WebKit 3529d49:06e8485 (svn 202554:202555)
[chromium-blink-merge.git] / remoting / webapp / base / js / client_session_factory.js
blobd78b96642403c55f9456cad2a21588adb301d84d
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 || {};
8 (function() {
10 'use strict';
12 /**
13  * @param {Element} container parent element for the plugin to be created.
14  * @param {Array<string>} capabilities capabilities required by this
15  *     application.
16  * @constructor
17  */
18 remoting.ClientSessionFactory = function(container, capabilities) {
19   /** @private */
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
29   ];
31   // Append the app-specific capabilities.
32   this.requiredCapabilities_.push.apply(this.requiredCapabilities_,
33                                         capabilities);
36 /**
37  * Creates a session.
38  *
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.
44  */
45 remoting.ClientSessionFactory.prototype.createSession =
46     function(listener, logger, opt_useApiaryForLogging) {
47   var that = this;
48   /** @type {string} */
49   var token;
50   /** @type {remoting.SignalStrategy} */
51   var signalStrategy;
52   /** @type {remoting.ClientPlugin} */
53   var 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);
61     throw error;
62   }
64   var promise = remoting.identity.getToken().then(
65     function(/** string */ authToken) {
66     token = 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) ?
84                             logger :
85                             new remoting.LogToServer(signalStrategy);
86     return new remoting.ClientSession(
87         plugin, signalStrategy, sessionLogger, listener);
88   }).catch(
89     remoting.Error.handler(OnError)
90   );
92   return /** @type {Promise<!remoting.ClientSession>} */ (promise);
95 /**
96  * @param {string} email
97  * @param {string} token
98  * @return {Promise<!remoting.SignalStrategy>}
99  */
100 function connectSignaling(email, token) {
101   var signalStrategy = remoting.SignalStrategy.create();
102   var deferred = new base.Deferred();
103   function onSignalingState(/** remoting.SignalStrategy.State */ state) {
104     switch (state) {
105       case remoting.SignalStrategy.State.CONNECTED:
106         deferred.resolve(signalStrategy);
107         break;
109       case remoting.SignalStrategy.State.FAILED:
110         var error = signalStrategy.getError();
111         signalStrategy.dispose();
112         deferred.reject(error);
113         break;
114     }
115   }
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
125  *     application.
126  * @return {Promise<!remoting.ClientPlugin>}
127  */
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) {
134     if (!initialized) {
135       console.error('ERROR: remoting plugin not loaded');
136       plugin.dispose();
137       deferred.reject(new remoting.Error(remoting.Error.Tag.MISSING_PLUGIN));
138       return;
139     }
141     deferred.resolve(plugin);
142   }
143   plugin.initialize(onInitialized);
144   return deferred.promise();
147 })();