| 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 "webkit/api/public/WebBindings.h" | 8 #include "webkit/api/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 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 return 0.0; | 203 return 0.0; |
| 204 } | 204 } |
| 205 } | 205 } |
| 206 | 206 |
| 207 bool CppVariant::ToBoolean() const { | 207 bool CppVariant::ToBoolean() const { |
| 208 DCHECK(isBool()); | 208 DCHECK(isBool()); |
| 209 return value.boolValue; | 209 return value.boolValue; |
| 210 } | 210 } |
| 211 | 211 |
| 212 std::vector<std::wstring> CppVariant::ToStringVector() const { | 212 std::vector<std::wstring> CppVariant::ToStringVector() const { |
| 213 | |
| 214 DCHECK(isObject()); | 213 DCHECK(isObject()); |
| 215 std::vector<std::wstring> wstring_vector; | 214 std::vector<std::wstring> wstring_vector; |
| 216 NPObject* np_value = value.objectValue; | 215 NPObject* np_value = value.objectValue; |
| 217 NPIdentifier length_id = WebBindings::getStringIdentifier("length"); | 216 NPIdentifier length_id = WebBindings::getStringIdentifier("length"); |
| 218 | 217 |
| 219 if (WebBindings::hasProperty(NULL, np_value, length_id)) { | 218 if (WebBindings::hasProperty(NULL, np_value, length_id)) { |
| 220 NPVariant length_value; | 219 NPVariant length_value; |
| 221 if (WebBindings::getProperty(NULL, np_value, length_id, &length_value)) { | 220 if (WebBindings::getProperty(NULL, np_value, length_id, &length_value)) { |
| 222 int length = 0; | 221 int length = 0; |
| 223 // The length is a double in some cases. | 222 // The length is a double in some cases. |
| 224 if (NPVARIANT_IS_DOUBLE(length_value)) | 223 if (NPVARIANT_IS_DOUBLE(length_value)) |
| 225 length = static_cast<int>(NPVARIANT_TO_DOUBLE(length_value)); | 224 length = static_cast<int>(NPVARIANT_TO_DOUBLE(length_value)); |
| 226 else if (NPVARIANT_IS_INT32(length_value)) | 225 else if (NPVARIANT_IS_INT32(length_value)) |
| 227 length = NPVARIANT_TO_INT32(length_value); | 226 length = NPVARIANT_TO_INT32(length_value); |
| 228 WebBindings::releaseVariantValue(&length_value); | 227 WebBindings::releaseVariantValue(&length_value); |
| 229 | 228 |
| 230 // For sanity, only allow 100 items. | 229 // For sanity, only allow 100 items. |
| 231 length = std::min(100, length); | 230 length = std::min(100, length); |
| 232 for (int i = 0; i < length; ++i) { | 231 for (int i = 0; i < length; ++i) { |
| 233 // Get each of the items. | 232 // Get each of the items. |
| 234 std::string index = StringPrintf("%d", i); | 233 std::string index = StringPrintf("%d", i); |
| 235 NPIdentifier index_id = WebBindings::getStringIdentifier(index.c_str()); | 234 NPIdentifier index_id = WebBindings::getStringIdentifier(index.c_str()); |
| 236 if (WebBindings::hasProperty(NULL, np_value, index_id)) { | 235 if (WebBindings::hasProperty(NULL, np_value, index_id)) { |
| 237 NPVariant index_value; | 236 NPVariant index_value; |
| 238 if (WebBindings::getProperty(NULL, np_value, index_id, &index_value))
{ | 237 if (WebBindings::getProperty(NULL, np_value, index_id, |
| 238 &index_value)) { |
| 239 if (NPVARIANT_IS_STRING(index_value)) { | 239 if (NPVARIANT_IS_STRING(index_value)) { |
| 240 std::string string( | 240 std::string string( |
| 241 NPVARIANT_TO_STRING(index_value).UTF8Characters, | 241 NPVARIANT_TO_STRING(index_value).UTF8Characters, |
| 242 NPVARIANT_TO_STRING(index_value).UTF8Length); | 242 NPVARIANT_TO_STRING(index_value).UTF8Length); |
| 243 wstring_vector.push_back(UTF8ToWide(string)); | 243 wstring_vector.push_back(UTF8ToWide(string)); |
| 244 } | 244 } |
| 245 WebBindings::releaseVariantValue(&index_value); | 245 WebBindings::releaseVariantValue(&index_value); |
| 246 } | 246 } |
| 247 } | 247 } |
| 248 } | 248 } |
| 249 } | 249 } |
| 250 } | 250 } |
| 251 return wstring_vector; | 251 return wstring_vector; |
| 252 } | 252 } |
| 253 | 253 |
| 254 bool CppVariant::Invoke(const std::string& method, const CppVariant* args, | 254 bool CppVariant::Invoke(const std::string& method, const CppVariant* args, |
| 255 uint32 arg_count, CppVariant& result) const { | 255 uint32 arg_count, CppVariant* result) const { |
| 256 DCHECK(isObject()); | 256 DCHECK(isObject()); |
| 257 NPIdentifier method_name = WebBindings::getStringIdentifier(method.c_str()); | 257 NPIdentifier method_name = WebBindings::getStringIdentifier(method.c_str()); |
| 258 NPObject* np_object = value.objectValue; | 258 NPObject* np_object = value.objectValue; |
| 259 if (WebBindings::hasMethod(NULL, np_object, method_name)) { | 259 if (WebBindings::hasMethod(NULL, np_object, method_name)) { |
| 260 NPVariant r; | 260 NPVariant r; |
| 261 bool status = WebBindings::invoke(NULL, np_object, method_name, args, arg_co
unt, &r); | 261 bool status = WebBindings::invoke(NULL, np_object, method_name, args, |
| 262 result.Set(r); | 262 arg_count, &r); |
| 263 result->Set(r); |
| 263 return status; | 264 return status; |
| 264 } else { | 265 } else { |
| 265 return false; | 266 return false; |
| 266 } | 267 } |
| 267 } | 268 } |
| 269 |
| 270 bool CppVariant::GetProperty(const std::string& property, |
| 271 CppVariant* result) const { |
| 272 DCHECK(isObject()); |
| 273 NPIdentifier property_name = |
| 274 WebBindings::getStringIdentifier(property.c_str()); |
| 275 NPObject* np_object = value.objectValue; |
| 276 if (WebBindings::hasProperty(NULL, np_object, property_name)) { |
| 277 NPVariant r; |
| 278 bool status = WebBindings::getProperty(NULL, np_object, property_name, &r); |
| 279 result->Set(r); |
| 280 return status; |
| 281 } else { |
| 282 return false; |
| 283 } |
| 284 } |
| OLD | NEW |