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