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.
5 #ifndef CONTENT_RENDERER_PEPPER_HOST_VAR_TRACKER_H_
6 #define CONTENT_RENDERER_PEPPER_HOST_VAR_TRACKER_H_
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 #include "v8/include/v8.h"
23 typedef struct NPObject NPObject
;
34 // Adds NPObject var tracking to the standard PPAPI VarTracker for use in the
36 class HostVarTracker
: public ppapi::VarTracker
{
39 virtual ~HostVarTracker();
41 // Tracks all live NPObjectVar. This is so we can map between instance +
42 // NPObject and get the NPObjectVar corresponding to it. This Add/Remove
43 // function is called by the NPObjectVar when it is created and
45 void AddNPObjectVar(ppapi::NPObjectVar
* object_var
);
46 void RemoveNPObjectVar(ppapi::NPObjectVar
* object_var
);
48 // Looks up a previously registered NPObjectVar for the given NPObject and
49 // instance. Returns NULL if there is no NPObjectVar corresponding to the
50 // given NPObject for the given instance. See AddNPObjectVar above.
51 ppapi::NPObjectVar
* NPObjectVarForNPObject(PP_Instance instance
,
54 // Returns the number of NPObjectVar's associated with the given instance.
55 // Returns 0 if the instance isn't known.
56 CONTENT_EXPORT
int GetLiveNPObjectVarsForInstance(PP_Instance instance
) const;
58 // Tracks all live V8ObjectVar. This is so we can map between instance +
59 // V8Object and get the V8ObjectVar corresponding to it. This Add/Remove
60 // function is called by the V8ObjectVar when it is created and destroyed.
61 void AddV8ObjectVar(ppapi::V8ObjectVar
* object_var
) { NOTIMPLEMENTED(); }
62 void RemoveV8ObjectVar(ppapi::V8ObjectVar
* object_var
) { NOTIMPLEMENTED(); }
63 // Creates or retrieves a V8ObjectVar.
64 PP_Var
V8ObjectVarForV8Object(PP_Instance instance
,
65 v8::Handle
<v8::Object
> object
) {
67 return PP_MakeUndefined();
70 // VarTracker public implementation.
71 virtual PP_Var
MakeResourcePPVarFromMessage(
73 const IPC::Message
& creation_message
,
74 int pending_renderer_id
,
75 int pending_browser_id
) OVERRIDE
;
76 virtual ppapi::ResourceVar
* MakeResourceVar(PP_Resource pp_resource
) OVERRIDE
;
77 virtual void DidDeleteInstance(PP_Instance instance
) OVERRIDE
;
79 virtual int TrackSharedMemoryHandle(PP_Instance instance
,
80 base::SharedMemoryHandle file
,
81 uint32 size_in_bytes
) OVERRIDE
;
82 virtual bool StopTrackingSharedMemoryHandle(int id
,
84 base::SharedMemoryHandle
* handle
,
85 uint32
* size_in_bytes
) OVERRIDE
;
88 // VarTracker private implementation.
89 virtual ppapi::ArrayBufferVar
* CreateArrayBuffer(uint32 size_in_bytes
)
91 virtual ppapi::ArrayBufferVar
* CreateShmArrayBuffer(
93 base::SharedMemoryHandle handle
) OVERRIDE
;
95 // Clear the reference count of the given object and remove it from
97 void ForceReleaseNPObject(ppapi::NPObjectVar
* object_var
);
99 typedef std::map
<NPObject
*, ppapi::NPObjectVar
*> NPObjectToNPObjectVarMap
;
101 // Lists all known NPObjects, first indexed by the corresponding instance,
102 // then by the NPObject*. This allows us to look up an NPObjectVar given
103 // these two pieces of information.
105 // The instance map is lazily managed, so we'll add the
106 // NPObjectToNPObjectVarMap lazily when the first NPObject var is created,
107 // and delete it when it's empty.
108 typedef std::map
<PP_Instance
, linked_ptr
<NPObjectToNPObjectVarMap
> >
110 InstanceMap instance_map_
;
112 // Tracks all shared memory handles used for transmitting array buffers.
113 struct SharedMemoryMapEntry
{
114 PP_Instance instance
;
115 base::SharedMemoryHandle handle
;
116 uint32 size_in_bytes
;
118 typedef std::map
<int, SharedMemoryMapEntry
> SharedMemoryMap
;
119 SharedMemoryMap shared_memory_map_
;
120 uint32_t last_shared_memory_map_id_
;
122 DISALLOW_COPY_AND_ASSIGN(HostVarTracker
);
125 } // namespace content
127 #endif // CONTENT_RENDERER_PEPPER_HOST_VAR_TRACKER_H_