172a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// found in the LICENSE file.
4c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
5c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch/*
6c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  This file contains the declaration for CppVariant, a type used by C++ classes
7c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  that are to be bound to JavaScript objects.
8c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
9c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  CppVariant exists primarily as an interface between C++ callers and the
10c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  corresponding NPVariant type.  CppVariant also provides a number of
11c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  convenience constructors and accessors, so that the NPVariantType values
12c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  don't need to be exposed, and a destructor to free any memory allocated for
13c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  string values.
14c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
15c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  For a usage example, see cpp_binding_example.{h|cc}.
16c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch*/
17c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
18c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#ifndef WEBKIT_GLUE_CPP_VARIANT_H__
19c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#define WEBKIT_GLUE_CPP_VARIANT_H__
20c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
21c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <string>
22c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <vector>
23c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
24c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/basictypes.h"
25c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "third_party/npapi/bindings/npruntime.h"
26c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
27c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass CppVariant : public NPVariant {
28c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public:
29c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  CppVariant();
30c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  ~CppVariant();
31c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void SetNull();
32c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void Set(bool value);
33c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void Set(int32_t value);
34c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void Set(double value);
35c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
36c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Note that setting a CppVariant to a string value involves copying the
37c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // string data, which must be freed with a call to FreeData() when the
38c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // CppVariant is set to a different value or is no longer needed.  Normally
39c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // this is handled by the other Set() methods and by the destructor.
40c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void Set(const char* value);  // Must be a null-terminated string.
41c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void Set(const std::string& value);
42c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void Set(const NPString& new_value);
43c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void Set(const NPVariant& new_value);
44c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
45c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Note that setting a CppVariant to an NPObject involves ref-counting
46c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // the actual object. FreeData() should only be called if the CppVariant
47c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // is no longer needed. The other Set() methods handle this internally.
48c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Also, the object's NPClass is expected to be a static object: neither
49c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // the NP runtime nor CPPVariant will ever free it.
50c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void Set(NPObject* new_value);
51c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
52c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // These three methods all perform deep copies of any string data.  This
53c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // allows local CppVariants to be released by the destructor without
54c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // corrupting their sources. In performance-critical code, or when strings
55c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // are very long, avoid creating new CppVariants.
56c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // In case of NPObject as the data, the copying involves ref-counting
57c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // as opposed to deep-copying. The ref-counting ensures that sources don't
58c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // get corrupted when the copies get destroyed.
59c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void CopyToNPVariant(NPVariant* result) const;
60c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  CppVariant& operator=(const CppVariant& original);
61c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  CppVariant(const CppVariant& original);
62c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
63c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Calls NPN_ReleaseVariantValue, which frees any string data
64c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // held by the object and sets its type to null.
65c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // In case of NPObject, the NPN_ReleaseVariantValue decrements
66c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // the ref-count (releases when ref-count becomes 0)
67c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void FreeData();
68c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
69c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Compares this CppVariant's type and value to another's.  They must be
70c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // identical in both type and value to be considered equal.  For string and
71c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // object types, a deep comparison is performed; that is, the contents of the
72c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // strings, or the classes and refcounts of the objects, must be the same,
73c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // but they need not be the same pointers.
74c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool isEqual(const CppVariant& other) const;
75c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
76c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The value of a CppVariant may be read directly from its NPVariant (but
77c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // should only be set using one of the Set() methods above). Although the
78c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // type of a CppVariant is likewise public, it can be accessed through these
79c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // functions rather than directly if a caller wishes to avoid dependence on
80c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // the NPVariantType values.
81c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool isBool() const { return (type == NPVariantType_Bool); }
82c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool isInt32() const { return (type == NPVariantType_Int32); }
83c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool isDouble() const { return (type == NPVariantType_Double); }
84c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool isNumber() const { return (isInt32() || isDouble()); }
85c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool isString() const { return (type == NPVariantType_String); }
86c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool isVoid() const { return (type == NPVariantType_Void); }
87c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool isNull() const { return (type == NPVariantType_Null); }
88c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool isEmpty() const { return (isVoid() || isNull()); }
89c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool isObject() const { return (type == NPVariantType_Object); }
90c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
91c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Converters.  The CppVariant must be of a type convertible to these values.
9272a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // For example, ToInt32() works only if isNumber() is true.
93c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  std::string ToString() const;
94c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  int32_t ToInt32() const;
95c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  double ToDouble() const;
96c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool ToBoolean() const;
9772a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // Returns a vector of CppVariant for the specified variant. This should only
9872a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // be called on an Object. If the object has no "length" property an empty
9972a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  // vector is returned.
10072a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen  std::vector<CppVariant> ToVector() const;
101c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
102c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Invoke method of the given name on an object with the supplied arguments.
103c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The first argument should be the object on which the method is to be
104c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // invoked.  Returns whether the method was successfully invoked.  If the
105c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // method was invoked successfully, any return value is stored in the
106c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // CppVariant specified by result.
107c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool Invoke(const std::string& method, const CppVariant* args,
108c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch              uint32 arg_count, CppVariant& result) const;
109c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch};
110c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
111c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif  // WEBKIT_GLUE_CPP_VARIANT_H__
112