Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 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 // 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/Source/WebKit/chromium/public/WebBindings.h" | 8 #include "third_party/WebKit/Source/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" |
| 11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| (...skipping 192 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::string> CppVariant::ToStringVector() const { | 214 std::vector<CppVariant> CppVariant::ToVector() const { |
| 215 DCHECK(isObject()); | 215 DCHECK(isObject()); |
| 216 std::vector<std::string> string_vector; | 216 std::vector<CppVariant> vector; |
| 217 NPObject* np_value = value.objectValue; | 217 NPObject* np_value = value.objectValue; |
| 218 NPIdentifier length_id = WebBindings::getStringIdentifier("length"); | 218 NPIdentifier length_id = WebBindings::getStringIdentifier("length"); |
| 219 | 219 |
| 220 if (WebBindings::hasProperty(NULL, np_value, length_id)) { | 220 if (WebBindings::hasProperty(NULL, np_value, length_id)) { |
| 221 NPVariant length_value; | 221 CppVariant length_value; |
| 222 if (WebBindings::getProperty(NULL, np_value, length_id, &length_value)) { | 222 if (WebBindings::getProperty(NULL, np_value, length_id, &length_value)) { |
| 223 int length = 0; | 223 int length = 0; |
| 224 // The length is a double in some cases. | 224 // The length is a double in some cases. |
| 225 if (NPVARIANT_IS_DOUBLE(length_value)) | 225 if (NPVARIANT_IS_DOUBLE(length_value)) |
| 226 length = static_cast<int>(NPVARIANT_TO_DOUBLE(length_value)); | 226 length = static_cast<int>(NPVARIANT_TO_DOUBLE(length_value)); |
| 227 else if (NPVARIANT_IS_INT32(length_value)) | 227 else if (NPVARIANT_IS_INT32(length_value)) |
| 228 length = NPVARIANT_TO_INT32(length_value); | 228 length = NPVARIANT_TO_INT32(length_value); |
| 229 WebBindings::releaseVariantValue(&length_value); | 229 else |
| 230 NOTREACHED(); | |
| 230 | 231 |
| 231 // For sanity, only allow 60000 items. | 232 // For sanity, only allow 60000 items. |
| 232 length = std::min(60000, length); | 233 length = std::min(60000, length); |
| 233 for (int i = 0; i < length; ++i) { | 234 for (int i = 0; i < length; ++i) { |
| 234 // Get each of the items. | 235 // Get each of the items. |
| 235 std::string index = base::StringPrintf("%d", i); | 236 NPIdentifier index = WebBindings::getIntIdentifier(i); |
| 236 NPIdentifier index_id = WebBindings::getStringIdentifier(index.c_str()); | 237 if (WebBindings::hasProperty(NULL, np_value, index)) { |
| 237 if (WebBindings::hasProperty(NULL, np_value, index_id)) { | 238 CppVariant index_value; |
|
Evan Martin
2011/01/26 19:21:10
Just to confirm, by using CppVariant here it impli
arv (Not doing code reviews)
2011/01/26 19:38:58
Yes, CppVariant calls FreeData in its destructor.
| |
| 238 NPVariant index_value; | 239 if (WebBindings::getProperty(NULL, np_value, index, &index_value)) { |
| 239 if (WebBindings::getProperty(NULL, np_value, index_id, &index_value)) { | 240 CppVariant cpp; |
| 240 if (NPVARIANT_IS_STRING(index_value)) { | 241 cpp.Set(index_value); |
| 241 std::string string( | 242 vector.push_back(cpp); |
| 242 NPVARIANT_TO_STRING(index_value).UTF8Characters, | |
| 243 NPVARIANT_TO_STRING(index_value).UTF8Length); | |
| 244 string_vector.push_back(string); | |
| 245 } | |
| 246 WebBindings::releaseVariantValue(&index_value); | |
| 247 } | 243 } |
| 248 } | 244 } |
| 249 } | 245 } |
| 250 } | 246 } |
| 247 } else { | |
| 248 NOTREACHED(); | |
| 251 } | 249 } |
| 252 return string_vector; | 250 return vector; |
| 253 } | 251 } |
| 254 | 252 |
| 255 bool CppVariant::Invoke(const std::string& method, const CppVariant* args, | 253 bool CppVariant::Invoke(const std::string& method, const CppVariant* args, |
| 256 uint32 arg_count, CppVariant& result) const { | 254 uint32 arg_count, CppVariant& result) const { |
| 257 DCHECK(isObject()); | 255 DCHECK(isObject()); |
| 258 NPIdentifier method_name = WebBindings::getStringIdentifier(method.c_str()); | 256 NPIdentifier method_name = WebBindings::getStringIdentifier(method.c_str()); |
| 259 NPObject* np_object = value.objectValue; | 257 NPObject* np_object = value.objectValue; |
| 260 if (WebBindings::hasMethod(NULL, np_object, method_name)) { | 258 if (WebBindings::hasMethod(NULL, np_object, method_name)) { |
| 261 NPVariant r; | 259 NPVariant r; |
| 262 bool status = WebBindings::invoke(NULL, np_object, method_name, args, arg_co unt, &r); | 260 bool status = WebBindings::invoke(NULL, np_object, method_name, args, arg_co unt, &r); |
| 263 result.Set(r); | 261 result.Set(r); |
| 264 return status; | 262 return status; |
| 265 } else { | 263 } else { |
| 266 return false; | 264 return false; |
| 267 } | 265 } |
| 268 } | 266 } |
| OLD | NEW |