OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 O3D_GPU_PLUGIN_NP_UTILS_BASE_NP_OBJECT_H_ | 5 #ifndef O3D_GPU_PLUGIN_NP_UTILS_NP_CLASS_H_ |
6 #define O3D_GPU_PLUGIN_NP_UTILS_BASE_NP_OBJECT_H_ | 6 #define O3D_GPU_PLUGIN_NP_UTILS_NP_CLASS_H_ |
7 | 7 |
| 8 #include "o3d/gpu_plugin/np_utils/np_object_pointer.h" |
8 #include "third_party/npapi/bindings/npapi.h" | 9 #include "third_party/npapi/bindings/npapi.h" |
9 #include "third_party/npapi/bindings/npruntime.h" | 10 #include "third_party/npapi/bindings/npruntime.h" |
10 | 11 |
| 12 // This file implements NPGetClass<T>. This function returns an NPClass |
| 13 // that can be used to instantiate an NPObject subclass T. The NPClass |
| 14 // function pointers will invoke the most derived corresponding member |
| 15 // functions in T. |
| 16 |
11 namespace o3d { | 17 namespace o3d { |
12 namespace gpu_plugin { | 18 namespace gpu_plugin { |
13 | 19 |
14 // This class is a base class for NPObjects implemented in C++. It forwards | 20 namespace np_class_impl { |
15 // The NPObject calls through to virtual functions implemented by the subclass. | |
16 class BaseNPObject : public NPObject { | |
17 public: | |
18 // Returns the NPClass for the concrete BaseNPObject subclass T. | |
19 template <typename NPObjectType> | |
20 static const NPClass* GetNPClass(); | |
21 | |
22 NPP npp() const { | |
23 return npp_; | |
24 } | |
25 | |
26 // Override these in subclass. | |
27 virtual void Invalidate(); | |
28 | |
29 virtual bool HasMethod(NPIdentifier name); | |
30 | |
31 virtual bool Invoke(NPIdentifier name, | |
32 const NPVariant* args, | |
33 uint32_t num_args, | |
34 NPVariant* result); | |
35 | |
36 virtual bool InvokeDefault(const NPVariant* args, | |
37 uint32_t num_args, | |
38 NPVariant* result); | |
39 | |
40 virtual bool HasProperty(NPIdentifier name); | |
41 | |
42 virtual bool GetProperty(NPIdentifier name, NPVariant* result); | |
43 | |
44 virtual bool SetProperty(NPIdentifier name, const NPVariant* value); | |
45 | |
46 virtual bool RemoveProperty(NPIdentifier name); | |
47 | |
48 virtual bool Enumerate(NPIdentifier** names, | |
49 uint32_t* count); | |
50 | |
51 virtual bool Construct(const NPVariant* args, | |
52 uint32_t num_args, | |
53 NPVariant* result); | |
54 | |
55 protected: | |
56 | |
57 explicit BaseNPObject(NPP npp); | |
58 virtual ~BaseNPObject(); | |
59 | |
60 private: | |
61 // This template version of the NPClass allocate function creates a subclass | 21 // This template version of the NPClass allocate function creates a subclass |
62 // of BaseNPObject. | 22 // of BaseNPObject. |
63 template <typename NPObjectType> | 23 template <typename NPObjectType> |
64 static NPObject* AllocateImpl(NPP npp, NPClass*) { | 24 static NPObject* Allocate(NPP npp, NPClass*) { |
65 return new NPObjectType(npp); | 25 return new NPObjectType(npp); |
66 } | 26 } |
67 | 27 |
68 // These implementations of the NPClass functions forward to the virtual | 28 // These implementations of the NPClass functions forward to the virtual |
69 // functions in BaseNPObject. | 29 // functions in DefaultNPObject. |
70 static void DeallocateImpl(NPObject* object); | 30 template <typename NPObjectType> |
| 31 static void Deallocate(NPObject* object) { |
| 32 delete static_cast<NPObjectType*>(object); |
| 33 } |
71 | 34 |
72 static void InvalidateImpl(NPObject* object); | 35 template <typename NPObjectType> |
| 36 static void Invalidate(NPObject* object) { |
| 37 return static_cast<NPObjectType*>(object)->Invalidate(); |
| 38 } |
73 | 39 |
74 static bool HasMethodImpl(NPObject* object, NPIdentifier name); | 40 template <typename NPObjectType> |
| 41 static bool HasMethod(NPObject* object, NPIdentifier name) { |
| 42 return static_cast<NPObjectType*>(object)->HasMethod(name); |
| 43 } |
75 | 44 |
76 static bool InvokeImpl(NPObject* object, | 45 template <typename NPObjectType> |
77 NPIdentifier name, | 46 static bool Invoke(NPObject* object, |
78 const NPVariant* args, | 47 NPIdentifier name, |
79 uint32_t num_args, | 48 const NPVariant* args, |
80 NPVariant* result); | 49 uint32_t num_args, |
| 50 NPVariant* result) { |
| 51 return static_cast<NPObjectType*>(object)->Invoke( |
| 52 name, args, num_args, result); |
| 53 } |
81 | 54 |
82 static bool InvokeDefaultImpl(NPObject* object, | 55 template <typename NPObjectType> |
83 const NPVariant* args, | 56 static bool InvokeDefault(NPObject* object, |
84 uint32_t num_args, | |
85 NPVariant* result); | |
86 | |
87 static bool HasPropertyImpl(NPObject* object, NPIdentifier name); | |
88 | |
89 static bool GetPropertyImpl(NPObject* object, | |
90 NPIdentifier name, | |
91 NPVariant* result); | |
92 | |
93 static bool SetPropertyImpl(NPObject* object, | |
94 NPIdentifier name, | |
95 const NPVariant* value); | |
96 | |
97 static bool RemovePropertyImpl(NPObject* object, NPIdentifier name); | |
98 | |
99 static bool EnumerateImpl(NPObject* object, | |
100 NPIdentifier** names, | |
101 uint32_t* count); | |
102 | |
103 static bool ConstructImpl(NPObject* object, | |
104 const NPVariant* args, | 57 const NPVariant* args, |
105 uint32_t num_args, | 58 uint32_t num_args, |
106 NPVariant* result); | 59 NPVariant* result) { |
| 60 return static_cast<NPObjectType*>(object)->InvokeDefault( |
| 61 args, num_args, result); |
| 62 } |
107 | 63 |
108 NPP npp_; | 64 template <typename NPObjectType> |
| 65 static bool HasProperty(NPObject* object, NPIdentifier name) { |
| 66 return static_cast<NPObjectType*>(object)->HasProperty(name); |
| 67 } |
109 | 68 |
110 DISALLOW_COPY_AND_ASSIGN(BaseNPObject); | 69 template <typename NPObjectType> |
111 }; | 70 static bool GetProperty(NPObject* object, |
| 71 NPIdentifier name, |
| 72 NPVariant* result) { |
| 73 return static_cast<NPObjectType*>(object)->GetProperty(name, result); |
| 74 } |
| 75 |
| 76 template <typename NPObjectType> |
| 77 static bool SetProperty(NPObject* object, |
| 78 NPIdentifier name, |
| 79 const NPVariant* value) { |
| 80 return static_cast<NPObjectType*>(object)->SetProperty(name, value); |
| 81 } |
| 82 |
| 83 template <typename NPObjectType> |
| 84 static bool RemoveProperty(NPObject* object, NPIdentifier name) { |
| 85 return static_cast<NPObjectType*>(object)->RemoveProperty(name); |
| 86 } |
| 87 |
| 88 template <typename NPObjectType> |
| 89 static bool Enumerate(NPObject* object, |
| 90 NPIdentifier** names, |
| 91 uint32_t* count) { |
| 92 return static_cast<NPObjectType*>(object)->Enumerate(names, count); |
| 93 }; |
| 94 |
| 95 template <typename NPObjectType> |
| 96 static bool Construct(NPObject* object, |
| 97 const NPVariant* args, |
| 98 uint32_t num_args, |
| 99 NPVariant* result) { |
| 100 return static_cast<NPObjectType*>(object)->Construct( |
| 101 args, num_args, result); |
| 102 } |
| 103 } // namespace np_class_impl; |
112 | 104 |
113 template <typename NPObjectType> | 105 template <typename NPObjectType> |
114 const NPClass* BaseNPObject::GetNPClass() { | 106 const NPClass* NPGetClass() { |
115 static NPClass np_class = { | 107 static const NPClass np_class = { |
116 NP_CLASS_STRUCT_VERSION, | 108 NP_CLASS_STRUCT_VERSION, |
117 AllocateImpl<NPObjectType>, | 109 np_class_impl::Allocate<NPObjectType>, |
118 DeallocateImpl, | 110 np_class_impl::Deallocate<NPObjectType>, |
119 InvalidateImpl, | 111 np_class_impl::Invalidate<NPObjectType>, |
120 HasMethodImpl, | 112 np_class_impl::HasMethod<NPObjectType>, |
121 InvokeImpl, | 113 np_class_impl::Invoke<NPObjectType>, |
122 InvokeDefaultImpl, | 114 np_class_impl::InvokeDefault<NPObjectType>, |
123 HasPropertyImpl, | 115 np_class_impl::HasProperty<NPObjectType>, |
124 GetPropertyImpl, | 116 np_class_impl::GetProperty<NPObjectType>, |
125 SetPropertyImpl, | 117 np_class_impl::SetProperty<NPObjectType>, |
126 RemovePropertyImpl, | 118 np_class_impl::RemoveProperty<NPObjectType>, |
127 EnumerateImpl, | 119 np_class_impl::Enumerate<NPObjectType>, |
128 ConstructImpl, | 120 np_class_impl::Construct<NPObjectType>, |
129 }; | 121 }; |
130 return &np_class; | 122 return &np_class; |
131 }; | 123 }; |
| 124 |
132 } // namespace gpu_plugin | 125 } // namespace gpu_plugin |
133 } // namespace o3d | 126 } // namespace o3d |
134 | 127 |
135 #endif // O3D_GPU_PLUGIN_NP_UTILS_BASE_NP_OBJECT_H_ | 128 #endif // O3D_GPU_PLUGIN_NP_UTILS_NP_CLASS_H_ |
OLD | NEW |