- add sources.
[platform/framework/web/crosswalk.git] / src / ppapi / cpp / private / var_private.h
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
3 // found in the LICENSE file.
4
5 #ifndef PPAPI_CPP_PRIVATE_VAR_PRIVATE_H_
6 #define PPAPI_CPP_PRIVATE_VAR_PRIVATE_H_
7
8 #include "ppapi/cpp/var.h"
9
10 namespace pp {
11
12 class InstanceHandle;
13
14 namespace deprecated {
15 class ScriptableObject;
16 }
17
18 // VarPrivate is a version of Var that exposes the private scripting API.
19 // It's designed to be mostly interchangeable with Var since most callers will
20 // be dealing with Vars from various places.
21 class VarPrivate : public Var {
22  public:
23   VarPrivate() : Var() {}
24   VarPrivate(Null) : Var(Null()) {}
25   VarPrivate(bool b) : Var(b) {}
26   VarPrivate(int32_t i) : Var(i) {}
27   VarPrivate(double d) : Var(d) {}
28   VarPrivate(const char* utf8_str) : Var(utf8_str) {}
29   VarPrivate(const std::string& utf8_str) : Var(utf8_str) {}
30   VarPrivate(PassRef, PP_Var var) : Var(PassRef(), var) {}
31   VarPrivate(DontManage, PP_Var var) : Var(DontManage(), var) {}
32   VarPrivate(const InstanceHandle& instance,
33              deprecated::ScriptableObject* object);
34   VarPrivate(const Var& other) : Var(other) {}
35
36   virtual ~VarPrivate() {}
37
38   // This assumes the object is of type object. If it's not, it will assert in
39   // debug mode. If it is not an object or not a ScriptableObject type, returns
40   // NULL.
41   deprecated::ScriptableObject* AsScriptableObject() const;
42
43   bool HasProperty(const Var& name, Var* exception = NULL) const;
44   bool HasMethod(const Var& name, Var* exception = NULL) const;
45   VarPrivate GetProperty(const Var& name, Var* exception = NULL) const;
46   void GetAllPropertyNames(std::vector<Var>* properties,
47                            Var* exception = NULL) const;
48   void SetProperty(const Var& name, const Var& value, Var* exception = NULL);
49   void RemoveProperty(const Var& name, Var* exception = NULL);
50   VarPrivate Call(const Var& method_name, uint32_t argc, Var* argv,
51            Var* exception = NULL);
52   VarPrivate Construct(uint32_t argc, Var* argv, Var* exception = NULL) const;
53
54   // Convenience functions for calling functions with small # of args.
55   VarPrivate Call(const Var& method_name, Var* exception = NULL);
56   VarPrivate Call(const Var& method_name, const Var& arg1,
57                   Var* exception = NULL);
58   VarPrivate Call(const Var& method_name, const Var& arg1, const Var& arg2,
59                   Var* exception = NULL);
60   VarPrivate Call(const Var& method_name, const Var& arg1, const Var& arg2,
61                   const Var& arg3, Var* exception = NULL);
62   VarPrivate Call(const Var& method_name, const Var& arg1, const Var& arg2,
63                   const Var& arg3, const Var& arg4, Var* exception = NULL);
64
65   // For use when calling the raw C PPAPI when using the C++ Var as a possibly
66   // NULL exception. This will handle getting the address of the internal value
67   // out if it's non-NULL and fixing up the reference count.
68   //
69   // Danger: this will only work for things with exception semantics, i.e. that
70   // the value will not be changed if it's a non-undefined exception. Otherwise,
71   // this class will mess up the refcounting.
72   //
73   // This is a bit subtle:
74   // - If NULL is passed, we return NULL from get() and do nothing.
75   //
76   // - If a undefined value is passed, we return the address of a undefined var
77   //   from get and have the output value take ownership of that var.
78   //
79   // - If a non-undefined value is passed, we return the address of that var
80   //   from get, and nothing else should change.
81   //
82   // Example:
83   //   void FooBar(a, b, Var* exception = NULL) {
84   //     foo_interface->Bar(a, b, VarPrivate::OutException(exception).get());
85   //   }
86   class OutException {
87    public:
88     OutException(Var* v)
89         : output_(v),
90           originally_had_exception_(v && !v->is_undefined()) {
91       if (output_) {
92         temp_ = output_->pp_var();
93       } else {
94         temp_.padding = 0;
95         temp_.type = PP_VARTYPE_UNDEFINED;
96       }
97     }
98     ~OutException() {
99       if (output_ && !originally_had_exception_)
100         *output_ = Var(PassRef(), temp_);
101     }
102
103     PP_Var* get() {
104       if (output_)
105         return &temp_;
106       return NULL;
107     }
108
109    private:
110     Var* output_;
111     bool originally_had_exception_;
112     PP_Var temp_;
113   };
114
115  private:
116   // Prevent an arbitrary pointer argument from being implicitly converted to
117   // a bool at Var construction. If somebody makes such a mistake, (s)he will
118   // get a compilation error.
119   VarPrivate(void* non_scriptable_object_pointer);
120 };
121
122 }  // namespace pp
123
124 #endif  // PPAPI_CPP_PRIVATE_VAR_PRIVATE_H_