Revert 264226 "Reduce dependency of TiclInvalidationService on P..."
[chromium-blink-merge.git] / remoting / webapp / host_session.js
blob4aa70cba34327e17fa201a4b1d0dcd4f8b733bf6
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.
5 /**
6 * @fileoverview
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.
14 'use strict';
16 /** @suppress {duplicate} */
17 var remoting = remoting || {};
19 /**
20 * @constructor
22 remoting.HostSession = function() {
23 /** @type {remoting.HostIt2MeDispatcher} @private */
24 this.hostDispatcher_ = null;
27 // Note that these values are copied directly from host_script_object.h and
28 // must be kept in sync.
29 /** @enum {number} */
30 remoting.HostSession.State = {
31 UNKNOWN: -1,
32 DISCONNECTED: 0,
33 STARTING: 1,
34 REQUESTED_ACCESS_CODE: 2,
35 RECEIVED_ACCESS_CODE: 3,
36 CONNECTED: 4,
37 DISCONNECTING: 5,
38 ERROR: 6,
39 INVALID_DOMAIN_ERROR: 7
42 /**
43 * @param {string} stateString The string representation of the host state.
44 * @return {remoting.HostSession.State} The HostSession.State enum value
45 * corresponding to stateString.
47 remoting.HostSession.State.fromString = function(stateString) {
48 if (!remoting.HostSession.State.hasOwnProperty(stateString)) {
49 console.error('Unexpected HostSession.State string: ', stateString);
50 return remoting.HostSession.State.UNKNOWN;
52 return remoting.HostSession.State[stateString];
55 /**
56 * Initiates a connection.
57 * @param {remoting.HostIt2MeDispatcher} hostDispatcher It2Me host dispatcher
58 * to use.
59 * @param {string} email The user's email address.
60 * @param {string} accessToken A valid OAuth2 access token.
61 * @param {function(remoting.HostSession.State):void} onStateChanged
62 * Callback for notifications of changes to the host plugin's state.
63 * @param {function(boolean):void} onNatTraversalPolicyChanged Callback
64 * for notification of changes to the NAT traversal policy.
65 * @param {function(string):void} logDebugInfo Callback allowing the plugin
66 * to log messages to the debug log.
67 * @param {function():void} onError Callback to invoke in case of an error.
69 remoting.HostSession.prototype.connect =
70 function(hostDispatcher, email, accessToken, onStateChanged,
71 onNatTraversalPolicyChanged, logDebugInfo, onError) {
72 /** @private */
73 this.hostDispatcher_ = hostDispatcher;
75 this.hostDispatcher_.connect(
76 email, 'oauth2:' + accessToken,
77 onStateChanged, onNatTraversalPolicyChanged, logDebugInfo,
78 remoting.settings.XMPP_SERVER_ADDRESS,
79 remoting.settings.XMPP_SERVER_USE_TLS,
80 remoting.settings.DIRECTORY_BOT_JID,
81 onError);
84 /**
85 * Get the access code generated by the it2me host. Valid only after the
86 * host state is RECEIVED_ACCESS_CODE.
87 * @return {string} The access code.
89 remoting.HostSession.prototype.getAccessCode = function() {
90 return this.hostDispatcher_.getAccessCode();
93 /**
94 * Get the lifetime for the access code. Valid only after the host state is
95 * RECEIVED_ACCESS_CODE.
96 * @return {number} The access code lifetime, in seconds.
98 remoting.HostSession.prototype.getAccessCodeLifetime = function() {
99 return this.hostDispatcher_.getAccessCodeLifetime();
103 * Get the email address of the connected client. Valid only after the plugin
104 * state is CONNECTED.
105 * @return {string} The client's email address.
107 remoting.HostSession.prototype.getClient = function() {
108 return this.hostDispatcher_.getClient();
112 * Disconnect the it2me session.
113 * @return {void} Nothing.
115 remoting.HostSession.prototype.disconnect = function() {
116 this.hostDispatcher_.disconnect();
121 * @return {void} Nothing.
123 remoting.HostSession.prototype.cleanup = function() {
124 this.hostDispatcher_.cleanup();