• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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"
10 
11 namespace pp {
12 
13 namespace deprecated {
14 
15 namespace {
16 
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 {
21  public:
ExceptionConverter(PP_Var * out)22   ExceptionConverter(PP_Var* out) : out_(out) {
23   }
~ExceptionConverter()24   ~ExceptionConverter() {
25     if (!exception_.is_undefined())
26       *out_ = exception_.Detach();
27   }
28 
Get()29   Var* Get() { return &exception_; }
30 
31  private:
32   PP_Var* out_;
33   Var exception_;
34 };
35 
36 // Used internally to convert a C-style array of PP_Var to a vector of Var.
ArgListToVector(uint32_t argc,PP_Var * argv,std::vector<Var> * output)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]));
41 }
42 
HasProperty(void * object,PP_Var name,PP_Var * exception)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());
47 }
48 
HasMethod(void * object,PP_Var name,PP_Var * exception)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());
53 }
54 
GetProperty(void * object,PP_Var name,PP_Var * exception)55 PP_Var GetProperty(void* object,
56                    PP_Var name,
57                    PP_Var* exception) {
58   ExceptionConverter e(exception);
59   return static_cast<ScriptableObject*>(object)->GetProperty(
60       Var(Var::DontManage(), name), e.Get()).Detach();
61 }
62 
GetAllPropertyNames(void * object,uint32_t * property_count,PP_Var ** properties,PP_Var * exception)63 void GetAllPropertyNames(void* object,
64                          uint32_t* property_count,
65                          PP_Var** properties,
66                          PP_Var* exception) {
67   ExceptionConverter e(exception);
68   std::vector<Var> props;
69   static_cast<ScriptableObject*>(object)->GetAllPropertyNames(&props, e.Get());
70   if (props.empty())
71     return;
72   *property_count = static_cast<uint32_t>(props.size());
73 
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()));
78 
79   for (size_t i = 0; i < props.size(); ++i)
80     (*properties)[i] = props[i].Detach();
81 }
82 
SetProperty(void * object,PP_Var name,PP_Var value,PP_Var * exception)83 void SetProperty(void* object,
84                  PP_Var name,
85                  PP_Var value,
86                  PP_Var* exception) {
87   ExceptionConverter e(exception);
88   static_cast<ScriptableObject*>(object)->SetProperty(
89       Var(Var::DontManage(), name), Var(Var::DontManage(), value), e.Get());
90 }
91 
RemoveProperty(void * object,PP_Var name,PP_Var * exception)92 void RemoveProperty(void* object,
93                     PP_Var name,
94                     PP_Var* exception) {
95   ExceptionConverter e(exception);
96   static_cast<ScriptableObject*>(object)->RemoveProperty(
97       Var(Var::DontManage(), name), e.Get());
98 }
99 
Call(void * object,PP_Var method_name,uint32_t argc,PP_Var * argv,PP_Var * exception)100 PP_Var Call(void* object,
101             PP_Var method_name,
102             uint32_t argc,
103             PP_Var* argv,
104             PP_Var* exception) {
105   ExceptionConverter e(exception);
106 
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();
111 }
112 
Construct(void * object,uint32_t argc,PP_Var * argv,PP_Var * exception)113 PP_Var Construct(void* object,
114                  uint32_t argc,
115                  PP_Var* argv,
116                  PP_Var* exception) {
117   ExceptionConverter e(exception);
118 
119   std::vector<Var> args;
120   ArgListToVector(argc, argv, &args);
121   return static_cast<ScriptableObject*>(object)->Construct(
122       args, e.Get()).Detach();
123 }
124 
Deallocate(void * object)125 void Deallocate(void* object) {
126   delete static_cast<ScriptableObject*>(object);
127 }
128 
129 PPP_Class_Deprecated plugin_class = {
130   &HasProperty,
131   &HasMethod,
132   &GetProperty,
133   &GetAllPropertyNames,
134   &SetProperty,
135   &RemoveProperty,
136   &Call,
137   &Construct,
138   &Deallocate
139 };
140 
141 }  // namespace
142 
HasProperty(const Var &,Var *)143 bool ScriptableObject::HasProperty(const Var& /*name*/, Var* /*exception*/) {
144   return false;
145 }
146 
HasMethod(const Var &,Var *)147 bool ScriptableObject::HasMethod(const Var& /*name*/, Var* /*exception*/) {
148   return false;
149 }
150 
GetProperty(const Var &,Var * exception)151 Var ScriptableObject::GetProperty(const Var& /*name*/, Var* exception) {
152   *exception = Var("Property does not exist on ScriptableObject");
153   return Var();
154 }
155 
GetAllPropertyNames(std::vector<Var> *,Var *)156 void ScriptableObject::GetAllPropertyNames(std::vector<Var>* /*properties*/,
157                                            Var* /*exception*/) {
158 }
159 
SetProperty(const Var &,const Var &,Var * exception)160 void ScriptableObject::SetProperty(const Var& /*name*/,
161                                    const Var& /*value*/,
162                                    Var* exception) {
163   *exception = Var("Property can not be set on ScriptableObject");
164 }
165 
RemoveProperty(const Var &,Var * exception)166 void ScriptableObject::RemoveProperty(const Var& /*name*/,
167                                       Var* exception) {
168   *exception = Var(
169       "Property does does not exist to be removed in ScriptableObject");
170 }
171 
Call(const Var &,const std::vector<Var> &,Var * exception)172 Var ScriptableObject::Call(const Var& /*method_name*/,
173                            const std::vector<Var>& /*args*/,
174                            Var* exception) {
175   *exception = Var("Method does not exist to call in ScriptableObject");
176   return Var();
177 }
178 
Construct(const std::vector<Var> &,Var * exception)179 Var ScriptableObject::Construct(const std::vector<Var>& /*args*/,
180                                 Var* exception) {
181   *exception = Var("Construct method does not exist in ScriptableObject");
182   return Var();
183 }
184 
185 // static
GetClass()186 const PPP_Class_Deprecated* ScriptableObject::GetClass() {
187   return &plugin_class;
188 }
189 
190 }  // namespace deprecated
191 
192 }  // namespace pp
193