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 #include "ppapi/cpp/private/var_private.h"
7 #include "ppapi/c/dev/ppb_memory_dev.h"
8 #include "ppapi/c/dev/ppb_var_deprecated.h"
9 #include "ppapi/cpp/instance_handle.h"
10 #include "ppapi/cpp/private/instance_private.h"
11 #include "ppapi/cpp/logging.h"
12 #include "ppapi/cpp/module_impl.h"
13 #include "ppapi/cpp/dev/scriptable_object_deprecated.h"
19 template <> const char* interface_name
<PPB_Var_Deprecated
>() {
20 return PPB_VAR_DEPRECATED_INTERFACE
;
25 using namespace deprecated
;
27 VarPrivate::VarPrivate(const InstanceHandle
& instance
,
28 ScriptableObject
* object
) {
29 if (has_interface
<PPB_Var_Deprecated
>()) {
30 var_
= get_interface
<PPB_Var_Deprecated
>()->CreateObject(
31 instance
.pp_instance(), object
->GetClass(), object
);
33 var_
.type
= PP_VARTYPE_NULL
;
39 ScriptableObject
* VarPrivate::AsScriptableObject() const {
42 } else if (has_interface
<PPB_Var_Deprecated
>()) {
44 if (get_interface
<PPB_Var_Deprecated
>()->IsInstanceOf(
45 var_
, ScriptableObject::GetClass(), &object
)) {
46 return reinterpret_cast<ScriptableObject
*>(object
);
52 bool VarPrivate::HasProperty(const Var
& name
, Var
* exception
) const {
53 if (!has_interface
<PPB_Var_Deprecated
>())
55 return get_interface
<PPB_Var_Deprecated
>()->HasProperty(
56 var_
, name
.pp_var(), OutException(exception
).get());
59 bool VarPrivate::HasMethod(const Var
& name
, Var
* exception
) const {
60 if (!has_interface
<PPB_Var_Deprecated
>())
62 return get_interface
<PPB_Var_Deprecated
>()->HasMethod(
63 var_
, name
.pp_var(), OutException(exception
).get());
66 VarPrivate
VarPrivate::GetProperty(const Var
& name
, Var
* exception
) const {
67 if (!has_interface
<PPB_Var_Deprecated
>())
69 return Var(PassRef(), get_interface
<PPB_Var_Deprecated
>()->GetProperty(
70 var_
, name
.pp_var(), OutException(exception
).get()));
73 void VarPrivate::GetAllPropertyNames(std::vector
<Var
>* properties
,
74 Var
* exception
) const {
75 if (!has_interface
<PPB_Var_Deprecated
>())
78 uint32_t prop_count
= 0;
79 get_interface
<PPB_Var_Deprecated
>()->GetAllPropertyNames(
80 var_
, &prop_count
, &props
, OutException(exception
).get());
83 properties
->resize(prop_count
);
84 for (uint32_t i
= 0; i
< prop_count
; ++i
) {
85 Var
temp(PassRef(), props
[i
]);
86 (*properties
)[i
] = temp
;
88 const PPB_Memory_Dev
* memory_if
= static_cast<const PPB_Memory_Dev
*>(
89 pp::Module::Get()->GetBrowserInterface(PPB_MEMORY_DEV_INTERFACE
));
90 memory_if
->MemFree(props
);
93 void VarPrivate::SetProperty(const Var
& name
, const Var
& value
,
95 if (!has_interface
<PPB_Var_Deprecated
>())
97 get_interface
<PPB_Var_Deprecated
>()->SetProperty(
98 var_
, name
.pp_var(), value
.pp_var(), OutException(exception
).get());
101 void VarPrivate::RemoveProperty(const Var
& name
, Var
* exception
) {
102 if (!has_interface
<PPB_Var_Deprecated
>())
104 get_interface
<PPB_Var_Deprecated
>()->RemoveProperty(
105 var_
, name
.pp_var(), OutException(exception
).get());
108 VarPrivate
VarPrivate::Call(const Var
& method_name
, uint32_t argc
, Var
* argv
,
110 if (!has_interface
<PPB_Var_Deprecated
>())
113 std::vector
<PP_Var
> args
;
115 for (size_t i
= 0; i
< argc
; i
++)
116 args
.push_back(argv
[i
].pp_var());
117 return Var(PassRef(), get_interface
<PPB_Var_Deprecated
>()->Call(
118 var_
, method_name
.pp_var(), argc
, &args
[0],
119 OutException(exception
).get()));
121 // Don't try to get the address of a vector if it's empty.
122 return Var(PassRef(), get_interface
<PPB_Var_Deprecated
>()->Call(
123 var_
, method_name
.pp_var(), 0, NULL
,
124 OutException(exception
).get()));
128 VarPrivate
VarPrivate::Construct(uint32_t argc
, Var
* argv
,
129 Var
* exception
) const {
130 if (!has_interface
<PPB_Var_Deprecated
>())
133 std::vector
<PP_Var
> args
;
135 for (size_t i
= 0; i
< argc
; i
++)
136 args
.push_back(argv
[i
].pp_var());
137 return Var(PassRef(), get_interface
<PPB_Var_Deprecated
>()->Construct(
138 var_
, argc
, &args
[0], OutException(exception
).get()));
140 // Don't try to get the address of a vector if it's empty.
141 return Var(PassRef(), get_interface
<PPB_Var_Deprecated
>()->Construct(
142 var_
, 0, NULL
, OutException(exception
).get()));
146 VarPrivate
VarPrivate::Call(const Var
& method_name
, Var
* exception
) {
147 if (!has_interface
<PPB_Var_Deprecated
>())
149 return Var(PassRef(), get_interface
<PPB_Var_Deprecated
>()->Call(
150 var_
, method_name
.pp_var(), 0, NULL
, OutException(exception
).get()));
153 VarPrivate
VarPrivate::Call(const Var
& method_name
, const Var
& arg1
,
155 if (!has_interface
<PPB_Var_Deprecated
>())
157 PP_Var args
[1] = {arg1
.pp_var()};
158 return Var(PassRef(), get_interface
<PPB_Var_Deprecated
>()->Call(
159 var_
, method_name
.pp_var(), 1, args
, OutException(exception
).get()));
162 VarPrivate
VarPrivate::Call(const Var
& method_name
, const Var
& arg1
,
163 const Var
& arg2
, Var
* exception
) {
164 if (!has_interface
<PPB_Var_Deprecated
>())
166 PP_Var args
[2] = {arg1
.pp_var(), arg2
.pp_var()};
167 return Var(PassRef(), get_interface
<PPB_Var_Deprecated
>()->Call(
168 var_
, method_name
.pp_var(), 2, args
, OutException(exception
).get()));
171 VarPrivate
VarPrivate::Call(const Var
& method_name
, const Var
& arg1
,
172 const Var
& arg2
, const Var
& arg3
, Var
* exception
) {
173 if (!has_interface
<PPB_Var_Deprecated
>())
175 PP_Var args
[3] = {arg1
.pp_var(), arg2
.pp_var(), arg3
.pp_var()};
176 return Var(PassRef(), get_interface
<PPB_Var_Deprecated
>()->Call(
177 var_
, method_name
.pp_var(), 3, args
, OutException(exception
).get()));
180 VarPrivate
VarPrivate::Call(const Var
& method_name
, const Var
& arg1
,
181 const Var
& arg2
, const Var
& arg3
, const Var
& arg4
,
183 if (!has_interface
<PPB_Var_Deprecated
>())
185 PP_Var args
[4] = {arg1
.pp_var(), arg2
.pp_var(), arg3
.pp_var(), arg4
.pp_var()};
186 return Var(PassRef(), get_interface
<PPB_Var_Deprecated
>()->Call(
187 var_
, method_name
.pp_var(), 4, args
, OutException(exception
).get()));