Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: extensions/renderer/event_emitter.cc

Issue 2605353003: [Extensions Bindings] Allow event listener unregistration in handling (Closed)
Patch Set: . Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « extensions/renderer/api_event_handler_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "extensions/renderer/event_emitter.h" 5 #include "extensions/renderer/event_emitter.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "gin/object_template_builder.h" 9 #include "gin/object_template_builder.h"
10 #include "gin/per_context_data.h" 10 #include "gin/per_context_data.h"
(...skipping 16 matching lines...) Expand all
27 .SetMethod("hasListeners", &EventEmitter::HasListeners) 27 .SetMethod("hasListeners", &EventEmitter::HasListeners)
28 // The following methods aren't part of the public API, but are used 28 // The following methods aren't part of the public API, but are used
29 // by our custom bindings and exposed on the public event object. :( 29 // by our custom bindings and exposed on the public event object. :(
30 // TODO(devlin): Once we convert all custom bindings that use these, 30 // TODO(devlin): Once we convert all custom bindings that use these,
31 // they can be removed. 31 // they can be removed.
32 .SetMethod("dispatch", &EventEmitter::Dispatch); 32 .SetMethod("dispatch", &EventEmitter::Dispatch);
33 } 33 }
34 34
35 void EventEmitter::Fire(v8::Local<v8::Context> context, 35 void EventEmitter::Fire(v8::Local<v8::Context> context,
36 std::vector<v8::Local<v8::Value>>* args) { 36 std::vector<v8::Local<v8::Value>>* args) {
37 for (const auto& listener : listeners_) { 37 // We create a local copy of listeners_ since the array can be modified during
38 run_js_.Run(listener.Get(context->GetIsolate()), context, args->size(), 38 // handling.
39 args->data()); 39 std::vector<v8::Local<v8::Function>> listeners;
40 } 40 listeners.reserve(listeners_.size());
41 for (const auto& listener : listeners_)
42 listeners.push_back(listener.Get(context->GetIsolate()));
43
44 for (const auto& listener : listeners)
45 run_js_.Run(listener, context, args->size(), args->data());
41 } 46 }
42 47
43 void EventEmitter::AddListener(gin::Arguments* arguments) { 48 void EventEmitter::AddListener(gin::Arguments* arguments) {
44 v8::Local<v8::Function> listener; 49 v8::Local<v8::Function> listener;
45 if (!arguments->GetNext(&listener)) 50 if (!arguments->GetNext(&listener))
46 return; 51 return;
47 52
48 v8::Local<v8::Object> holder; 53 v8::Local<v8::Object> holder;
49 CHECK(arguments->GetHolder(&holder)); 54 CHECK(arguments->GetHolder(&holder));
50 CHECK(!holder.IsEmpty()); 55 CHECK(!holder.IsEmpty());
(...skipping 29 matching lines...) Expand all
80 arguments->isolate()->GetCurrentContext(); 85 arguments->isolate()->GetCurrentContext();
81 std::vector<v8::Local<v8::Value>> v8_args; 86 std::vector<v8::Local<v8::Value>> v8_args;
82 if (arguments->Length() > 0) { 87 if (arguments->Length() > 0) {
83 // Converting to v8::Values should never fail. 88 // Converting to v8::Values should never fail.
84 CHECK(arguments->GetRemaining(&v8_args)); 89 CHECK(arguments->GetRemaining(&v8_args));
85 } 90 }
86 Fire(context, &v8_args); 91 Fire(context, &v8_args);
87 } 92 }
88 93
89 } // namespace extensions 94 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/api_event_handler_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698