Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / content / renderer / pepper / host_var_tracker.h
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 CONTENT_RENDERER_PEPPER_HOST_VAR_TRACKER_H_
6 #define CONTENT_RENDERER_PEPPER_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 "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"
22
23 namespace ppapi {
24 class ArrayBufferVar;
25 class V8ObjectVar;
26 class Var;
27 }
28
29 namespace content {
30
31 class HostVarTracker : public ppapi::VarTracker {
32  public:
33   HostVarTracker();
34   virtual ~HostVarTracker();
35
36   // Tracks all live V8ObjectVar. This is so we can map between instance +
37   // V8Object and get the V8ObjectVar corresponding to it. This Add/Remove
38   // function is called by the V8ObjectVar when it is created and destroyed.
39   void AddV8ObjectVar(ppapi::V8ObjectVar* object_var);
40   void RemoveV8ObjectVar(ppapi::V8ObjectVar* object_var);
41   // Creates or retrieves a V8ObjectVar.
42   PP_Var V8ObjectVarForV8Object(PP_Instance instance,
43                                 v8::Handle<v8::Object> object);
44   // Returns the number of V8ObjectVars associated with the given instance.
45   // Returns 0 if the instance isn't known.
46   CONTENT_EXPORT int GetLiveV8ObjectVarsForTest(PP_Instance instance);
47
48   // VarTracker public implementation.
49   virtual PP_Var MakeResourcePPVarFromMessage(
50       PP_Instance instance,
51       const IPC::Message& creation_message,
52       int pending_renderer_id,
53       int pending_browser_id) OVERRIDE;
54   virtual ppapi::ResourceVar* MakeResourceVar(PP_Resource pp_resource) OVERRIDE;
55   virtual void DidDeleteInstance(PP_Instance pp_instance) OVERRIDE;
56
57   virtual int TrackSharedMemoryHandle(PP_Instance instance,
58                                       base::SharedMemoryHandle file,
59                                       uint32 size_in_bytes) OVERRIDE;
60   virtual bool StopTrackingSharedMemoryHandle(int id,
61                                               PP_Instance instance,
62                                               base::SharedMemoryHandle* handle,
63                                               uint32* size_in_bytes) OVERRIDE;
64
65  private:
66   // VarTracker private implementation.
67   virtual ppapi::ArrayBufferVar* CreateArrayBuffer(uint32 size_in_bytes)
68       OVERRIDE;
69   virtual ppapi::ArrayBufferVar* CreateShmArrayBuffer(
70       uint32 size_in_bytes,
71       base::SharedMemoryHandle handle) OVERRIDE;
72
73   // Clear the reference count of the given object and remove it from
74   // live_vars_.
75   void ForceReleaseV8Object(ppapi::V8ObjectVar* object_var);
76
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();
83
84     bool operator<(const V8ObjectVarKey& other) const;
85
86     PP_Instance instance;
87     int hash;
88   };
89   typedef std::multimap<V8ObjectVarKey, ppapi::V8ObjectVar*> ObjectMap;
90
91   // Returns an iterator into |object_map| which points to V8Object which
92   // is associated with the given instance and object.
93   ObjectMap::iterator GetForV8Object(PP_Instance instance,
94                                      v8::Handle<v8::Object> object);
95
96
97   // A multimap of V8ObjectVarKey -> ObjectMap.
98   ObjectMap object_map_;
99
100   // Tracks all shared memory handles used for transmitting array buffers.
101   struct SharedMemoryMapEntry {
102     PP_Instance instance;
103     base::SharedMemoryHandle handle;
104     uint32 size_in_bytes;
105   };
106   typedef std::map<int, SharedMemoryMapEntry> SharedMemoryMap;
107   SharedMemoryMap shared_memory_map_;
108   uint32_t last_shared_memory_map_id_;
109
110   DISALLOW_COPY_AND_ASSIGN(HostVarTracker);
111 };
112
113 }  // namespace content
114
115 #endif  // CONTENT_RENDERER_PEPPER_HOST_VAR_TRACKER_H_