| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/renderer/extensions/event_bindings.h" | 5 #include "chrome/renderer/extensions/event_bindings.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 self->extension_dispatcher()->v8_context_set(); | 70 self->extension_dispatcher()->v8_context_set(); |
| 71 ChromeV8Context* context = context_set.GetCurrent(); | 71 ChromeV8Context* context = context_set.GetCurrent(); |
| 72 CHECK(context); | 72 CHECK(context); |
| 73 std::string event_name(*v8::String::AsciiValue(args[0])); | 73 std::string event_name(*v8::String::AsciiValue(args[0])); |
| 74 | 74 |
| 75 ExtensionDispatcher* extension_dispatcher = self->extension_dispatcher(); | 75 ExtensionDispatcher* extension_dispatcher = self->extension_dispatcher(); |
| 76 if (!extension_dispatcher->CheckCurrentContextAccessToExtensionAPI( | 76 if (!extension_dispatcher->CheckCurrentContextAccessToExtensionAPI( |
| 77 event_name)) | 77 event_name)) |
| 78 return v8::Undefined(); | 78 return v8::Undefined(); |
| 79 | 79 |
| 80 std::string extension_id = context->GetExtensionID(); |
| 80 EventListenerCounts& listener_counts = | 81 EventListenerCounts& listener_counts = |
| 81 g_listener_counts.Get()[context->extension_id()]; | 82 g_listener_counts.Get()[extension_id]; |
| 82 if (++listener_counts[event_name] == 1) { | 83 if (++listener_counts[event_name] == 1) { |
| 83 content::RenderThread::Get()->Send( | 84 content::RenderThread::Get()->Send( |
| 84 new ExtensionHostMsg_AddListener(context->extension_id(), | 85 new ExtensionHostMsg_AddListener(extension_id, event_name)); |
| 85 event_name)); | |
| 86 } | 86 } |
| 87 | 87 |
| 88 // This is called the first time the page has added a listener. Since | 88 // This is called the first time the page has added a listener. Since |
| 89 // the background page is the only lazy page, we know this is the first | 89 // the background page is the only lazy page, we know this is the first |
| 90 // time this listener has been registered. | 90 // time this listener has been registered. |
| 91 if (self->IsLazyBackgroundPage(context->extension_id())) { | 91 if (self->IsLazyBackgroundPage(context->extension())) { |
| 92 content::RenderThread::Get()->Send( | 92 content::RenderThread::Get()->Send( |
| 93 new ExtensionHostMsg_AddLazyListener(context->extension_id(), | 93 new ExtensionHostMsg_AddLazyListener(extension_id, event_name)); |
| 94 event_name)); | |
| 95 } | 94 } |
| 96 } | 95 } |
| 97 | 96 |
| 98 return v8::Undefined(); | 97 return v8::Undefined(); |
| 99 } | 98 } |
| 100 | 99 |
| 101 static v8::Handle<v8::Value> DetachEvent(const v8::Arguments& args) { | 100 static v8::Handle<v8::Value> DetachEvent(const v8::Arguments& args) { |
| 102 DCHECK(args.Length() == 2); | 101 DCHECK(args.Length() == 2); |
| 103 // TODO(erikkay) should enforce that event name is a string in the bindings | 102 // TODO(erikkay) should enforce that event name is a string in the bindings |
| 104 DCHECK(args[0]->IsString() || args[0]->IsUndefined()); | 103 DCHECK(args[0]->IsString() || args[0]->IsUndefined()); |
| 105 | 104 |
| 106 if (args[0]->IsString() && args[1]->IsBoolean()) { | 105 if (args[0]->IsString() && args[1]->IsBoolean()) { |
| 107 ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args); | 106 ExtensionImpl* self = GetFromArguments<ExtensionImpl>(args); |
| 108 const ChromeV8ContextSet& context_set = | 107 const ChromeV8ContextSet& context_set = |
| 109 self->extension_dispatcher()->v8_context_set(); | 108 self->extension_dispatcher()->v8_context_set(); |
| 110 ChromeV8Context* context = context_set.GetCurrent(); | 109 ChromeV8Context* context = context_set.GetCurrent(); |
| 111 if (!context) | 110 if (!context) |
| 112 return v8::Undefined(); | 111 return v8::Undefined(); |
| 113 | 112 |
| 113 std::string extension_id = context->GetExtensionID(); |
| 114 EventListenerCounts& listener_counts = | 114 EventListenerCounts& listener_counts = |
| 115 g_listener_counts.Get()[context->extension_id()]; | 115 g_listener_counts.Get()[extension_id]; |
| 116 std::string event_name(*v8::String::AsciiValue(args[0])); | 116 std::string event_name(*v8::String::AsciiValue(args[0])); |
| 117 bool is_manual = args[1]->BooleanValue(); | 117 bool is_manual = args[1]->BooleanValue(); |
| 118 | 118 |
| 119 if (--listener_counts[event_name] == 0) { | 119 if (--listener_counts[event_name] == 0) { |
| 120 content::RenderThread::Get()->Send( | 120 content::RenderThread::Get()->Send( |
| 121 new ExtensionHostMsg_RemoveListener(context->extension_id(), | 121 new ExtensionHostMsg_RemoveListener(extension_id, event_name)); |
| 122 event_name)); | |
| 123 } | 122 } |
| 124 | 123 |
| 125 // DetachEvent is called when the last listener for the context is | 124 // DetachEvent is called when the last listener for the context is |
| 126 // removed. If the context is the background page, and it removes the | 125 // removed. If the context is the background page, and it removes the |
| 127 // last listener manually, then we assume that it is no longer interested | 126 // last listener manually, then we assume that it is no longer interested |
| 128 // in being awakened for this event. | 127 // in being awakened for this event. |
| 129 if (is_manual && self->IsLazyBackgroundPage(context->extension_id())) { | 128 if (is_manual && self->IsLazyBackgroundPage(context->extension())) { |
| 130 content::RenderThread::Get()->Send( | 129 content::RenderThread::Get()->Send( |
| 131 new ExtensionHostMsg_RemoveLazyListener(context->extension_id(), | 130 new ExtensionHostMsg_RemoveLazyListener(extension_id, event_name)); |
| 132 event_name)); | |
| 133 } | 131 } |
| 134 } | 132 } |
| 135 | 133 |
| 136 return v8::Undefined(); | 134 return v8::Undefined(); |
| 137 } | 135 } |
| 138 | 136 |
| 139 private: | 137 private: |
| 140 | 138 |
| 141 bool IsLazyBackgroundPage(const std::string& extension_id) { | 139 bool IsLazyBackgroundPage(const Extension* extension) { |
| 142 content::RenderView* render_view = GetCurrentRenderView(); | 140 content::RenderView* render_view = GetCurrentRenderView(); |
| 143 if (!render_view) | 141 if (!render_view) |
| 144 return false; | 142 return false; |
| 145 | 143 |
| 146 ExtensionHelper* helper = ExtensionHelper::Get(render_view); | 144 ExtensionHelper* helper = ExtensionHelper::Get(render_view); |
| 147 const ::Extension* extension = | |
| 148 extension_dispatcher()->extensions()->GetByID(extension_id); | |
| 149 return (extension && extension->has_lazy_background_page() && | 145 return (extension && extension->has_lazy_background_page() && |
| 150 helper->view_type() == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); | 146 helper->view_type() == chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE); |
| 151 } | 147 } |
| 152 }; | 148 }; |
| 153 | 149 |
| 154 } // namespace | 150 } // namespace |
| 155 | 151 |
| 156 ChromeV8Extension* EventBindings::Get(ExtensionDispatcher* dispatcher) { | 152 ChromeV8Extension* EventBindings::Get(ExtensionDispatcher* dispatcher) { |
| 157 return new ExtensionImpl(dispatcher); | 153 return new ExtensionImpl(dispatcher); |
| 158 } | 154 } |
| OLD | NEW |