summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/client_plugin_v1.js
blob: e67887bd4401525ac3d30464666a5ae0a8f6d80b (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// 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 that wraps low-level details of interacting with the client plugin.
 *
 * This abstracts a <embed> element and controls the plugin which does
 * the actual remoting work. It also handles differences between
 * client plugins versions when it is necessary.
 */

'use strict';

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

/**
 * @param {remoting.ViewerPlugin} plugin The plugin embed element.
 * @constructor
 * @implements {remoting.ClientPlugin}
 */
remoting.ClientPluginV1 = function(plugin) {
  this.plugin = plugin;

  this.desktopWidth = 0;
  this.desktopHeight = 0;

  /** @param {string} iq The Iq stanza received from the host. */
  this.onOutgoingIqHandler = function (iq) {};
  /** @param {string} message Log message. */
  this.onDebugMessageHandler = function (message) {};
  /**
   * @param {number} status The plugin status.
   * @param {number} error The plugin error status, if any.
   */
  this.onConnectionStatusUpdateHandler = function(status, error) {};
  this.onDesktopSizeUpdateHandler = function () {};

  // Connect event handlers.

  /** @type {remoting.ClientPluginV1} */
  var that = this;

  /** @param {string} iq The IQ stanza to send. */
  this.plugin.sendIq = function(iq) { that.onSendIq_(iq); };

  /** @param {string} msg The message to log. */
  this.plugin.debugInfo = function(msg) { that.onDebugInfo_(msg); };

  /**
   * @param {number} status The plugin status.
   * @param {number} error The plugin error status, if any.
   */
  this.plugin.connectionInfoUpdate = function(status, error) {
    that.onConnectionInfoUpdate_(status, error);
  };
  this.plugin.desktopSizeUpdate = function() { that.onDesktopSizeUpdate_(); };
};

/**
 * Chromoting session API version (for this javascript).
 * This is compared with the plugin API version to verify that they are
 * compatible.
 *
 * @const
 * @private
 */
remoting.ClientPluginV1.prototype.API_VERSION_ = 4;

/**
 * The oldest API version that we support.
 * This will differ from the |API_VERSION_| if we maintain backward
 * compatibility with older API versions.
 *
 * @const
 * @private
 */
remoting.ClientPluginV1.prototype.API_MIN_VERSION_ = 2;

/** @param {string} iq The Iq stanza received from the host. */
remoting.ClientPluginV1.prototype.onSendIq_ = function(iq) {
  this.onOutgoingIqHandler(iq);
}

  /** @param {string} message The IQ stanza to send. */
remoting.ClientPluginV1.prototype.onDebugInfo_ = function(message) {
  this.onDebugMessageHandler(message);
}

/**
 * @param {number} status The plugin status.
 * @param {number} error The plugin error status, if any.
 */
remoting.ClientPluginV1.prototype.onConnectionInfoUpdate_=
    function(status, error) {
  // Old plugins didn't pass the status and error values, so get
  // them directly. Note that there is a race condition inherent in
  // this approach.
  if (typeof(status) == 'undefined')
    status = this.plugin.status;
  if (typeof(error) == 'undefined')
    error = this.plugin.error;
  this.onConnectionStatusUpdateHandler(status, error);
};

remoting.ClientPluginV1.prototype.onDesktopSizeUpdate_ = function() {
  this.desktopWidth = this.plugin.desktopWidth;
  this.desktopHeight = this.plugin.desktopHeight;
  this.onDesktopSizeUpdateHandler();
}

/**
 * Deletes the plugin.
 */
remoting.ClientPluginV1.prototype.cleanup = function() {
  this.plugin.parentNode.removeChild(this.plugin);
};

/**
 * @return {Element} HTML element that correspods to the plugin.
 */
remoting.ClientPluginV1.prototype.element = function() {
  return this.plugin;
};

/**
 * @param {function(boolean): void} onDone
 */
remoting.ClientPluginV1.prototype.initialize = function(onDone) {
  onDone(typeof this.plugin.connect === 'function');
};

/**
 * @return {boolean} True if the plugin and web-app versions are compatible.
 */
remoting.ClientPluginV1.prototype.isSupportedVersion = function() {
  return this.API_VERSION_ >= this.plugin.apiMinVersion &&
      this.plugin.apiVersion >= this.API_MIN_VERSION_;
};

/**
 * @param {remoting.ClientPlugin.Feature} feature The feature to test for.
 * @return {boolean} True if the plugin supports the named feature.
 */
remoting.ClientPluginV1.prototype.hasFeature = function(feature) {
  if (feature == remoting.ClientPlugin.Feature.HIGH_QUALITY_SCALING)
    return this.plugin.apiVersion >= 3;
  return false;
};

/**
 * @return {boolean} True if the plugin supports the injectKeyEvent API.
 */
remoting.ClientPluginV1.prototype.isInjectKeyEventSupported = function() {
  return false;
};

/**
 * @param {string} iq Incoming IQ stanza.
 */
remoting.ClientPluginV1.prototype.onIncomingIq = function(iq) {
  if (this.plugin && this.plugin.onIq) {
    this.plugin.onIq(iq);
  } else {
    // plugin.onIq may not be set after the plugin has been shut
    // down. Particularly this happens when we receive response to
    // session-terminate stanza.
    console.warn('plugin.onIq is not set so dropping incoming message.');
  }
};

/**
 * @param {string} hostJid The jid of the host to connect to.
 * @param {string} hostPublicKey The base64 encoded version of the host's
 *     public key.
 * @param {string} clientJid Local jid.
 * @param {string} sharedSecret The access code for IT2Me or the PIN
 *     for Me2Me.
 * @param {string} authenticationMethods Comma-separated list of
 *     authentication methods the client should attempt to use.
 * @param {string} authenticationTag A host-specific tag to mix into
 *     authentication hashes.
 */
remoting.ClientPluginV1.prototype.connect = function(
    hostJid, hostPublicKey, clientJid, sharedSecret,
    authenticationMethods, authenticationTag) {
  if (this.plugin.apiVersion < 4) {
    // Client plugin versions prior to 4 didn't support the last two
    // parameters.
    this.plugin.connect(hostJid, hostPublicKey, clientJid, sharedSecret);
  } else {
    this.plugin.connect(hostJid, hostPublicKey, clientJid, sharedSecret,
                        authenticationMethods, authenticationTag);
  }
};

/**
 * @param {boolean} scaleToFit True if scale-to-fit should be enabled.
 */
remoting.ClientPluginV1.prototype.setScaleToFit = function(scaleToFit) {
  // scaleToFit() will be removed in future versions of the plugin.
  if (!!this.plugin && typeof this.plugin.setScaleToFit === 'function')
    this.plugin.setScaleToFit(scaleToFit);
};


/**
 * Release all currently pressed keys.
 */
remoting.ClientPluginV1.prototype.releaseAllKeys = function() {
  this.plugin.releaseAllKeys();
};

/**
 * Returns an associative array with a set of stats for this connection.
 *
 * @return {remoting.ClientSession.PerfStats} The connection statistics.
 */
remoting.ClientPluginV1.prototype.getPerfStats = function() {
  /** @type {remoting.ClientSession.PerfStats} */
  return { videoBandwidth: this.plugin.videoBandwidth,
           videoFrameRate: this.plugin.videoFrameRate,
           captureLatency: this.plugin.videoCaptureLatency,
           encodeLatency: this.plugin.videoEncodeLatency,
           decodeLatency: this.plugin.videoDecodeLatency,
           renderLatency: this.plugin.videoRenderLatency,
           roundtripLatency: this.plugin.roundTripLatency };
};

/**
 * This dummy method exists only so that this class implements ClientPlugin.
 *
 * @param {string} mimeType The MIME type of the clipboard item.
 * @param {string} item The clipboard item.
 */
remoting.ClientPluginV1.prototype.sendClipboardItem = function(mimeType, item) {
  return;
};

/**
 * This dummy method exists only so that this class implements ClientPlugin.
 *
 * @param {number} usbKeycode The USB-style code of the key to inject.
 * @param {boolean} pressed True to inject a key press, False for a release.
 */
remoting.ClientPluginV1.prototype.injectKeyEvent =
    function(usbKeycode, pressed) {
  return;
};

/**
 * Remap one USB keycode to another in all subsequent key events.
 *
 * @param {number} fromKeycode The USB-style code of the key to remap.
 * @param {number} toKeycode The USB-style code to remap the key to.
 */
remoting.ClientPluginV1.prototype.remapKey =
    function(fromKeycode, toKeycode) {
  return;
};