OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 /** | |
6 * @fileoverview | |
7 * Class handling creation and teardown of a remoting host session. | |
8 * | |
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. | |
12 */ | |
13 | |
14 'use strict'; | |
15 | |
16 /** @suppress {duplicate} */ | |
17 var remoting = remoting || {}; | |
18 | |
19 /** | |
20 * @constructor | |
21 */ | |
22 remoting.HostSession = function() { | |
23 /** @private */ | |
24 this.HOST_PLUGIN_ID_ = 'host-plugin-id'; | |
25 }; | |
26 | |
27 /** @type {remoting.HostPlugin} */ | |
28 remoting.HostSession.prototype.plugin = null; | |
29 | |
30 // Note that these values are copied directly from host_script_object.h and | |
31 // must be kept in sync. | |
Jamie
2011/10/27 20:41:36
I decided against having a type mapping like we ha
garykac
2011/10/27 22:32:39
OK
| |
32 /** @enum {number} */ | |
33 remoting.HostSession.State = { | |
34 UNKNOWN: -1, | |
35 DISCONNECTED: 0, | |
36 STARTING: 1, | |
37 REQUESTED_ACCESS_CODE: 2, | |
38 RECEIVED_ACCESS_CODE: 3, | |
39 CONNECTED: 4, | |
40 DISCONNECTING: 5, | |
41 ERROR: 6 | |
42 }; | |
43 | |
44 /** | |
45 * Create the host plugin and initiate a connection. | |
46 * @param {Element} container The parent element to which to add the plugin. | |
47 * @param {string} email The user's email address. | |
48 * @param {string} accessToken A valid OAuth2 access token. | |
49 * @param {function(boolean):void} onNatTraversalPolicyChanged Callback | |
50 * for notification of changes to the NAT traversal policy. | |
51 * @param {function(remoting.HostSession.State):void} onStateChanged | |
52 * Callback for notifications of changes to the host plugin's state. | |
53 * @param {function(string):void} logDebugInfo Callback allowing the plugin | |
54 * to log messages to the debug log. | |
55 */ | |
56 remoting.HostSession.prototype.createPluginAndConnect = | |
57 function(container, email, accessToken, | |
58 onNatTraversalPolicyChanged, onStateChanged, logDebugInfo) { | |
59 this.plugin = /** @type {remoting.HostPlugin} */ | |
60 document.createElement('embed'); | |
61 this.plugin.type = remoting.PLUGIN_MIMETYPE; | |
62 this.plugin.id = this.HOST_PLUGIN_ID_; | |
63 // Hiding the plugin means it doesn't load, so make it size zero instead. | |
64 this.plugin.width = 0; | |
65 this.plugin.height = 0; | |
66 container.appendChild(this.plugin); | |
67 this.plugin.onNatTraversalPolicyChanged = onNatTraversalPolicyChanged; | |
68 this.plugin.onStateChanged = onStateChanged; | |
69 this.plugin.logDebugInfo = logDebugInfo; | |
70 this.plugin.localize(chrome.i18n.getMessage); | |
71 this.plugin.connect(email, 'oauth2:' + accessToken); | |
72 }; | |
73 | |
74 /** | |
75 * Get the access code generated by the host plugin. Valid only after the | |
76 * plugin state is RECEIVED_ACCESS_CODE. | |
77 * @return {string} The access code. | |
78 */ | |
79 remoting.HostSession.prototype.getAccessCode = function() { | |
80 return this.plugin.accessCode; | |
81 }; | |
82 | |
83 /** | |
84 * Get the lifetime for the access code. Valid only after the plugin state is | |
85 * RECEIVED_ACCESS_CODE. | |
86 * @return {number} The access code lifetime, in seconds. | |
87 */ | |
88 remoting.HostSession.prototype.getAccessCodeLifetime = function() { | |
89 return this.plugin.accessCodeLifetime; | |
90 }; | |
91 | |
92 /** | |
93 * Get the email address of the connected client. Valid only after the plugin | |
94 * state is CONNECTED. | |
95 * @return {string} The client's email address. | |
96 */ | |
97 remoting.HostSession.prototype.getClient = function() { | |
98 return this.plugin.client; | |
99 }; | |
100 | |
101 /** | |
102 * Disconnect the client. | |
103 * @return {void} Nothing. | |
104 */ | |
105 remoting.HostSession.prototype.disconnect = function() { | |
106 this.plugin.disconnect(); | |
107 }; | |
108 | |
109 | |
110 /** | |
111 * Remove the plugin element from the document. | |
112 * @return {void} Nothing. | |
113 */ | |
114 remoting.HostSession.prototype.removePlugin = function() { | |
115 this.plugin.parentNode.removeChild(this.plugin); | |
116 }; | |
OLD | NEW |