Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / remoting / webapp / base / js / client_session_factory.js
blob57844304fa14b8bb3564004040fe023dd0420f16
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   ];
30   // Append the app-specific capabilities.
31   this.requiredCapabilities_.push.apply(this.requiredCapabilities_,
32                                         capabilities);
35 /**
36  * Creates a session.
37  *
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.
42  */
43 remoting.ClientSessionFactory.prototype.createSession =
44     function(listener, opt_useApiaryForLogging) {
45   var that = this;
46   /** @type {string} */
47   var token;
48   /** @type {remoting.SignalStrategy} */
49   var signalStrategy;
50   /** @type {remoting.ClientPlugin} */
51   var clientPlugin;
52   var useApiaryForLogging = Boolean(opt_useApiaryForLogging);
54   function OnError(/** remoting.Error */ error) {
55     base.dispose(signalStrategy);
56     base.dispose(clientPlugin);
57     throw error;
58   }
60   var promise = remoting.identity.getToken().then(
61     function(/** string */ authToken) {
62     token = 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);
73   }).catch(
74     remoting.Error.handler(OnError)
75   );
77   return /** @type {Promise<!remoting.ClientSession>} */ (promise);
80 /**
81  * @param {string} email
82  * @param {string} token
83  * @return {Promise<!remoting.SignalStrategy>}
84  */
85 function connectSignaling(email, token) {
86   var signalStrategy = remoting.SignalStrategy.create();
87   var deferred = new base.Deferred();
88   function onSignalingState(/** remoting.SignalStrategy.State */ state) {
89     switch (state) {
90       case remoting.SignalStrategy.State.CONNECTED:
91         deferred.resolve(signalStrategy);
92         break;
94       case remoting.SignalStrategy.State.FAILED:
95         var error = signalStrategy.getError();
96         signalStrategy.dispose();
97         deferred.reject(error);
98         break;
99     }
100   }
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
110  *     application.
111  * @return {Promise<!remoting.ClientPlugin>}
112  */
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) {
119     if (!initialized) {
120       console.error('ERROR: remoting plugin not loaded');
121       plugin.dispose();
122       deferred.reject(new remoting.Error(remoting.Error.Tag.MISSING_PLUGIN));
123       return;
124     }
126     if (!plugin.isSupportedVersion()) {
127       console.error('ERROR: bad plugin version');
128       plugin.dispose();
129       deferred.reject(
130           new remoting.Error(remoting.Error.Tag.BAD_PLUGIN_VERSION));
131       return;
132     }
133     deferred.resolve(plugin);
134   }
135   plugin.initialize(onInitialized);
136   return deferred.promise();
139 })();