summaryrefslogtreecommitdiffstats
path: root/webkit/plugins/ppapi/var_object_class.cc
blob: 282ad722052062edc0fd40f2c6ec25e54d0ffcc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "webkit/plugins/ppapi/var_object_class.h"

#include "third_party/WebKit/WebKit/chromium/public/WebBindings.h"
#include "webkit/plugins/ppapi/npapi_glue.h"
#include "webkit/plugins/ppapi/resource_tracker.h"
#include "webkit/plugins/ppapi/var.h"

using WebKit::WebBindings;

namespace webkit {
namespace ppapi {

namespace {

// VarObjectAccessorWithIdentifier ---------------------------------------------

// Helper class for the new (PPB_Class) NPObject wrapper. This converts a call
// from WebKit where it gives us an NPObject and an NPIdentifier to an
// easily-accessible InstanceData (corresponding to the NPObject) and
// std::string and Property (corresponding to the NPIdentifier).
class VarObjectAccessorWithIdentifier {
 public:
  VarObjectAccessorWithIdentifier(NPObject* object, NPIdentifier identifier)
      : exists_(false),
        instance_(static_cast<VarObjectClass::InstanceData*>(object)),
        property_(NULL) {
    if (instance_) {
      const NPUTF8* string_value = NULL;
      int32_t int_value = 0;
      bool is_string = false;
      WebBindings::extractIdentifierData(identifier, string_value, int_value,
                                         is_string);
      if (is_string) {
        property_name_ = string_value;

        const VarObjectClass::PropertyMap& properties =
            instance_->object_class->properties();
        VarObjectClass::PropertyMap::const_iterator it =
            properties.find(property_name_);
        if (it != properties.end()) {
          property_ = &it->second;
          exists_ = true;
        }
      }
    }
  }

  // Return true if the object is valid, the identifier is valid, and the
  // property with said name exists.
  bool exists() const { return exists_; }
  bool is_method() const { return exists() && property_->method; }
  bool is_readable() const { return exists() && property_->getter; }
  bool is_writable() const {
    return exists() && property_->setter && property_->writable;
  }
  const VarObjectClass::InstanceData* instance() const { return instance_; }
  const VarObjectClass::Property* property() const { return property_; }
  PluginModule* module() const {
    return instance_ ? instance_->object_class->module() : NULL;
  }

 private:
  bool exists_;
  const VarObjectClass::InstanceData* instance_;
  std::string property_name_;
  const VarObjectClass::Property* property_;

