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

Unified Diff: chrome/renderer/extensions/event_bindings.cc

Issue 9386001: Implement a module system for the extension bindings JS. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: re-jigged to use ModuleSystem Created 8 years, 10 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 side-by-side diff with in-line comments
Download patch
Index: chrome/renderer/extensions/event_bindings.cc
diff --git a/chrome/renderer/extensions/event_bindings.cc b/chrome/renderer/extensions/event_bindings.cc
index 9bab21a945d08f7f2261a6f47c3a2499b18d8a9a..8a425043f5d3ab3bbcca2e0e5ccc3965ed94fd28 100644
--- a/chrome/renderer/extensions/event_bindings.cc
+++ b/chrome/renderer/extensions/event_bindings.cc
@@ -39,24 +39,22 @@ using content::RenderThread;
namespace {
+// TODO(koz): Merge this into EventBindings.
class ExtensionImpl : public ChromeV8Extension {
public:
- explicit ExtensionImpl(ExtensionDispatcher* dispatcher)
- : ChromeV8Extension("extensions/event.js",
- IDR_EVENT_BINDINGS_JS,
- dispatcher) {
- }
- ~ExtensionImpl() {}
+ // A map of event names to the number of contexts listening to that event.
+ // We notify the browser about event listeners when we transition between 0
+ // and 1.
+ typedef std::map<std::string, int> EventListenerCounts;
- virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
- v8::Handle<v8::String> name) {
- if (name->Equals(v8::String::New("AttachEvent"))) {
- return v8::FunctionTemplate::New(AttachEvent, v8::External::New(this));
- } else if (name->Equals(v8::String::New("DetachEvent"))) {
- return v8::FunctionTemplate::New(DetachEvent, v8::External::New(this));
- }
- return ChromeV8Extension::GetNativeFunction(name);
+ ExtensionImpl(ExtensionDispatcher* dispatcher,
+ std::map<std::string, EventListenerCounts>* listener_counts)
+ : ChromeV8Extension(dispatcher),
+ listener_counts_(listener_counts) {
+ RouteStaticFunction("AttachEvent", &AttachEvent);
+ RouteStaticFunction("DetachEvent", &DetachEvent);
Matt Perry 2012/02/28 20:34:20 Both of these access the ExtensionImpl via GetFrom
koz (OOO until 15th September) 2012/03/01 03:41:56 Yes, that's right. I was planning on making those
}
+ ~ExtensionImpl() {}
// Attach an event name to an object.
static v8::Handle<v8::Value> AttachEvent(const v8::Arguments& args) {
@@ -76,7 +74,7 @@ class ExtensionImpl : public ChromeV8Extension {
return v8::Undefined();
EventListenerCounts& listener_counts =
- self->listener_counts_[context->extension_id()];
+ (*(self->listener_counts_))[context->extension_id()];
if (++listener_counts[event_name] == 1) {
content::RenderThread::Get()->Send(
new ExtensionHostMsg_AddListener(context->extension_id(),
@@ -110,7 +108,7 @@ class ExtensionImpl : public ChromeV8Extension {
return v8::Undefined();
EventListenerCounts& listener_counts =
- self->listener_counts_[context->extension_id()];
+ (*(self->listener_counts_))[context->extension_id()];
std::string event_name(*v8::String::AsciiValue(args[0]));
bool is_manual = args[1]->BooleanValue();
@@ -135,10 +133,6 @@ class ExtensionImpl : public ChromeV8Extension {
}
private:
- // A map of event names to the number of contexts listening to that event.
- // We notify the browser about event listeners when we transition between 0
- // and 1.
- typedef std::map<std::string, int> EventListenerCounts;
bool IsLazyBackgroundPage(const std::string& extension_id) {
content::RenderView* render_view = GetCurrentRenderView();
@@ -152,13 +146,18 @@ class ExtensionImpl : public ChromeV8Extension {
helper->view_type() == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE);
}
- // A map of extension IDs to listener counts for that extension.
- std::map<std::string, EventListenerCounts> listener_counts_;
+ std::map<std::string, ExtensionImpl::EventListenerCounts>* listener_counts_;
};
} // namespace
-v8::Extension* EventBindings::Get(ExtensionDispatcher* dispatcher) {
- static v8::Extension* extension = new ExtensionImpl(dispatcher);
- return extension;
+ChromeV8Extension* EventBindings::Get(ExtensionDispatcher* dispatcher) {
+ // A map of extension IDs to listener counts for that extension.
+ static std::map<std::string, ExtensionImpl::EventListenerCounts>*
+ listener_counts = NULL;
+ if (!listener_counts) {
+ listener_counts =
+ new std::map<std::string, ExtensionImpl::EventListenerCounts>();
+ }
+ return new ExtensionImpl(dispatcher, listener_counts);
Matt Perry 2012/02/28 20:34:20 I don't understand this set of changes. Why change
koz (OOO until 15th September) 2012/03/01 03:41:56 With this change ChromeV8Extensions are no longer
}

Powered by Google App Engine
This is Rietveld 408576698