Remove redundant cross_navigation_pending_ member from RFHM.
[chromium-blink-merge.git] / remoting / webapp / crd / js / client_session_factory.js
blob24cfa2ad28f37b62431e93b9ef18337e2208606f
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   ];
29   // Append the app-specific capabilities.
30   this.requiredCapabilities_.push.apply(this.requiredCapabilities_,
31                                         capabilities);
34 /**
35  * Creates a session.
36  *
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.
40  */
41 remoting.ClientSessionFactory.prototype.createSession = function(listener) {
42   var that = this;
43   /** @type {string} */
44   var token;
45   /** @type {remoting.SignalStrategy} */
46   var signalStrategy;
47   /** @type {remoting.ClientPlugin} */
48   var clientPlugin;
50   function OnError(/** remoting.Error */ error) {
51     base.dispose(signalStrategy);
52     base.dispose(clientPlugin);
53     throw error;
54   }
56   var promise = remoting.identity.getToken().then(
57     function(/** string */ authToken) {
58     token = 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);
68   }).catch(
69     remoting.Error.handler(OnError)
70   );
72   return /** @type {Promise<!remoting.ClientSession>} */ (promise);
75 /**
76  * @param {string} email
77  * @param {string} token
78  * @return {Promise<!remoting.SignalStrategy>}
79  */
80 function connectSignaling(email, token) {
81   var signalStrategy = remoting.SignalStrategy.create();
82   var deferred = new base.Deferred();
83   function onSignalingState(/** remoting.SignalStrategy.State */ state) {
84     switch (state) {
85       case remoting.SignalStrategy.State.CONNECTED:
86         deferred.resolve(signalStrategy);
87         break;
89       case remoting.SignalStrategy.State.FAILED:
90         var error = signalStrategy.getError();
91         signalStrategy.dispose();
92         deferred.reject(error);
93         break;
94     }
95   }
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
105  *     application.
106  * @return {Promise<!remoting.ClientPlugin>}
107  */
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) {
114     if (!initialized) {
115       console.error('ERROR: remoting plugin not loaded');
116       plugin.dispose();
117       deferred.reject(new remoting.Error(remoting.Error.Tag.MISSING_PLUGIN));
118       return;
119     }
121     if (!plugin.isSupportedVersion()) {
122       console.error('ERROR: bad plugin version');
123       plugin.dispose();
124       deferred.reject(
125           new remoting.Error(remoting.Error.Tag.BAD_PLUGIN_VERSION));
126       return;
127     }
128     deferred.resolve(plugin);
129   }
130   plugin.initialize(onInitialized);
131   return deferred.promise();
134 })();