| OLD | NEW |
| (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 <string> | |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "base/macros.h" | |
| 12 #include "extensions/common/event_filter.h" | |
| 13 #include "extensions/renderer/api_binding_types.h" | |
| 14 #include "extensions/renderer/event_emitter.h" | |
| 15 #include "v8/include/v8.h" | |
| 16 | |
| 17 namespace base { | |
| 18 class ListValue; | |
| 19 } | |
| 20 | |
| 21 namespace extensions { | |
| 22 struct EventFilteringInfo; | |
| 23 | |
| 24 // The object to handle API events. This includes vending v8::Objects for the | |
| 25 // event; handling adding, removing, and querying listeners; and firing events | |
| 26 // to subscribed listeners. Designed to be used across JS contexts, but on a | |
| 27 // single thread. | |
| 28 class APIEventHandler { | |
| 29 public: | |
| 30 // The callback to be called when event listeners change. |was_manual| | |
| 31 // indicates that the change was due to an extension calling addListener or | |
| 32 // removeListener, rather than through something like context destruction. | |
| 33 // See also APIEventListeners. | |
| 34 using EventListenersChangedMethod = | |
| 35 base::Callback<void(const std::string& event_name, | |
| 36 binding::EventListenersChanged, | |
| 37 const base::DictionaryValue* filter, | |
| 38 bool was_manual, | |
| 39 v8::Local<v8::Context>)>; | |
| 40 | |
| 41 APIEventHandler(const binding::RunJSFunction& call_js, | |
| 42 const binding::RunJSFunctionSync& call_js_sync, | |
| 43 const EventListenersChangedMethod& listeners_changed); | |
| 44 ~APIEventHandler(); | |
| 45 | |
| 46 // Returns a new v8::Object for an event with the given |event_name|. If | |
| 47 // |notify_on_change| is true, notifies whenever listeners state is changed. | |
| 48 v8::Local<v8::Object> CreateEventInstance(const std::string& event_name, | |
| 49 bool supports_filters, | |
| 50 int max_listeners, | |
| 51 bool notify_on_change, | |
| 52 v8::Local<v8::Context> context); | |
| 53 | |
| 54 // Creates a new event without any name. This is used by custom bindings when | |
| 55 // the entirety of the logic for the event is contained in the renderer. These | |
| 56 // events do not notify of new/removed listeners or allow for dispatching | |
| 57 // through FireEventInContext(). | |
| 58 v8::Local<v8::Object> CreateAnonymousEventInstance( | |
| 59 v8::Local<v8::Context> context); | |
| 60 | |
| 61 // Invalidates the given |event|. | |
| 62 void InvalidateCustomEvent(v8::Local<v8::Context> context, | |
| 63 v8::Local<v8::Object> event); | |
| 64 | |
| 65 // Notifies all listeners of the event with the given |event_name| in the | |
| 66 // specified |context|, sending the included |arguments|. | |
| 67 void FireEventInContext(const std::string& event_name, | |
| 68 v8::Local<v8::Context> context, | |
| 69 const base::ListValue& arguments, | |
| 70 const EventFilteringInfo& filter); | |
| 71 | |
| 72 // Registers a |function| to serve as an "argument massager" for the given | |
| 73 // |event_name|, mutating the original arguments. | |
| 74 // The function is called with two arguments: the array of original arguments | |
| 75 // being dispatched to the event, and the function to dispatch the event to | |
| 76 // listeners. | |
| 77 void RegisterArgumentMassager(v8::Local<v8::Context> context, | |
| 78 const std::string& event_name, | |
| 79 v8::Local<v8::Function> function); | |
| 80 | |
| 81 // Returns true if there is a listener for the given |event_name| in the | |
| 82 // given |context|. | |
| 83 bool HasListenerForEvent(const std::string& event_name, | |
| 84 v8::Local<v8::Context> context); | |
| 85 | |
| 86 // Invalidates listeners for the given |context|. It's a shame we have to | |
| 87 // have this separately (as opposed to hooking into e.g. a PerContextData | |
| 88 // destructor), but we need to do this before the context is fully removed | |
| 89 // (because the associated extension ScriptContext needs to be valid). | |
| 90 void InvalidateContext(v8::Local<v8::Context> context); | |
| 91 | |
| 92 // Returns the number of event listeners for a given |event_name| and | |
| 93 // |context|. | |
| 94 size_t GetNumEventListenersForTesting(const std::string& event_name, | |
| 95 v8::Local<v8::Context> context); | |
| 96 | |
| 97 private: | |
| 98 // Method to run a given v8::Function. Curried in for testing. | |
| 99 binding::RunJSFunction call_js_; | |
| 100 binding::RunJSFunctionSync call_js_sync_; | |
| 101 | |
| 102 EventListenersChangedMethod listeners_changed_; | |
| 103 | |
| 104 // The associated EventFilter; shared across all contexts and events. | |
| 105 EventFilter event_filter_; | |
| 106 | |
| 107 DISALLOW_COPY_AND_ASSIGN(APIEventHandler); | |
| 108 }; | |
| 109 | |
| 110 } // namespace extensions | |
| 111 | |
| 112 #endif // EXTENSIONS_RENDERER_API_EVENT_HANDLER_H_ | |
| OLD | NEW |