OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_RENDERER_PEPPER_HOST_VAR_TRACKER_H_ | 5 #ifndef CONTENT_RENDERER_PEPPER_HOST_VAR_TRACKER_H_ |
6 #define CONTENT_RENDERER_PEPPER_HOST_VAR_TRACKER_H_ | 6 #define CONTENT_RENDERER_PEPPER_HOST_VAR_TRACKER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/containers/hash_tables.h" | 12 #include "base/containers/hash_tables.h" |
13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
14 #include "base/memory/linked_ptr.h" | 14 #include "base/memory/linked_ptr.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
17 #include "ppapi/c/pp_instance.h" | 17 #include "ppapi/c/pp_instance.h" |
18 #include "ppapi/shared_impl/host_resource.h" | 18 #include "ppapi/shared_impl/host_resource.h" |
19 #include "ppapi/shared_impl/resource_tracker.h" | 19 #include "ppapi/shared_impl/resource_tracker.h" |
20 #include "ppapi/shared_impl/var_tracker.h" | 20 #include "ppapi/shared_impl/var_tracker.h" |
21 | 21 #include "v8/include/v8.h" |
22 typedef struct NPObject NPObject; | |
23 | 22 |
24 namespace ppapi { | 23 namespace ppapi { |
25 class ArrayBufferVar; | 24 class ArrayBufferVar; |
26 class NPObjectVar; | 25 class V8ObjectVar; |
27 class Var; | 26 class Var; |
28 } | 27 } |
29 | 28 |
30 namespace content { | 29 namespace content { |
31 | 30 |
32 // Adds NPObject var tracking to the standard PPAPI VarTracker for use in the | |
33 // renderer. | |
34 class HostVarTracker : public ppapi::VarTracker { | 31 class HostVarTracker : public ppapi::VarTracker { |
35 public: | 32 public: |
36 HostVarTracker(); | 33 HostVarTracker(); |
37 virtual ~HostVarTracker(); | 34 virtual ~HostVarTracker(); |
38 | 35 |
39 // Tracks all live NPObjectVar. This is so we can map between instance + | 36 // Tracks all live V8ObjectVar. This is so we can map between instance + |
40 // NPObject and get the NPObjectVar corresponding to it. This Add/Remove | 37 // V8Object and get the V8ObjectVar corresponding to it. This Add/Remove |
41 // function is called by the NPObjectVar when it is created and | 38 // function is called by the V8ObjectVar when it is created and destroyed. |
42 // destroyed. | 39 void AddV8ObjectVar(ppapi::V8ObjectVar* object_var); |
43 void AddNPObjectVar(ppapi::NPObjectVar* object_var); | 40 void RemoveV8ObjectVar(ppapi::V8ObjectVar* object_var); |
44 void RemoveNPObjectVar(ppapi::NPObjectVar* object_var); | |
45 | 41 |
46 // Looks up a previously registered NPObjectVar for the given NPObject and | 42 // Creates or retrieves a V8ObjectVar. |
47 // instance. Returns NULL if there is no NPObjectVar corresponding to the | 43 PP_Var V8ObjectVarForV8Object(PP_Instance instance, |
48 // given NPObject for the given instance. See AddNPObjectVar above. | 44 v8::Handle<v8::Object> object); |
49 ppapi::NPObjectVar* NPObjectVarForNPObject(PP_Instance instance, | |
50 NPObject* np_object); | |
51 | 45 |
52 // Returns the number of NPObjectVar's associated with the given instance. | 46 // Returns the number of AddV8ObjectVar's associated with the given instance. |
53 // Returns 0 if the instance isn't known. | 47 // Returns 0 if the instance isn't known. |
54 CONTENT_EXPORT int GetLiveNPObjectVarsForInstance(PP_Instance instance) const; | 48 CONTENT_EXPORT int GetLiveV8ObjectVarsForInstance(PP_Instance instance) const; |
55 | 49 |
56 // VarTracker public implementation. | 50 // VarTracker public implementation. |
57 virtual PP_Var MakeResourcePPVarFromMessage( | 51 virtual PP_Var MakeResourcePPVarFromMessage( |
58 PP_Instance instance, | 52 PP_Instance instance, |
59 const IPC::Message& creation_message, | 53 const IPC::Message& creation_message, |
60 int pending_renderer_id, | 54 int pending_renderer_id, |
61 int pending_browser_id) OVERRIDE; | 55 int pending_browser_id) OVERRIDE; |
62 virtual ppapi::ResourceVar* MakeResourceVar(PP_Resource pp_resource) OVERRIDE; | 56 virtual ppapi::ResourceVar* MakeResourceVar(PP_Resource pp_resource) OVERRIDE; |
63 virtual void DidDeleteInstance(PP_Instance instance) OVERRIDE; | 57 virtual void DidDeleteInstance(PP_Instance instance) OVERRIDE; |
64 | 58 |
65 virtual int TrackSharedMemoryHandle(PP_Instance instance, | 59 virtual int TrackSharedMemoryHandle(PP_Instance instance, |
66 base::SharedMemoryHandle file, | 60 base::SharedMemoryHandle file, |
67 uint32 size_in_bytes) OVERRIDE; | 61 uint32 size_in_bytes) OVERRIDE; |
68 virtual bool StopTrackingSharedMemoryHandle(int id, | 62 virtual bool StopTrackingSharedMemoryHandle(int id, |
69 PP_Instance instance, | 63 PP_Instance instance, |
70 base::SharedMemoryHandle* handle, | 64 base::SharedMemoryHandle* handle, |
71 uint32* size_in_bytes) OVERRIDE; | 65 uint32* size_in_bytes) OVERRIDE; |
72 | 66 |
73 private: | 67 private: |
74 // VarTracker private implementation. | 68 // VarTracker private implementation. |
75 virtual ppapi::ArrayBufferVar* CreateArrayBuffer(uint32 size_in_bytes) | 69 virtual ppapi::ArrayBufferVar* CreateArrayBuffer(uint32 size_in_bytes) |
76 OVERRIDE; | 70 OVERRIDE; |
77 virtual ppapi::ArrayBufferVar* CreateShmArrayBuffer( | 71 virtual ppapi::ArrayBufferVar* CreateShmArrayBuffer( |
78 uint32 size_in_bytes, | 72 uint32 size_in_bytes, |
79 base::SharedMemoryHandle handle) OVERRIDE; | 73 base::SharedMemoryHandle handle) OVERRIDE; |
80 | 74 |
81 // Clear the reference count of the given object and remove it from | 75 // Clear the reference count of the given object and remove it from |
82 // live_vars_. | 76 // live_vars_. |
83 void ForceReleaseNPObject(ppapi::NPObjectVar* object_var); | 77 void ForceReleaseV8Object(ppapi::V8ObjectVar* object_var); |
84 | 78 |
85 typedef std::map<NPObject*, ppapi::NPObjectVar*> NPObjectToNPObjectVarMap; | 79 struct V8ObjectVarKey { |
| 80 explicit V8ObjectVarKey(ppapi::V8ObjectVar* object_var); |
| 81 V8ObjectVarKey(PP_Instance i, v8::Handle<v8::Object> object); |
| 82 ~V8ObjectVarKey(); |
86 | 83 |
87 // Lists all known NPObjects, first indexed by the corresponding instance, | 84 bool operator<(const V8ObjectVarKey& other) const; |
88 // then by the NPObject*. This allows us to look up an NPObjectVar given | 85 bool operator==(const V8ObjectVarKey& other) const; |
89 // these two pieces of information. | 86 |
90 // | 87 PP_Instance instance; |
91 // The instance map is lazily managed, so we'll add the | 88 int hash; |
92 // NPObjectToNPObjectVarMap lazily when the first NPObject var is created, | 89 }; |
93 // and delete it when it's empty. | 90 typedef std::multimap<V8ObjectVarKey, ppapi::V8ObjectVar*> ObjectMap; |
94 typedef std::map<PP_Instance, linked_ptr<NPObjectToNPObjectVarMap> > | 91 ObjectMap::const_iterator GetForV8Object(PP_Instance instance, |
95 InstanceMap; | 92 v8::Handle<v8::Object> object) const; |
96 InstanceMap instance_map_; | 93 |
| 94 ObjectMap object_map_; |
97 | 95 |
98 // Tracks all shared memory handles used for transmitting array buffers. | 96 // Tracks all shared memory handles used for transmitting array buffers. |
99 struct SharedMemoryMapEntry { | 97 struct SharedMemoryMapEntry { |
100 PP_Instance instance; | 98 PP_Instance instance; |
101 base::SharedMemoryHandle handle; | 99 base::SharedMemoryHandle handle; |
102 uint32 size_in_bytes; | 100 uint32 size_in_bytes; |
103 }; | 101 }; |
104 typedef std::map<int, SharedMemoryMapEntry> SharedMemoryMap; | 102 typedef std::map<int, SharedMemoryMapEntry> SharedMemoryMap; |
105 SharedMemoryMap shared_memory_map_; | 103 SharedMemoryMap shared_memory_map_; |
106 uint32_t last_shared_memory_map_id_; | 104 uint32_t last_shared_memory_map_id_; |
107 | 105 |
108 DISALLOW_COPY_AND_ASSIGN(HostVarTracker); | 106 DISALLOW_COPY_AND_ASSIGN(HostVarTracker); |
109 }; | 107 }; |
110 | 108 |
111 } // namespace content | 109 } // namespace content |
112 | 110 |
113 #endif // CONTENT_RENDERER_PEPPER_HOST_VAR_TRACKER_H_ | 111 #endif // CONTENT_RENDERER_PEPPER_HOST_VAR_TRACKER_H_ |
OLD | NEW |