Chromium Code Reviews| Index: extensions/renderer/event_bindings.cc |
| diff --git a/extensions/renderer/event_bindings.cc b/extensions/renderer/event_bindings.cc |
| index 385906c33989dcbf508b716b1f9f707a5a81fe9a..bda86c152595d6a09377fb58264702fadb6be3cb 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,29 @@ 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(); |
| + 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 +171,10 @@ 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) { |
| + CHECK(crx_file::id_util::IdIsValid(extension_id) || |
|
not at google - send to devlin
2014/12/03 17:15:45
Can you do this CHECK inside the GetKeyForScriptCo
sadrul
2014/12/03 20:53:37
Done.
|
| + context()->GetURL().is_valid()); |
| content::RenderThread::Get()->Send(new ExtensionHostMsg_AddListener( |
| extension_id, context()->GetURL(), event_name)); |
| } |
| @@ -173,10 +198,10 @@ 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) { |
| + CHECK(crx_file::id_util::IdIsValid(extension_id) || |
| + context()->GetURL().is_valid()); |
| content::RenderThread::Get()->Send(new ExtensionHostMsg_RemoveListener( |
| extension_id, context()->GetURL(), event_name)); |
| } |