1dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block/* 2dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * Copyright (C) 2010 Google Inc. All rights reserved. 3dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * 4dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * Redistribution and use in source and binary forms, with or without 5dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * modification, are permitted provided that the following conditions are 6dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * met: 7dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * 8dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * * Redistributions of source code must retain the above copyright 9dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * notice, this list of conditions and the following disclaimer. 10dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * * Redistributions in binary form must reproduce the above 11dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * copyright notice, this list of conditions and the following disclaimer 12dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * in the documentation and/or other materials provided with the 13dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * distribution. 14dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * * Neither the name of Google Inc. nor the names of its 15dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * contributors may be used to endorse or promote products derived from 16dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * this software without specific prior written permission. 17dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * 18dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block */ 30dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block 31dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block/* 32dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block This file contains the declaration for CppVariant, a type used by C++ classes 33dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block that are to be bound to JavaScript objects. 34dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block 35dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block CppVariant exists primarily as an interface between C++ callers and the 36dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block corresponding NPVariant type. CppVariant also provides a number of 37dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block convenience constructors and accessors, so that the NPVariantType values 38dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block don't need to be exposed, and a destructor to free any memory allocated for 39dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block string values. 40dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block*/ 41dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block 42dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block#ifndef CppVariant_h 43dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block#define CppVariant_h 44dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block 45a94275402997c11dd2e778633dacf4b7e630a35dBen Murdoch#include "WebBindings.h" 46dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch#include "webkit/support/webkit_support.h" 47dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block#include <string> 48dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block#include <wtf/Vector.h> 49dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block 50dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Blockclass CppVariant : public NPVariant { 51dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Blockpublic: 52dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block CppVariant(); 53dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block ~CppVariant(); 54dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block void setNull(); 55dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block void set(bool); 56dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block void set(int32_t); 57dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block void set(double); 58dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block 59dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // Note that setting a CppVariant to a string value involves copying the 60dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // string data, which must be freed with a call to freeData() when the 61dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // CppVariant is set to a different value or is no longer needed. Normally 62dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // this is handled by the other set() methods and by the destructor. 63dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block void set(const char*); // Must be a null-terminated string. 64dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block void set(const std::string&); 65dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block void set(const NPString&); 66dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block void set(const NPVariant&); 67dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block 68dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // Note that setting a CppVariant to an NPObject involves ref-counting 69dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // the actual object. freeData() should only be called if the CppVariant 70dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // is no longer needed. The other set() methods handle this internally. 71dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // Also, the object's NPClass is expected to be a static object: neither 72dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // the NP runtime nor CppVariant will ever free it. 73dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block void set(NPObject*_value); 74dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block 75dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // These three methods all perform deep copies of any string data. This 76dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // allows local CppVariants to be released by the destructor without 77dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // corrupting their sources. In performance-critical code, or when strings 78dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // are very long, avoid creating new CppVariants. 79dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // In case of NPObject as the data, the copying involves ref-counting 80dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // as opposed to deep-copying. The ref-counting ensures that sources don't 81dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // get corrupted when the copies get destroyed. 82dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block void copyToNPVariant(NPVariant* result) const; 83dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block CppVariant& operator=(const CppVariant& original); 84dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block CppVariant(const CppVariant& original); 85dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block 86dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // Calls NPN_ReleaseVariantValue, which frees any string data 87dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // held by the object and sets its type to null. 88dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // In case of NPObject, the NPN_ReleaseVariantValue decrements 89dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // the ref-count (releases when ref-count becomes 0) 90dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block void freeData(); 91dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block 92dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // Compares this CppVariant's type and value to another's. They must be 93dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // identical in both type and value to be considered equal. For string and 94dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // object types, a deep comparison is performed; that is, the contents of the 95dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // strings, or the classes and refcounts of the objects, must be the same, 96dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // but they need not be the same pointers. 97dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block bool isEqual(const CppVariant&) const; 98dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block 99dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // The value of a CppVariant may be read directly from its NPVariant (but 100dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // should only be set using one of the set() methods above). Although the 101dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // type of a CppVariant is likewise public, it can be accessed through these 102dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // functions rather than directly if a caller wishes to avoid dependence on 103dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // the NPVariantType values. 104dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block bool isBool() const { return (type == NPVariantType_Bool); } 105dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block bool isInt32() const { return (type == NPVariantType_Int32); } 106dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block bool isDouble() const { return (type == NPVariantType_Double); } 107dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block bool isNumber() const { return (isInt32() || isDouble()); } 108dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block bool isString() const { return (type == NPVariantType_String); } 109dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block bool isVoid() const { return (type == NPVariantType_Void); } 110dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block bool isNull() const { return (type == NPVariantType_Null); } 111dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block bool isEmpty() const { return (isVoid() || isNull()); } 112dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block bool isObject() const { return (type == NPVariantType_Object); } 113dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block 114dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // Converters. The CppVariant must be of a type convertible to these values. 115dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // For example, toInt32() works only if isNumber() is true. 116dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block std::string toString() const; 117dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block int32_t toInt32() const; 118dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block double toDouble() const; 119dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block bool toBoolean() const; 120dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // Returns a vector of strings for the specified argument. This is useful 121dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // for converting a JavaScript array of strings into a vector of strings. 122dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block Vector<std::string> toStringVector() const; 123dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block 124dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // Invoke method of the given name on an object with the supplied arguments. 125dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // The first argument should be the object on which the method is to be 126dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // invoked. Returns whether the method was successfully invoked. If the 127dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // method was invoked successfully, any return value is stored in the 128dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block // CppVariant specified by result. 129dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block bool invoke(const std::string&, const CppVariant* arguments, 130dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block uint32_t argumentCount, CppVariant& result) const; 131dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block}; 132dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block 133dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block#endif // CppVariant_h 134