Pepper_47_C++_interfaces
scriptable_object_deprecated.cc
Go to the documentation of this file.
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 
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:
22  ExceptionConverter(PP_Var* out) : out_(out) {
23  }
24  ~ExceptionConverter() {
25  if (!exception_.is_undefined())
26  *out_ = exception_.Detach();
27  }
28 
29  Var* Get() { return &exception_; }
30 
31  private:
34 };
35 
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]));
41 }
42 
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 
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 
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 
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*>(memory_if->MemAlloc(
77  static_cast<uint32_t>(sizeof(PP_Var) * props.size())));
78 
79  for (size_t i = 0; i < props.size(); ++i)
80  (*properties)[i] = props[i].Detach();
81 }
82 
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 
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 
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 
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 
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 
143 bool ScriptableObject::HasProperty(const Var& /*name*/, Var* /*exception*/) {
144  return false;
145 }
146 
147 bool ScriptableObject::HasMethod(const Var& /*name*/, Var* /*exception*/) {
148  return false;
149 }
150 
151 Var ScriptableObject::GetProperty(const Var& /*name*/, Var* exception) {
152  *exception = Var("Property does not exist on ScriptableObject");
153  return Var();
154 }
155 
156 void ScriptableObject::GetAllPropertyNames(std::vector<Var>* /*properties*/,
157  Var* /*exception*/) {
158 }
159 
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 
167  Var* exception) {
168  *exception = Var(
169  "Property does does not exist to be removed in ScriptableObject");
170 }
171 
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 
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
186 const PPP_Class_Deprecated* ScriptableObject::GetClass() {
187  return &plugin_class;
188 }
189 
190 } // namespace deprecated
191 
192 } // namespace pp
virtual bool HasMethod(const Var &name, Var *exception)
virtual Var Call(const Var &method_name, const std::vector< Var > &args, Var *exception)
const void * GetBrowserInterface(const char *interface_name)
Definition: module.cc:179
virtual Var Construct(const std::vector< Var > &args, Var *exception)
virtual void RemoveProperty(const Var &name, Var *exception)
virtual Var GetProperty(const Var &name, Var *exception)
A generic type used for passing data types between the module and the page.
Definition: var.h:21
virtual bool HasProperty(const Var &name, Var *exception)
static Module * Get()
virtual void GetAllPropertyNames(std::vector< Var > *properties, Var *exception)
virtual void SetProperty(const Var &name, const Var &value, Var *exception)