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,
40 INVALID_DOMAIN_ERROR
: 7
44 * Create an instance of the host plugin.
45 * @return {remoting.HostPlugin} The new plugin instance.
47 remoting
.HostSession
.createPlugin = function() {
48 var plugin
= document
.createElement('embed');
49 plugin
.type
= remoting
.PLUGIN_MIMETYPE
;
50 // Hiding the plugin means it doesn't load, so make it size zero instead.
53 return /** @type {remoting.HostPlugin} */ (plugin
);
57 * Create the host plugin and initiate a connection.
58 * @param {Element} container The parent element to which to add the plugin.
59 * @param {string} email The user's email address.
60 * @param {string} accessToken A valid OAuth2 access token.
61 * @param {function(boolean):void} onNatTraversalPolicyChanged Callback
62 * for notification of changes to the NAT traversal policy.
63 * @param {function(remoting.HostSession.State):void} onStateChanged
64 * Callback for notifications of changes to the host plugin's state.
65 * @param {function(string):void} logDebugInfo Callback allowing the plugin
66 * to log messages to the debug log.
68 remoting
.HostSession
.prototype.createPluginAndConnect
=
69 function(container
, email
, accessToken
,
70 onNatTraversalPolicyChanged
, onStateChanged
, logDebugInfo
) {
71 this.plugin
= remoting
.HostSession
.createPlugin();
72 container
.appendChild(this.plugin
);
73 this.plugin
.onNatTraversalPolicyChanged
= onNatTraversalPolicyChanged
;
74 this.plugin
.onStateChanged
= onStateChanged
;
75 this.plugin
.logDebugInfo
= logDebugInfo
;
76 this.plugin
.localize(chrome
.i18n
.getMessage
);
77 this.plugin
.connect(email
, 'oauth2:' + accessToken
);
81 * Get the access code generated by the host plugin. Valid only after the
82 * plugin state is RECEIVED_ACCESS_CODE.
83 * @return {string} The access code.
85 remoting
.HostSession
.prototype.getAccessCode = function() {
86 return this.plugin
.accessCode
;
90 * Get the lifetime for the access code. Valid only after the plugin state is
91 * RECEIVED_ACCESS_CODE.
92 * @return {number} The access code lifetime, in seconds.
94 remoting
.HostSession
.prototype.getAccessCodeLifetime = function() {
95 return this.plugin
.accessCodeLifetime
;
99 * Get the email address of the connected client. Valid only after the plugin
100 * state is CONNECTED.
101 * @return {string} The client's email address.
103 remoting
.HostSession
.prototype.getClient = function() {
104 return this.plugin
.client
;
108 * Disconnect the client.
109 * @return {void} Nothing.
111 remoting
.HostSession
.prototype.disconnect = function() {
112 this.plugin
.disconnect();
117 * Remove the plugin element from the document.
118 * @return {void} Nothing.
120 remoting
.HostSession
.prototype.removePlugin = function() {
121 this.plugin
.parentNode
.removeChild(this.plugin
);