| 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 #include "v8/include/v8.h" | 21 #include "v8/include/v8.h" |
| 22 | 22 |
| 23 typedef struct NPObject NPObject; | |
| 24 | |
| 25 namespace ppapi { | 23 namespace ppapi { |
| 26 class ArrayBufferVar; | 24 class ArrayBufferVar; |
| 27 class NPObjectVar; | |
| 28 class V8ObjectVar; | 25 class V8ObjectVar; |
| 29 class Var; | 26 class Var; |
| 30 } | 27 } |
| 31 | 28 |
| 32 namespace content { | 29 namespace content { |
| 33 | 30 |
| 34 // Adds NPObject var tracking to the standard PPAPI VarTracker for use in the | |
| 35 // renderer. | |
| 36 class HostVarTracker : public ppapi::VarTracker { | 31 class HostVarTracker : public ppapi::VarTracker { |
| 37 public: | 32 public: |
| 38 HostVarTracker(); | 33 HostVarTracker(); |
| 39 virtual ~HostVarTracker(); | 34 virtual ~HostVarTracker(); |
| 40 | 35 |
| 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 | |
| 44 // destroyed. | |
| 45 void AddNPObjectVar(ppapi::NPObjectVar* object_var); | |
| 46 void RemoveNPObjectVar(ppapi::NPObjectVar* object_var); | |
| 47 | |
| 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, | |
| 52 NPObject* np_object); | |
| 53 | |
| 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; | |
| 57 | |
| 58 // Tracks all live V8ObjectVar. This is so we can map between instance + | 36 // Tracks all live V8ObjectVar. This is so we can map between instance + |
| 59 // V8Object and get the V8ObjectVar corresponding to it. This Add/Remove | 37 // V8Object and get the V8ObjectVar corresponding to it. This Add/Remove |
| 60 // function is called by the V8ObjectVar when it is created and destroyed. | 38 // function is called by the V8ObjectVar when it is created and destroyed. |
| 61 void AddV8ObjectVar(ppapi::V8ObjectVar* object_var) { NOTIMPLEMENTED(); } | 39 void AddV8ObjectVar(ppapi::V8ObjectVar* object_var); |
| 62 void RemoveV8ObjectVar(ppapi::V8ObjectVar* object_var) { NOTIMPLEMENTED(); } | 40 void RemoveV8ObjectVar(ppapi::V8ObjectVar* object_var); |
| 63 // Creates or retrieves a V8ObjectVar. | 41 // Creates or retrieves a V8ObjectVar. |
| 64 PP_Var V8ObjectVarForV8Object(PP_Instance instance, | 42 PP_Var V8ObjectVarForV8Object(PP_Instance instance, |
| 65 v8::Handle<v8::Object> object) { | 43 v8::Handle<v8::Object> object); |
| 66 NOTIMPLEMENTED(); | 44 // Returns the number of V8ObjectVars associated with the given instance. |
| 67 return PP_MakeUndefined(); | 45 // Returns 0 if the instance isn't known. |
| 68 } | 46 CONTENT_EXPORT int GetLiveV8ObjectVarsForInstance(PP_Instance instance) const; |
| 69 | 47 |
| 70 // VarTracker public implementation. | 48 // VarTracker public implementation. |
| 71 virtual PP_Var MakeResourcePPVarFromMessage( | 49 virtual PP_Var MakeResourcePPVarFromMessage( |
| 72 PP_Instance instance, | 50 PP_Instance instance, |
| 73 const IPC::Message& creation_message, | 51 const IPC::Message& creation_message, |
| 74 int pending_renderer_id, | 52 int pending_renderer_id, |
| 75 int pending_browser_id) OVERRIDE; | 53 int pending_browser_id) OVERRIDE; |
| 76 virtual ppapi::ResourceVar* MakeResourceVar(PP_Resource pp_resource) OVERRIDE; | 54 virtual ppapi::ResourceVar* MakeResourceVar(PP_Resource pp_resource) OVERRIDE; |
| 77 virtual void DidDeleteInstance(PP_Instance instance) OVERRIDE; | 55 virtual void DidDeleteInstance(PP_Instance instance) OVERRIDE; |
| 78 | 56 |
| 79 virtual int TrackSharedMemoryHandle(PP_Instance instance, | 57 virtual int TrackSharedMemoryHandle(PP_Instance instance, |
| 80 base::SharedMemoryHandle file, | 58 base::SharedMemoryHandle file, |
| 81 uint32 size_in_bytes) OVERRIDE; | 59 uint32 size_in_bytes) OVERRIDE; |
| 82 virtual bool StopTrackingSharedMemoryHandle(int id, | 60 virtual bool StopTrackingSharedMemoryHandle(int id, |
| 83 PP_Instance instance, | 61 PP_Instance instance, |
| 84 base::SharedMemoryHandle* handle, | 62 base::SharedMemoryHandle* handle, |
| 85 uint32* size_in_bytes) OVERRIDE; | 63 uint32* size_in_bytes) OVERRIDE; |
| 86 | 64 |
| 87 private: | 65 private: |
| 88 // VarTracker private implementation. | 66 // VarTracker private implementation. |
| 89 virtual ppapi::ArrayBufferVar* CreateArrayBuffer(uint32 size_in_bytes) | 67 virtual ppapi::ArrayBufferVar* CreateArrayBuffer(uint32 size_in_bytes) |
| 90 OVERRIDE; | 68 OVERRIDE; |
| 91 virtual ppapi::ArrayBufferVar* CreateShmArrayBuffer( | 69 virtual ppapi::ArrayBufferVar* CreateShmArrayBuffer( |
| 92 uint32 size_in_bytes, | 70 uint32 size_in_bytes, |
| 93 base::SharedMemoryHandle handle) OVERRIDE; | 71 base::SharedMemoryHandle handle) OVERRIDE; |
| 94 | 72 |
| 95 // Clear the reference count of the given object and remove it from | 73 // Clear the reference count of the given object and remove it from |
| 96 // live_vars_. | 74 // live_vars_. |
| 97 void ForceReleaseNPObject(ppapi::NPObjectVar* object_var); | 75 void ForceReleaseV8Object(ppapi::V8ObjectVar* object_var); |
| 98 | 76 |
| 99 typedef std::map<NPObject*, ppapi::NPObjectVar*> NPObjectToNPObjectVarMap; | 77 // A non-unique, ordered key for a V8ObjectVar. Contains the hash of the v8 |
| 78 // and the instance it is associated with. |
| 79 struct V8ObjectVarKey { |
| 80 explicit V8ObjectVarKey(ppapi::V8ObjectVar* object_var); |
| 81 V8ObjectVarKey(PP_Instance i, v8::Handle<v8::Object> object); |
| 82 ~V8ObjectVarKey(); |
| 100 | 83 |
| 101 // Lists all known NPObjects, first indexed by the corresponding instance, | 84 bool operator<(const V8ObjectVarKey& other) const; |
| 102 // then by the NPObject*. This allows us to look up an NPObjectVar given | 85 bool operator==(const V8ObjectVarKey& other) const; |
| 103 // these two pieces of information. | 86 |
| 104 // | 87 PP_Instance instance; |
| 105 // The instance map is lazily managed, so we'll add the | 88 int hash; |
| 106 // NPObjectToNPObjectVarMap lazily when the first NPObject var is created, | 89 }; |
| 107 // and delete it when it's empty. | 90 typedef std::multimap<V8ObjectVarKey, ppapi::V8ObjectVar*> ObjectMap; |
| 108 typedef std::map<PP_Instance, linked_ptr<NPObjectToNPObjectVarMap> > | 91 |
| 109 InstanceMap; | 92 // Returns an iterator into |object_map| which points to V8Object which |
| 110 InstanceMap instance_map_; | 93 // is associated with the given instance and object. |
| 94 ObjectMap::const_iterator GetForV8Object(PP_Instance instance, |
| 95 v8::Handle<v8::Object> object) const; |
| 96 |
| 97 |
| 98 // A multimap of V8ObjectVarKey -> ObjectMap. |
| 99 ObjectMap object_map_; |
| 111 | 100 |
| 112 // Tracks all shared memory handles used for transmitting array buffers. | 101 // Tracks all shared memory handles used for transmitting array buffers. |
| 113 struct SharedMemoryMapEntry { | 102 struct SharedMemoryMapEntry { |
| 114 PP_Instance instance; | 103 PP_Instance instance; |
| 115 base::SharedMemoryHandle handle; | 104 base::SharedMemoryHandle handle; |
| 116 uint32 size_in_bytes; | 105 uint32 size_in_bytes; |
| 117 }; | 106 }; |
| 118 typedef std::map<int, SharedMemoryMapEntry> SharedMemoryMap; | 107 typedef std::map<int, SharedMemoryMapEntry> SharedMemoryMap; |
| 119 SharedMemoryMap shared_memory_map_; | 108 SharedMemoryMap shared_memory_map_; |
| 120 uint32_t last_shared_memory_map_id_; | 109 uint32_t last_shared_memory_map_id_; |
| 121 | 110 |
| 122 DISALLOW_COPY_AND_ASSIGN(HostVarTracker); | 111 DISALLOW_COPY_AND_ASSIGN(HostVarTracker); |
| 123 }; | 112 }; |
| 124 | 113 |
| 125 } // namespace content | 114 } // namespace content |
| 126 | 115 |
| 127 #endif // CONTENT_RENDERER_PEPPER_HOST_VAR_TRACKER_H_ | 116 #endif // CONTENT_RENDERER_PEPPER_HOST_VAR_TRACKER_H_ |
| OLD | NEW |