OLD | NEW |
---|---|
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 // This file contains definitions for CppVariant. | 5 // This file contains definitions for CppVariant. |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include "third_party/WebKit/WebKit/chromium/public/WebBindings.h" | 8 #include "third_party/WebKit/WebKit/chromium/public/WebBindings.h" |
9 #include "webkit/glue/cpp_variant.h" | 9 #include "webkit/glue/cpp_variant.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
204 NOTREACHED(); | 204 NOTREACHED(); |
205 return 0.0; | 205 return 0.0; |
206 } | 206 } |
207 } | 207 } |
208 | 208 |
209 bool CppVariant::ToBoolean() const { | 209 bool CppVariant::ToBoolean() const { |
210 DCHECK(isBool()); | 210 DCHECK(isBool()); |
211 return value.boolValue; | 211 return value.boolValue; |
212 } | 212 } |
213 | 213 |
214 std::vector<std::wstring> CppVariant::ToStringVector() const { | 214 std::vector<std::string> CppVariant::ToStringVector() const { |
215 | |
216 DCHECK(isObject()); | 215 DCHECK(isObject()); |
217 std::vector<std::wstring> wstring_vector; | 216 std::vector<std::string> string_vector; |
218 NPObject* np_value = value.objectValue; | 217 NPObject* np_value = value.objectValue; |
219 NPIdentifier length_id = WebBindings::getStringIdentifier("length"); | 218 NPIdentifier length_id = WebBindings::getStringIdentifier("length"); |
220 | 219 |
221 if (WebBindings::hasProperty(NULL, np_value, length_id)) { | 220 if (WebBindings::hasProperty(NULL, np_value, length_id)) { |
222 NPVariant length_value; | 221 NPVariant length_value; |
223 if (WebBindings::getProperty(NULL, np_value, length_id, &length_value)) { | 222 if (WebBindings::getProperty(NULL, np_value, length_id, &length_value)) { |
224 int length = 0; | 223 int length = 0; |
225 // The length is a double in some cases. | 224 // The length is a double in some cases. |
226 if (NPVARIANT_IS_DOUBLE(length_value)) | 225 if (NPVARIANT_IS_DOUBLE(length_value)) |
227 length = static_cast<int>(NPVARIANT_TO_DOUBLE(length_value)); | 226 length = static_cast<int>(NPVARIANT_TO_DOUBLE(length_value)); |
228 else if (NPVARIANT_IS_INT32(length_value)) | 227 else if (NPVARIANT_IS_INT32(length_value)) |
229 length = NPVARIANT_TO_INT32(length_value); | 228 length = NPVARIANT_TO_INT32(length_value); |
230 WebBindings::releaseVariantValue(&length_value); | 229 WebBindings::releaseVariantValue(&length_value); |
231 | 230 |
232 // For sanity, only allow 60000 items. | 231 // For sanity, only allow 60000 items. |
233 length = std::min(60000, length); | 232 length = std::min(60000, length); |
234 for (int i = 0; i < length; ++i) { | 233 for (int i = 0; i < length; ++i) { |
235 // Get each of the items. | 234 // Get each of the items. |
236 std::string index = base::StringPrintf("%d", i); | 235 std::string index = base::StringPrintf("%d", i); |
237 NPIdentifier index_id = WebBindings::getStringIdentifier(index.c_str()); | 236 NPIdentifier index_id = WebBindings::getStringIdentifier(index.c_str()); |
238 if (WebBindings::hasProperty(NULL, np_value, index_id)) { | 237 if (WebBindings::hasProperty(NULL, np_value, index_id)) { |
239 NPVariant index_value; | 238 NPVariant index_value; |
240 if (WebBindings::getProperty(NULL, np_value, index_id, &index_value)) { | 239 if (WebBindings::getProperty(NULL, np_value, index_id, &index_value)) { |
241 if (NPVARIANT_IS_STRING(index_value)) { | 240 if (NPVARIANT_IS_STRING(index_value)) { |
242 std::string string( | 241 std::string string( |
243 NPVARIANT_TO_STRING(index_value).UTF8Characters, | 242 NPVARIANT_TO_STRING(index_value).UTF8Characters, |
244 NPVARIANT_TO_STRING(index_value).UTF8Length); | 243 NPVARIANT_TO_STRING(index_value).UTF8Length); |
245 wstring_vector.push_back(UTF8ToWide(string)); | 244 string_vector.push_back(string); |
viettrungluu
2010/12/03 02:00:16
Reading the code, I suppose it's clear that this i
| |
246 } | 245 } |
247 WebBindings::releaseVariantValue(&index_value); | 246 WebBindings::releaseVariantValue(&index_value); |
248 } | 247 } |
249 } | 248 } |
250 } | 249 } |
251 } | 250 } |
252 } | 251 } |
253 return wstring_vector; | 252 return string_vector; |
254 } | 253 } |
255 | 254 |
256 bool CppVariant::Invoke(const std::string& method, const CppVariant* args, | 255 bool CppVariant::Invoke(const std::string& method, const CppVariant* args, |
257 uint32 arg_count, CppVariant& result) const { | 256 uint32 arg_count, CppVariant& result) const { |
258 DCHECK(isObject()); | 257 DCHECK(isObject()); |
259 NPIdentifier method_name = WebBindings::getStringIdentifier(method.c_str()); | 258 NPIdentifier method_name = WebBindings::getStringIdentifier(method.c_str()); |
260 NPObject* np_object = value.objectValue; | 259 NPObject* np_object = value.objectValue; |
261 if (WebBindings::hasMethod(NULL, np_object, method_name)) { | 260 if (WebBindings::hasMethod(NULL, np_object, method_name)) { |
262 NPVariant r; | 261 NPVariant r; |
263 bool status = WebBindings::invoke(NULL, np_object, method_name, args, arg_co unt, &r); | 262 bool status = WebBindings::invoke(NULL, np_object, method_name, args, arg_co unt, &r); |
264 result.Set(r); | 263 result.Set(r); |
265 return status; | 264 return status; |
266 } else { | 265 } else { |
267 return false; | 266 return false; |
268 } | 267 } |
269 } | 268 } |
OLD | NEW |