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.
5 #ifndef PPAPI_CPP_PRIVATE_VAR_PRIVATE_H_
6 #define PPAPI_CPP_PRIVATE_VAR_PRIVATE_H_
8 #include "ppapi/cpp/var.h"
14 namespace deprecated
{
15 class ScriptableObject
;
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
{
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
) {}
36 virtual ~VarPrivate() {}
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
41 deprecated::ScriptableObject
* AsScriptableObject() const;
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;
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
);
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.
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.
73 // This is a bit subtle:
74 // - If NULL is passed, we return NULL from get() and do nothing.
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.
79 // - If a non-undefined value is passed, we return the address of that var
80 // from get, and nothing else should change.
83 // void FooBar(a, b, Var* exception = NULL) {
84 // foo_interface->Bar(a, b, VarPrivate::OutException(exception).get());
90 originally_had_exception_(v
&& !v
->is_undefined()) {
92 temp_
= output_
->pp_var();
95 temp_
.type
= PP_VARTYPE_UNDEFINED
;
99 if (output_
&& !originally_had_exception_
)
100 *output_
= Var(PassRef(), temp_
);
111 bool originally_had_exception_
;
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
);
124 #endif // PPAPI_CPP_PRIVATE_VAR_PRIVATE_H_