Chromium Code Reviews| 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 <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 "extensions/renderer/event_emitter.h" | |
| 19 #include "v8/include/v8.h" | |
| 20 | |
| 21 namespace base { | |
| 22 class ListValue; | |
| 23 } | |
| 24 | |
| 25 namespace gin { | |
| 26 class Arguments; | |
| 27 } | |
| 28 | |
| 29 namespace extensions { | |
| 30 | |
| 31 // The object to handle API events. This includes vending v8::Objects for the | |
| 32 // event; handling adding, removing, and querying listeners; and firing events | |
| 33 // to subscribed listeners. Designed to be used across JS contexts, but on a | |
| 34 // single thread. | |
| 35 class APIEventHandler { | |
| 36 public: | |
| 37 using HandlerCallback = | |
|
jbroman
2016/11/02 19:50:14
unused?
Devlin
2016/11/02 22:02:21
Removed.
| |
| 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 EventEmitter::Listeners& GetEventListenersForTesting( | |
|
jbroman
2016/11/02 19:50:14
nit: It looks like the unit tests only look at the
Devlin
2016/11/02 22:02:22
Sure, done.
| |
| 55 const std::string& event_name, | |
| 56 v8::Local<v8::Context> context); | |
| 57 | |
| 58 private: | |
| 59 struct APIEventPerContextData : public base::SupportsUserData::Data { | |
|
jbroman
2016/11/02 19:50:14
nit: Since this class doesn't seem to be used outs
Devlin
2016/11/02 22:02:21
Done.
| |
| 60 APIEventPerContextData(); | |
| 61 ~APIEventPerContextData() override; | |
| 62 | |
| 63 // A map from event name -> event emitter. | |
| 64 std::map<std::string, std::unique_ptr<EventEmitter>> event_data; | |
|
jbroman
2016/11/02 19:50:14
As mentioned elsewhere, you shouldn't hold a uniqu
Devlin
2016/11/02 22:02:21
Thanks for the detailed explanation! That makes a
| |
| 65 }; | |
| 66 | |
| 67 // Method to run a given v8::Function. Curried in for testing. | |
| 68 binding::RunJSFunction call_js_; | |
| 69 | |
| 70 base::WeakPtrFactory<APIEventHandler> weak_factory_; | |
|
jbroman
2016/11/02 19:50:14
Does anything use |weak_factory_|?
Devlin
2016/11/02 22:02:22
Not anymore; removed.
| |
| 71 | |
| 72 DISALLOW_COPY_AND_ASSIGN(APIEventHandler); | |
| 73 }; | |
| 74 | |
| 75 } // namespace extensions | |
| 76 | |
| 77 #endif // EXTENSIONS_RENDERER_API_EVENT_HANDLER_H_ | |
| OLD | NEW |