Home | History | Annotate | Download | only in pepper
      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 
     35   PepperPluginInstanceImpl* instance() const { return instance_; }
     36 
     37   const PPP_Class_Deprecated* ppp_class() { return ppp_class_; }
     38   void* ppp_class_data() { return ppp_class_data_; };
     39 
     40   NPObject* GetNPObject() const;
     41 
     42   // Returns true if the given var is an object implemented by the same plugin
     43   // that owns the var object, and that the class matches. If it matches,
     44   // returns true and places the class data into |*ppp_class_data| (which can
     45   // optionally be NULL if no class data is desired).
     46   static bool IsInstanceOf(NPObject* np_object,
     47                            const PPP_Class_Deprecated* ppp_class,
     48                            void** ppp_class_data);
     49 
     50   // Converts the given NPObject to the corresponding ObjectVar.
     51   //
     52   // The given NPObject must be one corresponding to a PluginObject or this
     53   // will crash. If the object is a PluginObject but the plugin has gone
     54   // away (the object could still be alive because of a reference from JS),
     55   // then the return value will be NULL.
     56   static PluginObject* FromNPObject(NPObject* object);
     57 
     58   // Allocates a plugin wrapper object and returns it as an NPObject. This is
     59   // used internally only.
     60   static NPObject* AllocateObjectWrapper();
     61 
     62  private:
     63   struct NPObjectWrapper;
     64 
     65   // This object must be created using the CreateObject function of the which
     66   // will set up the correct NPObject.
     67   //
     68   // The NPObjectWrapper (an NPObject) should already have the reference
     69   // incremented on it, and this class will take ownership of that reference.
     70   PluginObject(PepperPluginInstanceImpl* instance,
     71                NPObjectWrapper* object_wrapper,
     72                const PPP_Class_Deprecated* ppp_class,
     73                void* ppp_class_data);
     74 
     75   PepperPluginInstanceImpl* instance_;
     76 
     77   // Holds a pointer to the NPObject wrapper backing the var. This class
     78   // derives from NPObject and we hold a reference to it, so it must be
     79   // refcounted. When the type is not an object, this value will be NULL.
     80   //
     81   // We don't actually own this pointer, it's the NPObject that actually
     82   // owns us.
     83   NPObjectWrapper* object_wrapper_;
     84 
     85   const PPP_Class_Deprecated* ppp_class_;
     86   void* ppp_class_data_;
     87 
     88   DISALLOW_COPY_AND_ASSIGN(PluginObject);
     89 };
     90 
     91 }  // namespace content
     92 
     93 #endif  // CONTENT_RENDERER_PEPPER_PLUGIN_OBJECT_H_
     94