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