| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ppapi/proxy/ppb_char_set_proxy.h" | 5 #include "ppapi/proxy/ppb_char_set_proxy.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" |
| 7 #include "ppapi/c/dev/ppb_char_set_dev.h" | 8 #include "ppapi/c/dev/ppb_char_set_dev.h" |
| 8 #include "ppapi/c/ppb_core.h" | 9 #include "ppapi/c/ppb_core.h" |
| 9 #include "ppapi/proxy/plugin_dispatcher.h" | 10 #include "ppapi/proxy/plugin_dispatcher.h" |
| 10 #include "ppapi/proxy/ppapi_messages.h" | 11 #include "ppapi/proxy/ppapi_messages.h" |
| 11 #include "ppapi/proxy/serialized_var.h" | 12 #include "ppapi/proxy/serialized_var.h" |
| 13 #include "ppapi/shared_impl/char_set_impl.h" |
| 12 | 14 |
| 13 namespace pp { | 15 namespace pp { |
| 14 namespace proxy { | 16 namespace proxy { |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 | 19 |
| 18 char* UTF16ToCharSet(PP_Instance instance, | 20 const PPB_Core* GetCoreInterface() { |
| 21 return static_cast<const PPB_Core*>( |
| 22 PluginDispatcher::GetInterfaceFromDispatcher(PPB_CORE_INTERFACE)); |
| 23 } |
| 24 |
| 25 char* UTF16ToCharSet(PP_Instance /* instance */, |
| 19 const uint16_t* utf16, uint32_t utf16_len, | 26 const uint16_t* utf16, uint32_t utf16_len, |
| 20 const char* output_char_set, | 27 const char* output_char_set, |
| 21 PP_CharSet_ConversionError on_error, | 28 PP_CharSet_ConversionError on_error, |
| 22 uint32_t* output_length) { | 29 uint32_t* output_length) { |
| 23 *output_length = 0; | 30 return pp::shared_impl::CharSetImpl::UTF16ToCharSet( |
| 24 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | 31 GetCoreInterface(), utf16, utf16_len, output_char_set, on_error, |
| 25 if (!dispatcher) | 32 output_length); |
| 26 return NULL; | |
| 27 | |
| 28 bool output_is_success = false; | |
| 29 std::string result; | |
| 30 dispatcher->Send(new PpapiHostMsg_PPBCharSet_UTF16ToCharSet( | |
| 31 INTERFACE_ID_PPB_CHAR_SET, instance, | |
| 32 string16(reinterpret_cast<const char16*>(utf16), utf16_len), | |
| 33 std::string(output_char_set), static_cast<int32_t>(on_error), | |
| 34 &result, &output_is_success)); | |
| 35 if (!output_is_success) | |
| 36 return NULL; | |
| 37 | |
| 38 *output_length = static_cast<uint32_t>(result.size()); | |
| 39 char* ret_val = static_cast<char*>(malloc(result.size() + 1)); | |
| 40 memcpy(ret_val, result.c_str(), result.size() + 1); | |
| 41 return ret_val; | |
| 42 } | 33 } |
| 43 | 34 |
| 44 uint16_t* CharSetToUTF16(PP_Instance instance, | 35 uint16_t* CharSetToUTF16(PP_Instance /* instance */, |
| 45 const char* input, uint32_t input_len, | 36 const char* input, uint32_t input_len, |
| 46 const char* input_char_set, | 37 const char* input_char_set, |
| 47 PP_CharSet_ConversionError on_error, | 38 PP_CharSet_ConversionError on_error, |
| 48 uint32_t* output_length) { | 39 uint32_t* output_length) { |
| 49 *output_length = 0; | 40 return pp::shared_impl::CharSetImpl::CharSetToUTF16( |
| 50 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | 41 GetCoreInterface(), input, input_len, input_char_set, on_error, |
| 51 if (!dispatcher) | 42 output_length); |
| 52 return NULL; | |
| 53 | |
| 54 bool output_is_success = false; | |
| 55 string16 result; | |
| 56 dispatcher->Send(new PpapiHostMsg_PPBCharSet_CharSetToUTF16( | |
| 57 INTERFACE_ID_PPB_CHAR_SET, instance, | |
| 58 std::string(input, input_len), | |
| 59 std::string(input_char_set), static_cast<int32_t>(on_error), | |
| 60 &result, &output_is_success)); | |
| 61 if (!output_is_success) | |
| 62 return NULL; | |
| 63 | |
| 64 *output_length = static_cast<uint32_t>(result.size()); | |
| 65 uint16_t* ret_val = static_cast<uint16_t*>( | |
| 66 malloc((result.size() + 1) * sizeof(uint16_t))); | |
| 67 memcpy(ret_val, result.c_str(), (result.size() + 1) * sizeof(uint16_t)); | |
| 68 return ret_val; | |
| 69 } | 43 } |
| 70 | 44 |
| 71 PP_Var GetDefaultCharSet(PP_Instance instance) { | 45 PP_Var GetDefaultCharSet(PP_Instance instance) { |
| 72 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); | 46 PluginDispatcher* dispatcher = PluginDispatcher::GetForInstance(instance); |
| 73 if (!dispatcher) | 47 if (!dispatcher) |
| 74 return PP_MakeUndefined(); | 48 return PP_MakeUndefined(); |
| 75 | 49 |
| 76 ReceiveSerializedVarReturnValue result; | 50 ReceiveSerializedVarReturnValue result; |
| 77 dispatcher->Send(new PpapiHostMsg_PPBCharSet_GetDefaultCharSet( | 51 dispatcher->Send(new PpapiHostMsg_PPBCharSet_GetDefaultCharSet( |
| 78 INTERFACE_ID_PPB_CHAR_SET, instance, &result)); | 52 INTERFACE_ID_PPB_CHAR_SET, instance, &result)); |
| 79 return result.Return(dispatcher); | 53 return result.Return(dispatcher); |
| 80 } | 54 } |
| 81 | 55 |
| 82 const PPB_CharSet_Dev charset_interface = { | 56 const PPB_CharSet_Dev charset_interface = { |
| 83 &UTF16ToCharSet, | 57 &UTF16ToCharSet, |
| 84 &CharSetToUTF16, | 58 &CharSetToUTF16, |
| 85 &GetDefaultCharSet | 59 &GetDefaultCharSet |
| 86 }; | 60 }; |
| 87 | 61 |
| 88 InterfaceProxy* CreateCharSetProxy(Dispatcher* dispatcher, | 62 InterfaceProxy* CreateCharSetProxy(Dispatcher* dispatcher, |
| 89 const void* target_interface) { | 63 const void* target_interface) { |
| 90 return new PPB_CharSet_Proxy(dispatcher, target_interface); | 64 return new PPB_CharSet_Proxy(dispatcher, target_interface); |
| 91 } | 65 } |
| 92 | 66 |
| 93 } // namespace | 67 } // namespace |
| 94 | 68 |
| 95 PPB_CharSet_Proxy::PPB_CharSet_Proxy(Dispatcher* dispatcher, | 69 PPB_CharSet_Proxy::PPB_CharSet_Proxy(Dispatcher* dispatcher, |
| 96 const void* target_interface) | 70 const void* target_interface) |
| 97 : InterfaceProxy(dispatcher, target_interface), | 71 : InterfaceProxy(dispatcher, target_interface) { |
| 98 core_interface_(NULL) { | |
| 99 } | 72 } |
| 100 | 73 |
| 101 PPB_CharSet_Proxy::~PPB_CharSet_Proxy() { | 74 PPB_CharSet_Proxy::~PPB_CharSet_Proxy() { |
| 102 } | 75 } |
| 103 | 76 |
| 104 // static | 77 // static |
| 105 const InterfaceProxy::Info* PPB_CharSet_Proxy::GetInfo() { | 78 const InterfaceProxy::Info* PPB_CharSet_Proxy::GetInfo() { |
| 106 static const Info info = { | 79 static const Info info = { |
| 107 &charset_interface, | 80 &charset_interface, |
| 108 PPB_CHAR_SET_DEV_INTERFACE, | 81 PPB_CHAR_SET_DEV_INTERFACE, |
| 109 INTERFACE_ID_PPB_CHAR_SET, | 82 INTERFACE_ID_PPB_CHAR_SET, |
| 110 false, | 83 false, |
| 111 &CreateCharSetProxy, | 84 &CreateCharSetProxy, |
| 112 }; | 85 }; |
| 113 return &info; | 86 return &info; |
| 114 } | 87 } |
| 115 | 88 |
| 116 bool PPB_CharSet_Proxy::OnMessageReceived(const IPC::Message& msg) { | 89 bool PPB_CharSet_Proxy::OnMessageReceived(const IPC::Message& msg) { |
| 117 bool handled = true; | 90 bool handled = true; |
| 118 IPC_BEGIN_MESSAGE_MAP(PPB_CharSet_Proxy, msg) | 91 IPC_BEGIN_MESSAGE_MAP(PPB_CharSet_Proxy, msg) |
| 119 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBCharSet_UTF16ToCharSet, | |
| 120 OnMsgUTF16ToCharSet) | |
| 121 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBCharSet_CharSetToUTF16, | |
| 122 OnMsgCharSetToUTF16) | |
| 123 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBCharSet_GetDefaultCharSet, | 92 IPC_MESSAGE_HANDLER(PpapiHostMsg_PPBCharSet_GetDefaultCharSet, |
| 124 OnMsgGetDefaultCharSet) | 93 OnMsgGetDefaultCharSet) |
| 125 IPC_MESSAGE_UNHANDLED(handled = false) | 94 IPC_MESSAGE_UNHANDLED(handled = false) |
| 126 IPC_END_MESSAGE_MAP() | 95 IPC_END_MESSAGE_MAP() |
| 127 return handled; | 96 return handled; |
| 128 } | 97 } |
| 129 | 98 |
| 130 void PPB_CharSet_Proxy::OnMsgUTF16ToCharSet(PP_Instance instance, | |
| 131 const string16& utf16, | |
| 132 const std::string& char_set, | |
| 133 int32_t on_error, | |
| 134 std::string* output, | |
| 135 bool* output_is_success) { | |
| 136 uint32_t output_len = 0; | |
| 137 char* result = ppb_char_set_target()->UTF16ToCharSet( | |
| 138 instance, reinterpret_cast<const uint16_t*>(utf16.c_str()), | |
| 139 static_cast<uint32_t>(utf16.size()), | |
| 140 char_set.c_str(), static_cast<PP_CharSet_ConversionError>(on_error), | |
| 141 &output_len); | |
| 142 if (result) { | |
| 143 output->assign(result, output_len); | |
| 144 GetCoreInterface()->MemFree(result); | |
| 145 *output_is_success = true; | |
| 146 } else { | |
| 147 *output_is_success = false; | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 void PPB_CharSet_Proxy::OnMsgCharSetToUTF16(PP_Instance instance, | |
| 152 const std::string& input, | |
| 153 const std::string& char_set, | |
| 154 int32_t on_error, | |
| 155 string16* output, | |
| 156 bool* output_is_success) { | |
| 157 uint32_t output_len = 0; | |
| 158 uint16_t* result = ppb_char_set_target()->CharSetToUTF16( | |
| 159 instance, input.c_str(), static_cast<uint32_t>(input.size()), | |
| 160 char_set.c_str(), static_cast<PP_CharSet_ConversionError>(on_error), | |
| 161 &output_len); | |
| 162 if (result) { | |
| 163 output->assign(reinterpret_cast<const char16*>(result), output_len); | |
| 164 GetCoreInterface()->MemFree(result); | |
| 165 *output_is_success = true; | |
| 166 } else { | |
| 167 *output_is_success = false; | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 void PPB_CharSet_Proxy::OnMsgGetDefaultCharSet( | 99 void PPB_CharSet_Proxy::OnMsgGetDefaultCharSet( |
| 172 PP_Instance instance, | 100 PP_Instance instance, |
| 173 SerializedVarReturnValue result) { | 101 SerializedVarReturnValue result) { |
| 174 result.Return(dispatcher(), | 102 result.Return(dispatcher(), |
| 175 ppb_char_set_target()->GetDefaultCharSet(instance)); | 103 ppb_char_set_target()->GetDefaultCharSet(instance)); |
| 176 } | 104 } |
| 177 | 105 |
| 178 const PPB_Core* PPB_CharSet_Proxy::GetCoreInterface() { | |
| 179 if (!core_interface_) { | |
| 180 core_interface_ = static_cast<const PPB_Core*>( | |
| 181 dispatcher()->GetLocalInterface(PPB_CORE_INTERFACE)); | |
| 182 } | |
| 183 return core_interface_; | |
| 184 } | |
| 185 | |
| 186 } // namespace proxy | 106 } // namespace proxy |
| 187 } // namespace pp | 107 } // namespace pp |
| OLD | NEW |