Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: webkit/glue/cpp_variant.cc

Issue 6254018: Allow chrome.send to pass number, boolean, null and arrays of those (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Use malloc+strcpy instead of strdup Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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;
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 vector.push_back(index_value);
240 if (NPVARIANT_IS_STRING(index_value)) {
241 std::string string(
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 }
248 } 241 }
249 } 242 }
250 } 243 }
244 } else {
245 NOTREACHED();
251 } 246 }
252 return string_vector; 247 return vector;
253 } 248 }
254 249
255 bool CppVariant::Invoke(const std::string& method, const CppVariant* args, 250 bool CppVariant::Invoke(const std::string& method, const CppVariant* args,
256 uint32 arg_count, CppVariant& result) const { 251 uint32 arg_count, CppVariant& result) const {
257 DCHECK(isObject()); 252 DCHECK(isObject());
258 NPIdentifier method_name = WebBindings::getStringIdentifier(method.c_str()); 253 NPIdentifier method_name = WebBindings::getStringIdentifier(method.c_str());
259 NPObject* np_object = value.objectValue; 254 NPObject* np_object = value.objectValue;
260 if (WebBindings::hasMethod(NULL, np_object, method_name)) { 255 if (WebBindings::hasMethod(NULL, np_object, method_name)) {
261 NPVariant r; 256 NPVariant r;
262 bool status = WebBindings::invoke(NULL, np_object, method_name, args, arg_co unt, &r); 257 bool status = WebBindings::invoke(NULL, np_object, method_name, args, arg_co unt, &r);
263 result.Set(r); 258 result.Set(r);
264 return status; 259 return status;
265 } else { 260 } else {
266 return false; 261 return false;
267 } 262 }
268 } 263 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698