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(PP_Instance instance) const;
     55 
     56   // VarTracker public implementation.
     57   virtual PP_Var MakeResourcePPVarFromMessage(
     58       PP_Instance instance,
     59       const IPC::Message& creation_message,
     60       int pending_renderer_id,
     61       int pending_browser_id) OVERRIDE;
     62   virtual ppapi::ResourceVar* MakeResourceVar(PP_Resource pp_resource) OVERRIDE;
     63   virtual void DidDeleteInstance(PP_Instance instance) OVERRIDE;
     64 
     65   virtual int TrackSharedMemoryHandle(PP_Instance instance,
     66                                       base::SharedMemoryHandle file,
     67                                       uint32 size_in_bytes) OVERRIDE;
     68   virtual bool StopTrackingSharedMemoryHandle(int id,
     69                                               PP_Instance instance,
     70                                               base::SharedMemoryHandle* handle,
     71                                               uint32* size_in_bytes) OVERRIDE;
     72 
     73  private:
     74   // VarTracker private implementation.
     75   virtual ppapi::ArrayBufferVar* CreateArrayBuffer(uint32 size_in_bytes)
     76       OVERRIDE;
     77   virtual ppapi::ArrayBufferVar* CreateShmArrayBuffer(
     78       uint32 size_in_bytes,
     79       base::SharedMemoryHandle handle) OVERRIDE;
     80 
     81   // Clear the reference count of the given object and remove it from
     82   // live_vars_.
     83   void ForceReleaseNPObject(ppapi::NPObjectVar* object_var);
     84 
     85   typedef std::map<NPObject*, ppapi::NPObjectVar*> NPObjectToNPObjectVarMap;
     86 
     87   // Lists all known NPObjects, first indexed by the corresponding instance,
     88   // then by the NPObject*. This allows us to look up an NPObjectVar given
     89   // these two pieces of information.
     90   //
     91   // The instance map is lazily managed, so we'll add the
     92   // NPObjectToNPObjectVarMap lazily when the first NPObject var is created,
     93   // and delete it when it's empty.
     94   typedef std::map<PP_Instance, linked_ptr<NPObjectToNPObjectVarMap> >
     95       InstanceMap;
     96   InstanceMap instance_map_;
     97 
     98   // Tracks all shared memory handles used for transmitting array buffers.
     99   struct SharedMemoryMapEntry {
    100     PP_Instance instance;
    101     base::SharedMemoryHandle handle;
    102     uint32 size_in_bytes;
    103   };
    104   typedef std::map<int, SharedMemoryMapEntry> SharedMemoryMap;
    105   SharedMemoryMap shared_memory_map_;
    106   uint32_t last_shared_memory_map_id_;
    107 
    108   DISALLOW_COPY_AND_ASSIGN(HostVarTracker);
    109 };
    110 
    111 }  // namespace content
    112 
    113 #endif  // CONTENT_RENDERER_PEPPER_HOST_VAR_TRACKER_H_
    114