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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
|
// 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/host/plugin/host_script_object.h"
#include "base/bind.h"
#include "base/message_loop.h"
#include "base/message_loop_proxy.h"
#include "base/threading/platform_thread.h"
#include "remoting/base/auth_token_util.h"
#include "remoting/base/util.h"
#include "remoting/host/chromoting_host.h"
#include "remoting/host/chromoting_host_context.h"
#include "remoting/host/desktop_environment.h"
#include "remoting/host/host_config.h"
#include "remoting/host/host_key_pair.h"
#include "remoting/host/in_memory_host_config.h"
#include "remoting/host/plugin/policy_hack/nat_policy.h"
#include "remoting/host/register_support_host_request.h"
#include "remoting/host/support_access_verifier.h"
#include "remoting/host/ui_strings.h"
namespace remoting {
// Supported Javascript interface:
// readonly attribute string accessCode;
// readonly attribute int accessCodeLifetime;
// readonly attribute string client;
// readonly attribute int state;
//
// state: {
// DISCONNECTED,
// REQUESTED_ACCESS_CODE,
// RECEIVED_ACCESS_CODE,
// CONNECTED,
// AFFIRMING_CONNECTION,
// ERROR,
// }
//
// attribute Function void logDebugInfo(string);
// attribute Function void onStateChanged();
// attribute Function string localizeString(string,...);
//
// // The |auth_service_with_token| parameter should be in the format
// // "auth_service:auth_token". An example would be "oauth2:1/2a3912vd".
// void connect(string uid, string auth_service_with_token);
// void disconnect();
namespace {
const char* kAttrNameAccessCode = "accessCode";
const char* kAttrNameAccessCodeLifetime = "accessCodeLifetime";
const char* kAttrNameClient = "client";
const char* kAttrNameState = "state";
const char* kAttrNameLocalizeString = "localizeString";
const char* kAttrNameLogDebugInfo = "logDebugInfo";
const char* kAttrNameOnStateChanged = "onStateChanged";
const char* kFuncNameConnect = "connect";
const char* kFuncNameDisconnect = "disconnect";
// States.
const char* kAttrNameDisconnected = "DISCONNECTED";
const char* kAttrNameRequestedAccessCode = "REQUESTED_ACCESS_CODE";
const char* kAttrNameReceivedAccessCode = "RECEIVED_ACCESS_CODE";
const char* kAttrNameConnected = "CONNECTED";
const char* kAttrNameAffirmingConnection = "AFFIRMING_CONNECTION";
const char* kAttrNameError = "ERROR";
const int kMaxLoginAttempts = 5;
} // namespace
// This flag blocks LOGs to the UI if we're already in the middle of logging
// to the UI. This prevents a potential infinite loop if we encounter an error
// while sending the log message to the UI.
static bool g_logging_to_plugin = false;
static HostNPScriptObject* g_logging_scriptable_object = NULL;
static logging::LogMessageHandlerFunction g_logging_old_handler = NULL;
HostNPScriptObject::HostNPScriptObject(
NPP plugin,
NPObject* parent,
PluginMessageLoopProxy::Delegate* plugin_thread_delegate)
: plugin_(plugin),
parent_(parent),
state_(kDisconnected),
np_thread_id_(base::PlatformThread::CurrentId()),
plugin_message_loop_proxy_(
new PluginMessageLoopProxy(plugin_thread_delegate)),
host_context_(plugin_message_loop_proxy_),
failed_login_attempts_(0),
disconnected_event_(true, false),
nat_traversal_enabled_(false),
policy_received_(false) {
// Set up log message handler.
// Note that this approach doesn't quite support having multiple instances
// of Chromoting running. In that case, the most recently opened tab will
// grab all the debug log messages, and when any Chromoting tab is closed
// the logging handler will go away.
// Since having multiple Chromoting tabs is not a primary use case, and this
// is just debug logging, we're punting improving debug log support for that
// case.
if (g_logging_old_handler == NULL)
g_logging_old_handler = logging::GetLogMessageHandler();
logging::SetLogMessageHandler(&LogToUI);
g_logging_scriptable_object = this;
}
HostNPScriptObject::~HostNPScriptObject() {
CHECK_EQ(base::PlatformThread::CurrentId(), np_thread_id_);
// Shutdown DesktopEnvironment first so that it doesn't try to post
// tasks on the UI thread while we are stopping the host.
desktop_environment_->Shutdown();
logging::SetLogMessageHandler(g_logging_old_handler);
g_logging_old_handler = NULL;
g_logging_scriptable_object = NULL;
plugin_message_loop_proxy_->Detach();
// Stop listening for policy updates.
if (nat_policy_.get()) {
base::WaitableEvent nat_policy_stopped_(true, false);
nat_policy_->StopWatching(&nat_policy_stopped_);
nat_policy_stopped_.Wait();
nat_policy_.reset();
}
// Disconnect synchronously. We cannot disconnect asynchronously
// here because |host_context_| needs to be stopped on the plugin
// thread, but the plugin thread may not exist after the instance
// is destroyed.
disconnected_event_.Reset();
DisconnectInternal();
disconnected_event_.Wait();
// Stop all threads.
host_context_.Stop();
}
bool HostNPScriptObject::Init() {
VLOG(2) << "Init";
// TODO(wez): This starts a bunch of threads, which might fail.
host_context_.Start();
nat_policy_.reset(
policy_hack::NatPolicy::Create(host_context_.network_message_loop()));
nat_policy_->StartWatching(
base::Bind(&HostNPScriptObject::OnNatPolicyUpdate,
base::Unretained(this)));
return true;
}
bool HostNPScriptObject::HasMethod(const std::string& method_name) {
VLOG(2) << "HasMethod " << method_name;
CHECK_EQ(base::PlatformThread::CurrentId(), np_thread_id_);
return (method_name == kFuncNameConnect ||
method_name == kFuncNameDisconnect);
}
bool HostNPScriptObject::InvokeDefault(const NPVariant* args,
uint32_t argCount,
NPVariant* result) {
VLOG(2) << "InvokeDefault";
CHECK_EQ(base::PlatformThread::CurrentId(), np_thread_id_);
SetException("exception during default invocation");
return false;
}
bool HostNPScriptObject::Invoke(const std::string& method_name,
const NPVariant* args,
uint32_t argCount,
NPVariant* result) {
VLOG(2) << "Invoke " << method_name;
CHECK_EQ(base::PlatformThread::CurrentId(), np_thread_id_);
if (method_name == kFuncNameConnect) {
return Connect(args, argCount, result);
} else if (method_name == kFuncNameDisconnect) {
return Disconnect(args, argCount, result);
} else {
SetException("Invoke: unknown method " + method_name);
return false;
}
}
bool HostNPScriptObject::HasProperty(const std::string& property_name) {
VLOG(2) << "HasProperty " << property_name;
CHECK_EQ(base::PlatformThread::CurrentId(), np_thread_id_);
return (property_name == kAttrNameAccessCode ||
property_name == kAttrNameAccessCodeLifetime ||
property_name == kAttrNameClient ||
property_name == kAttrNameState ||
property_name == kAttrNameLocalizeString ||
property_name == kAttrNameLogDebugInfo ||
property_name == kAttrNameOnStateChanged ||
property_name == kAttrNameDisconnected ||
property_name == kAttrNameRequestedAccessCode ||
property_name == kAttrNameReceivedAccessCode ||
property_name == kAttrNameConnected ||
property_name == kAttrNameAffirmingConnection ||
property_name == kAttrNameError);
}
bool HostNPScriptObject::GetProperty(const std::string& property_name,
NPVariant* result) {
VLOG(2) << "GetProperty " << property_name;
CHECK_EQ(base::PlatformThread::CurrentId(), np_thread_id_);
if (!result) {
SetException("GetProperty: NULL result");
return false;
}
if (property_name == kAttrNameOnStateChanged) {
OBJECT_TO_NPVARIANT(on_state_changed_func_.get(), *result);
return true;
} else if (property_name == kAttrNameLocalizeString) {
OBJECT_TO_NPVARIANT(localize_func_.get(), *result);
return true;
} else if (property_name == kAttrNameLogDebugInfo) {
OBJECT_TO_NPVARIANT(log_debug_info_func_.get(), *result);
return true;
} else if (property_name == kAttrNameState) {
INT32_TO_NPVARIANT(state_, *result);
return true;
} else if (property_name == kAttrNameAccessCode) {
*result = NPVariantFromString(access_code_);
return true;
} else if (property_name == kAttrNameAccessCodeLifetime) {
INT32_TO_NPVARIANT(access_code_lifetime_.InSeconds(), *result);
return true;
} else if (property_name == kAttrNameClient) {
*result = NPVariantFromString(client_username_);
return true;
} else if (property_name == kAttrNameDisconnected) {
INT32_TO_NPVARIANT(kDisconnected, *result);
return true;
} else if (property_name == kAttrNameRequestedAccessCode) {
INT32_TO_NPVARIANT(kRequestedAccessCode, *result);
return true;
} else if (property_name == kAttrNameReceivedAccessCode) {
INT32_TO_NPVARIANT(kReceivedAccessCode, *result);
return true;
} else if (property_name == kAttrNameConnected) {
INT32_TO_NPVARIANT(kConnected, *result);
return true;
} else if (property_name == kAttrNameAffirmingConnection) {
INT32_TO_NPVARIANT(kAffirmingConnection, *result);
return true;
} else if (property_name == kAttrNameError) {
INT32_TO_NPVARIANT(kError, *result);
return true;
} else {
SetException("GetProperty: unsupported property " + property_name);
return false;
}
}
bool HostNPScriptObject::SetProperty(const std::string& property_name,
const NPVariant* value) {
VLOG(2) << "SetProperty " << property_name;
CHECK_EQ(base::PlatformThread::CurrentId(), np_thread_id_);
if (property_name == kAttrNameOnStateChanged) {
if (NPVARIANT_IS_OBJECT(*value)) {
on_state_changed_func_ = NPVARIANT_TO_OBJECT(*value);
return true;
} else {
SetException("SetProperty: unexpected type for property " +
property_name);
}
return false;
}
if (property_name == kAttrNameLocalizeString) {
if (NPVARIANT_IS_OBJECT(*value)) {
localize_func_ = NPVARIANT_TO_OBJECT(*value);
return true;
} else {
SetException("SetProperty: unexpected type for property " +
property_name);
}
return false;
}
if (property_name == kAttrNameLogDebugInfo) {
if (NPVARIANT_IS_OBJECT(*value)) {
log_debug_info_func_ = NPVARIANT_TO_OBJECT(*value);
return true;
} else {
SetException("SetProperty: unexpected type for property " +
property_name);
}
return false;
}
return false;
}
bool HostNPScriptObject::RemoveProperty(const std::string& property_name) {
VLOG(2) << "RemoveProperty " << property_name;
CHECK_EQ(base::PlatformThread::CurrentId(), np_thread_id_);
return false;
}
bool HostNPScriptObject::Enumerate(std::vector<std::string>* values) {
VLOG(2) << "Enumerate";
CHECK_EQ(base::PlatformThread::CurrentId(), np_thread_id_);
const char* entries[] = {
kAttrNameAccessCode,
kAttrNameState,
kAttrNameLocalizeString,
kAttrNameLogDebugInfo,
kAttrNameOnStateChanged,
kFuncNameConnect,
kFuncNameDisconnect,
kAttrNameDisconnected,
kAttrNameRequestedAccessCode,
kAttrNameReceivedAccessCode,
kAttrNameConnected,
kAttrNameAffirmingConnection,
kAttrNameError
};
for (size_t i = 0; i < arraysize(entries); ++i) {
values->push_back(entries[i]);
}
return true;
}
void HostNPScriptObject::OnSignallingConnected(SignalStrategy* signal_strategy,
const std::string& full_jid) {
}
void HostNPScriptObject::OnSignallingDisconnected() {
}
void HostNPScriptObject::OnAccessDenied() {
DCHECK(host_context_.network_message_loop()->BelongsToCurrentThread());
++failed_login_attempts_;
if (failed_login_attempts_ == kMaxLoginAttempts)
DisconnectInternal();
}
void HostNPScriptObject::OnClientAuthenticated(
remoting::protocol::ConnectionToClient* client) {
DCHECK_NE(base::PlatformThread::CurrentId(), np_thread_id_);
client_username_ = client->session()->jid();
size_t pos = client_username_.find('/');
if (pos != std::string::npos)
client_username_.replace(pos, std::string::npos, "");
LOG(INFO) << "Client " << client_username_ << " connected.";
LocalizeStrings();
OnStateChanged(kConnected);
}
void HostNPScriptObject::OnClientDisconnected(
remoting::protocol::ConnectionToClient* client) {
client_username_.clear();
OnStateChanged(kDisconnected);
}
void HostNPScriptObject::OnShutdown() {
DCHECK_EQ(MessageLoop::current(), host_context_.main_message_loop());
OnStateChanged(kDisconnected);
}
// string uid, string auth_token
bool HostNPScriptObject::Connect(const NPVariant* args,
uint32_t arg_count,
NPVariant* result) {
CHECK_EQ(base::PlatformThread::CurrentId(), np_thread_id_);
LOG(INFO) << "Connecting...";
if (arg_count != 2) {
SetException("connect: bad number of arguments");
return false;
}
std::string uid = StringFromNPVariant(args[0]);
if (uid.empty()) {
SetException("connect: bad uid argument");
return false;
}
std::string auth_service_with_token = StringFromNPVariant(args[1]);
std::string auth_token;
std::string auth_service;
ParseAuthTokenWithService(auth_service_with_token, &auth_token,
&auth_service);
if (auth_token.empty()) {
SetException("connect: auth_service_with_token argument has empty token");
return false;
}
ReadPolicyAndConnect(uid, auth_token, auth_service);
return true;
}
void HostNPScriptObject::ReadPolicyAndConnect(const std::string& uid,
const std::string& auth_token,
const std::string& auth_service) {
if (MessageLoop::current() != host_context_.main_message_loop()) {
host_context_.main_message_loop()->PostTask(
FROM_HERE, base::Bind(
&HostNPScriptObject::ReadPolicyAndConnect, base::Unretained(this),
uid, auth_token, auth_service));
return;
}
// Only proceed to FinishConnect() if at least one policy update has been
// received.
if (policy_received_) {
FinishConnect(uid, auth_token, auth_service);
} else {
// Otherwise, create the policy watcher, and thunk the connect.
pending_connect_ =
base::Bind(&HostNPScriptObject::FinishConnect,
base::Unretained(this), uid, auth_token, auth_service);
}
}
void HostNPScriptObject::FinishConnect(
const std::string& uid,
const std::string& auth_token,
const std::string& auth_service) {
if (MessageLoop::current() != host_context_.main_message_loop()) {
host_context_.main_message_loop()->PostTask(
FROM_HERE, base::Bind(
&HostNPScriptObject::FinishConnect, base::Unretained(this),
uid, auth_token, auth_service));
return;
}
// Store the supplied user ID and token to the Host configuration.
scoped_refptr<MutableHostConfig> host_config = new InMemoryHostConfig();
host_config->SetString(kXmppLoginConfigPath, uid);
host_config->SetString(kXmppAuthTokenConfigPath, auth_token);
host_config->SetString(kXmppAuthServiceConfigPath, auth_service);
// Create an access verifier and fetch the host secret.
scoped_ptr<SupportAccessVerifier> access_verifier;
access_verifier.reset(new SupportAccessVerifier());
// Generate a key pair for the Host to use.
// TODO(wez): Move this to the worker thread.
HostKeyPair host_key_pair;
host_key_pair.Generate();
host_key_pair.Save(host_config);
// Request registration of the host for support.
scoped_ptr<RegisterSupportHostRequest> register_request(
new RegisterSupportHostRequest());
if (!register_request->Init(
host_config.get(),
base::Bind(&HostNPScriptObject::OnReceivedSupportID,
base::Unretained(this),
access_verifier.get()))) {
OnStateChanged(kDisconnected);
return;
}
// Nothing went wrong, so lets save the host, config and request.
host_config_ = host_config;
register_request_.reset(register_request.release());
// Create DesktopEnvironment.
desktop_environment_.reset(DesktopEnvironment::Create(&host_context_));
if (desktop_environment_.get() == NULL) {
OnStateChanged(kError);
return;
}
// Create the Host.
LOG(INFO) << "Connecting with NAT state: " << nat_traversal_enabled_;
host_ = ChromotingHost::Create(
&host_context_, host_config_, desktop_environment_.get(),
access_verifier.release(), nat_traversal_enabled_);
host_->AddStatusObserver(this);
host_->AddStatusObserver(register_request_.get());
host_->set_it2me(true);
// Start the Host.
host_->Start();
OnStateChanged(kRequestedAccessCode);
return;
}
bool HostNPScriptObject::Disconnect(const NPVariant* args,
uint32_t arg_count,
NPVariant* result) {
CHECK_EQ(base::PlatformThread::CurrentId(), np_thread_id_);
if (arg_count != 0) {
SetException("disconnect: bad number of arguments");
return false;
}
DisconnectInternal();
return true;
}
void HostNPScriptObject::DisconnectInternal() {
if (MessageLoop::current() != host_context_.main_message_loop()) {
host_context_.main_message_loop()->PostTask(
FROM_HERE, base::Bind(&HostNPScriptObject::DisconnectInternal,
base::Unretained(this)));
return;
}
if (!host_) {
disconnected_event_.Signal();
return;
}
host_->Shutdown(
NewRunnableMethod(this, &HostNPScriptObject::OnShutdownFinished));
}
void HostNPScriptObject::OnShutdownFinished() {
DCHECK_EQ(MessageLoop::current(), host_context_.main_message_loop());
host_ = NULL;
register_request_.reset();
host_config_ = NULL;
disconnected_event_.Signal();
}
void HostNPScriptObject::OnNatPolicyUpdate(bool nat_traversal_enabled) {
if (MessageLoop::current() != host_context_.main_message_loop()) {
host_context_.main_message_loop()->PostTask(
FROM_HERE,
base::Bind(&HostNPScriptObject::OnNatPolicyUpdate,
base::Unretained(this), nat_traversal_enabled));
return;
}
VLOG(2) << "OnNatPolicyUpdate: " << nat_traversal_enabled;
// When transitioning from enabled to disabled, force disconnect any
// existing session.
if (nat_traversal_enabled_ && !nat_traversal_enabled) {
DisconnectInternal();
}
policy_received_ = true;
nat_traversal_enabled_ = nat_traversal_enabled;
if (!pending_connect_.is_null()) {
pending_connect_.Run();
pending_connect_.Reset();
}
}
void HostNPScriptObject::OnReceivedSupportID(
SupportAccessVerifier* access_verifier,
bool success,
const std::string& support_id,
const base::TimeDelta& lifetime) {
CHECK_NE(base::PlatformThread::CurrentId(), np_thread_id_);
if (!success) {
// TODO(wez): Replace the success/fail flag with full error reporting.
DisconnectInternal();
return;
}
// Inform the AccessVerifier of our Support-Id, for authentication.
access_verifier->OnIT2MeHostRegistered(success, support_id);
// Combine the Support Id with the Host Id to make the Access Code.
// TODO(wez): Locking, anyone?
access_code_ = support_id + access_verifier->host_secret();
access_code_lifetime_ = lifetime;
// Tell the ChromotingHost the access code, to use as shared-secret.
host_->set_access_code(access_code_);
// Let the caller know that life is good.
OnStateChanged(kReceivedAccessCode);
}
void HostNPScriptObject::OnStateChanged(State state) {
if (!plugin_message_loop_proxy_->BelongsToCurrentThread()) {
plugin_message_loop_proxy_->PostTask(
FROM_HERE, base::Bind(&HostNPScriptObject::OnStateChanged,
base::Unretained(this), state));
return;
}
state_ = state;
if (on_state_changed_func_.get()) {
VLOG(2) << "Calling state changed " << state;
bool is_good = InvokeAndIgnoreResult(on_state_changed_func_.get(), NULL, 0);
LOG_IF(ERROR, !is_good) << "OnStateChanged failed";
}
}
// static
bool HostNPScriptObject::LogToUI(int severity, const char* file, int line,
size_t message_start,
const std::string& str) {
// The |g_logging_to_plugin| check is to prevent logging to the scriptable
// object if we're already in the middle of logging.
// This can occur if we try to log an error while we're in the scriptable
// object logging code.
if (g_logging_scriptable_object && !g_logging_to_plugin) {
g_logging_to_plugin = true;
std::string message = remoting::GetTimestampString();
message += (str.c_str() + message_start);
g_logging_scriptable_object->LogDebugInfo(message);
g_logging_to_plugin = false;
}
if (g_logging_old_handler)
return (g_logging_old_handler)(severity, file, line, message_start, str);
return false;
}
void HostNPScriptObject::LogDebugInfo(const std::string& message) {
if (!plugin_message_loop_proxy_->BelongsToCurrentThread()) {
plugin_message_loop_proxy_->PostTask(
FROM_HERE, base::Bind(&HostNPScriptObject::LogDebugInfo,
base::Unretained(this), message));
return;
}
if (log_debug_info_func_.get()) {
NPVariant log_message;
STRINGZ_TO_NPVARIANT(message.c_str(), log_message);
bool is_good = InvokeAndIgnoreResult(log_debug_info_func_.get(),
&log_message, 1);
LOG_IF(ERROR, !is_good) << "LogDebugInfo failed";
}
}
void HostNPScriptObject::SetException(const std::string& exception_string) {
CHECK_EQ(base::PlatformThread::CurrentId(), np_thread_id_);
g_npnetscape_funcs->setexception(parent_, exception_string.c_str());
LOG(INFO) << exception_string;
}
void HostNPScriptObject::LocalizeStrings() {
UiStrings* ui_strings = host_->ui_strings();
std::string direction;
LocalizeString("@@bidi_dir", NULL, &direction);
ui_strings->direction =
direction == "rtl" ? remoting::UiStrings::RTL
: remoting::UiStrings::LTR;
LocalizeString("PRODUCT_NAME", NULL, &ui_strings->product_name);
LocalizeString("DISCONNECT_BUTTON", NULL,
&ui_strings->disconnect_button_text);
LocalizeString(
#if defined(OS_WIN)
"DISCONNECT_BUTTON_PLUS_SHORTCUT_WINDOWS",
#elif defined(OS_MAC)
"DISCONNECT_BUTTON_PLUS_SHORTCUT_MAC_OS_X",
#else
"DISCONNECT_BUTTON_PLUS_SHORTCUT_LINUX",
#endif
NULL, &ui_strings->disconnect_button_text_plus_shortcut);
LocalizeString("CONTINUE_PROMPT", NULL, &ui_strings->continue_prompt);
LocalizeString("CONTINUE_BUTTON", NULL, &ui_strings->continue_button_text);
LocalizeString("STOP_SHARING_BUTTON", NULL,
&ui_strings->stop_sharing_button_text);
LocalizeString("MESSAGE_SHARED", client_username_.c_str(),
&ui_strings->disconnect_message);
}
bool HostNPScriptObject::LocalizeString(const char* tag,
const char* substitution,
std::string* result) {
NPVariant args[2];
STRINGZ_TO_NPVARIANT(tag, args[0]);
int arg_count = 1;
if (substitution) {
STRINGZ_TO_NPVARIANT(substitution, args[1]);
++arg_count;
}
NPVariant np_result;
bool is_good = g_npnetscape_funcs->invokeDefault(
plugin_, localize_func_.get(), &args[0], arg_count, &np_result);
if (!is_good) {
LOG(ERROR) << "Localization failed for " << tag;
return false;
}
std::string translation = StringFromNPVariant(np_result);
g_npnetscape_funcs->releasevariantvalue(&np_result);
if (translation.empty()) {
LOG(ERROR) << "Missing translation for " << tag;
return false;
}
*result = translation;
return true;
}
bool HostNPScriptObject::InvokeAndIgnoreResult(NPObject* func,
const NPVariant* args,
uint32_t argCount) {
NPVariant np_result;
bool is_good = g_npnetscape_funcs->invokeDefault(plugin_, func, args,
argCount, &np_result);
if (is_good)
g_npnetscape_funcs->releasevariantvalue(&np_result);
return is_good;
}
} // namespace remoting
|