| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/host/plugin/host_script_object.h" | 5 #include "remoting/host/plugin/host_script_object.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| (...skipping 639 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 650 } | 650 } |
| 651 | 651 |
| 652 bool HostNPScriptObject::GenerateKeyPair(const NPVariant* args, | 652 bool HostNPScriptObject::GenerateKeyPair(const NPVariant* args, |
| 653 uint32_t arg_count, | 653 uint32_t arg_count, |
| 654 NPVariant* result) { | 654 NPVariant* result) { |
| 655 if (arg_count != 1) { | 655 if (arg_count != 1) { |
| 656 SetException("generateKeyPair: bad number of arguments"); | 656 SetException("generateKeyPair: bad number of arguments"); |
| 657 return false; | 657 return false; |
| 658 } | 658 } |
| 659 | 659 |
| 660 NPObject* callback_obj = ObjectFromNPVariant(args[0]); | 660 ScopedRefNPObject callback_obj(ObjectFromNPVariant(args[0])); |
| 661 if (!callback_obj) { | 661 if (!callback_obj.get()) { |
| 662 SetException("generateKeyPair: invalid callback parameter"); | 662 SetException("generateKeyPair: invalid callback parameter"); |
| 663 return false; | 663 return false; |
| 664 } | 664 } |
| 665 | 665 |
| 666 callback_obj = g_npnetscape_funcs->retainobject(callback_obj); | |
| 667 | |
| 668 worker_thread_.message_loop_proxy()->PostTask( | 666 worker_thread_.message_loop_proxy()->PostTask( |
| 669 FROM_HERE, base::Bind(&HostNPScriptObject::DoGenerateKeyPair, | 667 FROM_HERE, base::Bind(&HostNPScriptObject::DoGenerateKeyPair, |
| 670 base::Unretained(this), callback_obj)); | 668 base::Unretained(this), callback_obj)); |
| 671 return true; | 669 return true; |
| 672 } | 670 } |
| 673 | 671 |
| 674 bool HostNPScriptObject::UpdateDaemonConfig(const NPVariant* args, | 672 bool HostNPScriptObject::UpdateDaemonConfig(const NPVariant* args, |
| 675 uint32_t arg_count, | 673 uint32_t arg_count, |
| 676 NPVariant* result) { | 674 NPVariant* result) { |
| 677 if (arg_count != 2) { | 675 if (arg_count != 2) { |
| 678 SetException("updateDaemonConfig: bad number of arguments"); | 676 SetException("updateDaemonConfig: bad number of arguments"); |
| 679 return false; | 677 return false; |
| 680 } | 678 } |
| 681 | 679 |
| 682 std::string config_str = StringFromNPVariant(args[0]); | 680 std::string config_str = StringFromNPVariant(args[0]); |
| 683 scoped_ptr<base::Value> config(base::JSONReader::Read(config_str, true)); | 681 scoped_ptr<base::Value> config(base::JSONReader::Read(config_str, true)); |
| 684 if (config_str.empty() || !config.get() || | 682 if (config_str.empty() || !config.get() || |
| 685 !config->IsType(base::Value::TYPE_DICTIONARY)) { | 683 !config->IsType(base::Value::TYPE_DICTIONARY)) { |
| 686 SetException("updateDaemonConfig: bad config parameter"); | 684 SetException("updateDaemonConfig: bad config parameter"); |
| 687 return false; | 685 return false; |
| 688 } | 686 } |
| 689 scoped_ptr<base::DictionaryValue> config_dict( | 687 scoped_ptr<base::DictionaryValue> config_dict( |
| 690 reinterpret_cast<base::DictionaryValue*>(config.release())); | 688 reinterpret_cast<base::DictionaryValue*>(config.release())); |
| 691 | 689 |
| 692 NPObject* callback_obj = ObjectFromNPVariant(args[1]); | 690 ScopedRefNPObject callback_obj(ObjectFromNPVariant(args[1])); |
| 693 if (!callback_obj) { | 691 if (callback_obj.get()) { |
| 694 SetException("updateDaemonConfig: invalid callback parameter"); | 692 SetException("updateDaemonConfig: invalid callback parameter"); |
| 695 return false; | 693 return false; |
| 696 } | 694 } |
| 697 | 695 |
| 698 if (config_dict->HasKey(kHostIdConfigPath) || | 696 if (config_dict->HasKey(kHostIdConfigPath) || |
| 699 config_dict->HasKey(kXmppLoginConfigPath)) { | 697 config_dict->HasKey(kXmppLoginConfigPath)) { |
| 700 SetException("updateDaemonConfig: trying to update immutable config " | 698 SetException("updateDaemonConfig: trying to update immutable config " |
| 701 "parameters"); | 699 "parameters"); |
| 702 return false; | 700 return false; |
| 703 } | 701 } |
| 704 | 702 |
| 705 callback_obj = g_npnetscape_funcs->retainobject(callback_obj); | |
| 706 | |
| 707 daemon_controller_->UpdateConfig( | 703 daemon_controller_->UpdateConfig( |
| 708 config_dict.Pass(), | 704 config_dict.Pass(), |
| 709 base::Bind(&HostNPScriptObject::InvokeAsyncResultCallback, | 705 base::Bind(&HostNPScriptObject::InvokeAsyncResultCallback, |
| 710 base::Unretained(this), callback_obj)); | 706 base::Unretained(this), callback_obj)); |
| 711 return true; | 707 return true; |
| 712 } | 708 } |
| 713 | 709 |
| 714 bool HostNPScriptObject::GetDaemonConfig(const NPVariant* args, | 710 bool HostNPScriptObject::GetDaemonConfig(const NPVariant* args, |
| 715 uint32_t arg_count, | 711 uint32_t arg_count, |
| 716 NPVariant* result) { | 712 NPVariant* result) { |
| 717 if (arg_count != 1) { | 713 if (arg_count != 1) { |
| 718 SetException("getDaemonConfig: bad number of arguments"); | 714 SetException("getDaemonConfig: bad number of arguments"); |
| 719 return false; | 715 return false; |
| 720 } | 716 } |
| 721 | 717 |
| 722 NPObject* callback_obj = ObjectFromNPVariant(args[0]); | 718 ScopedRefNPObject callback_obj(ObjectFromNPVariant(args[0])); |
| 723 if (!callback_obj) { | 719 if (!callback_obj.get()) { |
| 724 SetException("getDaemonConfig: invalid callback parameter"); | 720 SetException("getDaemonConfig: invalid callback parameter"); |
| 725 return false; | 721 return false; |
| 726 } | 722 } |
| 727 | 723 |
| 728 callback_obj = g_npnetscape_funcs->retainobject(callback_obj); | |
| 729 | |
| 730 // We control lifetime of the |daemon_controller_| so it's safe to | 724 // We control lifetime of the |daemon_controller_| so it's safe to |
| 731 // use base::Unretained() here. | 725 // use base::Unretained() here. |
| 732 daemon_controller_->GetConfig( | 726 daemon_controller_->GetConfig( |
| 733 base::Bind(&HostNPScriptObject::InvokeGetDaemonConfigCallback, | 727 base::Bind(&HostNPScriptObject::InvokeGetDaemonConfigCallback, |
| 734 base::Unretained(this), callback_obj)); | 728 base::Unretained(this), callback_obj)); |
| 735 | 729 |
| 736 return true; | 730 return true; |
| 737 } | 731 } |
| 738 | 732 |
| 739 bool HostNPScriptObject::StartDaemon(const NPVariant* args, | 733 bool HostNPScriptObject::StartDaemon(const NPVariant* args, |
| 740 uint32_t arg_count, | 734 uint32_t arg_count, |
| 741 NPVariant* result) { | 735 NPVariant* result) { |
| 742 if (arg_count != 2) { | 736 if (arg_count != 2) { |
| 743 SetException("startDaemon: bad number of arguments"); | 737 SetException("startDaemon: bad number of arguments"); |
| 744 return false; | 738 return false; |
| 745 } | 739 } |
| 746 | 740 |
| 747 std::string config_str = StringFromNPVariant(args[0]); | 741 std::string config_str = StringFromNPVariant(args[0]); |
| 748 scoped_ptr<base::Value> config(base::JSONReader::Read(config_str, true)); | 742 scoped_ptr<base::Value> config(base::JSONReader::Read(config_str, true)); |
| 749 if (config_str.empty() || !config.get() || | 743 if (config_str.empty() || !config.get() || |
| 750 !config->IsType(base::Value::TYPE_DICTIONARY)) { | 744 !config->IsType(base::Value::TYPE_DICTIONARY)) { |
| 751 SetException("updateDaemonConfig: bad config parameter"); | 745 SetException("updateDaemonConfig: bad config parameter"); |
| 752 return false; | 746 return false; |
| 753 } | 747 } |
| 754 scoped_ptr<base::DictionaryValue> config_dict( | 748 scoped_ptr<base::DictionaryValue> config_dict( |
| 755 reinterpret_cast<base::DictionaryValue*>(config.release())); | 749 reinterpret_cast<base::DictionaryValue*>(config.release())); |
| 756 | 750 |
| 757 NPObject* callback_obj = ObjectFromNPVariant(args[1]); | 751 ScopedRefNPObject callback_obj(ObjectFromNPVariant(args[1])); |
| 758 if (!callback_obj) { | 752 if (!callback_obj.get()) { |
| 759 SetException("startDaemon: invalid callback parameter"); | 753 SetException("startDaemon: invalid callback parameter"); |
| 760 return false; | 754 return false; |
| 761 } | 755 } |
| 762 | 756 |
| 763 callback_obj = g_npnetscape_funcs->retainobject(callback_obj); | |
| 764 | |
| 765 daemon_controller_->SetConfigAndStart( | 757 daemon_controller_->SetConfigAndStart( |
| 766 config_dict.Pass(), | 758 config_dict.Pass(), |
| 767 base::Bind(&HostNPScriptObject::InvokeAsyncResultCallback, | 759 base::Bind(&HostNPScriptObject::InvokeAsyncResultCallback, |
| 768 base::Unretained(this), callback_obj)); | 760 base::Unretained(this), callback_obj)); |
| 769 return true; | 761 return true; |
| 770 } | 762 } |
| 771 | 763 |
| 772 bool HostNPScriptObject::StopDaemon(const NPVariant* args, | 764 bool HostNPScriptObject::StopDaemon(const NPVariant* args, |
| 773 uint32_t arg_count, | 765 uint32_t arg_count, |
| 774 NPVariant* result) { | 766 NPVariant* result) { |
| 775 if (arg_count != 1) { | 767 if (arg_count != 1) { |
| 776 SetException("stopDaemon: bad number of arguments"); | 768 SetException("stopDaemon: bad number of arguments"); |
| 777 return false; | 769 return false; |
| 778 } | 770 } |
| 779 | 771 |
| 780 NPObject* callback_obj = ObjectFromNPVariant(args[0]); | 772 ScopedRefNPObject callback_obj(ObjectFromNPVariant(args[0])); |
| 781 if (!callback_obj) { | 773 if (!callback_obj.get()) { |
| 782 SetException("stopDaemon: invalid callback parameter"); | 774 SetException("stopDaemon: invalid callback parameter"); |
| 783 return false; | 775 return false; |
| 784 } | 776 } |
| 785 | 777 |
| 786 callback_obj = g_npnetscape_funcs->retainobject(callback_obj); | |
| 787 | |
| 788 daemon_controller_->Stop( | 778 daemon_controller_->Stop( |
| 789 base::Bind(&HostNPScriptObject::InvokeAsyncResultCallback, | 779 base::Bind(&HostNPScriptObject::InvokeAsyncResultCallback, |
| 790 base::Unretained(this), callback_obj)); | 780 base::Unretained(this), callback_obj)); |
| 791 return true; | 781 return true; |
| 792 } | 782 } |
| 793 | 783 |
| 794 void HostNPScriptObject::DisconnectInternal() { | 784 void HostNPScriptObject::DisconnectInternal() { |
| 795 if (!host_context_->network_message_loop()->BelongsToCurrentThread()) { | 785 if (!host_context_->network_message_loop()->BelongsToCurrentThread()) { |
| 796 host_context_->network_message_loop()->PostTask( | 786 host_context_->network_message_loop()->PostTask( |
| 797 FROM_HERE, base::Bind(&HostNPScriptObject::DisconnectInternal, | 787 FROM_HERE, base::Bind(&HostNPScriptObject::DisconnectInternal, |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1027 return; | 1017 return; |
| 1028 } | 1018 } |
| 1029 if (on_nat_traversal_policy_changed_func_.get()) { | 1019 if (on_nat_traversal_policy_changed_func_.get()) { |
| 1030 NPVariant policy; | 1020 NPVariant policy; |
| 1031 BOOLEAN_TO_NPVARIANT(nat_traversal_enabled, policy); | 1021 BOOLEAN_TO_NPVARIANT(nat_traversal_enabled, policy); |
| 1032 InvokeAndIgnoreResult(on_nat_traversal_policy_changed_func_.get(), | 1022 InvokeAndIgnoreResult(on_nat_traversal_policy_changed_func_.get(), |
| 1033 &policy, 1); | 1023 &policy, 1); |
| 1034 } | 1024 } |
| 1035 } | 1025 } |
| 1036 | 1026 |
| 1037 void HostNPScriptObject::DoGenerateKeyPair(NPObject* callback) { | 1027 void HostNPScriptObject::DoGenerateKeyPair(const ScopedRefNPObject& callback) { |
| 1038 HostKeyPair key_pair; | 1028 HostKeyPair key_pair; |
| 1039 key_pair.Generate(); | 1029 key_pair.Generate(); |
| 1040 InvokeGenerateKeyPairCallback(callback, key_pair.GetAsString(), | 1030 InvokeGenerateKeyPairCallback(callback, key_pair.GetAsString(), |
| 1041 key_pair.GetPublicKey()); | 1031 key_pair.GetPublicKey()); |
| 1042 } | 1032 } |
| 1043 | 1033 |
| 1044 void HostNPScriptObject::InvokeGenerateKeyPairCallback( | 1034 void HostNPScriptObject::InvokeGenerateKeyPairCallback( |
| 1045 NPObject* callback, | 1035 const ScopedRefNPObject& callback, |
| 1046 const std::string& private_key, | 1036 const std::string& private_key, |
| 1047 const std::string& public_key) { | 1037 const std::string& public_key) { |
| 1048 if (!plugin_message_loop_proxy_->BelongsToCurrentThread()) { | 1038 if (!plugin_message_loop_proxy_->BelongsToCurrentThread()) { |
| 1049 plugin_message_loop_proxy_->PostTask( | 1039 plugin_message_loop_proxy_->PostTask( |
| 1050 FROM_HERE, base::Bind( | 1040 FROM_HERE, base::Bind( |
| 1051 &HostNPScriptObject::InvokeGenerateKeyPairCallback, | 1041 &HostNPScriptObject::InvokeGenerateKeyPairCallback, |
| 1052 base::Unretained(this), callback, private_key, public_key)); | 1042 base::Unretained(this), callback, private_key, public_key)); |
| 1053 return; | 1043 return; |
| 1054 } | 1044 } |
| 1055 | 1045 |
| 1056 NPVariant params[2]; | 1046 NPVariant params[2]; |
| 1057 params[0] = NPVariantFromString(private_key); | 1047 params[0] = NPVariantFromString(private_key); |
| 1058 params[1] = NPVariantFromString(public_key); | 1048 params[1] = NPVariantFromString(public_key); |
| 1059 InvokeAndIgnoreResult(callback, params, arraysize(params)); | 1049 InvokeAndIgnoreResult(callback.get(), params, arraysize(params)); |
| 1060 g_npnetscape_funcs->releasevariantvalue(&(params[0])); | 1050 g_npnetscape_funcs->releasevariantvalue(&(params[0])); |
| 1061 g_npnetscape_funcs->releasevariantvalue(&(params[1])); | 1051 g_npnetscape_funcs->releasevariantvalue(&(params[1])); |
| 1062 g_npnetscape_funcs->releaseobject(callback); | |
| 1063 } | 1052 } |
| 1064 | 1053 |
| 1065 void HostNPScriptObject::InvokeAsyncResultCallback( | 1054 void HostNPScriptObject::InvokeAsyncResultCallback( |
| 1066 NPObject* callback, | 1055 const ScopedRefNPObject& callback, |
| 1067 DaemonController::AsyncResult result) { | 1056 DaemonController::AsyncResult result) { |
| 1068 if (!plugin_message_loop_proxy_->BelongsToCurrentThread()) { | 1057 if (!plugin_message_loop_proxy_->BelongsToCurrentThread()) { |
| 1069 plugin_message_loop_proxy_->PostTask( | 1058 plugin_message_loop_proxy_->PostTask( |
| 1070 FROM_HERE, base::Bind( | 1059 FROM_HERE, base::Bind( |
| 1071 &HostNPScriptObject::InvokeAsyncResultCallback, | 1060 &HostNPScriptObject::InvokeAsyncResultCallback, |
| 1072 base::Unretained(this), callback, result)); | 1061 base::Unretained(this), callback, result)); |
| 1073 return; | 1062 return; |
| 1074 } | 1063 } |
| 1075 | 1064 |
| 1076 NPVariant result_var; | 1065 NPVariant result_var; |
| 1077 INT32_TO_NPVARIANT(static_cast<int32>(result), result_var); | 1066 INT32_TO_NPVARIANT(static_cast<int32>(result), result_var); |
| 1078 InvokeAndIgnoreResult(callback, &result_var, 1); | 1067 InvokeAndIgnoreResult(callback.get(), &result_var, 1); |
| 1079 g_npnetscape_funcs->releasevariantvalue(&result_var); | 1068 g_npnetscape_funcs->releasevariantvalue(&result_var); |
| 1080 g_npnetscape_funcs->releaseobject(callback); | |
| 1081 } | 1069 } |
| 1082 | 1070 |
| 1083 void HostNPScriptObject::InvokeGetDaemonConfigCallback( | 1071 void HostNPScriptObject::InvokeGetDaemonConfigCallback( |
| 1084 NPObject* callback, | 1072 const ScopedRefNPObject& callback, |
| 1085 scoped_ptr<base::DictionaryValue> config) { | 1073 scoped_ptr<base::DictionaryValue> config) { |
| 1086 if (!plugin_message_loop_proxy_->BelongsToCurrentThread()) { | 1074 if (!plugin_message_loop_proxy_->BelongsToCurrentThread()) { |
| 1087 plugin_message_loop_proxy_->PostTask( | 1075 plugin_message_loop_proxy_->PostTask( |
| 1088 FROM_HERE, base::Bind( | 1076 FROM_HERE, base::Bind( |
| 1089 &HostNPScriptObject::InvokeGetDaemonConfigCallback, | 1077 &HostNPScriptObject::InvokeGetDaemonConfigCallback, |
| 1090 base::Unretained(this), callback, base::Passed(&config))); | 1078 base::Unretained(this), callback, base::Passed(&config))); |
| 1091 return; | 1079 return; |
| 1092 } | 1080 } |
| 1093 | 1081 |
| 1094 // There is no easy way to create a dictionary from an NPAPI plugin | 1082 // There is no easy way to create a dictionary from an NPAPI plugin |
| 1095 // so we have to serialize the dictionary to pass it to JavaScript. | 1083 // so we have to serialize the dictionary to pass it to JavaScript. |
| 1096 std::string config_str; | 1084 std::string config_str; |
| 1097 if (config.get()) | 1085 if (config.get()) |
| 1098 base::JSONWriter::Write(config.get(), &config_str); | 1086 base::JSONWriter::Write(config.get(), &config_str); |
| 1099 | 1087 |
| 1100 NPVariant config_val = NPVariantFromString(config_str); | 1088 NPVariant config_val = NPVariantFromString(config_str); |
| 1101 InvokeAndIgnoreResult(callback, &config_val, 1); | 1089 InvokeAndIgnoreResult(callback.get(), &config_val, 1); |
| 1102 g_npnetscape_funcs->releasevariantvalue(&config_val); | 1090 g_npnetscape_funcs->releasevariantvalue(&config_val); |
| 1103 g_npnetscape_funcs->releaseobject(callback); | |
| 1104 } | 1091 } |
| 1105 | 1092 |
| 1106 void HostNPScriptObject::LogDebugInfo(const std::string& message) { | 1093 void HostNPScriptObject::LogDebugInfo(const std::string& message) { |
| 1107 DCHECK(plugin_message_loop_proxy_->BelongsToCurrentThread()); | 1094 DCHECK(plugin_message_loop_proxy_->BelongsToCurrentThread()); |
| 1108 if (log_debug_info_func_.get()) { | 1095 if (log_debug_info_func_.get()) { |
| 1109 am_currently_logging_ = true; | 1096 am_currently_logging_ = true; |
| 1110 NPVariant log_message; | 1097 NPVariant log_message; |
| 1111 STRINGZ_TO_NPVARIANT(message.c_str(), log_message); | 1098 STRINGZ_TO_NPVARIANT(message.c_str(), log_message); |
| 1112 bool is_good = InvokeAndIgnoreResult(log_debug_info_func_.get(), | 1099 bool is_good = InvokeAndIgnoreResult(log_debug_info_func_.get(), |
| 1113 &log_message, 1); | 1100 &log_message, 1); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1130 return is_good; | 1117 return is_good; |
| 1131 } | 1118 } |
| 1132 | 1119 |
| 1133 void HostNPScriptObject::SetException(const std::string& exception_string) { | 1120 void HostNPScriptObject::SetException(const std::string& exception_string) { |
| 1134 DCHECK(plugin_message_loop_proxy_->BelongsToCurrentThread()); | 1121 DCHECK(plugin_message_loop_proxy_->BelongsToCurrentThread()); |
| 1135 g_npnetscape_funcs->setexception(parent_, exception_string.c_str()); | 1122 g_npnetscape_funcs->setexception(parent_, exception_string.c_str()); |
| 1136 LOG(INFO) << exception_string; | 1123 LOG(INFO) << exception_string; |
| 1137 } | 1124 } |
| 1138 | 1125 |
| 1139 } // namespace remoting | 1126 } // namespace remoting |
| OLD | NEW |