OLD | NEW |
| (Empty) |
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. | |
4 | |
5 /** @suppress {duplicate} */ | |
6 var remoting = remoting || {}; | |
7 | |
8 (function() { | |
9 | |
10 'use strict'; | |
11 | |
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); | |
21 | |
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 ]; | |
28 | |
29 // Append the app-specific capabilities. | |
30 this.requiredCapabilities_.push.apply(this.requiredCapabilities_, | |
31 capabilities); | |
32 }; | |
33 | |
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; | |
49 | |
50 function OnError(/** remoting.Error */ error) { | |
51 base.dispose(signalStrategy); | |
52 base.dispose(clientPlugin); | |
53 throw error; | |
54 } | |
55 | |
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 ); | |
71 | |
72 return /** @type {Promise<!remoting.ClientSession>} */ (promise); | |
73 }; | |
74 | |
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; | |
88 | |
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(); | |
99 } | |
100 | |
101 /** | |
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(); | |
112 | |
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 } | |
120 | |
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(); | |
132 } | |
133 | |
134 })(); | |
OLD | NEW |