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"
22 typedef struct NPObject NPObject;
32 // Adds NPObject var tracking to the standard PPAPI VarTracker for use in the
34 class HostVarTracker : public ppapi::VarTracker {
37 virtual ~HostVarTracker();
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
43 void AddNPObjectVar(ppapi::NPObjectVar* object_var);
44 void RemoveNPObjectVar(ppapi::NPObjectVar* object_var);
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,
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;
57 // VarTracker public implementation.
58 virtual PP_Var MakeResourcePPVarFromMessage(
60 const IPC::Message& creation_message,
61 int pending_renderer_id,
62 int pending_browser_id) OVERRIDE;
63 virtual ppapi::ResourceVar* MakeResourceVar(PP_Resource pp_resource) OVERRIDE;
64 virtual void DidDeleteInstance(PP_Instance instance) OVERRIDE;
66 virtual int TrackSharedMemoryHandle(PP_Instance instance,
67 base::SharedMemoryHandle file,
68 uint32 size_in_bytes) OVERRIDE;
69 virtual bool StopTrackingSharedMemoryHandle(int id,
71 base::SharedMemoryHandle* handle,
72 uint32* size_in_bytes) OVERRIDE;
75 // VarTracker private implementation.
76 virtual ppapi::ArrayBufferVar* CreateArrayBuffer(
77 uint32 size_in_bytes) OVERRIDE;
78 virtual ppapi::ArrayBufferVar* CreateShmArrayBuffer(
79 uint32 size_in_bytes, base::SharedMemoryHandle handle) OVERRIDE;
81 // Clear the reference count of the given object and remove it from
83 void ForceReleaseNPObject(ppapi::NPObjectVar* object_var);
85 typedef std::map<NPObject*, ppapi::NPObjectVar*> NPObjectToNPObjectVarMap;
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.
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> >
96 InstanceMap instance_map_;
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;
104 typedef std::map<int, SharedMemoryMapEntry> SharedMemoryMap;
105 SharedMemoryMap shared_memory_map_;
106 uint32_t last_shared_memory_map_id_;
108 DISALLOW_COPY_AND_ASSIGN(HostVarTracker);
111 } // namespace content
113 #endif // CONTENT_RENDERER_PEPPER_HOST_VAR_TRACKER_H_