Home | History | Annotate | Download | only in pepper
      1 // Copyright (c) 2012 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_HOST_VAR_TRACKER_H_
      6 #define CONTENT_RENDERER_PEPPER_HOST_VAR_TRACKER_H_
      7 
      8 #include <map>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/compiler_specific.h"
     12 #include "base/containers/hash_tables.h"
     13 #include "base/gtest_prod_util.h"
     14 #include "base/memory/linked_ptr.h"
     15 #include "base/memory/ref_counted.h"
     16 #include "content/common/content_export.h"
     17 #include "ppapi/c/pp_instance.h"
     18 #include "ppapi/shared_impl/host_resource.h"
     19 #include "ppapi/shared_impl/resource_tracker.h"
     20 #include "ppapi/shared_impl/var_tracker.h"
     21 
     22 typedef struct NPObject NPObject;
     23 
     24 namespace ppapi {
     25 class ArrayBufferVar;
     26 class NPObjectVar;
     27 class Var;
     28 }
     29 
     30 namespace content {
     31 
     32 // Adds NPObject var tracking to the standard PPAPI VarTracker for use in the
     33 // renderer.
     34 class HostVarTracker : public ppapi::VarTracker {
     35  public:
     36   HostVarTracker();
     37   virtual ~HostVarTracker();
     38 
     39   // Tracks all live NPObjectVar. This is so we can map between instance +
     40   // NPObject and get the NPObjectVar corresponding to it. This Add/Remove
     41   // function is called by the NPObjectVar when it is created and
     42   // destroyed.
     43   void AddNPObjectVar(ppapi::NPObjectVar* object_var);
     44   void RemoveNPObjectVar(ppapi::NPObjectVar* object_var);
     45 
     46   // Looks up a previously registered NPObjectVar for the given NPObject and
     47   // instance. Returns NULL if there is no NPObjectVar corresponding to the
     48   // given NPObject for the given instance. See AddNPObjectVar above.
     49   ppapi::NPObjectVar* NPObjectVarForNPObject(PP_Instance instance,
     50                                              NPObject* np_object);
     51 
     52   // Returns the number of NPObjectVar's associated with the given instance.
     53   // Returns 0 if the instance isn't known.
     54   CONTENT_EXPORT int GetLiveNPObjectVarsForInstance(
     55       PP_Instance instance) const;
     56 
     57   // VarTracker public implementation.
     58   virtual void DidDeleteInstance(PP_Instance instance) OVERRIDE;
     59 
     60   virtual int TrackSharedMemoryHandle(PP_Instance instance,
     61                                       base::SharedMemoryHandle file,
     62                                       uint32 size_in_bytes) OVERRIDE;
     63   virtual bool StopTrackingSharedMemoryHandle(int id,
     64                                               PP_Instance instance,
     65                                               base::SharedMemoryHandle* handle,
     66                                               uint32* size_in_bytes) OVERRIDE;
     67 
     68  private:
     69   // VarTracker private implementation.
     70   virtual ppapi::ArrayBufferVar* CreateArrayBuffer(
     71       uint32 size_in_bytes) OVERRIDE;
     72   virtual ppapi::ArrayBufferVar* CreateShmArrayBuffer(
     73       uint32 size_in_bytes, base::SharedMemoryHandle handle) OVERRIDE;
     74 
     75   // Clear the reference count of the given object and remove it from
     76   // live_vars_.
     77   void ForceReleaseNPObject(ppapi::NPObjectVar* object_var);
     78 
     79   typedef std::map<NPObject*, ppapi::NPObjectVar*> NPObjectToNPObjectVarMap;
     80 
     81   // Lists all known NPObjects, first indexed by the corresponding instance,
     82   // then by the NPObject*. This allows us to look up an NPObjectVar given
     83   // these two pieces of information.
     84   //
     85   // The instance map is lazily managed, so we'll add the
     86   // NPObjectToNPObjectVarMap lazily when the first NPObject var is created,
     87   // and delete it when it's empty.
     88   typedef std::map<PP_Instance, linked_ptr<NPObjectToNPObjectVarMap> >
     89       InstanceMap;
     90   InstanceMap instance_map_;
     91 
     92   // Tracks all shared memory handles used for transmitting array buffers.
     93   struct SharedMemoryMapEntry {
     94     PP_Instance instance;
     95     base::SharedMemoryHandle handle;
     96     uint32 size_in_bytes;
     97   };
     98   typedef std::map<int, SharedMemoryMapEntry> SharedMemoryMap;
     99   SharedMemoryMap shared_memory_map_;
    100   uint32_t last_shared_memory_map_id_;
    101 
    102   DISALLOW_COPY_AND_ASSIGN(HostVarTracker);
    103 };
    104 
    105 }  // namespace content
    106 
    107 #endif  // CONTENT_RENDERER_PEPPER_HOST_VAR_TRACKER_H_
    108