OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "ppapi/shared_impl/char_set_impl.h" | 5 #include "ppapi/shared_impl/ppb_char_set_shared.h" |
6 | 6 |
7 #include "base/i18n/icu_string_conversions.h" | 7 #include "base/i18n/icu_string_conversions.h" |
8 #include "ppapi/c/dev/ppb_memory_dev.h" | 8 #include "ppapi/c/dev/ppb_memory_dev.h" |
9 #include "ppapi/thunk/thunk.h" | 9 #include "ppapi/thunk/thunk.h" |
10 #include "unicode/ucnv.h" | 10 #include "unicode/ucnv.h" |
11 #include "unicode/ucnv_cb.h" | 11 #include "unicode/ucnv_cb.h" |
12 #include "unicode/ucnv_err.h" | 12 #include "unicode/ucnv_err.h" |
13 #include "unicode/ustring.h" | 13 #include "unicode/ustring.h" |
14 | 14 |
15 namespace ppapi { | 15 namespace ppapi { |
(...skipping 20 matching lines...) Expand all Loading... |
36 } | 36 } |
37 } | 37 } |
38 | 38 |
39 } // namespace | 39 } // namespace |
40 | 40 |
41 // static | 41 // static |
42 // The "substitution" behavior of this function does not match the | 42 // The "substitution" behavior of this function does not match the |
43 // implementation in base, so we partially duplicate the code from | 43 // implementation in base, so we partially duplicate the code from |
44 // icu_string_conversions.cc with the correct error handling setup required | 44 // icu_string_conversions.cc with the correct error handling setup required |
45 // by the PPAPI interface. | 45 // by the PPAPI interface. |
46 char* CharSetImpl::UTF16ToCharSet(const uint16_t* utf16, | 46 char* PPB_CharSet_Shared::UTF16ToCharSet( |
47 uint32_t utf16_len, | 47 const uint16_t* utf16, |
48 const char* output_char_set, | 48 uint32_t utf16_len, |
49 PP_CharSet_ConversionError on_error, | 49 const char* output_char_set, |
50 uint32_t* output_length) { | 50 PP_CharSet_ConversionError on_error, |
| 51 uint32_t* output_length) { |
51 if (!utf16 || !output_char_set || !output_length) | 52 if (!utf16 || !output_char_set || !output_length) |
52 return NULL; | 53 return NULL; |
53 | 54 |
54 *output_length = 0; | 55 *output_length = 0; |
55 | 56 |
56 UErrorCode status = U_ZERO_ERROR; | 57 UErrorCode status = U_ZERO_ERROR; |
57 UConverter* converter = ucnv_open(output_char_set, &status); | 58 UConverter* converter = ucnv_open(output_char_set, &status); |
58 if (!U_SUCCESS(status)) | 59 if (!U_SUCCESS(status)) |
59 return NULL; | 60 return NULL; |
60 | 61 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 if (!U_SUCCESS(status)) { | 111 if (!U_SUCCESS(status)) { |
111 thunk::GetPPB_Memory_Dev_Thunk()->MemFree(encoded); | 112 thunk::GetPPB_Memory_Dev_Thunk()->MemFree(encoded); |
112 return NULL; | 113 return NULL; |
113 } | 114 } |
114 encoded[actual_size] = 0; | 115 encoded[actual_size] = 0; |
115 *output_length = actual_size; | 116 *output_length = actual_size; |
116 return encoded; | 117 return encoded; |
117 } | 118 } |
118 | 119 |
119 // static | 120 // static |
120 uint16_t* CharSetImpl::CharSetToUTF16(const char* input, | 121 uint16_t* PPB_CharSet_Shared::CharSetToUTF16( |
121 uint32_t input_len, | 122 const char* input, |
122 const char* input_char_set, | 123 uint32_t input_len, |
123 PP_CharSet_ConversionError on_error, | 124 const char* input_char_set, |
124 uint32_t* output_length) { | 125 PP_CharSet_ConversionError on_error, |
| 126 uint32_t* output_length) { |
125 if (!input || !input_char_set || !output_length) | 127 if (!input || !input_char_set || !output_length) |
126 return NULL; | 128 return NULL; |
127 | 129 |
128 *output_length = 0; | 130 *output_length = 0; |
129 | 131 |
130 base::OnStringConversionError::Type base_on_error; | 132 base::OnStringConversionError::Type base_on_error; |
131 if (!PPToBaseConversionError(on_error, &base_on_error)) | 133 if (!PPToBaseConversionError(on_error, &base_on_error)) |
132 return NULL; // Invalid enum value. | 134 return NULL; // Invalid enum value. |
133 | 135 |
134 // We can convert this call to the implementation in base to avoid code | 136 // We can convert this call to the implementation in base to avoid code |
135 // duplication, although this does introduce an extra copy of the data. | 137 // duplication, although this does introduce an extra copy of the data. |
136 string16 output; | 138 string16 output; |
137 if (!base::CodepageToUTF16(std::string(input, input_len), input_char_set, | 139 if (!base::CodepageToUTF16(std::string(input, input_len), input_char_set, |
138 base_on_error, &output)) | 140 base_on_error, &output)) |
139 return NULL; | 141 return NULL; |
140 | 142 |
141 uint16_t* ret_buf = static_cast<uint16_t*>( | 143 uint16_t* ret_buf = static_cast<uint16_t*>( |
142 thunk::GetPPB_Memory_Dev_Thunk()->MemAlloc( | 144 thunk::GetPPB_Memory_Dev_Thunk()->MemAlloc( |
143 (output.size() + 1) * sizeof(uint16_t))); | 145 (output.size() + 1) * sizeof(uint16_t))); |
144 if (!ret_buf) | 146 if (!ret_buf) |
145 return NULL; | 147 return NULL; |
146 | 148 |
147 *output_length = static_cast<uint32_t>(output.size()); | 149 *output_length = static_cast<uint32_t>(output.size()); |
148 memcpy(ret_buf, output.c_str(), (output.size() + 1) * sizeof(uint16_t)); | 150 memcpy(ret_buf, output.c_str(), (output.size() + 1) * sizeof(uint16_t)); |
149 return ret_buf; | 151 return ret_buf; |
150 } | 152 } |
151 | 153 |
152 } // namespace ppapi | 154 } // namespace ppapi |
OLD | NEW |