summaryrefslogtreecommitdiff
path: root/chromium/extensions/renderer/api_event_handler.h
blob: fbf9b42a7ea742f1accc48e10775af037a6b8e0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// 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 <string>

#include "base/callback.h"
#include "base/macros.h"
#include "extensions/renderer/api_binding_types.h"
#include "extensions/renderer/event_emitter.h"
#include "v8/include/v8.h"

namespace base {
class ListValue;
}

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 EventListenersChangedMethod =
      base::Callback<void(const std::string& event_name,
                          binding::EventListenersChanged,
                          v8::Local<v8::Context>)>;

  APIEventHandler(const binding::RunJSFunction& call_js,
                  const EventListenersChangedMethod& listeners_changed);
  ~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|.
  size_t GetNumEventListenersForTesting(const std::string& event_name,
                                        v8::Local<v8::Context> context);

 private:
  // Method to run a given v8::Function. Curried in for testing.
  binding::RunJSFunction call_js_;

  EventListenersChangedMethod listeners_changed_;

  DISALLOW_COPY_AND_ASSIGN(APIEventHandler);
};

}  // namespace extensions

#endif  // EXTENSIONS_RENDERER_API_EVENT_HANDLER_H_