summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/browser_test/mock_session_connector.js
blob: 090e344b6120b82e30329569e2db2568e99fed76 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2014 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
 * Mock implementation of SessionConnector for testing.
 * @suppress {checkTypes}
 */

'use strict';

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

/**
 * @param {HTMLElement} clientContainer Container element for the client view.
 * @param {function(remoting.ClientSession):void} onConnected Callback on
 *     success.
 * @param {function(remoting.Error):void} onError Callback on error.
 * @param {function(string, string):boolean} onExtensionMessage The handler for
 *     protocol extension messages. Returns true if a message is recognized;
 *     false otherwise.
 * @param {function(remoting.Error):void} onConnectionFailed Callback for when
 *     the connection fails.
 * @param {Array<string>} requiredCapabilities Connector capabilities
 *     required by this application.
 * @param {string} defaultRemapKeys The default set of key mappings for the
 *     client session to use.
 * @constructor
 * @implements {remoting.SessionConnector}
 */
remoting.MockSessionConnector = function(clientContainer, onConnected, onError,
                                         onExtensionMessage,
                                         onConnectionFailed,
                                         requiredCapabilities,
                                         defaultRemapKeys) {
  this.clientContainer_ = clientContainer;
  /** @type {function(remoting.ClientSession):void} */
  this.onConnected_ = onConnected;
  this.onError = onError;
  this.onExtensionMessage_ = onExtensionMessage;
  this.onConnectionFailed_ = onConnectionFailed;
  this.requiredCapabilities_ = requiredCapabilities;
  this.defaultRemapKeys_ = defaultRemapKeys;

  /** @type {remoting.DesktopConnectedView.Mode} */
  this.mode_ = remoting.DesktopConnectedView.Mode.ME2ME;

  this.reset();
};

remoting.MockSessionConnector.prototype.reset = function() {
  /** @type {string}  @private */
  this.hostId_ = '';
};

remoting.MockSessionConnector.prototype.connectMe2Me =
    function(host, fetchPin, fetchThirdPartyToken,
             clientPairingId, clientPairedSecret) {
  this.mode_ = remoting.DesktopConnectedView.Mode.ME2ME;
  this.connect_();
};

remoting.MockSessionConnector.prototype.retryConnectMe2Me =
    function(host, fetchPin, fetchThirdPartyToken,
             clientPairingId, clientPairedSecret) {
  this.mode_ = remoting.DesktopConnectedView.Mode.ME2ME;
  this.connect_();
};

remoting.MockSessionConnector.prototype.connectMe2App =
    function(host, fetchThirdPartyToken) {
  this.mode_ = remoting.DesktopConnectedView.Mode.APP_REMOTING;
  this.connect_();
};

remoting.MockSessionConnector.prototype.updatePairingInfo =
    function(clientId, sharedSecret) {
};

remoting.MockSessionConnector.prototype.connectIT2Me =
    function(accessCode) {
  this.mode_ = remoting.DesktopConnectedView.Mode.ME2ME;
  this.connect_();
};

remoting.MockSessionConnector.prototype.reconnect = function() {
  base.debug.assert(this.mode_ == remoting.DesktopConnectedView.Mode.ME2ME);
  this.connect_();
};

remoting.MockSessionConnector.prototype.cancel = function() {
};

remoting.MockSessionConnector.prototype.getConnectionMode = function() {
  return this.mode_;
};

remoting.MockSessionConnector.prototype.getHostId = function() {
  return this.hostId_;
};

remoting.MockSessionConnector.prototype.connect_ = function() {
  var signalling = new remoting.MockSignalStrategy();
  signalling.setStateForTesting(remoting.SignalStrategy.State.CONNECTED);
  var hostName = 'Mock host';
  var accessCode = '';
  var authenticationMethods = '';
  var hostId = '';
  var hostJid = '';
  var hostPublicKey = '';
  var clientPairingId = '';
  var clientPairedSecret = '';

  /**
   * @param {boolean} offerPairing
   * @param {function(string):void} callback
   */
  var fetchPin = function(offerPairing, callback) {
    window.setTimeout(function() { callback('') }, 0);
  };

  /**
   * @param {string} tokenUrl
   * @param {string} hostPublicKey
   * @param {string} scope
   * @param {function(string, string):void} callback
   */
  var fetchThirdPartyToken = function(tokenUrl, hostPublicKey, scope,
                                      callback) {
    window.setTimeout(function() { callback('', '') }, 0);
  };

  var clientSession = new remoting.ClientSession(
      signalling, this.clientContainer_, hostName,
      accessCode, fetchPin, fetchThirdPartyToken,
      authenticationMethods, hostId, hostJid, hostPublicKey,
      this.mode_, clientPairingId, clientPairedSecret);

  var that = this;
  /** @param {remoting.ClientSession.StateEvent} event */
  var onStateChange = function(event) {
    if (event.current == remoting.ClientSession.State.CONNECTED) {
      that.onConnected_(clientSession);
    }
  };

  clientSession.addEventListener(
      remoting.ClientSession.Events.stateChanged,
      onStateChange);
  clientSession.createPluginAndConnect(this.onExtensionMessage_);
};


/**
 * @constructor
 * @extends {remoting.SessionConnectorFactory}
 */
remoting.MockSessionConnectorFactory = function() {};

/**
 * @param {HTMLElement} clientContainer Container element for the client view.
 * @param {function(remoting.ClientSession):void} onConnected Callback on
 *     success.
 * @param {function(remoting.Error):void} onError Callback on error.
 * @param {function(string, string):boolean} onExtensionMessage The handler for
 *     protocol extension messages. Returns true if a message is recognized;
 *     false otherwise.
 * @param {function(remoting.Error):void} onConnectionFailed Callback for when
 *     the connection fails.
 * @param {Array<string>} requiredCapabilities Connector capabilities
 *     required by this application.
 * @param {string} defaultRemapKeys The default set of key mappings to use
 *     in the client session.
 * @return {remoting.MockSessionConnector}
 */
remoting.MockSessionConnectorFactory.prototype.createConnector =
    function(clientContainer, onConnected, onError, onExtensionMessage,
             onConnectionFailed, requiredCapabilities, defaultRemapKeys) {
  return new remoting.MockSessionConnector(
      clientContainer, onConnected, onError, onExtensionMessage,
      onConnectionFailed, requiredCapabilities, defaultRemapKeys);
};