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

Unified Diff: extensions/renderer/api_event_handler.h

Issue 2469593002: [Extensions Bindings] Add Events support (Closed)
Patch Set: . Created 4 years, 1 month 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
Index: extensions/renderer/api_event_handler.h
diff --git a/extensions/renderer/api_event_handler.h b/extensions/renderer/api_event_handler.h
new file mode 100644
index 0000000000000000000000000000000000000000..3b77443b0d20ac498c67bd9b14a557ce84b242b1
--- /dev/null
+++ b/extensions/renderer/api_event_handler.h
@@ -0,0 +1,94 @@
+// Copyright 2016 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.
+
+#ifndef EXTENSIONS_RENDERER_API_EVENT_HANDLER_H_
+#define EXTENSIONS_RENDERER_API_EVENT_HANDLER_H_
+
+#include <map>
+#include <memory>
+#include <string>
+#include <vector>
+
+#include "base/callback.h"
+#include "base/macros.h"
+#include "base/memory/weak_ptr.h"
+#include "base/supports_user_data.h"
+#include "extensions/renderer/api_binding_types.h"
+#include "v8/include/v8.h"
+
+namespace base {
+class ListValue;
+}
+
+namespace gin {
+class Arguments;
+}
+
+namespace extensions {
+
+// The object to handle API events. This includes vending v8::Objects for the
+// event; handling adding, removing, and querying listeners; and firing events
+// to subscribed listeners. Designed to be used across JS contexts, but on a
+// single thread.
+class APIEventHandler {
+ public:
+ using EventListeners = std::vector<v8::Global<v8::Function>>;
+ using HandlerCallback =
+ base::Callback<void(v8::Local<v8::Object>, gin::Arguments*)>;
+
+ APIEventHandler(const binding::RunJSFunction& call_js);
+ ~APIEventHandler();
+
+ // Returns a new v8::Object for an event with the given |event_name|.
+ v8::Local<v8::Object> CreateEventInstance(const std::string& event_name,
+ v8::Local<v8::Context> context);
+
+ // Notifies all listeners of the event with the given |event_name| in the
+ // specified |context|, sending the included |arguments|.
+ void FireEventInContext(const std::string& event_name,
+ v8::Local<v8::Context> context,
+ const base::ListValue& arguments);
+
+ // Returns the EventListeners for a given |event_name| and |context|.
+ const EventListeners& GetEventListenersForTesting(
+ const std::string& event_name,
+ v8::Local<v8::Context> context);
+
+ private:
+ struct APIEventPerContextData : public base::SupportsUserData::Data {
+ APIEventPerContextData();
+ ~APIEventPerContextData() override;
+
+ // A map from event name -> event listeners.
+ std::map<std::string, std::unique_ptr<EventListeners>> event_data;
+
+ std::vector<std::unique_ptr<HandlerCallback>> callbacks;
+ };
+
+ void InitializeTemplate(v8::Isolate* isolate, APIEventPerContextData* data);
+
+ // Returns the EventListeners associated with the given |object|.
+ EventListeners* GetListenersForObject(v8::Local<v8::Object> object,
+ v8::Local<v8::Context> context);
+
+ // Functions to add, remove, or query event listeners. Forwarded from the
+ // JS entry points.
+ void AddListener(v8::Local<v8::Object> caller, gin::Arguments* arguments);
+ void RemoveListener(v8::Local<v8::Object> caller, gin::Arguments* arguments);
+ void HasListener(v8::Local<v8::Object> caller, gin::Arguments* arguments);
+ void HasListeners(v8::Local<v8::Object> caller, gin::Arguments* arguments);
+
+ // Method to run a given v8::Function. Curried in for testing.
+ binding::RunJSFunction call_js_;
+
+ v8::Global<v8::ObjectTemplate> event_template_;
+
+ base::WeakPtrFactory<APIEventHandler> weak_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(APIEventHandler);
+};
+
+} // namespace extensions
+
+#endif // EXTENSIONS_RENDERER_API_EVENT_HANDLER_H_

Powered by Google App Engine
This is Rietveld 408576698