summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/host_session.js
blob: 4aa70cba34327e17fa201a4b1d0dcd4f8b733bf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview
 * Class handling creation and teardown of a remoting host session.
 *
 * This abstracts a <embed> element and controls the plugin which does the
 * actual remoting work.  There should be no UI code inside this class.  It
 * should be purely thought of as a controller of sorts.
 */

'use strict';

/** @suppress {duplicate} */
var remoting = remoting || {};

/**
 * @constructor
 */
remoting.HostSession = function() {
  /** @type {remoting.HostIt2MeDispatcher} @private */
  this.hostDispatcher_ = null;
};

// Note that these values are copied directly from host_script_object.h and
// must be kept in sync.
/** @enum {number} */
remoting.HostSession.State = {
  UNKNOWN: -1,
  DISCONNECTED: 0,
  STARTING: 1,
  REQUESTED_ACCESS_CODE: 2,
  RECEIVED_ACCESS_CODE: 3,
  CONNECTED: 4,
  DISCONNECTING: 5,
  ERROR: 6,
  INVALID_DOMAIN_ERROR: 7
};

/**
 * @param {string} stateString The string representation of the host state.
 * @return {remoting.HostSession.State} The HostSession.State enum value
 *     corresponding to stateString.
 */
remoting.HostSession.State.fromString = function(stateString) {
  if (!remoting.HostSession.State.hasOwnProperty(stateString)) {
    console.error('Unexpected HostSession.State string: ', stateString);
    return remoting.HostSession.State.UNKNOWN;
  }
  return remoting.HostSession.State[stateString];
}

/**
 * Initiates a connection.
 * @param {remoting.HostIt2MeDispatcher}  hostDispatcher It2Me host dispatcher
 *     to use.
 * @param {string} email The user's email address.
 * @param {string} accessToken A valid OAuth2 access token.
 * @param {function(remoting.HostSession.State):void} onStateChanged
 *     Callback for notifications of changes to the host plugin's state.
 * @param {function(boolean):void} onNatTraversalPolicyChanged Callback
 *     for notification of changes to the NAT traversal policy.
 * @param {function(string):void} logDebugInfo Callback allowing the plugin
 *     to log messages to the debug log.
 * @param {function():void} onError Callback to invoke in case of an error.
 */
remoting.HostSession.prototype.connect =
    function(hostDispatcher, email, accessToken, onStateChanged,
             onNatTraversalPolicyChanged, logDebugInfo, onError) {
  /** @private */
  this.hostDispatcher_ = hostDispatcher;

  this.hostDispatcher_.connect(
      email, 'oauth2:' + accessToken,
      onStateChanged, onNatTraversalPolicyChanged, logDebugInfo,
      remoting.settings.XMPP_SERVER_ADDRESS,
      remoting.settings.XMPP_SERVER_USE_TLS,
      remoting.settings.DIRECTORY_BOT_JID,
      onError);
};

/**
 * Get the access code generated by the it2me host. Valid only after the
 * host state is RECEIVED_ACCESS_CODE.
 * @return {string} The access code.
 */
remoting.HostSession.prototype.getAccessCode = function() {
  return this.hostDispatcher_.getAccessCode();
};

/**
 * Get the lifetime for the access code. Valid only after the host state is
 * RECEIVED_ACCESS_CODE.
 * @return {number} The access code lifetime, in seconds.
 */
remoting.HostSession.prototype.getAccessCodeLifetime = function() {
  return this.hostDispatcher_.getAccessCodeLifetime();
};

/**
 * Get the email address of the connected client. Valid only after the plugin
 * state is CONNECTED.
 * @return {string} The client's email address.
 */
remoting.HostSession.prototype.getClient = function() {
  return this.hostDispatcher_.getClient();
};

/**
 * Disconnect the it2me session.
 * @return {void} Nothing.
 */
remoting.HostSession.prototype.disconnect = function() {
  this.hostDispatcher_.disconnect();
};


/**
 * @return {void} Nothing.
 */
remoting.HostSession.prototype.cleanup = function() {
  this.hostDispatcher_.cleanup();
};