diff options
author | yzshen@chromium.org <yzshen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-15 17:57:00 +0000 |
---|---|---|
committer | yzshen@chromium.org <yzshen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-15 17:57:00 +0000 |
commit | 26f9e71c23a1ef0b4716ef3e4e0747992dd7911f (patch) | |
tree | 55ef36bdc69a08c11eae979c37f56c9dcf468753 /webkit/plugins/ppapi/ppb_char_set_impl.cc | |
parent | c394e045bfe187e5c88f47b238b9259ffea88ae4 (diff) | |
download | chromium_src-26f9e71c23a1ef0b4716ef3e4e0747992dd7911f.zip chromium_src-26f9e71c23a1ef0b4716ef3e4e0747992dd7911f.tar.gz chromium_src-26f9e71c23a1ef0b4716ef3e4e0747992dd7911f.tar.bz2 |
Run PPB_CharSet_Dev logic directly in the plugin process.
- Move part of the character set conversion logic into ppapi/shared_impl/char_set_impl.{h,cc}.
- Change ppb_char_set_proxy to use it directly in the plugin process.
- Another minor change is to avoid using malloc/free during character set conversion, and use PPB_Core.MemAlloc/MemFree instead.
BUG=none
TEST=Pass test_char_set.{h,cc}
Review URL: http://codereview.chromium.org/6694011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@78232 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/plugins/ppapi/ppb_char_set_impl.cc')
-rw-r--r-- | webkit/plugins/ppapi/ppb_char_set_impl.cc | 123 |
1 files changed, 8 insertions, 115 deletions
diff --git a/webkit/plugins/ppapi/ppb_char_set_impl.cc b/webkit/plugins/ppapi/ppb_char_set_impl.cc index 74144ed..dfa7bd3 100644 --- a/webkit/plugins/ppapi/ppb_char_set_impl.cc +++ b/webkit/plugins/ppapi/ppb_char_set_impl.cc @@ -4,15 +4,10 @@ #include "webkit/plugins/ppapi/ppb_char_set_impl.h" -#include <stdlib.h> - -#include "base/i18n/icu_string_conversions.h" #include "ppapi/c/dev/ppb_char_set_dev.h" -#include "unicode/ucnv.h" -#include "unicode/ucnv_cb.h" -#include "unicode/ucnv_err.h" -#include "unicode/ustring.h" +#include "ppapi/shared_impl/char_set_impl.h" #include "webkit/plugins/ppapi/plugin_delegate.h" +#include "webkit/plugins/ppapi/plugin_module.h" #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" #include "webkit/plugins/ppapi/resource_tracker.h" #include "webkit/plugins/ppapi/var.h" @@ -22,97 +17,14 @@ namespace ppapi { namespace { -// Converts the given PP error handling behavior to the version in base, -// placing the result in |*result| and returning true on success. Returns false -// if the enum is invalid. -bool PPToBaseConversionError(PP_CharSet_ConversionError on_error, - base::OnStringConversionError::Type* result) { - switch (on_error) { - case PP_CHARSET_CONVERSIONERROR_FAIL: - *result = base::OnStringConversionError::FAIL; - return true; - case PP_CHARSET_CONVERSIONERROR_SKIP: - *result = base::OnStringConversionError::SKIP; - return true; - case PP_CHARSET_CONVERSIONERROR_SUBSTITUTE: - *result = base::OnStringConversionError::SUBSTITUTE; - return true; - default: - return false; - } -} - -// The "substitution" behavior of this function does not match the -// implementation in base, so we partially duplicate the code from -// icu_string_conversions.cc with the correct error handling setup required -// by this PPAPI interface. char* UTF16ToCharSet(PP_Instance /* instance */, const uint16_t* utf16, uint32_t utf16_len, const char* output_char_set, PP_CharSet_ConversionError on_error, uint32_t* output_length) { - *output_length = 0; - - UErrorCode status = U_ZERO_ERROR; - UConverter* converter = ucnv_open(output_char_set, &status); - if (!U_SUCCESS(status)) - return NULL; - - int encoded_max_length = UCNV_GET_MAX_BYTES_FOR_STRING(utf16_len, - ucnv_getMaxCharSize(converter)); - - // Setup our error handler. - switch (on_error) { - case PP_CHARSET_CONVERSIONERROR_FAIL: - ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_STOP, 0, - NULL, NULL, &status); - break; - case PP_CHARSET_CONVERSIONERROR_SKIP: - ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_SKIP, 0, - NULL, NULL, &status); - break; - case PP_CHARSET_CONVERSIONERROR_SUBSTITUTE: { - // ICU sets the substitution char for some character sets (like latin1) - // to be the ASCII "substitution character" (26). We want to use '?' - // instead for backwards-compat with Windows behavior. - char subst_chars[32]; - int8_t subst_chars_len = 32; - ucnv_getSubstChars(converter, subst_chars, &subst_chars_len, &status); - if (subst_chars_len == 1 && subst_chars[0] == 26) { - // Override to the question mark character if possible. When using - // setSubstString, the input is a Unicode character. The function will - // try to convert it to the destination character set and fail if that - // can not be converted to the destination character set. - // - // We just ignore any failure. If the dest char set has no - // representation for '?', then we'll just stick to the ICU default - // substitution character. - UErrorCode subst_status = U_ZERO_ERROR; - UChar question_mark = '?'; - ucnv_setSubstString(converter, &question_mark, 1, &subst_status); - } - - ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_SUBSTITUTE, 0, - NULL, NULL, &status); - break; - } - default: - return NULL; - } - - // ucnv_fromUChars returns size not including terminating null. - char* encoded = static_cast<char*>(malloc(encoded_max_length + 1)); - int actual_size = ucnv_fromUChars(converter, encoded, - encoded_max_length, reinterpret_cast<const UChar*>(utf16), utf16_len, - &status); - ucnv_close(converter); - if (!U_SUCCESS(status)) { - free(encoded); - return NULL; - } - encoded[actual_size] = 0; - *output_length = actual_size; - return encoded; + return pp::shared_impl::CharSetImpl::UTF16ToCharSet( + PluginModule::GetCore(), utf16, utf16_len, output_char_set, on_error, + output_length); } uint16_t* CharSetToUTF16(PP_Instance /* instance */, @@ -120,27 +32,9 @@ uint16_t* CharSetToUTF16(PP_Instance /* instance */, const char* input_char_set, PP_CharSet_ConversionError on_error, uint32_t* output_length) { - *output_length = 0; - - base::OnStringConversionError::Type base_on_error; - if (!PPToBaseConversionError(on_error, &base_on_error)) - return NULL; // Invalid enum value. - - // We can convert this call to the implementation in base to avoid code - // duplication, although this does introduce an extra copy of the data. - string16 output; - if (!base::CodepageToUTF16(std::string(input, input_len), input_char_set, - base_on_error, &output)) - return NULL; - - uint16_t* ret_buf = static_cast<uint16_t*>( - malloc((output.size() + 1) * sizeof(uint16_t))); - if (!ret_buf) - return NULL; - - *output_length = static_cast<uint32_t>(output.size()); - memcpy(ret_buf, output.c_str(), (output.size() + 1) * sizeof(uint16_t)); - return ret_buf; + return pp::shared_impl::CharSetImpl::CharSetToUTF16( + PluginModule::GetCore(), input, input_len, input_char_set, on_error, + output_length); } PP_Var GetDefaultCharSet(PP_Instance instance_id) { @@ -167,4 +61,3 @@ const struct PPB_CharSet_Dev* PPB_CharSet_Impl::GetInterface() { } // namespace ppapi } // namespace webkit - |