1 // Copyright (c) 2011 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 #include "base/logging.h"
6 #include "chrome/renderer/weak_v8_function_map.h"
8 // Extra data to be passed to MakeWeak/RemoveFromMap to know which entry
9 // to remove from which map.
10 struct WeakV8FunctionMapData
{
11 base::WeakPtr
<WeakV8FunctionMap
> map
;
15 // Disposes of a callback function and its corresponding entry in the callback
16 // map, if that callback map is still alive.
17 static void RemoveFromMap(v8::Persistent
<v8::Value
> context
,
19 WeakV8FunctionMapData
* map_data
=
20 static_cast<WeakV8FunctionMapData
*>(data
);
22 map_data
->map
->Remove(map_data
->key
);
27 WeakV8FunctionMap::WeakV8FunctionMap()
28 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {}
30 WeakV8FunctionMap::~WeakV8FunctionMap() {}
33 void WeakV8FunctionMap::Add(int key
,
34 v8::Local
<v8::Function
> callback_function
) {
35 WeakV8FunctionMapData
* map_data
= new WeakV8FunctionMapData();
37 map_data
->map
= weak_ptr_factory_
.GetWeakPtr();
38 v8::Persistent
<v8::Function
> wrapper
=
39 v8::Persistent
<v8::Function
>::New(callback_function
);
41 wrapper
.MakeWeak(map_data
, RemoveFromMap
);
44 v8::Persistent
<v8::Function
> WeakV8FunctionMap::Remove(int key
) {
45 WeakV8FunctionMap::Map::iterator i
= map_
.find(key
);
47 return v8::Persistent
<v8::Function
>();
48 v8::Persistent
<v8::Function
> callback
= i
->second
;