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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
|
// Copyright (c) 2011 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.
#include "remoting/client/plugin/chromoting_scriptable_object.h"
#include "base/logging.h"
#include "base/stringprintf.h"
#include "ppapi/cpp/var.h"
#include "remoting/base/auth_token_util.h"
#include "remoting/client/client_config.h"
#include "remoting/client/chromoting_stats.h"
#include "remoting/client/plugin/chromoting_instance.h"
#include "remoting/client/plugin/pepper_xmpp_proxy.h"
using pp::Var;
namespace remoting {
namespace {
const char kApiVersionAttribute[] = "apiVersion";
const char kApiMinVersionAttribute[] = "apiMinVersion";
const char kConnectionInfoUpdate[] = "connectionInfoUpdate";
const char kDebugInfo[] = "debugInfo";
const char kDesktopHeight[] = "desktopHeight";
const char kDesktopWidth[] = "desktopWidth";
const char kDesktopSizeUpdate[] = "desktopSizeUpdate";
const char kLoginChallenge[] = "loginChallenge";
const char kSendIq[] = "sendIq";
const char kQualityAttribute[] = "quality";
const char kStatusAttribute[] = "status";
const char kVideoBandwidthAttribute[] = "videoBandwidth";
const char kVideoCaptureLatencyAttribute[] = "videoCaptureLatency";
const char kVideoEncodeLatencyAttribute[] = "videoEncodeLatency";
const char kVideoDecodeLatencyAttribute[] = "videoDecodeLatency";
const char kVideoRenderLatencyAttribute[] = "videoRenderLatency";
const char kRoundTripLatencyAttribute[] = "roundTripLatency";
} // namespace
ChromotingScriptableObject::ChromotingScriptableObject(
ChromotingInstance* instance)
: instance_(instance) {
}
ChromotingScriptableObject::~ChromotingScriptableObject() {
}
void ChromotingScriptableObject::Init() {
// Property addition order should match the interface description at the
// top of chromoting_scriptable_object.h.
// Plugin API version.
// This should be incremented whenever the API interface changes.
AddAttribute(kApiVersionAttribute, Var(1));
// This should be updated whenever we remove support for an older version
// of the API.
AddAttribute(kApiMinVersionAttribute, Var(1));
// Connection status.
AddAttribute(kStatusAttribute, Var(STATUS_UNKNOWN));
// Connection status values.
AddAttribute("STATUS_UNKNOWN", Var(STATUS_UNKNOWN));
AddAttribute("STATUS_CONNECTING", Var(STATUS_CONNECTING));
AddAttribute("STATUS_INITIALIZING", Var(STATUS_INITIALIZING));
AddAttribute("STATUS_CONNECTED", Var(STATUS_CONNECTED));
AddAttribute("STATUS_CLOSED", Var(STATUS_CLOSED));
AddAttribute("STATUS_FAILED", Var(STATUS_FAILED));
// Connection quality.
AddAttribute(kQualityAttribute, Var(QUALITY_UNKNOWN));
// Connection quality values.
AddAttribute("QUALITY_UNKNOWN", Var(QUALITY_UNKNOWN));
AddAttribute("QUALITY_GOOD", Var(QUALITY_GOOD));
AddAttribute("QUALITY_BAD", Var(QUALITY_BAD));
// Debug info to display.
AddAttribute(kConnectionInfoUpdate, Var());
AddAttribute(kDebugInfo, Var());
AddAttribute(kDesktopSizeUpdate, Var());
AddAttribute(kLoginChallenge, Var());
AddAttribute(kSendIq, Var());
AddAttribute(kDesktopWidth, Var(0));
AddAttribute(kDesktopHeight, Var(0));
// Statistics.
AddAttribute(kVideoBandwidthAttribute, Var());
AddAttribute(kVideoCaptureLatencyAttribute, Var());
AddAttribute(kVideoEncodeLatencyAttribute, Var());
AddAttribute(kVideoDecodeLatencyAttribute, Var());
AddAttribute(kVideoRenderLatencyAttribute, Var());
AddAttribute(kRoundTripLatencyAttribute, Var());
AddMethod("connect", &ChromotingScriptableObject::DoConnect);
AddMethod("connectUnsandboxed",
&ChromotingScriptableObject::DoConnectUnsandboxed);
AddMethod("disconnect", &ChromotingScriptableObject::DoDisconnect);
AddMethod("submitLoginInfo", &ChromotingScriptableObject::DoSubmitLogin);
AddMethod("setScaleToFit", &ChromotingScriptableObject::DoSetScaleToFit);
AddMethod("onIq", &ChromotingScriptableObject::DoOnIq);
}
bool ChromotingScriptableObject::HasProperty(const Var& name, Var* exception) {
// TODO(ajwong): Check if all these name.is_string() sentinels are required.
if (!name.is_string()) {
*exception = Var("HasProperty expects a string for the name.");
return false;
}
PropertyNameMap::const_iterator iter = property_names_.find(name.AsString());
if (iter == property_names_.end()) {
return false;
}
// TODO(ajwong): Investigate why ARM build breaks if you do:
// properties_[iter->second].method == NULL;
// Somehow the ARM compiler is thinking that the above is using NULL as an
// arithmetic expression.
return properties_[iter->second].method == 0;
}
bool ChromotingScriptableObject::HasMethod(const Var& name, Var* exception) {
// TODO(ajwong): Check if all these name.is_string() sentinels are required.
if (!name.is_string()) {
*exception = Var("HasMethod expects a string for the name.");
return false;
}
PropertyNameMap::const_iterator iter = property_names_.find(name.AsString());
if (iter == property_names_.end()) {
return false;
}
// See comment from HasProperty about why to use 0 instead of NULL here.
return properties_[iter->second].method != 0;
}
Var ChromotingScriptableObject::GetProperty(const Var& name, Var* exception) {
// TODO(ajwong): Check if all these name.is_string() sentinels are required.
if (!name.is_string()) {
*exception = Var("GetProperty expects a string for the name.");
return Var();
}
PropertyNameMap::const_iterator iter = property_names_.find(name.AsString());
// No property found.
if (iter == property_names_.end()) {
return ScriptableObject::GetProperty(name, exception);
}
// If this is a statistics attribute then return the value from
// ChromotingStats structure.
ChromotingStats* stats = instance_->GetStats();
if (name.AsString() == kVideoBandwidthAttribute)
return stats ? stats->video_bandwidth()->Rate() : Var();
if (name.AsString() == kVideoCaptureLatencyAttribute)
return stats ? stats->video_capture_ms()->Average() : Var();
if (name.AsString() == kVideoEncodeLatencyAttribute)
return stats ? stats->video_encode_ms()->Average() : Var();
if (name.AsString() == kVideoDecodeLatencyAttribute)
return stats ? stats->video_decode_ms()->Average() : Var();
if (name.AsString() == kVideoRenderLatencyAttribute)
return stats ? stats->video_paint_ms()->Average() : Var();
if (name.AsString() == kRoundTripLatencyAttribute)
return stats ? stats->round_trip_ms()->Average() : Var();
// TODO(ajwong): This incorrectly return a null object if a function
// property is requested.
return properties_[iter->second].attribute;
}
void ChromotingScriptableObject::GetAllPropertyNames(
std::vector<Var>* properties,
Var* exception) {
for (size_t i = 0; i < properties_.size(); i++) {
properties->push_back(Var(properties_[i].name));
}
}
void ChromotingScriptableObject::SetProperty(const Var& name,
const Var& value,
Var* exception) {
// TODO(ajwong): Check if all these name.is_string() sentinels are required.
if (!name.is_string()) {
*exception = Var("SetProperty expects a string for the name.");
return;
}
// Allow writing to connectionInfoUpdate or loginChallenge. See top of
// chromoting_scriptable_object.h for the object interface definition.
std::string property_name = name.AsString();
if (property_name != kConnectionInfoUpdate &&
property_name != kDebugInfo &&
property_name != kDesktopSizeUpdate &&
property_name != kLoginChallenge &&
property_name != kSendIq &&
property_name != kDesktopWidth &&
property_name != kDesktopHeight) {
*exception =
Var("Cannot set property " + property_name + " on this object.");
return;
}
// Since we're whitelisting the propertie that are settable above, we can
// assume that the property exists in the map.
properties_[property_names_[property_name]].attribute = value;
}
Var ChromotingScriptableObject::Call(const Var& method_name,
const std::vector<Var>& args,
Var* exception) {
PropertyNameMap::const_iterator iter =
property_names_.find(method_name.AsString());
if (iter == property_names_.end()) {
return pp::deprecated::ScriptableObject::Call(method_name, args, exception);
}
return (this->*(properties_[iter->second].method))(args, exception);
}
void ChromotingScriptableObject::SetConnectionInfo(ConnectionStatus status,
ConnectionQuality quality) {
int status_index = property_names_[kStatusAttribute];
int quality_index = property_names_[kQualityAttribute];
LogDebugInfo(
base::StringPrintf("Connection status is updated: %d.", status));
if (properties_[status_index].attribute.AsInt() != status ||
properties_[quality_index].attribute.AsInt() != quality) {
// Update the connection state properties..
properties_[status_index].attribute = Var(status);
properties_[quality_index].attribute = Var(quality);
// Signal the Chromoting Tab UI to get the update connection state values.
SignalConnectionInfoChange();
}
}
void ChromotingScriptableObject::LogDebugInfo(const std::string& info) {
Var exception;
Var cb = GetProperty(Var(kDebugInfo), &exception);
// Var() means call the object directly as a function rather than calling
// a method in the object.
cb.Call(Var(), Var(info), &exception);
if (!exception.is_undefined()) {
LOG(WARNING) << "Exception when invoking debugInfo JS callback: "
<< exception.DebugString();
}
}
void ChromotingScriptableObject::SetDesktopSize(int width, int height) {
int width_index = property_names_[kDesktopWidth];
int height_index = property_names_[kDesktopHeight];
if (properties_[width_index].attribute.AsInt() != width ||
properties_[height_index].attribute.AsInt() != height) {
properties_[width_index].attribute = Var(width);
properties_[height_index].attribute = Var(height);
SignalDesktopSizeChange();
}
LogDebugInfo(base::StringPrintf("Update desktop size to: %d x %d.",
width, height));
}
void ChromotingScriptableObject::AddAttribute(const std::string& name,
Var attribute) {
property_names_[name] = properties_.size();
properties_.push_back(PropertyDescriptor(name, attribute));
}
void ChromotingScriptableObject::AddMethod(const std::string& name,
MethodHandler handler) {
property_names_[name] = properties_.size();
properties_.push_back(PropertyDescriptor(name, handler));
}
void ChromotingScriptableObject::SignalConnectionInfoChange() {
Var exception;
Var cb = GetProperty(Var(kConnectionInfoUpdate), &exception);
// Var() means call the object directly as a function rather than calling
// a method in the object.
cb.Call(Var(), &exception);
if (!exception.is_undefined())
LogDebugInfo(
"Exception when invoking connectionInfoUpdate JS callback.");
}
void ChromotingScriptableObject::SignalDesktopSizeChange() {
Var exception;
// The JavaScript callback function is the 'callback' property on the
// 'desktopSizeUpdate' object.
Var cb = GetProperty(Var(kDesktopSizeUpdate), &exception);
// Var() means call the object directly as a function rather than calling
// a method in the object.
cb.Call(Var(), 0, NULL, &exception);
if (!exception.is_undefined()) {
LOG(WARNING) << "Exception when invoking JS callback"
<< exception.DebugString();
}
}
void ChromotingScriptableObject::SignalLoginChallenge() {
Var exception;
Var cb = GetProperty(Var(kLoginChallenge), &exception);
// Var() means call the object directly as a function rather than calling
// a method in the object.
cb.Call(Var(), &exception);
if (!exception.is_undefined())
LogDebugInfo("Exception when invoking loginChallenge JS callback.");
}
void ChromotingScriptableObject::AttachXmppProxy(PepperXmppProxy* xmpp_proxy) {
xmpp_proxy_ = xmpp_proxy;
}
void ChromotingScriptableObject::SendIq(const std::string& message_xml) {
Var exception;
Var cb = GetProperty(Var(kSendIq), &exception);
// Var() means call the object directly as a function rather than calling
// a method in the object.
cb.Call(Var(), Var(message_xml), &exception);
if (!exception.is_undefined())
LogDebugInfo("Exception when invoking sendiq JS callback.");
}
Var ChromotingScriptableObject::DoConnect(const std::vector<Var>& args,
Var* exception) {
// Parameter order is:
// host_jid
// client_jid
// access_code (optional)
unsigned int arg = 0;
if (!args[arg].is_string()) {
*exception = Var("The host_jid must be a string.");
return Var();
}
std::string host_jid = args[arg++].AsString();
if (!args[arg].is_string()) {
*exception = Var("The client_jid must be a string.");
return Var();
}
std::string client_jid = args[arg++].AsString();
std::string access_code;
if (args.size() > arg) {
if (!args[arg].is_string()) {
*exception = Var("The access code must be a string.");
return Var();
}
access_code = args[arg++].AsString();
}
if (args.size() != arg) {
*exception = Var("Too many agruments passed to connect().");
return Var();
}
LogDebugInfo("Connecting to host.");
VLOG(1) << "client_jid: " << client_jid << ", host_jid: " << host_jid
<< ", access_code: " << access_code;
instance_->ConnectSandboxed(client_jid, host_jid, access_code);
return Var();
}
Var ChromotingScriptableObject::DoConnectUnsandboxed(
const std::vector<Var>& args,
Var* exception) {
// Parameter order is:
// host_jid
// username
// auth_token
// access_code (optional)
unsigned int arg = 0;
if (!args[arg].is_string()) {
*exception = Var("The host_jid must be a string.");
return Var();
}
std::string host_jid = args[arg++].AsString();
if (!args[arg].is_string()) {
*exception = Var("The username must be a string.");
return Var();
}
std::string username = args[arg++].AsString();
if (!args[arg].is_string()) {
*exception = Var("The auth_token_with_service must be a string.");
return Var();
}
std::string auth_token_with_service = args[arg++].AsString();
std::string auth_service;
std::string auth_token;
ParseAuthTokenWithService(auth_token_with_service, &auth_token,
&auth_service);
std::string access_code;
if (args.size() > arg) {
if (!args[arg].is_string()) {
*exception = Var("The access code must be a string.");
return Var();
}
access_code = args[arg++].AsString();
}
if (args.size() != arg) {
*exception = Var("Too many agruments passed to connect().");
return Var();
}
LogDebugInfo("Connecting to host.");
ClientConfig config;
config.host_jid = host_jid;
config.username = username;
config.auth_token = auth_token;
config.auth_service = auth_service;
config.nonce = access_code;
VLOG(1) << "host_jid: " << host_jid << ", username: " << username
<< ", auth_service: " << auth_service
<< ", access_code: " << access_code;
instance_->Connect(config);
return Var();
}
Var ChromotingScriptableObject::DoDisconnect(const std::vector<Var>& args,
Var* exception) {
LogDebugInfo("Disconnecting from host.");
instance_->Disconnect();
return Var();
}
Var ChromotingScriptableObject::DoSubmitLogin(const std::vector<Var>& args,
Var* exception) {
if (args.size() != 2) {
*exception = Var("Usage: login(username, password)");
return Var();
}
if (!args[0].is_string()) {
*exception = Var("Username must be a string.");
return Var();
}
std::string username = args[0].AsString();
if (!args[1].is_string()) {
*exception = Var("Password must be a string.");
return Var();
}
std::string password = args[1].AsString();
LogDebugInfo("Submitting login info to host.");
instance_->SubmitLoginInfo(username, password);
return Var();
}
Var ChromotingScriptableObject::DoSetScaleToFit(const std::vector<Var>& args,
Var* exception) {
if (args.size() != 1) {
*exception = Var("Usage: setScaleToFit(scale_to_fit)");
return Var();
}
if (!args[0].is_bool()) {
*exception = Var("scale_to_fit must be a boolean.");
return Var();
}
LogDebugInfo("Setting scale-to-fit.");
instance_->SetScaleToFit(args[0].AsBool());
return Var();
}
Var ChromotingScriptableObject::DoOnIq(const std::vector<Var>& args,
Var* exception) {
if (args.size() != 1) {
*exception = Var("Usage: onIq(response_xml)");
return Var();
}
if (!args[0].is_string()) {
*exception = Var("response_xml must be a string.");
return Var();
}
xmpp_proxy_->OnIq(args[0].AsString());
return Var();
}
} // namespace remoting
|