Fixed service side implementation of glTexStorage2DEXT to only initialize the number of
[chromium-blink-merge.git] / chrome / renderer / weak_v8_function_map.cc
blob0d7052381dc622f23b4d2422240eb0503fa534fd
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;
12 int key;
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();
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();
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);
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;