Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: ppapi/shared_impl/char_set_impl.cc

Issue 6694011: Run PPB_CharSet_Dev logic directly in the plugin process. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ppapi/shared_impl/char_set_impl.h ('k') | webkit/plugins/ppapi/ppb_char_set_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ppapi/shared_impl/char_set_impl.h"
6
7 #include "base/i18n/icu_string_conversions.h"
8 #include "ppapi/c/ppb_core.h"
9 #include "unicode/ucnv.h"
10 #include "unicode/ucnv_cb.h"
11 #include "unicode/ucnv_err.h"
12 #include "unicode/ustring.h"
13
14 namespace pp {
15 namespace shared_impl {
16
17 namespace {
18
19 // Converts the given PP error handling behavior to the version in base,
20 // placing the result in |*result| and returning true on success. Returns false
21 // if the enum is invalid.
22 bool PPToBaseConversionError(PP_CharSet_ConversionError on_error,
23 base::OnStringConversionError::Type* result) {
24 switch (on_error) {
25 case PP_CHARSET_CONVERSIONERROR_FAIL:
26 *result = base::OnStringConversionError::FAIL;
27 return true;
28 case PP_CHARSET_CONVERSIONERROR_SKIP:
29 *result = base::OnStringConversionError::SKIP;
30 return true;
31 case PP_CHARSET_CONVERSIONERROR_SUBSTITUTE:
32 *result = base::OnStringConversionError::SUBSTITUTE;
33 return true;
34 default:
35 return false;
36 }
37 }
38
39 } // namespace
40
41 // static
42 // The "substitution" behavior of this function does not match the
43 // implementation in base, so we partially duplicate the code from
44 // icu_string_conversions.cc with the correct error handling setup required
45 // by the PPAPI interface.
46 char* CharSetImpl::UTF16ToCharSet(const PPB_Core* core,
47 const uint16_t* utf16,
48 uint32_t utf16_len,
49 const char* output_char_set,
50 PP_CharSet_ConversionError on_error,
51 uint32_t* output_length) {
52 if (!core || !utf16 || !output_char_set || !output_length)
53 return NULL;
54
55 *output_length = 0;
56
57 UErrorCode status = U_ZERO_ERROR;
58 UConverter* converter = ucnv_open(output_char_set, &status);
59 if (!U_SUCCESS(status))
60 return NULL;
61
62 int encoded_max_length = UCNV_GET_MAX_BYTES_FOR_STRING(utf16_len,
63 ucnv_getMaxCharSize(converter));
64
65 // Setup our error handler.
66 switch (on_error) {
67 case PP_CHARSET_CONVERSIONERROR_FAIL:
68 ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_STOP, 0,
69 NULL, NULL, &status);
70 break;
71 case PP_CHARSET_CONVERSIONERROR_SKIP:
72 ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_SKIP, 0,
73 NULL, NULL, &status);
74 break;
75 case PP_CHARSET_CONVERSIONERROR_SUBSTITUTE: {
76 // ICU sets the substitution char for some character sets (like latin1)
77 // to be the ASCII "substitution character" (26). We want to use '?'
78 // instead for backwards-compat with Windows behavior.
79 char subst_chars[32];
80 int8_t subst_chars_len = 32;
81 ucnv_getSubstChars(converter, subst_chars, &subst_chars_len, &status);
82 if (subst_chars_len == 1 && subst_chars[0] == 26) {
83 // Override to the question mark character if possible. When using
84 // setSubstString, the input is a Unicode character. The function will
85 // try to convert it to the destination character set and fail if that
86 // can not be converted to the destination character set.
87 //
88 // We just ignore any failure. If the dest char set has no
89 // representation for '?', then we'll just stick to the ICU default
90 // substitution character.
91 UErrorCode subst_status = U_ZERO_ERROR;
92 UChar question_mark = '?';
93 ucnv_setSubstString(converter, &question_mark, 1, &subst_status);
94 }
95
96 ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_SUBSTITUTE, 0,
97 NULL, NULL, &status);
98 break;
99 }
100 default:
101 return NULL;
102 }
103
104 // ucnv_fromUChars returns size not including terminating null.
105 char* encoded = static_cast<char*>(core->MemAlloc(encoded_max_length + 1));
106 int actual_size = ucnv_fromUChars(converter, encoded,
107 encoded_max_length, reinterpret_cast<const UChar*>(utf16), utf16_len,
108 &status);
109 ucnv_close(converter);
110 if (!U_SUCCESS(status)) {
111 core->MemFree(encoded);
112 return NULL;
113 }
114 encoded[actual_size] = 0;
115 *output_length = actual_size;
116 return encoded;
117 }
118
119 // static
120 uint16_t* CharSetImpl::CharSetToUTF16(const PPB_Core* core,
121 const char* input,
122 uint32_t input_len,
123 const char* input_char_set,
124 PP_CharSet_ConversionError on_error,
125 uint32_t* output_length) {
126 if (!core || !input || !input_char_set || !output_length)
127 return NULL;
128
129 *output_length = 0;
130
131 base::OnStringConversionError::Type base_on_error;
132 if (!PPToBaseConversionError(on_error, &base_on_error))
133 return NULL; // Invalid enum value.
134
135 // We can convert this call to the implementation in base to avoid code
136 // duplication, although this does introduce an extra copy of the data.
137 string16 output;
138 if (!base::CodepageToUTF16(std::string(input, input_len), input_char_set,
139 base_on_error, &output))
140 return NULL;
141
142 uint16_t* ret_buf = static_cast<uint16_t*>(
143 core->MemAlloc((output.size() + 1) * sizeof(uint16_t)));
144 if (!ret_buf)
145 return NULL;
146
147 *output_length = static_cast<uint32_t>(output.size());
148 memcpy(ret_buf, output.c_str(), (output.size() + 1) * sizeof(uint16_t));
149 return ret_buf;
150 }
151
152 } // namespace shared_impl
153 } // namespace pp
OLDNEW
« no previous file with comments | « ppapi/shared_impl/char_set_impl.h ('k') | webkit/plugins/ppapi/ppb_char_set_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698