| Index: extensions/renderer/event_bindings.cc
|
| diff --git a/extensions/renderer/event_bindings.cc b/extensions/renderer/event_bindings.cc
|
| index 385906c33989dcbf508b716b1f9f707a5a81fe9a..bad6a02542c6d7c49943f0c6e0718015ae5f0a2f 100644
|
| --- a/extensions/renderer/event_bindings.cc
|
| +++ b/extensions/renderer/event_bindings.cc
|
| @@ -13,6 +13,7 @@
|
| #include "base/bind.h"
|
| #include "base/lazy_instance.h"
|
| #include "base/memory/scoped_ptr.h"
|
| +#include "components/crx_file/id_util.h"
|
| #include "content/public/renderer/render_thread.h"
|
| #include "content/public/renderer/render_view.h"
|
| #include "content/public/renderer/v8_value_converter.h"
|
| @@ -53,6 +54,31 @@ base::LazyInstance<std::map<std::string, FilteredEventListenerCounts> >
|
|
|
| base::LazyInstance<EventFilter> g_event_filter = LAZY_INSTANCE_INITIALIZER;
|
|
|
| +std::string GetKeyForScriptContext(ScriptContext* script_context) {
|
| + const std::string& extension_id = script_context->GetExtensionID();
|
| + CHECK(crx_file::id_util::IdIsValid(extension_id) ||
|
| + script_context->GetURL().is_valid());
|
| + return crx_file::id_util::IdIsValid(extension_id)
|
| + ? extension_id
|
| + : script_context->GetURL().spec();
|
| +}
|
| +
|
| +// Increments the number of event-listeners for the given |event_name| and
|
| +// ScriptContext. Returns the count after the increment.
|
| +int IncrementEventListenerCount(ScriptContext* script_context,
|
| + const std::string& event_name) {
|
| + return ++g_listener_counts
|
| + .Get()[GetKeyForScriptContext(script_context)][event_name];
|
| +}
|
| +
|
| +// Decrements the number of event-listeners for the given |event_name| and
|
| +// ScriptContext. Returns the count after the increment.
|
| +int DecrementEventListenerCount(ScriptContext* script_context,
|
| + const std::string& event_name) {
|
| + return --g_listener_counts
|
| + .Get()[GetKeyForScriptContext(script_context)][event_name];
|
| +}
|
| +
|
| bool IsLazyBackgroundPage(content::RenderView* render_view,
|
| const Extension* extension) {
|
| if (!render_view)
|
| @@ -147,9 +173,8 @@ void EventBindings::AttachEvent(
|
| if (!dispatcher_->CheckContextAccessToExtensionAPI(event_name, context()))
|
| return;
|
|
|
| - std::string extension_id = context()->GetExtensionID();
|
| - EventListenerCounts& listener_counts = g_listener_counts.Get()[extension_id];
|
| - if (++listener_counts[event_name] == 1) {
|
| + const std::string& extension_id = context()->GetExtensionID();
|
| + if (IncrementEventListenerCount(context(), event_name) == 1) {
|
| content::RenderThread::Get()->Send(new ExtensionHostMsg_AddListener(
|
| extension_id, context()->GetURL(), event_name));
|
| }
|
| @@ -173,10 +198,8 @@ void EventBindings::DetachEvent(
|
| std::string event_name = *v8::String::Utf8Value(args[0]);
|
| bool is_manual = args[1]->BooleanValue();
|
|
|
| - std::string extension_id = context()->GetExtensionID();
|
| - EventListenerCounts& listener_counts = g_listener_counts.Get()[extension_id];
|
| -
|
| - if (--listener_counts[event_name] == 0) {
|
| + const std::string& extension_id = context()->GetExtensionID();
|
| + if (DecrementEventListenerCount(context(), event_name) == 0) {
|
| content::RenderThread::Get()->Send(new ExtensionHostMsg_RemoveListener(
|
| extension_id, context()->GetURL(), event_name));
|
| }
|
|
|