Chromium Code Reviews| Index: remoting/host/plugin/host_script_object.cc |
| diff --git a/remoting/host/plugin/host_script_object.cc b/remoting/host/plugin/host_script_object.cc |
| index 2ba2c468dda1327ce00b3802c8d244df4d71afd5..f2b6612397fae7b4209d526eac2f2323a7cb036e 100644 |
| --- a/remoting/host/plugin/host_script_object.cc |
| +++ b/remoting/host/plugin/host_script_object.cc |
| @@ -15,9 +15,9 @@ |
| #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/host_plugin_utils.h" |
| #include "remoting/host/register_support_host_request.h" |
| #include "remoting/host/support_access_verifier.h" |
| +#include "remoting/host/ui_strings.h" |
| namespace remoting { |
| @@ -38,6 +38,7 @@ namespace remoting { |
| // |
| // attribute Function void logDebugInfo(string); |
| // attribute Function void onStateChanged(); |
| +// attribute Function string localize(string,...); |
|
Wez
2011/08/13 01:35:32
nit: Call this "localizeString"?
Jamie
2011/08/13 02:20:15
Done.
|
| // |
| // // The |auth_service_with_token| parameter should be in the format |
| // // "auth_service:auth_token". An example would be "oauth2:1/2a3912vd". |
| @@ -50,6 +51,7 @@ const char* kAttrNameAccessCode = "accessCode"; |
| const char* kAttrNameAccessCodeLifetime = "accessCodeLifetime"; |
| const char* kAttrNameClient = "client"; |
| const char* kAttrNameState = "state"; |
| +const char* kAttrNameLocalize = "localize"; |
| const char* kAttrNameLogDebugInfo = "logDebugInfo"; |
| const char* kAttrNameOnStateChanged = "onStateChanged"; |
| const char* kFuncNameConnect = "connect"; |
| @@ -78,8 +80,6 @@ HostNPScriptObject::HostNPScriptObject(NPP plugin, NPObject* parent) |
| : plugin_(plugin), |
| parent_(parent), |
| state_(kDisconnected), |
| - log_debug_info_func_(NULL), |
| - on_state_changed_func_(NULL), |
| np_thread_id_(base::PlatformThread::CurrentId()), |
| failed_login_attempts_(0), |
| disconnected_event_(true, false) { |
| @@ -123,13 +123,6 @@ HostNPScriptObject::~HostNPScriptObject() { |
| // Stop all threads. |
| host_context_.Stop(); |
| - |
| - if (log_debug_info_func_) { |
| - g_npnetscape_funcs->releaseobject(log_debug_info_func_); |
| - } |
| - if (on_state_changed_func_) { |
| - g_npnetscape_funcs->releaseobject(on_state_changed_func_); |
| - } |
| } |
| bool HostNPScriptObject::Init() { |
| @@ -178,6 +171,7 @@ bool HostNPScriptObject::HasProperty(const std::string& property_name) { |
| property_name == kAttrNameAccessCodeLifetime || |
| property_name == kAttrNameClient || |
| property_name == kAttrNameState || |
| + property_name == kAttrNameLocalize || |
| property_name == kAttrNameLogDebugInfo || |
| property_name == kAttrNameOnStateChanged || |
| property_name == kAttrNameDisconnected || |
| @@ -198,10 +192,13 @@ bool HostNPScriptObject::GetProperty(const std::string& property_name, |
| } |
| if (property_name == kAttrNameOnStateChanged) { |
| - OBJECT_TO_NPVARIANT(on_state_changed_func_, *result); |
| + OBJECT_TO_NPVARIANT(on_state_changed_func_.get(), *result); |
| + return true; |
| + } else if (property_name == kAttrNameLocalize) { |
| + OBJECT_TO_NPVARIANT(localize_func_.get(), *result); |
| return true; |
| } else if (property_name == kAttrNameLogDebugInfo) { |
| - OBJECT_TO_NPVARIANT(log_debug_info_func_, *result); |
| + OBJECT_TO_NPVARIANT(log_debug_info_func_.get(), *result); |
| return true; |
| } else if (property_name == kAttrNameState) { |
| INT32_TO_NPVARIANT(state_, *result); |
| @@ -246,13 +243,42 @@ bool HostNPScriptObject::SetProperty(const std::string& property_name, |
| if (property_name == kAttrNameOnStateChanged) { |
| if (NPVARIANT_IS_OBJECT(*value)) { |
| - if (on_state_changed_func_) { |
| - g_npnetscape_funcs->releaseobject(on_state_changed_func_); |
| - } |
| - on_state_changed_func_ = NPVARIANT_TO_OBJECT(*value); |
| - if (on_state_changed_func_) { |
| - g_npnetscape_funcs->retainobject(on_state_changed_func_); |
| - } |
| + on_state_changed_func_.replace(NPVARIANT_TO_OBJECT(*value)); |
| + return true; |
| + } else { |
| + SetException("SetProperty: unexpected type for property " + |
| + property_name); |
| + } |
| + return false; |
| + } |
| + |
| + if (property_name == kAttrNameLocalize) { |
| + if (NPVARIANT_IS_OBJECT(*value)) { |
| + localize_func_.replace(NPVARIANT_TO_OBJECT(*value)); |
| + // Localize static strings. The disconnect window text can't be localized |
| + // until the user name is known. |
| + std::string direction; |
| + Localize("@@bidi_dir", NULL, &direction); |
| + remoting::ui_strings::direction = |
| + direction == "rtl" ? remoting::ui_strings::RTL |
| + : remoting::ui_strings::LTR; |
| + Localize("productName", NULL, &remoting::ui_strings::productName); |
| + Localize("disconnectButton", NULL, |
| + &remoting::ui_strings::disconnectButtonText); |
| + Localize( |
| +#if defined(OS_WIN) |
| + "disconnectButtonShortcutWindows", |
| +#elif defined(OS_MAC) |
| + "disconnectButtonShortcutMacOSX", |
| +#else |
| + "disconnectButtonShortcutLinux", |
| +#endif |
| + NULL, &remoting::ui_strings::disconnectButtonShortcut); |
| + Localize("continuePrompt", NULL, &remoting::ui_strings::continuePrompt); |
| + Localize("continueButton", NULL, |
| + &remoting::ui_strings::continueButtonText); |
| + Localize("stopSharingButton", |
| + NULL, &remoting::ui_strings::stopSharingButtonText); |
|
Wez
2011/08/13 01:35:32
Move these into a LocalizeStrings() function, rath
Jamie
2011/08/13 02:20:15
Done.
|
| return true; |
| } else { |
| SetException("SetProperty: unexpected type for property " + |
| @@ -263,13 +289,7 @@ bool HostNPScriptObject::SetProperty(const std::string& property_name, |
| if (property_name == kAttrNameLogDebugInfo) { |
| if (NPVARIANT_IS_OBJECT(*value)) { |
| - if (log_debug_info_func_) { |
| - g_npnetscape_funcs->releaseobject(log_debug_info_func_); |
| - } |
| - log_debug_info_func_ = NPVARIANT_TO_OBJECT(*value); |
| - if (log_debug_info_func_) { |
| - g_npnetscape_funcs->retainobject(log_debug_info_func_); |
| - } |
| + log_debug_info_func_.replace(NPVARIANT_TO_OBJECT(*value)); |
| return true; |
| } else { |
| SetException("SetProperty: unexpected type for property " + |
| @@ -293,6 +313,7 @@ bool HostNPScriptObject::Enumerate(std::vector<std::string>* values) { |
| const char* entries[] = { |
| kAttrNameAccessCode, |
| kAttrNameState, |
| + kAttrNameLocalize, |
| kAttrNameLogDebugInfo, |
| kAttrNameOnStateChanged, |
| kFuncNameConnect, |
| @@ -333,6 +354,9 @@ void HostNPScriptObject::OnClientAuthenticated( |
| if (pos != std::string::npos) |
| client_username_.replace(pos, std::string::npos, ""); |
| LOG(INFO) << "Client " << client_username_ << " connected."; |
| + // Localize the 'your desktop is shared' string now that we have the username. |
| + Localize("messageShared", client_username_.c_str(), |
| + &remoting::ui_strings::disconnectMessage); |
| OnStateChanged(kConnected); |
| } |
| @@ -523,9 +547,9 @@ void HostNPScriptObject::OnStateChanged(State state) { |
| return; |
| } |
| state_ = state; |
| - if (on_state_changed_func_) { |
| + if (on_state_changed_func_.get()) { |
| VLOG(2) << "Calling state changed " << state; |
| - bool is_good = InvokeAndIgnoreResult(on_state_changed_func_, NULL, 0); |
| + bool is_good = InvokeAndIgnoreResult(on_state_changed_func_.get(), NULL, 0); |
| LOG_IF(ERROR, !is_good) << "OnStateChanged failed"; |
| } |
| } |
| @@ -561,10 +585,10 @@ void HostNPScriptObject::LogDebugInfo(const std::string& message) { |
| return; |
| } |
| - if (log_debug_info_func_) { |
| + 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_, |
| + bool is_good = InvokeAndIgnoreResult(log_debug_info_func_.get(), |
| &log_message, 1); |
| LOG_IF(ERROR, !is_good) << "LogDebugInfo failed"; |
| } |
| @@ -576,6 +600,30 @@ void HostNPScriptObject::SetException(const std::string& exception_string) { |
| LOG(INFO) << exception_string; |
| } |
| +bool HostNPScriptObject::Localize(const char* tag, const char* parameter, |
|
Wez
2011/08/13 01:35:32
nit: |parameter| isn't very descriptive; is there
Jamie
2011/08/13 02:20:15
Hmmm... Now that you mention it, that's probably t
|
| + std::string* result) { |
| + NPVariant args[2]; |
| + STRINGZ_TO_NPVARIANT(tag, args[0]); |
| + if (parameter) { |
| + STRINGZ_TO_NPVARIANT(parameter, args[1]); |
| + } |
| + NPVariant np_result; |
| + bool is_good = g_npnetscape_funcs->invokeDefault( |
| + plugin_, localize_func_.get(), &args[0], parameter ? 2 : 1, &np_result); |
|
Wez
2011/08/13 01:35:32
nit: Maybe pre-calculate arg_count rather than use
Jamie
2011/08/13 02:20:15
Done.
|
| + if (!is_good) { |
| + LOG(ERROR) << "Localization failed for " << tag; |
|
Wez
2011/08/13 01:35:32
nit: localize() failed? This really means that th
Jamie
2011/08/13 02:20:15
I don't really know in what circumstances this mig
|
| + 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) { |