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 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_RENDERER_PEPPER_PLUGIN_OBJECT_H_ 6 #define CONTENT_RENDERER_PEPPER_PLUGIN_OBJECT_H_ 7 8 #include <string> 9 10 #include "base/basictypes.h" 11 12 struct PP_Var; 13 struct PPP_Class_Deprecated; 14 typedef struct NPObject NPObject; 15 typedef struct _NPVariant NPVariant; 16 17 namespace content { 18 19 class PepperPluginInstanceImpl; 20 21 // A PluginObject is a JS-accessible object implemented by the plugin. 22 // 23 // 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. 25 class PluginObject { 26 public: 27 virtual ~PluginObject(); 28 29 // Allocates a new PluginObject and returns it as a PP_Var with a 30 // refcount of 1. 31 static PP_Var Create(PepperPluginInstanceImpl* instance, 32 const PPP_Class_Deprecated* ppp_class, 33 void* ppp_class_data); 34 instance()35 PepperPluginInstanceImpl* instance() const { return instance_; } 36 ppp_class()37 const PPP_Class_Deprecated* ppp_class() { return ppp_class_; } ppp_class_data()38 void* ppp_class_data() { 39 return ppp_class_data_; 40 }; 41 42 NPObject* GetNPObject() const; 43 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 64 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, 73 NPObjectWrapper* object_wrapper, 74 const PPP_Class_Deprecated* ppp_class, 75 void* ppp_class_data); 76 77 PepperPluginInstanceImpl* instance_; 78 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_; 88 void* ppp_class_data_; 89 90 DISALLOW_COPY_AND_ASSIGN(PluginObject); 91 }; 92 93 } // namespace content 94 95 #endif // CONTENT_RENDERER_PEPPER_PLUGIN_OBJECT_H_ 96