Commit e1333fb5 authored by brettw@chromium.org's avatar brettw@chromium.org

Add support for Pepper charset conversions.

TEST=included unit test
BUG=none

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@57014 0039d316-1c4b-4281-b951-d872f2087c98
parent bae4e383
...@@ -123,3 +123,7 @@ TEST_F(PPAPITest, FLAKY_Scrollbar) { ...@@ -123,3 +123,7 @@ TEST_F(PPAPITest, FLAKY_Scrollbar) {
TEST_F(PPAPITest, UrlUtil) { TEST_F(PPAPITest, UrlUtil) {
RunTest("UrlUtil"); RunTest("UrlUtil");
} }
TEST_F(PPAPITest, CharSet) {
RunTest("CharSet");
}
// Copyright (c) 2010 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 "webkit/glue/plugins/pepper_char_set.h"
#include <stdlib.h>
#include "base/i18n/icu_string_conversions.h"
#include "third_party/ppapi/c/ppb_char_set.h"
#include "unicode/ucnv.h"
#include "unicode/ucnv_cb.h"
#include "unicode/ucnv_err.h"
#include "unicode/ustring.h"
namespace pepper {
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 pepper interface.
char* UTF16ToCharSet(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, 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;
}
uint16_t* CharSetToUTF16(const char* input, uint32_t input_len,
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;
}
PP_Var GetDefaultCodePageForUILanguage() {
// TODO(brettw) bug 52865: Implement this function.
return PP_MakeVoid();
}
const PPB_CharSetDev ppb_charset = {
&UTF16ToCharSet,
&CharSetToUTF16,
&GetDefaultCodePageForUILanguage
};
} // namespace
// static
const PPB_CharSetDev* CharSet::GetInterface() {
return &ppb_charset;
}
} // namespace pepper
// Copyright (c) 2010 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.
#ifndef WEBKIT_GLUE_PLUGINS_PEPPER_CHAR_SET_H_
#define WEBKIT_GLUE_PLUGINS_PEPPER_CHAR_SET_H_
typedef struct _ppb_CharSetDev PPB_CharSetDev;
namespace pepper {
class CharSet {
public:
// Returns a pointer to the interface implementing PPB_CharSet that is
// exposed to the plugin.
static const PPB_CharSetDev* GetInterface();
};
} // namespace pepper
#endif // WEBKIT_GLUE_PLUGINS_PEPPER_CHAR_SET_H_
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include "base/scoped_ptr.h" #include "base/scoped_ptr.h"
#include "base/time.h" #include "base/time.h"
#include "third_party/ppapi/c/ppb_buffer.h" #include "third_party/ppapi/c/ppb_buffer.h"
#include "third_party/ppapi/c/ppb_char_set.h"
#include "third_party/ppapi/c/ppb_core.h" #include "third_party/ppapi/c/ppb_core.h"
#include "third_party/ppapi/c/ppb_device_context_2d.h" #include "third_party/ppapi/c/ppb_device_context_2d.h"
#include "third_party/ppapi/c/ppb_file_io.h" #include "third_party/ppapi/c/ppb_file_io.h"
...@@ -39,6 +40,7 @@ ...@@ -39,6 +40,7 @@
#include "third_party/ppapi/c/pp_var.h" #include "third_party/ppapi/c/pp_var.h"
#include "webkit/glue/plugins/pepper_audio.h" #include "webkit/glue/plugins/pepper_audio.h"
#include "webkit/glue/plugins/pepper_buffer.h" #include "webkit/glue/plugins/pepper_buffer.h"
#include "webkit/glue/plugins/pepper_char_set.h"
#include "webkit/glue/plugins/pepper_device_context_2d.h" #include "webkit/glue/plugins/pepper_device_context_2d.h"
#include "webkit/glue/plugins/pepper_directory_reader.h" #include "webkit/glue/plugins/pepper_directory_reader.h"
#include "webkit/glue/plugins/pepper_file_chooser.h" #include "webkit/glue/plugins/pepper_file_chooser.h"
...@@ -215,6 +217,8 @@ const void* GetInterface(const char* name) { ...@@ -215,6 +217,8 @@ const void* GetInterface(const char* name) {
return FileChooser::GetInterface(); return FileChooser::GetInterface();
if (strcmp(name, PPB_VIDEODECODER_INTERFACE) == 0) if (strcmp(name, PPB_VIDEODECODER_INTERFACE) == 0)
return VideoDecoder::GetInterface(); return VideoDecoder::GetInterface();
if (strcmp(name, PPB_CHAR_SET_DEV) == 0)
return CharSet::GetInterface();
// Only support the testing interface when the command line switch is // Only support the testing interface when the command line switch is
// specified. This allows us to prevent people from (ab)using this interface // specified. This allows us to prevent people from (ab)using this interface
......
...@@ -167,6 +167,8 @@ ...@@ -167,6 +167,8 @@
'plugins/pepper_audio.h', 'plugins/pepper_audio.h',
'plugins/pepper_buffer.cc', 'plugins/pepper_buffer.cc',
'plugins/pepper_buffer.h', 'plugins/pepper_buffer.h',
'plugins/pepper_char_set.cc',
'plugins/pepper_char_set.h',
'plugins/pepper_device_context_2d.cc', 'plugins/pepper_device_context_2d.cc',
'plugins/pepper_device_context_2d.h', 'plugins/pepper_device_context_2d.h',
'plugins/pepper_directory_reader.cc', 'plugins/pepper_directory_reader.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment