Chromium Code Reviews| 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..22fdddc0ad7f002eb6d81e735c8d72697a04e55e |
| --- /dev/null |
| +++ b/extensions/renderer/api_event_handler.h |
| @@ -0,0 +1,77 @@ |
| +// 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 "extensions/renderer/event_emitter.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 HandlerCallback = |
|
jbroman
2016/11/02 19:50:14
unused?
Devlin
2016/11/02 22:02:21
Removed.
|
| + 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 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.
|
| + const std::string& event_name, |
| + v8::Local<v8::Context> context); |
| + |
| + private: |
| + 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.
|
| + APIEventPerContextData(); |
| + ~APIEventPerContextData() override; |
| + |
| + // A map from event name -> event emitter. |
| + 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
|
| + }; |
| + |
| + // Method to run a given v8::Function. Curried in for testing. |
| + binding::RunJSFunction call_js_; |
| + |
| + 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.
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(APIEventHandler); |
| +}; |
| + |
| +} // namespace extensions |
| + |
| +#endif // EXTENSIONS_RENDERER_API_EVENT_HANDLER_H_ |