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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 #ifndef EXTENSIONS_RENDERER_API_EVENT_HANDLER_H_
6 #define EXTENSIONS_RENDERER_API_EVENT_HANDLER_H_
7
8 #include <map>
9 #include <memory>
10 #include <string>
11 #include <vector>
12
13 #include "base/callback.h"
14 #include "base/macros.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/supports_user_data.h"
17 #include "extensions/renderer/api_binding_types.h"
18 #include "v8/include/v8.h"
19
20 namespace base {
21 class ListValue;
22 }
23
24 namespace gin {
25 class Arguments;
26 }
27
28 namespace extensions {
29
30 // The object to handle API events. This includes vending v8::Objects for the
31 // event; handling adding, removing, and querying listeners; and firing events
32 // to subscribed listeners. Designed to be used across JS contexts, but on a
33 // single thread.
34 class APIEventHandler {
35 public:
36 using EventListeners = std::vector<v8::Global<v8::Function>>;
37 using HandlerCallback =
38 base::Callback<void(v8::Local<v8::Object>, gin::Arguments*)>;
39
40 APIEventHandler(const binding::RunJSFunction& call_js);
41 ~APIEventHandler();
42
43 // Returns a new v8::Object for an event with the given |event_name|.
44 v8::Local<v8::Object> CreateEventInstance(const std::string& event_name,
45 v8::Local<v8::Context> context);
46
47 // Notifies all listeners of the event with the given |event_name| in the
48 // specified |context|, sending the included |arguments|.
49 void FireEventInContext(const std::string& event_name,
50 v8::Local<v8::Context> context,
51 const base::ListValue& arguments);
52
53 // Returns the EventListeners for a given |event_name| and |context|.
54 const EventListeners& GetEventListenersForTesting(
55 const std::string& event_name,
56 v8::Local<v8::Context> context);
57
58 private:
59 struct APIEventPerContextData : public base::SupportsUserData::Data {
60 APIEventPerContextData();
61 ~APIEventPerContextData() override;
62
63 // A map from event name -> event listeners.
64 std::map<std::string, std::unique_ptr<EventListeners>> event_data;
65
66 std::vector<std::unique_ptr<HandlerCallback>> callbacks;
67 };
68
69 void InitializeTemplate(v8::Isolate* isolate, APIEventPerContextData* data);
70
71 // Returns the EventListeners associated with the given |object|.
72 EventListeners* GetListenersForObject(v8::Local<v8::Object> object,
73 v8::Local<v8::Context> context);
74
75 // Functions to add, remove, or query event listeners. Forwarded from the
76 // JS entry points.
77 void AddListener(v8::Local<v8::Object> caller, gin::Arguments* arguments);
78 void RemoveListener(v8::Local<v8::Object> caller, gin::Arguments* arguments);
79 void HasListener(v8::Local<v8::Object> caller, gin::Arguments* arguments);
80 void HasListeners(v8::Local<v8::Object> caller, gin::Arguments* arguments);
81
82 // Method to run a given v8::Function. Curried in for testing.
83 binding::RunJSFunction call_js_;
84
85 v8::Global<v8::ObjectTemplate> event_template_;
86
87 base::WeakPtrFactory<APIEventHandler> weak_factory_;
88
89 DISALLOW_COPY_AND_ASSIGN(APIEventHandler);
90 };
91
92 } // namespace extensions
93
94 #endif // EXTENSIONS_RENDERER_API_EVENT_HANDLER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698