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

Side by Side Diff: webkit/glue/plugins/pepper_char_set.cc

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

Powered by Google App Engine
This is Rietveld 408576698