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

Side by Side Diff: webkit/plugins/ppapi/ppb_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.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "webkit/plugins/ppapi/ppb_char_set_impl.h" 5 #include "webkit/plugins/ppapi/ppb_char_set_impl.h"
6 6
7 #include <stdlib.h>
8
9 #include "base/i18n/icu_string_conversions.h"
10 #include "ppapi/c/dev/ppb_char_set_dev.h" 7 #include "ppapi/c/dev/ppb_char_set_dev.h"
11 #include "unicode/ucnv.h" 8 #include "ppapi/shared_impl/char_set_impl.h"
12 #include "unicode/ucnv_cb.h"
13 #include "unicode/ucnv_err.h"
14 #include "unicode/ustring.h"
15 #include "webkit/plugins/ppapi/plugin_delegate.h" 9 #include "webkit/plugins/ppapi/plugin_delegate.h"
10 #include "webkit/plugins/ppapi/plugin_module.h"
16 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" 11 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
17 #include "webkit/plugins/ppapi/resource_tracker.h" 12 #include "webkit/plugins/ppapi/resource_tracker.h"
18 #include "webkit/plugins/ppapi/var.h" 13 #include "webkit/plugins/ppapi/var.h"
19 14
20 namespace webkit { 15 namespace webkit {
21 namespace ppapi { 16 namespace ppapi {
22 17
23 namespace { 18 namespace {
24 19
25 // Converts the given PP error handling behavior to the version in base,
26 // placing the result in |*result| and returning true on success. Returns false
27 // if the enum is invalid.
28 bool PPToBaseConversionError(PP_CharSet_ConversionError on_error,
29 base::OnStringConversionError::Type* result) {
30 switch (on_error) {
31 case PP_CHARSET_CONVERSIONERROR_FAIL:
32 *result = base::OnStringConversionError::FAIL;
33 return true;
34 case PP_CHARSET_CONVERSIONERROR_SKIP:
35 *result = base::OnStringConversionError::SKIP;
36 return true;
37 case PP_CHARSET_CONVERSIONERROR_SUBSTITUTE:
38 *result = base::OnStringConversionError::SUBSTITUTE;
39 return true;
40 default:
41 return false;
42 }
43 }
44
45 // The "substitution" behavior of this function does not match the
46 // implementation in base, so we partially duplicate the code from
47 // icu_string_conversions.cc with the correct error handling setup required
48 // by this PPAPI interface.
49 char* UTF16ToCharSet(PP_Instance /* instance */, 20 char* UTF16ToCharSet(PP_Instance /* instance */,
50 const uint16_t* utf16, uint32_t utf16_len, 21 const uint16_t* utf16, uint32_t utf16_len,
51 const char* output_char_set, 22 const char* output_char_set,
52 PP_CharSet_ConversionError on_error, 23 PP_CharSet_ConversionError on_error,
53 uint32_t* output_length) { 24 uint32_t* output_length) {
54 *output_length = 0; 25 return pp::shared_impl::CharSetImpl::UTF16ToCharSet(
55 26 PluginModule::GetCore(), utf16, utf16_len, output_char_set, on_error,
56 UErrorCode status = U_ZERO_ERROR; 27 output_length);
57 UConverter* converter = ucnv_open(output_char_set, &status);
58 if (!U_SUCCESS(status))
59 return NULL;
60
61 int encoded_max_length = UCNV_GET_MAX_BYTES_FOR_STRING(utf16_len,
62 ucnv_getMaxCharSize(converter));
63
64 // Setup our error handler.
65 switch (on_error) {
66 case PP_CHARSET_CONVERSIONERROR_FAIL:
67 ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_STOP, 0,
68 NULL, NULL, &status);
69 break;
70 case PP_CHARSET_CONVERSIONERROR_SKIP:
71 ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_SKIP, 0,
72 NULL, NULL, &status);
73 break;
74 case PP_CHARSET_CONVERSIONERROR_SUBSTITUTE: {
75 // ICU sets the substitution char for some character sets (like latin1)
76 // to be the ASCII "substitution character" (26). We want to use '?'
77 // instead for backwards-compat with Windows behavior.
78 char subst_chars[32];
79 int8_t subst_chars_len = 32;
80 ucnv_getSubstChars(converter, subst_chars, &subst_chars_len, &status);
81 if (subst_chars_len == 1 && subst_chars[0] == 26) {
82 // Override to the question mark character if possible. When using
83 // setSubstString, the input is a Unicode character. The function will
84 // try to convert it to the destination character set and fail if that
85 // can not be converted to the destination character set.
86 //
87 // We just ignore any failure. If the dest char set has no
88 // representation for '?', then we'll just stick to the ICU default
89 // substitution character.
90 UErrorCode subst_status = U_ZERO_ERROR;
91 UChar question_mark = '?';
92 ucnv_setSubstString(converter, &question_mark, 1, &subst_status);
93 }
94
95 ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_SUBSTITUTE, 0,
96 NULL, NULL, &status);
97 break;
98 }
99 default:
100 return NULL;
101 }
102
103 // ucnv_fromUChars returns size not including terminating null.
104 char* encoded = static_cast<char*>(malloc(encoded_max_length + 1));
105 int actual_size = ucnv_fromUChars(converter, encoded,
106 encoded_max_length, reinterpret_cast<const UChar*>(utf16), utf16_len,
107 &status);
108 ucnv_close(converter);
109 if (!U_SUCCESS(status)) {
110 free(encoded);
111 return NULL;
112 }
113 encoded[actual_size] = 0;
114 *output_length = actual_size;
115 return encoded;
116 } 28 }
117 29
118 uint16_t* CharSetToUTF16(PP_Instance /* instance */, 30 uint16_t* CharSetToUTF16(PP_Instance /* instance */,
119 const char* input, uint32_t input_len, 31 const char* input, uint32_t input_len,
120 const char* input_char_set, 32 const char* input_char_set,
121 PP_CharSet_ConversionError on_error, 33 PP_CharSet_ConversionError on_error,
122 uint32_t* output_length) { 34 uint32_t* output_length) {
123 *output_length = 0; 35 return pp::shared_impl::CharSetImpl::CharSetToUTF16(
124 36 PluginModule::GetCore(), input, input_len, input_char_set, on_error,
125 base::OnStringConversionError::Type base_on_error; 37 output_length);
126 if (!PPToBaseConversionError(on_error, &base_on_error))
127 return NULL; // Invalid enum value.
128
129 // We can convert this call to the implementation in base to avoid code
130 // duplication, although this does introduce an extra copy of the data.
131 string16 output;
132 if (!base::CodepageToUTF16(std::string(input, input_len), input_char_set,
133 base_on_error, &output))
134 return NULL;
135
136 uint16_t* ret_buf = static_cast<uint16_t*>(
137 malloc((output.size() + 1) * sizeof(uint16_t)));
138 if (!ret_buf)
139 return NULL;
140
141 *output_length = static_cast<uint32_t>(output.size());
142 memcpy(ret_buf, output.c_str(), (output.size() + 1) * sizeof(uint16_t));
143 return ret_buf;
144 } 38 }
145 39
146 PP_Var GetDefaultCharSet(PP_Instance instance_id) { 40 PP_Var GetDefaultCharSet(PP_Instance instance_id) {
147 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); 41 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id);
148 if (!instance) 42 if (!instance)
149 return PP_MakeUndefined(); 43 return PP_MakeUndefined();
150 44
151 std::string encoding = instance->delegate()->GetDefaultEncoding(); 45 std::string encoding = instance->delegate()->GetDefaultEncoding();
152 return StringVar::StringToPPVar(instance->module(), encoding); 46 return StringVar::StringToPPVar(instance->module(), encoding);
153 } 47 }
154 48
155 const PPB_CharSet_Dev ppb_charset = { 49 const PPB_CharSet_Dev ppb_charset = {
156 &UTF16ToCharSet, 50 &UTF16ToCharSet,
157 &CharSetToUTF16, 51 &CharSetToUTF16,
158 &GetDefaultCharSet 52 &GetDefaultCharSet
159 }; 53 };
160 54
161 } // namespace 55 } // namespace
162 56
163 // static 57 // static
164 const struct PPB_CharSet_Dev* PPB_CharSet_Impl::GetInterface() { 58 const struct PPB_CharSet_Dev* PPB_CharSet_Impl::GetInterface() {
165 return &ppb_charset; 59 return &ppb_charset;
166 } 60 }
167 61
168 } // namespace ppapi 62 } // namespace ppapi
169 } // namespace webkit 63 } // namespace webkit
170
OLDNEW
« no previous file with comments | « ppapi/shared_impl/char_set_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698