File: api_event_handler.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (148 lines) | stat: -rw-r--r-- 5,480 bytes parent folder | download
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// 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.

#include "extensions/renderer/api_event_handler.h"

#include <algorithm>
#include <map>
#include <memory>
#include <vector>

#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/supports_user_data.h"
#include "base/values.h"
#include "content/public/child/v8_value_converter.h"
#include "extensions/renderer/event_emitter.h"
#include "gin/handle.h"
#include "gin/per_context_data.h"

namespace extensions {

namespace {

const char kExtensionAPIEventPerContextKey[] = "extension_api_events";

struct APIEventPerContextData : public base::SupportsUserData::Data {
  APIEventPerContextData(v8::Isolate* isolate) : isolate(isolate) {}
  ~APIEventPerContextData() override {
    v8::HandleScope scope(isolate);
    // We explicitly clear the event data map here to remove all references to
    // v8 objects. This helps us avoid cycles in v8 where an event listener
    // could hold a reference to the event, which in turn holds the reference
    // to the listener.
    for (const auto& pair : event_data) {
      EventEmitter* emitter = nullptr;
      gin::Converter<EventEmitter*>::FromV8(
          isolate, pair.second.Get(isolate), &emitter);
      CHECK(emitter);
      emitter->listeners()->clear();
    }
  }

  // The associated v8::Isolate. Since this object is cleaned up at context
  // destruction, this should always be valid.
  v8::Isolate* isolate;

  // A map from event name -> event emitter.
  std::map<std::string, v8::Global<v8::Object>> event_data;
};

}  // namespace

APIEventHandler::APIEventHandler(
    const binding::RunJSFunction& call_js,
    const EventListenersChangedMethod& listeners_changed)
    : call_js_(call_js), listeners_changed_(listeners_changed) {}
APIEventHandler::~APIEventHandler() {}

v8::Local<v8::Object> APIEventHandler::CreateEventInstance(
    const std::string& event_name,
    v8::Local<v8::Context> context) {
  // We need a context scope since gin::CreateHandle only takes the isolate
  // and infers the context from that.
  // TODO(devlin): This could be avoided if gin::CreateHandle could take a
  // context directly.
  v8::Context::Scope context_scope(context);

  gin::PerContextData* per_context_data = gin::PerContextData::From(context);
  DCHECK(per_context_data);
  APIEventPerContextData* data = static_cast<APIEventPerContextData*>(
      per_context_data->GetUserData(kExtensionAPIEventPerContextKey));
  if (!data) {
    auto api_data =
        base::MakeUnique<APIEventPerContextData>(context->GetIsolate());
    data = api_data.get();
    per_context_data->SetUserData(kExtensionAPIEventPerContextKey,
                                  api_data.release());
  }

  DCHECK(data->event_data.find(event_name) == data->event_data.end());

  gin::Handle<EventEmitter> emitter_handle = gin::CreateHandle(
      context->GetIsolate(),
      new EventEmitter(call_js_, base::Bind(listeners_changed_, event_name)));
  CHECK(!emitter_handle.IsEmpty());
  v8::Local<v8::Value> emitter_value = emitter_handle.ToV8();
  CHECK(emitter_value->IsObject());
  v8::Local<v8::Object> emitter_object =
      v8::Local<v8::Object>::Cast(emitter_value);
  data->event_data[event_name] =
      v8::Global<v8::Object>(context->GetIsolate(), emitter_object);

  return emitter_object;
}

void APIEventHandler::FireEventInContext(const std::string& event_name,
                                         v8::Local<v8::Context> context,
                                         const base::ListValue& args) {
  gin::PerContextData* per_context_data = gin::PerContextData::From(context);
  DCHECK(per_context_data);
  APIEventPerContextData* data = static_cast<APIEventPerContextData*>(
      per_context_data->GetUserData(kExtensionAPIEventPerContextKey));
  if (!data)
    return;

  auto iter = data->event_data.find(event_name);
  if (iter == data->event_data.end())
    return;

  // Note: since we only convert the arguments once, if a listener modifies an
  // object (including an array), other listeners will see that modification.
  // TODO(devlin): This is how it's always been, but should it be?
  std::vector<v8::Local<v8::Value>> v8_args;
  v8_args.reserve(args.GetSize());
  std::unique_ptr<content::V8ValueConverter> converter(
      content::V8ValueConverter::create());
  for (const auto& arg : args)
    v8_args.push_back(converter->ToV8Value(arg.get(), context));

  EventEmitter* emitter = nullptr;
  gin::Converter<EventEmitter*>::FromV8(
      context->GetIsolate(), iter->second.Get(context->GetIsolate()), &emitter);
  CHECK(emitter);

  emitter->Fire(context, &v8_args);
}

size_t APIEventHandler::GetNumEventListenersForTesting(
    const std::string& event_name,
    v8::Local<v8::Context> context) {
  gin::PerContextData* per_context_data = gin::PerContextData::From(context);
  DCHECK(per_context_data);
  APIEventPerContextData* data = static_cast<APIEventPerContextData*>(
      per_context_data->GetUserData(kExtensionAPIEventPerContextKey));
  DCHECK(data);

  auto iter = data->event_data.find(event_name);
  DCHECK(iter != data->event_data.end());
  EventEmitter* emitter = nullptr;
  gin::Converter<EventEmitter*>::FromV8(
      context->GetIsolate(), iter->second.Get(context->GetIsolate()), &emitter);
  CHECK(emitter);
  return emitter->listeners()->size();
}

}  // namespace extensions