| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/singleton.h" | 8 #include "base/singleton.h" |
| 9 #include "chrome/common/render_messages.h" | 9 #include "chrome/common/render_messages.h" |
| 10 #include "chrome/renderer/extensions/bindings_utils.h" | 10 #include "chrome/renderer/extensions/bindings_utils.h" |
| 11 #include "chrome/renderer/extensions/event_bindings.h" | 11 #include "chrome/renderer/extensions/event_bindings.h" |
| 12 #include "chrome/renderer/js_only_v8_extensions.h" | 12 #include "chrome/renderer/js_only_v8_extensions.h" |
| 13 #include "chrome/renderer/render_thread.h" | 13 #include "chrome/renderer/render_thread.h" |
| 14 #include "grit/renderer_resources.h" | 14 #include "grit/renderer_resources.h" |
| 15 #include "webkit/api/public/WebScriptSource.h" |
| 16 #include "webkit/glue/webframe.h" |
| 17 |
| 18 using WebKit::WebScriptSource; |
| 19 using WebKit::WebString; |
| 15 | 20 |
| 16 namespace { | 21 namespace { |
| 17 | 22 |
| 18 // Keep a local cache of RenderThread so that we can mock it out for unit tests. | 23 // Keep a local cache of RenderThread so that we can mock it out for unit tests. |
| 19 static RenderThreadBase* render_thread = NULL; | 24 static RenderThreadBase* render_thread = NULL; |
| 20 | 25 |
| 21 static RenderThreadBase* GetRenderThread() { | |
| 22 return render_thread ? render_thread : RenderThread::current(); | |
| 23 } | |
| 24 | |
| 25 // Keep a list of contexts that have registered themselves with us. This lets | 26 // Keep a list of contexts that have registered themselves with us. This lets |
| 26 // us know where to dispatch events when we receive them. | 27 // us know where to dispatch events when we receive them. |
| 27 typedef std::list< v8::Persistent<v8::Context> > ContextList; | 28 typedef std::list< v8::Persistent<v8::Context> > ContextList; |
| 28 struct ExtensionData { | 29 struct ExtensionData { |
| 29 ContextList contexts; | 30 ContextList contexts; |
| 30 std::map<std::string, int> listener_count; | 31 std::map<std::string, int> listener_count; |
| 31 }; | 32 }; |
| 32 ContextList& GetRegisteredContexts() { | 33 ContextList& GetRegisteredContexts() { |
| 33 return Singleton<ExtensionData>::get()->contexts; | 34 return Singleton<ExtensionData>::get()->contexts; |
| 34 } | 35 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 GetRegisteredContexts().push_back(context); | 85 GetRegisteredContexts().push_back(context); |
| 85 context.MakeWeak(NULL, WeakContextCallback); | 86 context.MakeWeak(NULL, WeakContextCallback); |
| 86 } | 87 } |
| 87 global->SetHiddenValue( | 88 global->SetHiddenValue( |
| 88 v8::String::New(kContextAttachCount), | 89 v8::String::New(kContextAttachCount), |
| 89 v8::Integer::New(account_count_value + 1)); | 90 v8::Integer::New(account_count_value + 1)); |
| 90 | 91 |
| 91 if (args[0]->IsString()) { | 92 if (args[0]->IsString()) { |
| 92 std::string event_name(*v8::String::AsciiValue(args[0])); | 93 std::string event_name(*v8::String::AsciiValue(args[0])); |
| 93 if (EventIncrementListenerCount(event_name) == 1) { | 94 if (EventIncrementListenerCount(event_name) == 1) { |
| 94 GetRenderThread()->Send( | 95 EventBindings::GetRenderThread()->Send( |
| 95 new ViewHostMsg_ExtensionAddListener(event_name)); | 96 new ViewHostMsg_ExtensionAddListener(event_name)); |
| 96 } | 97 } |
| 97 } | 98 } |
| 98 | 99 |
| 99 return v8::Undefined(); | 100 return v8::Undefined(); |
| 100 } | 101 } |
| 101 | 102 |
| 102 static v8::Handle<v8::Value> DetachEvent(const v8::Arguments& args) { | 103 static v8::Handle<v8::Value> DetachEvent(const v8::Arguments& args) { |
| 103 DCHECK(args.Length() == 1); | 104 DCHECK(args.Length() == 1); |
| 104 // TODO(erikkay) should enforce that event name is a string in the bindings | 105 // TODO(erikkay) should enforce that event name is a string in the bindings |
| (...skipping 10 matching lines...) Expand all Loading... |
| 115 // Clean up after last detach. | 116 // Clean up after last detach. |
| 116 UnregisterContext(context); | 117 UnregisterContext(context); |
| 117 } | 118 } |
| 118 global->SetHiddenValue( | 119 global->SetHiddenValue( |
| 119 v8::String::New(kContextAttachCount), | 120 v8::String::New(kContextAttachCount), |
| 120 v8::Integer::New(account_count_value - 1)); | 121 v8::Integer::New(account_count_value - 1)); |
| 121 | 122 |
| 122 if (args[0]->IsString()) { | 123 if (args[0]->IsString()) { |
| 123 std::string event_name(*v8::String::AsciiValue(args[0])); | 124 std::string event_name(*v8::String::AsciiValue(args[0])); |
| 124 if (EventDecrementListenerCount(event_name) == 0) { | 125 if (EventDecrementListenerCount(event_name) == 0) { |
| 125 GetRenderThread()->Send( | 126 EventBindings::GetRenderThread()->Send( |
| 126 new ViewHostMsg_ExtensionRemoveListener(event_name)); | 127 new ViewHostMsg_ExtensionRemoveListener(event_name)); |
| 127 } | 128 } |
| 128 } | 129 } |
| 129 | 130 |
| 130 return v8::Undefined(); | 131 return v8::Undefined(); |
| 131 } | 132 } |
| 132 | 133 |
| 133 // Called when a registered context is garbage collected. | 134 // Called when a registered context is garbage collected. |
| 134 static void UnregisterContext(v8::Handle<void> context) { | 135 static void UnregisterContext(v8::Handle<void> context) { |
| 135 ContextList& contexts = GetRegisteredContexts(); | 136 ContextList& contexts = GetRegisteredContexts(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 157 | 158 |
| 158 v8::Extension* EventBindings::Get() { | 159 v8::Extension* EventBindings::Get() { |
| 159 return new ExtensionImpl(); | 160 return new ExtensionImpl(); |
| 160 } | 161 } |
| 161 | 162 |
| 162 // static | 163 // static |
| 163 void EventBindings::SetRenderThread(RenderThreadBase* thread) { | 164 void EventBindings::SetRenderThread(RenderThreadBase* thread) { |
| 164 render_thread = thread; | 165 render_thread = thread; |
| 165 } | 166 } |
| 166 | 167 |
| 168 // static |
| 169 RenderThreadBase* EventBindings::GetRenderThread() { |
| 170 return render_thread ? render_thread : RenderThread::current(); |
| 171 } |
| 172 |
| 173 // static |
| 174 void EventBindings::HandleDocumentReady(WebFrame* frame) { |
| 175 frame->ExecuteScript(WebScriptSource(WebString::fromUTF8( |
| 176 "chrome.dispatchOnLoad_();"))); |
| 177 } |
| 178 |
| 179 // static |
| 180 void EventBindings::HandleDocumentClose(WebFrame* frame) { |
| 181 frame->ExecuteScript(WebScriptSource(WebString::fromUTF8( |
| 182 "chrome.dispatchOnUnload_();"))); |
| 183 } |
| 184 |
| 185 // static |
| 167 void EventBindings::CallFunction(const std::string& function_name, | 186 void EventBindings::CallFunction(const std::string& function_name, |
| 168 int argc, v8::Handle<v8::Value>* argv) { | 187 int argc, v8::Handle<v8::Value>* argv) { |
| 169 for (ContextList::iterator it = GetRegisteredContexts().begin(); | 188 for (ContextList::iterator it = GetRegisteredContexts().begin(); |
| 170 it != GetRegisteredContexts().end(); ++it) { | 189 it != GetRegisteredContexts().end(); ++it) { |
| 171 CallFunctionInContext(*it, function_name, argc, argv); | 190 CallFunctionInContext(*it, function_name, argc, argv); |
| 172 } | 191 } |
| 173 } | 192 } |
| OLD | NEW |