Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4339)

Unified Diff: chrome/renderer/weak_v8_function_map.cc

Issue 8194005: Relanding: Add js api for hosted/packaged apps to request notification authorization (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/renderer/weak_v8_function_map.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/renderer/weak_v8_function_map.cc
diff --git a/chrome/renderer/weak_v8_function_map.cc b/chrome/renderer/weak_v8_function_map.cc
new file mode 100644
index 0000000000000000000000000000000000000000..0d7052381dc622f23b4d2422240eb0503fa534fd
--- /dev/null
+++ b/chrome/renderer/weak_v8_function_map.cc
@@ -0,0 +1,51 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/logging.h"
+#include "chrome/renderer/weak_v8_function_map.h"
+
+// Extra data to be passed to MakeWeak/RemoveFromMap to know which entry
+// to remove from which map.
+struct WeakV8FunctionMapData {
+ base::WeakPtr<WeakV8FunctionMap> map;
+ int key;
+};
+
+// Disposes of a callback function and its corresponding entry in the callback
+// map, if that callback map is still alive.
+static void RemoveFromMap(v8::Persistent<v8::Value> context,
+ void* data) {
+ WeakV8FunctionMapData* map_data =
+ static_cast<WeakV8FunctionMapData*>(data);
+ if (map_data->map)
+ map_data->map->Remove(map_data->key);
+ delete map_data;
+ context.Dispose();
+}
+
+WeakV8FunctionMap::WeakV8FunctionMap()
+ : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) {}
+
+WeakV8FunctionMap::~WeakV8FunctionMap() {}
+
+
+void WeakV8FunctionMap::Add(int key,
+ v8::Local<v8::Function> callback_function) {
+ WeakV8FunctionMapData* map_data = new WeakV8FunctionMapData();
+ map_data->key = key;
+ map_data->map = weak_ptr_factory_.GetWeakPtr();
+ v8::Persistent<v8::Function> wrapper =
+ v8::Persistent<v8::Function>::New(callback_function);
+ map_[key] = wrapper;
+ wrapper.MakeWeak(map_data, RemoveFromMap);
+}
+
+v8::Persistent<v8::Function> WeakV8FunctionMap::Remove(int key) {
+ WeakV8FunctionMap::Map::iterator i = map_.find(key);
+ if (i == map_.end())
+ return v8::Persistent<v8::Function>();
+ v8::Persistent<v8::Function> callback = i->second;
+ map_.erase(i);
+ return callback;
+}
« no previous file with comments | « chrome/renderer/weak_v8_function_map.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698