1 // Copyright (c) 2012 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.
7 * Class handling creation and teardown of a remoting host session.
9 * This abstracts a <embed> element and controls the plugin which does the
10 * actual remoting work. There should be no UI code inside this class. It
11 * should be purely thought of as a controller of sorts.
16 /** @suppress {duplicate} */
17 var remoting
= remoting
|| {};
22 remoting
.HostSession = function() {
25 /** @type {remoting.HostPlugin} */
26 remoting
.HostSession
.prototype.plugin
= null;
28 // Note that these values are copied directly from host_script_object.h and
29 // must be kept in sync.
31 remoting
.HostSession
.State
= {
35 REQUESTED_ACCESS_CODE
: 2,
36 RECEIVED_ACCESS_CODE
: 3,
43 * Create an instance of the host plugin.
44 * @return {remoting.HostPlugin} The new plugin instance.
46 remoting
.HostSession
.createPlugin = function() {
47 var plugin
= document
.createElement('embed');
48 plugin
.type
= remoting
.PLUGIN_MIMETYPE
;
49 // Hiding the plugin means it doesn't load, so make it size zero instead.
52 return /** @type {remoting.HostPlugin} */ (plugin
);
56 * Create the host plugin and initiate a connection.
57 * @param {Element} container The parent element to which to add the plugin.
58 * @param {string} email The user's email address.
59 * @param {string} accessToken A valid OAuth2 access token.
60 * @param {function(boolean):void} onNatTraversalPolicyChanged Callback
61 * for notification of changes to the NAT traversal policy.
62 * @param {function(remoting.HostSession.State):void} onStateChanged
63 * Callback for notifications of changes to the host plugin's state.
64 * @param {function(string):void} logDebugInfo Callback allowing the plugin
65 * to log messages to the debug log.
67 remoting
.HostSession
.prototype.createPluginAndConnect
=
68 function(container
, email
, accessToken
,
69 onNatTraversalPolicyChanged
, onStateChanged
, logDebugInfo
) {
70 this.plugin
= remoting
.HostSession
.createPlugin();
71 container
.appendChild(this.plugin
);
72 this.plugin
.onNatTraversalPolicyChanged
= onNatTraversalPolicyChanged
;
73 this.plugin
.onStateChanged
= onStateChanged
;
74 this.plugin
.logDebugInfo
= logDebugInfo
;
75 this.plugin
.localize(chrome
.i18n
.getMessage
);
76 this.plugin
.connect(email
, 'oauth2:' + accessToken
);
80 * Get the access code generated by the host plugin. Valid only after the
81 * plugin state is RECEIVED_ACCESS_CODE.
82 * @return {string} The access code.
84 remoting
.HostSession
.prototype.getAccessCode = function() {
85 return this.plugin
.accessCode
;
89 * Get the lifetime for the access code. Valid only after the plugin state is
90 * RECEIVED_ACCESS_CODE.
91 * @return {number} The access code lifetime, in seconds.
93 remoting
.HostSession
.prototype.getAccessCodeLifetime = function() {
94 return this.plugin
.accessCodeLifetime
;
98 * Get the email address of the connected client. Valid only after the plugin
100 * @return {string} The client's email address.
102 remoting
.HostSession
.prototype.getClient = function() {
103 return this.plugin
.client
;
107 * Disconnect the client.
108 * @return {void} Nothing.
110 remoting
.HostSession
.prototype.disconnect = function() {
111 this.plugin
.disconnect();
116 * Remove the plugin element from the document.
117 * @return {void} Nothing.
119 remoting
.HostSession
.prototype.removePlugin = function() {
120 this.plugin
.parentNode
.removeChild(this.plugin
);