From a0ab38510fca59d581d06e62d7973345ee5525d2 Mon Sep 17 00:00:00 2001 From: Sadrul Habib Chowdhury Date: Wed, 3 Dec 2014 15:53:54 -0500 Subject: [PATCH] extensions: Make sure a valid key is used to track the event-listener counts. The extension ID can be empty in some scenarios (e.g. in webui pages). In such cases, use the ScriptContext's URL as the key. BUG=434727 R=kalman@chromium.org Review URL: https://codereview.chromium.org/773073002 Cr-Commit-Position: refs/heads/master@{#306672} --- extensions/renderer/event_bindings.cc | 37 ++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/extensions/renderer/event_bindings.cc b/extensions/renderer/event_bindings.cc index 385906c33989..bad6a02542c6 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 > base::LazyInstance 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)); } -- 2.11.4.GIT