summaryrefslogtreecommitdiffstats
path: root/webkit
diff options
context:
space:
mode:
authorarv@chromium.org <arv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-01-28 19:35:03 +0000
committerarv@chromium.org <arv@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-01-28 19:35:03 +0000
commit7fc3513ddd11148c95769a4a03ebaa627c7e1e7f (patch)
tree78a5db7855922ded3a535fd4777bcdb3a926887d /webkit
parent6188a8fe1fb91bd4f77a566591a775e4b353ef24 (diff)
downloadchromium_src-7fc3513ddd11148c95769a4a03ebaa627c7e1e7f.zip
chromium_src-7fc3513ddd11148c95769a4a03ebaa627c7e1e7f.tar.gz
chromium_src-7fc3513ddd11148c95769a4a03ebaa627c7e1e7f.tar.bz2
DOMUI: Allow chrome.send to pass number, boolean, null and arrays of those
BUG=None TEST=cpp_variant_unittest covers most of the new functionality. I also verified that NTP works correctly. Review URL: http://codereview.chromium.org/6254018 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@72994 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit')
-rw-r--r--webkit/glue/cpp_variant.cc33
-rw-r--r--webkit/glue/cpp_variant.h11
-rw-r--r--webkit/glue/cpp_variant_unittest.cc69
3 files changed, 88 insertions, 25 deletions
diff --git a/webkit/glue/cpp_variant.cc b/webkit/glue/cpp_variant.cc
index eca554b..699f7f1 100644
--- a/webkit/glue/cpp_variant.cc
+++ b/webkit/glue/cpp_variant.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -211,14 +211,14 @@ bool CppVariant::ToBoolean() const {
return value.boolValue;
}
-std::vector<std::string> CppVariant::ToStringVector() const {
+std::vector<CppVariant> CppVariant::ToVector() const {
DCHECK(isObject());
- std::vector<std::string> string_vector;
+ std::vector<CppVariant> vector;
NPObject* np_value = value.objectValue;
NPIdentifier length_id = WebBindings::getStringIdentifier("length");
if (WebBindings::hasProperty(NULL, np_value, length_id)) {
- NPVariant length_value;
+ CppVariant length_value;
if (WebBindings::getProperty(NULL, np_value, length_id, &length_value)) {
int length = 0;
// The length is a double in some cases.
@@ -226,30 +226,25 @@ std::vector<std::string> CppVariant::ToStringVector() const {
length = static_cast<int>(NPVARIANT_TO_DOUBLE(length_value));
else if (NPVARIANT_IS_INT32(length_value))
length = NPVARIANT_TO_INT32(length_value);
- WebBindings::releaseVariantValue(&length_value);
+ else
+ NOTREACHED();
// For sanity, only allow 60000 items.
length = std::min(60000, length);
for (int i = 0; i < length; ++i) {
// Get each of the items.
- std::string index = base::StringPrintf("%d", i);
- NPIdentifier index_id = WebBindings::getStringIdentifier(index.c_str());
- if (WebBindings::hasProperty(NULL, np_value, index_id)) {
- NPVariant index_value;
- if (WebBindings::getProperty(NULL, np_value, index_id, &index_value)) {
- if (NPVARIANT_IS_STRING(index_value)) {
- std::string string(
- NPVARIANT_TO_STRING(index_value).UTF8Characters,
- NPVARIANT_TO_STRING(index_value).UTF8Length);
- string_vector.push_back(string);
- }
- WebBindings::releaseVariantValue(&index_value);
- }
+ NPIdentifier index = WebBindings::getIntIdentifier(i);
+ if (WebBindings::hasProperty(NULL, np_value, index)) {
+ CppVariant index_value;
+ if (WebBindings::getProperty(NULL, np_value, index, &index_value))
+ vector.push_back(index_value);
}
}
}
+ } else {
+ NOTREACHED();
}
- return string_vector;
+ return vector;
}
bool CppVariant::Invoke(const std::string& method, const CppVariant* args,
diff --git a/webkit/glue/cpp_variant.h b/webkit/glue/cpp_variant.h
index 34f843a..01efd7d 100644
--- a/webkit/glue/cpp_variant.h
+++ b/webkit/glue/cpp_variant.h
@@ -1,4 +1,4 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -89,14 +89,15 @@ class CppVariant : public NPVariant {
bool isObject() const { return (type == NPVariantType_Object); }
// Converters. The CppVariant must be of a type convertible to these values.
- // For example, ToInteger() works only if isNumber() is true.
+ // For example, ToInt32() works only if isNumber() is true.
std::string ToString() const;
int32_t ToInt32() const;
double ToDouble() const;
bool ToBoolean() const;
- // Returns a vector of strings for the specified argument. This is useful
- // for converting a JavaScript array of strings into a vector of strings.
- std::vector<std::string> ToStringVector() const;
+ // Returns a vector of CppVariant for the specified variant. This should only
+ // be called on an Object. If the object has no "length" property an empty
+ // vector is returned.
+ std::vector<CppVariant> ToVector() const;
// Invoke method of the given name on an object with the supplied arguments.
// The first argument should be the object on which the method is to be
diff --git a/webkit/glue/cpp_variant_unittest.cc b/webkit/glue/cpp_variant_unittest.cc
index d4eacc6..f3c6b61 100644
--- a/webkit/glue/cpp_variant_unittest.cc
+++ b/webkit/glue/cpp_variant_unittest.cc
@@ -1,8 +1,11 @@
-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <vector>
+
#include "base/compiler_specific.h"
+#include "base/string_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h"
#include "webkit/glue/cpp_variant.h"
@@ -424,3 +427,67 @@ TEST(CppVariantTest, IsTypeFunctionsWork) {
WebBindings::releaseObject(obj);
CheckObject(cpp);
}
+
+bool MockNPHasPropertyFunction(NPObject *npobj, NPIdentifier name) {
+ return true;
+}
+
+bool MockNPGetPropertyFunction(NPObject *npobj, NPIdentifier name,
+ NPVariant *result) {
+ if (WebBindings::getStringIdentifier("length") == name) {
+ DOUBLE_TO_NPVARIANT(4, *result);
+ } else if (WebBindings::getIntIdentifier(0) == name) {
+ DOUBLE_TO_NPVARIANT(0, *result);
+ } else if (WebBindings::getIntIdentifier(1) == name) {
+ BOOLEAN_TO_NPVARIANT(true, *result);
+ } else if (WebBindings::getIntIdentifier(2) == name) {
+ NULL_TO_NPVARIANT(*result);
+ } else if (WebBindings::getIntIdentifier(3) == name) {
+ const char* s = "string";
+ size_t length = strlen(s);
+ char* mem = static_cast<char*>(malloc(length + 1));
+ base::strlcpy(mem, s, length + 1);
+ STRINGZ_TO_NPVARIANT(mem, *result);
+ }
+
+ return true;
+}
+
+TEST(CppVariantTest, ToVector) {
+ NPClass array_like_class = {
+ NP_CLASS_STRUCT_VERSION,
+ 0, // NPAllocateFunctionPtr allocate;
+ 0, // NPDeallocateFunctionPtr deallocate;
+ 0, // NPInvalidateFunctionPtr invalidate;
+ 0, // NPHasMethodFunctionPtr hasMethod;
+ 0, // NPInvokeFunctionPtr invoke;
+ 0, // NPInvokeDefaultFunctionPtr invokeDefault;
+ MockNPHasPropertyFunction, // NPHasPropertyFunctionPtr hasProperty;
+ MockNPGetPropertyFunction, // NPGetPropertyFunctionPtr getProperty;
+ 0, // NPSetPropertyFunctionPtr setProperty;
+ 0, // NPRemovePropertyFunctionPtr removeProperty;
+ 0, // NPEnumerationFunctionPtr enumerate;
+ 0 // NPConstructFunctionPtr construct;
+ };
+
+ NPObject* obj = WebBindings::createObject(NULL, &array_like_class);
+
+ CppVariant cpp;
+ cpp.Set(obj);
+
+ std::vector<CppVariant> cpp_vector = cpp.ToVector();
+ EXPECT_EQ(4u, cpp_vector.size());
+
+ EXPECT_TRUE(cpp_vector[0].isDouble());
+ EXPECT_EQ(0, cpp_vector[0].ToDouble());
+
+ EXPECT_TRUE(cpp_vector[1].isBool());
+ EXPECT_EQ(true, cpp_vector[1].ToBoolean());
+
+ EXPECT_TRUE(cpp_vector[2].isNull());
+
+ EXPECT_TRUE(cpp_vector[3].isString());
+ CheckString("string", cpp_vector[3]);
+
+ WebBindings::releaseObject(obj);
+}