OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "base/logging.h" |
| 6 #include "chrome/renderer/weak_v8_function_map.h" |
| 7 |
| 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; |
| 12 int key; |
| 13 }; |
| 14 |
| 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, |
| 18 void* data) { |
| 19 WeakV8FunctionMapData* map_data = |
| 20 static_cast<WeakV8FunctionMapData*>(data); |
| 21 if (map_data->map) |
| 22 map_data->map->Remove(map_data->key); |
| 23 delete map_data; |
| 24 context.Dispose(); |
| 25 } |
| 26 |
| 27 WeakV8FunctionMap::WeakV8FunctionMap() |
| 28 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {} |
| 29 |
| 30 WeakV8FunctionMap::~WeakV8FunctionMap() {} |
| 31 |
| 32 |
| 33 void WeakV8FunctionMap::Add(int key, |
| 34 v8::Local<v8::Function> callback_function) { |
| 35 WeakV8FunctionMapData* map_data = new WeakV8FunctionMapData(); |
| 36 map_data->key = key; |
| 37 map_data->map = weak_ptr_factory_.GetWeakPtr(); |
| 38 v8::Persistent<v8::Function> wrapper = |
| 39 v8::Persistent<v8::Function>::New(callback_function); |
| 40 map_[key] = wrapper; |
| 41 wrapper.MakeWeak(map_data, RemoveFromMap); |
| 42 } |
| 43 |
| 44 v8::Persistent<v8::Function> WeakV8FunctionMap::Remove(int key) { |
| 45 WeakV8FunctionMap::Map::iterator i = map_.find(key); |
| 46 if (i == map_.end()) |
| 47 return v8::Persistent<v8::Function>(); |
| 48 v8::Persistent<v8::Function> callback = i->second; |
| 49 map_.erase(i); |
| 50 return callback; |
| 51 } |
OLD | NEW |