| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef EXTENSIONS_RENDERER_API_EVENT_HANDLER_H_ | 5 #ifndef EXTENSIONS_RENDERER_API_EVENT_HANDLER_H_ |
| 6 #define EXTENSIONS_RENDERER_API_EVENT_HANDLER_H_ | 6 #define EXTENSIONS_RENDERER_API_EVENT_HANDLER_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "extensions/common/event_filter.h" |
| 12 #include "extensions/renderer/api_binding_types.h" | 13 #include "extensions/renderer/api_binding_types.h" |
| 13 #include "extensions/renderer/event_emitter.h" | 14 #include "extensions/renderer/event_emitter.h" |
| 14 #include "v8/include/v8.h" | 15 #include "v8/include/v8.h" |
| 15 | 16 |
| 16 namespace base { | 17 namespace base { |
| 17 class ListValue; | 18 class ListValue; |
| 18 } | 19 } |
| 19 | 20 |
| 20 namespace extensions { | 21 namespace extensions { |
| 22 class EventFilteringInfo; |
| 21 | 23 |
| 22 // The object to handle API events. This includes vending v8::Objects for the | 24 // The object to handle API events. This includes vending v8::Objects for the |
| 23 // event; handling adding, removing, and querying listeners; and firing events | 25 // event; handling adding, removing, and querying listeners; and firing events |
| 24 // to subscribed listeners. Designed to be used across JS contexts, but on a | 26 // to subscribed listeners. Designed to be used across JS contexts, but on a |
| 25 // single thread. | 27 // single thread. |
| 26 class APIEventHandler { | 28 class APIEventHandler { |
| 27 public: | 29 public: |
| 28 using EventListenersChangedMethod = | 30 using EventListenersChangedMethod = |
| 29 base::Callback<void(const std::string& event_name, | 31 base::Callback<void(const std::string& event_name, |
| 30 binding::EventListenersChanged, | 32 binding::EventListenersChanged, |
| 33 const base::DictionaryValue* filter, |
| 31 v8::Local<v8::Context>)>; | 34 v8::Local<v8::Context>)>; |
| 32 | 35 |
| 33 APIEventHandler(const binding::RunJSFunction& call_js, | 36 APIEventHandler(const binding::RunJSFunction& call_js, |
| 34 const EventListenersChangedMethod& listeners_changed); | 37 const EventListenersChangedMethod& listeners_changed); |
| 35 ~APIEventHandler(); | 38 ~APIEventHandler(); |
| 36 | 39 |
| 37 // Returns a new v8::Object for an event with the given |event_name|. | 40 // Returns a new v8::Object for an event with the given |event_name|. |
| 38 v8::Local<v8::Object> CreateEventInstance(const std::string& event_name, | 41 v8::Local<v8::Object> CreateEventInstance(const std::string& event_name, |
| 42 bool supports_filters, |
| 39 v8::Local<v8::Context> context); | 43 v8::Local<v8::Context> context); |
| 40 | 44 |
| 41 // Creates a new event without any name. This is used by custom bindings when | 45 // Creates a new event without any name. This is used by custom bindings when |
| 42 // the entirety of the logic for the event is contained in the renderer. These | 46 // the entirety of the logic for the event is contained in the renderer. These |
| 43 // events do not notify of new/removed listeners or allow for dispatching | 47 // events do not notify of new/removed listeners or allow for dispatching |
| 44 // through FireEventInContext(). | 48 // through FireEventInContext(). |
| 45 v8::Local<v8::Object> CreateAnonymousEventInstance( | 49 v8::Local<v8::Object> CreateAnonymousEventInstance( |
| 46 v8::Local<v8::Context> context); | 50 v8::Local<v8::Context> context); |
| 47 | 51 |
| 48 // Invalidates the given |event|. | 52 // Invalidates the given |event|. |
| 49 void InvalidateCustomEvent(v8::Local<v8::Context> context, | 53 void InvalidateCustomEvent(v8::Local<v8::Context> context, |
| 50 v8::Local<v8::Object> event); | 54 v8::Local<v8::Object> event); |
| 51 | 55 |
| 52 // Notifies all listeners of the event with the given |event_name| in the | 56 // Notifies all listeners of the event with the given |event_name| in the |
| 53 // specified |context|, sending the included |arguments|. | 57 // specified |context|, sending the included |arguments|. |
| 54 void FireEventInContext(const std::string& event_name, | 58 void FireEventInContext(const std::string& event_name, |
| 55 v8::Local<v8::Context> context, | 59 v8::Local<v8::Context> context, |
| 56 const base::ListValue& arguments); | 60 const base::ListValue& arguments, |
| 61 const EventFilteringInfo& filter); |
| 57 | 62 |
| 58 // Registers a |function| to serve as an "argument massager" for the given | 63 // Registers a |function| to serve as an "argument massager" for the given |
| 59 // |event_name|, mutating the original arguments. | 64 // |event_name|, mutating the original arguments. |
| 60 // The function is called with two arguments: the array of original arguments | 65 // The function is called with two arguments: the array of original arguments |
| 61 // being dispatched to the event, and the function to dispatch the event to | 66 // being dispatched to the event, and the function to dispatch the event to |
| 62 // listeners. | 67 // listeners. |
| 63 void RegisterArgumentMassager(v8::Local<v8::Context> context, | 68 void RegisterArgumentMassager(v8::Local<v8::Context> context, |
| 64 const std::string& event_name, | 69 const std::string& event_name, |
| 65 v8::Local<v8::Function> function); | 70 v8::Local<v8::Function> function); |
| 66 | 71 |
| 67 // Returns the EventListeners for a given |event_name| and |context|. | 72 // Returns the EventListeners for a given |event_name| and |context|. |
| 68 size_t GetNumEventListenersForTesting(const std::string& event_name, | 73 size_t GetNumEventListenersForTesting(const std::string& event_name, |
| 69 v8::Local<v8::Context> context); | 74 v8::Local<v8::Context> context); |
| 70 | 75 |
| 71 // Invalidates listeners for the given |context|. It's a shame we have to | 76 // Invalidates listeners for the given |context|. It's a shame we have to |
| 72 // have this separately (as opposed to hooking into e.g. a PerContextData | 77 // have this separately (as opposed to hooking into e.g. a PerContextData |
| 73 // destructor), but we need to do this before the context is fully removed | 78 // destructor), but we need to do this before the context is fully removed |
| 74 // (because the associated extension ScriptContext needs to be valid). | 79 // (because the associated extension ScriptContext needs to be valid). |
| 75 void InvalidateContext(v8::Local<v8::Context> context); | 80 void InvalidateContext(v8::Local<v8::Context> context); |
| 76 | 81 |
| 77 private: | 82 private: |
| 78 // Method to run a given v8::Function. Curried in for testing. | 83 // Method to run a given v8::Function. Curried in for testing. |
| 79 binding::RunJSFunction call_js_; | 84 binding::RunJSFunction call_js_; |
| 80 | 85 |
| 81 EventListenersChangedMethod listeners_changed_; | 86 EventListenersChangedMethod listeners_changed_; |
| 82 | 87 |
| 88 // The associated EventFilter; shared across all contexts and events. |
| 89 EventFilter event_filter_; |
| 90 |
| 83 DISALLOW_COPY_AND_ASSIGN(APIEventHandler); | 91 DISALLOW_COPY_AND_ASSIGN(APIEventHandler); |
| 84 }; | 92 }; |
| 85 | 93 |
| 86 } // namespace extensions | 94 } // namespace extensions |
| 87 | 95 |
| 88 #endif // EXTENSIONS_RENDERER_API_EVENT_HANDLER_H_ | 96 #endif // EXTENSIONS_RENDERER_API_EVENT_HANDLER_H_ |
| OLD | NEW |