OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_RENDERER_PEPPER_PLUGIN_OBJECT_H_ | 5 #ifndef CONTENT_RENDERER_PEPPER_PLUGIN_OBJECT_H_ |
6 #define CONTENT_RENDERER_PEPPER_PLUGIN_OBJECT_H_ | 6 #define CONTENT_RENDERER_PEPPER_PLUGIN_OBJECT_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 12 #include "gin/interceptor.h" |
| 13 #include "gin/wrappable.h" |
| 14 #include "ppapi/c/pp_var.h" |
11 | 15 |
12 struct PP_Var; | |
13 struct PPP_Class_Deprecated; | 16 struct PPP_Class_Deprecated; |
14 typedef struct NPObject NPObject; | 17 |
15 typedef struct _NPVariant NPVariant; | 18 namespace gin { |
| 19 class Arguments; |
| 20 } // namespace gin |
16 | 21 |
17 namespace content { | 22 namespace content { |
18 | 23 |
19 class PepperPluginInstanceImpl; | 24 class PepperPluginInstanceImpl; |
20 | 25 |
21 // A PluginObject is a JS-accessible object implemented by the plugin. | 26 // A PluginObject is a JS-accessible object implemented by the plugin. |
22 // | 27 // |
23 // In contrast, a var of type PP_VARTYPE_OBJECT is a reference to a JS object, | 28 // In contrast, a var of type PP_VARTYPE_OBJECT is a reference to a JS object, |
24 // which might be implemented by the plugin (here) or by the JS engine. | 29 // which might be implemented by the plugin (here) or by the JS engine. |
25 class PluginObject { | 30 class PluginObject : public gin::Wrappable<PluginObject>, |
| 31 public gin::NamedPropertyInterceptor, |
| 32 public gin::IndexedPropertyInterceptor { |
26 public: | 33 public: |
| 34 static gin::WrapperInfo kWrapperInfo; |
| 35 |
27 virtual ~PluginObject(); | 36 virtual ~PluginObject(); |
28 | 37 |
| 38 // Returns the PluginObject which is contained in the given v8 object, or NULL |
| 39 // if the object isn't backed by a PluginObject. |
| 40 static PluginObject* FromV8Object(v8::Isolate* isolate, |
| 41 v8::Handle<v8::Object> v8_object); |
| 42 |
29 // Allocates a new PluginObject and returns it as a PP_Var with a | 43 // Allocates a new PluginObject and returns it as a PP_Var with a |
30 // refcount of 1. | 44 // refcount of 1. |
31 static PP_Var Create(PepperPluginInstanceImpl* instance, | 45 static PP_Var Create(PepperPluginInstanceImpl* instance, |
32 const PPP_Class_Deprecated* ppp_class, | 46 const PPP_Class_Deprecated* ppp_class, |
33 void* ppp_class_data); | 47 void* ppp_class_data); |
34 | 48 |
35 PepperPluginInstanceImpl* instance() const { return instance_; } | 49 // gin::NamedPropertyInterceptor |
| 50 virtual v8::Local<v8::Value> GetNamedProperty( |
| 51 v8::Isolate* isolate, |
| 52 const std::string& property) OVERRIDE; |
| 53 virtual void SetNamedProperty(v8::Isolate* isolate, |
| 54 const std::string& property, |
| 55 v8::Local<v8::Value> value) OVERRIDE; |
| 56 virtual std::vector<std::string> EnumerateNamedProperties( |
| 57 v8::Isolate* isolate) OVERRIDE; |
| 58 |
| 59 // gin::IndexedPropertyInterceptor |
| 60 virtual v8::Local<v8::Value> GetIndexedProperty(v8::Isolate* isolate, |
| 61 uint32_t index) OVERRIDE; |
| 62 virtual void SetIndexedProperty(v8::Isolate* isolate, |
| 63 uint32_t index, |
| 64 v8::Local<v8::Value> value) OVERRIDE; |
| 65 virtual std::vector<uint32_t> EnumerateIndexedProperties( |
| 66 v8::Isolate* isolate) OVERRIDE; |
36 | 67 |
37 const PPP_Class_Deprecated* ppp_class() { return ppp_class_; } | 68 const PPP_Class_Deprecated* ppp_class() { return ppp_class_; } |
38 void* ppp_class_data() { | 69 void* ppp_class_data() { return ppp_class_data_; } |
39 return ppp_class_data_; | |
40 }; | |
41 | 70 |
42 NPObject* GetNPObject() const; | 71 // Called when the instance is destroyed. |
43 | 72 void InstanceDeleted(); |
44 // Returns true if the given var is an object implemented by the same plugin | |
45 // that owns the var object, and that the class matches. If it matches, | |
46 // returns true and places the class data into |*ppp_class_data| (which can | |
47 // optionally be NULL if no class data is desired). | |
48 static bool IsInstanceOf(NPObject* np_object, | |
49 const PPP_Class_Deprecated* ppp_class, | |
50 void** ppp_class_data); | |
51 | |
52 // Converts the given NPObject to the corresponding ObjectVar. | |
53 // | |
54 // The given NPObject must be one corresponding to a PluginObject or this | |
55 // will crash. If the object is a PluginObject but the plugin has gone | |
56 // away (the object could still be alive because of a reference from JS), | |
57 // then the return value will be NULL. | |
58 static PluginObject* FromNPObject(NPObject* object); | |
59 | |
60 // Allocates a plugin wrapper object and returns it as an NPObject. This is | |
61 // used internally only. | |
62 static NPObject* AllocateObjectWrapper(); | |
63 | 73 |
64 private: | 74 private: |
65 struct NPObjectWrapper; | |
66 | |
67 // This object must be created using the CreateObject function of the which | |
68 // will set up the correct NPObject. | |
69 // | |
70 // The NPObjectWrapper (an NPObject) should already have the reference | |
71 // incremented on it, and this class will take ownership of that reference. | |
72 PluginObject(PepperPluginInstanceImpl* instance, | 75 PluginObject(PepperPluginInstanceImpl* instance, |
73 NPObjectWrapper* object_wrapper, | |
74 const PPP_Class_Deprecated* ppp_class, | 76 const PPP_Class_Deprecated* ppp_class, |
75 void* ppp_class_data); | 77 void* ppp_class_data); |
76 | 78 |
| 79 // gin::Wrappable |
| 80 virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder( |
| 81 v8::Isolate* isolate) OVERRIDE; |
| 82 |
| 83 // Helper methods for Get/Set/Enumerate Named/Indexed properties. |
| 84 v8::Local<v8::Value> GetPropertyOrMethod(v8::Isolate* isolate, |
| 85 PP_Var identifier_var); |
| 86 void SetProperty(v8::Isolate* isolate, |
| 87 PP_Var identifier_var, |
| 88 v8::Handle<v8::Value> value); |
| 89 // Returns a pair with a vector of named properties and a vector of indexed |
| 90 // properties. |
| 91 std::pair<std::vector<std::string>, std::vector<uint32_t> > |
| 92 EnumerateProperties(v8::Isolate* isolate); |
| 93 |
| 94 void Call(const std::string& identifier, gin::Arguments* args); |
| 95 |
77 PepperPluginInstanceImpl* instance_; | 96 PepperPluginInstanceImpl* instance_; |
78 | 97 |
79 // Holds a pointer to the NPObject wrapper backing the var. This class | |
80 // derives from NPObject and we hold a reference to it, so it must be | |
81 // refcounted. When the type is not an object, this value will be NULL. | |
82 // | |
83 // We don't actually own this pointer, it's the NPObject that actually | |
84 // owns us. | |
85 NPObjectWrapper* object_wrapper_; | |
86 | |
87 const PPP_Class_Deprecated* ppp_class_; | 98 const PPP_Class_Deprecated* ppp_class_; |
88 void* ppp_class_data_; | 99 void* ppp_class_data_; |
89 | 100 |
| 101 base::WeakPtrFactory<PluginObject> weak_factory_; |
| 102 |
90 DISALLOW_COPY_AND_ASSIGN(PluginObject); | 103 DISALLOW_COPY_AND_ASSIGN(PluginObject); |
91 }; | 104 }; |
92 | 105 |
93 } // namespace content | 106 } // namespace content |
94 | 107 |
95 #endif // CONTENT_RENDERER_PEPPER_PLUGIN_OBJECT_H_ | 108 #endif // CONTENT_RENDERER_PEPPER_PLUGIN_OBJECT_H_ |
OLD | NEW |