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/dev/scriptable_object_deprecated.h"
6 #include "ppapi/c/dev/ppb_memory_dev.h"
7 #include "ppapi/c/dev/ppp_class_deprecated.h"
8 #include "ppapi/cpp/module.h"
9 #include "ppapi/cpp/var.h"
13 namespace deprecated
{
17 // Allows converting an output param of a Var to an output param of a PP_Var
18 // for exceptions. The object is only copied if it is not void, which we
19 // take to mean an exception occurred.
20 class ExceptionConverter
{
22 ExceptionConverter(PP_Var
* out
) : out_(out
) {
24 ~ExceptionConverter() {
25 if (!exception_
.is_undefined())
26 *out_
= exception_
.Detach();
29 Var
* Get() { return &exception_
; }
36 // Used internally to convert a C-style array of PP_Var to a vector of Var.
37 void ArgListToVector(uint32_t argc
, PP_Var
* argv
, std::vector
<Var
>* output
) {
38 output
->reserve(argc
);
39 for (size_t i
= 0; i
< argc
; i
++)
40 output
->push_back(Var(Var::DontManage(), argv
[i
]));
43 bool HasProperty(void* object
, PP_Var name
, PP_Var
* exception
) {
44 ExceptionConverter
e(exception
);
45 return static_cast<ScriptableObject
*>(object
)->HasProperty(
46 Var(Var::DontManage(), name
), e
.Get());
49 bool HasMethod(void* object
, PP_Var name
, PP_Var
* exception
) {
50 ExceptionConverter
e(exception
);
51 return static_cast<ScriptableObject
*>(object
)->HasMethod(
52 Var(Var::DontManage(), name
), e
.Get());
55 PP_Var
GetProperty(void* object
,
58 ExceptionConverter
e(exception
);
59 return static_cast<ScriptableObject
*>(object
)->GetProperty(
60 Var(Var::DontManage(), name
), e
.Get()).Detach();
63 void GetAllPropertyNames(void* object
,
64 uint32_t* property_count
,
67 ExceptionConverter
e(exception
);
68 std::vector
<Var
> props
;
69 static_cast<ScriptableObject
*>(object
)->GetAllPropertyNames(&props
, e
.Get());
72 *property_count
= static_cast<uint32_t>(props
.size());
74 const PPB_Memory_Dev
* memory_if
= static_cast<const PPB_Memory_Dev
*>(
75 pp::Module::Get()->GetBrowserInterface(PPB_MEMORY_DEV_INTERFACE
));
76 *properties
= static_cast<PP_Var
*>(
77 memory_if
->MemAlloc(sizeof(PP_Var
) * props
.size()));
79 for (size_t i
= 0; i
< props
.size(); ++i
)
80 (*properties
)[i
] = props
[i
].Detach();
83 void SetProperty(void* object
,
87 ExceptionConverter
e(exception
);
88 static_cast<ScriptableObject
*>(object
)->SetProperty(
89 Var(Var::DontManage(), name
), Var(Var::DontManage(), value
), e
.Get());
92 void RemoveProperty(void* object
,
95 ExceptionConverter
e(exception
);
96 static_cast<ScriptableObject
*>(object
)->RemoveProperty(
97 Var(Var::DontManage(), name
), e
.Get());
100 PP_Var
Call(void* object
,
105 ExceptionConverter
e(exception
);
107 std::vector
<Var
> args
;
108 ArgListToVector(argc
, argv
, &args
);
109 return static_cast<ScriptableObject
*>(object
)->Call(
110 Var(Var::DontManage(), method_name
), args
, e
.Get()).Detach();
113 PP_Var
Construct(void* object
,
117 ExceptionConverter
e(exception
);
119 std::vector
<Var
> args
;
120 ArgListToVector(argc
, argv
, &args
);
121 return static_cast<ScriptableObject
*>(object
)->Construct(
122 args
, e
.Get()).Detach();
125 void Deallocate(void* object
) {
126 delete static_cast<ScriptableObject
*>(object
);
129 PPP_Class_Deprecated plugin_class
= {
133 &GetAllPropertyNames
,
143 bool ScriptableObject::HasProperty(const Var
& /*name*/, Var
* /*exception*/) {
147 bool ScriptableObject::HasMethod(const Var
& /*name*/, Var
* /*exception*/) {
151 Var
ScriptableObject::GetProperty(const Var
& /*name*/, Var
* exception
) {
152 *exception
= Var("Property does not exist on ScriptableObject");
156 void ScriptableObject::GetAllPropertyNames(std::vector
<Var
>* /*properties*/,
157 Var
* /*exception*/) {
160 void ScriptableObject::SetProperty(const Var
& /*name*/,
161 const Var
& /*value*/,
163 *exception
= Var("Property can not be set on ScriptableObject");
166 void ScriptableObject::RemoveProperty(const Var
& /*name*/,
169 "Property does does not exist to be removed in ScriptableObject");
172 Var
ScriptableObject::Call(const Var
& /*method_name*/,
173 const std::vector
<Var
>& /*args*/,
175 *exception
= Var("Method does not exist to call in ScriptableObject");
179 Var
ScriptableObject::Construct(const std::vector
<Var
>& /*args*/,
181 *exception
= Var("Construct method does not exist in ScriptableObject");
186 const PPP_Class_Deprecated
* ScriptableObject::GetClass() {
187 return &plugin_class
;
190 } // namespace deprecated