  DISALLOW_COPY_AND_ASSIGN(VarObjectAccessorWithIdentifier);
};

NPObject* VarObjectClassAllocate(NPP npp, NPClass* the_class) {
  return new VarObjectClass::InstanceData;
}

void VarObjectClassDeallocate(NPObject* object) {
  VarObjectClass::InstanceData* instance =
      static_cast<VarObjectClass::InstanceData*>(object);
  if (instance->object_class->instance_native_destructor())
    instance->object_class->instance_native_destructor()(instance->native_data);
  delete instance;
}

bool VarObjectClassHasMethod(NPObject* np_obj, NPIdentifier name) {
  VarObjectAccessorWithIdentifier accessor(np_obj, name);
  return accessor.is_method();
}

bool VarObjectClassInvoke(NPObject* np_obj, NPIdentifier name,
                          const NPVariant* args, uint32 arg_count,
                          NPVariant* result) {
  VarObjectAccessorWithIdentifier accessor(np_obj, name);
  if (!accessor.is_method())
    return false;

  PPResultAndExceptionToNPResult result_converter(np_obj, result);
  PPVarArrayFromNPVariantArray arguments(accessor.module(), arg_count, args);
  PPVarFromNPObject self(accessor.module(), np_obj);

  return result_converter.SetResult(accessor.property()->method(
    accessor.instance()->native_data, self.var(), arguments.array(), arg_count,
    result_converter.exception()));
}

bool VarObjectClassInvokeDefault(NPObject* np_obj,
                                 const NPVariant* args,
                                 uint32 arg_count,
                                 NPVariant* result) {
  VarObjectClass::InstanceData* instance =
      static_cast<VarObjectClass::InstanceData*>(np_obj);
  if (!instance || !instance->object_class->instance_invoke())
    return false;

  PPResultAndExceptionToNPResult result_converter(np_obj, result);
  PPVarArrayFromNPVariantArray arguments(instance->object_class->module(),
                                         arg_count, args);
  PPVarFromNPObject self(instance->object_class->module(), np_obj);

  return result_converter.SetResult(instance->object_class->instance_invoke()(
      instance->native_data, self.var(), arguments.array(), arg_count,
      result_converter.exception()));
}

bool VarObjectClassHasProperty(NPObject* np_obj, NPIdentifier name) {
  VarObjectAccessorWithIdentifier accessor(np_obj, name);
  return accessor.is_readable();
}

bool VarObjectClassGetProperty(NPObject* np_obj, NPIdentifier name,
                               NPVariant* result) {
  VarObjectAccessorWithIdentifier accessor(np_obj, name);
  if (!accessor.is_readable()) {
    return false;
  }

  PPResultAndExceptionToNPResult result_converter(np_obj, result);
  PPVarFromNPObject self(accessor.module(), np_obj);

  return result_converter.SetResult(accessor.property()->getter(
    accessor.instance()->native_data, self.var(), 0, 0,
    result_converter.exception()));
}

bool VarObjectClassSetProperty(NPObject* np_obj, NPIdentifier name,
                     const NPVariant* variant) {
  VarObjectAccessorWithIdentifier accessor(np_obj, name);
  if (!accessor.is_writable()) {
    return false;
  }

  PPResultAndExceptionToNPResult result_converter(np_obj, NULL);
  PPVarArrayFromNPVariantArray arguments(accessor.module(), 1, variant);
  PPVarFromNPObject self(accessor.module(), np_obj);

  // Ignore return value.
  Var::PluginReleasePPVar(accessor.property()->setter(
      accessor.instance()->native_data, self.var(), arguments.array(), 1,
      result_converter.exception()));

  return result_converter.CheckExceptionForNoResult();
}

bool VarObjectClassEnumerate(NPObject *np_obj, NPIdentifier **value,
                             uint32_t *count) {
  VarObjectClass::InstanceData* instance =
      static_cast<VarObjectClass::InstanceData*>(np_obj);
  *count = 0;
  *value = NULL;
  if (!instance)
    return false;

  const VarObjectClass::PropertyMap& properties =
      instance->object_class->properties();

  // Don't bother calculating the size of enumerable properties, just allocate
  // enough for all and then fill it partially.
  *value = static_cast<NPIdentifier*>(
      malloc(sizeof(NPIdentifier) * properties.size()));

  NPIdentifier* inserter = *value;
  for (VarObjectClass::PropertyMap::const_iterator i = properties.begin();
      i != properties.end(); ++i)
    if (i->second.enumerable)
      *inserter++ = WebBindings::getStringIdentifier(i->first.c_str());

  *count = inserter - *value;
  return true;
}

NPClass objectclassvar_class = {
  NP_CLASS_STRUCT_VERSION,
  &VarObjectClassAllocate,
  &VarObjectClassDeallocate,
  NULL,
  &VarObjectClassHasMethod,
  &VarObjectClassInvoke,
  &VarObjectClassInvokeDefault,
  &VarObjectClassHasProperty,
  &VarObjectClassGetProperty,
  &VarObjectClassSetProperty,
  NULL,
  &VarObjectClassEnumerate,
};

// PPB_Class -------------------------------------------------------------------

PP_Resource Create(PP_Module module, PP_ClassDestructor destruct,
                   PP_ClassFunction invoke, PP_ClassProperty* properties) {
  PluginModule* plugin_module = ResourceTracker::Get()->GetModule(module);
  if (!properties || !plugin_module)
    return 0;
  scoped_refptr<VarObjectClass> cls = new VarObjectClass(plugin_module,
                                                         destruct,
                                                         invoke,
                                                         properties);
  if (!cls)
    return 0;
  return cls->GetReference();
}

PP_Var Instantiate(PP_Resource class_object, void* native_data,
                   PP_Var* exception) {
  scoped_refptr<VarObjectClass> object_class =
      Resource::GetAs<VarObjectClass>(class_object);
  if (!object_class)
    return PP_MakeUndefined();
  NPObject* obj = WebBindings::createObject(NULL, &objectclassvar_class);
  VarObjectClass::InstanceData* instance_data =
      static_cast<VarObjectClass::InstanceData*>(obj);
  instance_data->object_class = object_class;
  instance_data->native_data = native_data;
  return ObjectVar::NPObjectToPPVar(object_class->module(), obj);
}

}  // namespace

// VarObjectClass --------------------------------------------------------------

VarObjectClass::Property::Property(const PP_ClassProperty& prop)
    : method(prop.method),
      getter(prop.getter),
      setter(prop.setter),
      writable(!(prop.modifiers & PP_OBJECTPROPERTY_MODIFIER_READONLY)),
      enumerable(!(prop.modifiers & PP_OBJECTPROPERTY_MODIFIER_DONTENUM)) {
}

VarObjectClass::InstanceData::InstanceData() : native_data(NULL) {
}

VarObjectClass::VarObjectClass(PluginModule* module,
                               PP_ClassDestructor destruct,
                               PP_ClassFunction invoke,
                               PP_ClassProperty* properties)
    : Resource(module),
      instance_native_destructor_(destruct),
      instance_invoke_(invoke) {
  PP_ClassProperty* prop = properties;
  while (prop->name) {
    properties_.insert(std::make_pair(std::string(prop->name),
                                      Property(*prop)));
    ++prop;
  }
}

// virtual
VarObjectClass::~VarObjectClass() {
}

VarObjectClass* VarObjectClass::AsVarObjectClass() {
  return this;
}

// static
const PPB_Class* VarObjectClass::GetInterface() {
  static PPB_Class interface = {
    &Create,
    &Instantiate
  };
  return &interface;
}

}  // namespace ppapi
}  // namespace webkit