| 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/common/url_constants.h" | 10 #include "chrome/common/url_constants.h" |
| 11 #include "chrome/renderer/extensions/bindings_utils.h" | 11 #include "chrome/renderer/extensions/bindings_utils.h" |
| 12 #include "chrome/renderer/extensions/event_bindings.h" | 12 #include "chrome/renderer/extensions/event_bindings.h" |
| 13 #include "chrome/renderer/js_only_v8_extensions.h" | 13 #include "chrome/renderer/js_only_v8_extensions.h" |
| 14 #include "chrome/renderer/render_thread.h" | 14 #include "chrome/renderer/render_thread.h" |
| 15 #include "chrome/renderer/render_view.h" | 15 #include "chrome/renderer/render_view.h" |
| 16 #include "grit/renderer_resources.h" | 16 #include "grit/renderer_resources.h" |
| 17 #include "webkit/api/public/WebDataSource.h" | 17 #include "webkit/api/public/WebDataSource.h" |
| 18 #include "webkit/api/public/WebFrame.h" |
| 18 #include "webkit/api/public/WebURLRequest.h" | 19 #include "webkit/api/public/WebURLRequest.h" |
| 19 #include "webkit/glue/webframe.h" | |
| 20 | 20 |
| 21 using bindings_utils::CallFunctionInContext; | 21 using bindings_utils::CallFunctionInContext; |
| 22 using bindings_utils::ContextInfo; | 22 using bindings_utils::ContextInfo; |
| 23 using bindings_utils::ContextList; | 23 using bindings_utils::ContextList; |
| 24 using bindings_utils::GetContexts; | 24 using bindings_utils::GetContexts; |
| 25 using bindings_utils::GetStringResource; | 25 using bindings_utils::GetStringResource; |
| 26 using bindings_utils::ExtensionBase; | 26 using bindings_utils::ExtensionBase; |
| 27 using bindings_utils::GetPendingRequestMap; | 27 using bindings_utils::GetPendingRequestMap; |
| 28 using bindings_utils::PendingRequestMap; | 28 using bindings_utils::PendingRequestMap; |
| 29 using WebKit::WebFrame; |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 // Keep a local cache of RenderThread so that we can mock it out for unit tests. | 33 // Keep a local cache of RenderThread so that we can mock it out for unit tests. |
| 33 static RenderThreadBase* render_thread = NULL; | 34 static RenderThreadBase* render_thread = NULL; |
| 34 static bool in_unit_tests = false; | 35 static bool in_unit_tests = false; |
| 35 | 36 |
| 36 // Set to true if these bindings are registered. Will be false when extensions | 37 // Set to true if these bindings are registered. Will be false when extensions |
| 37 // are disabled. | 38 // are disabled. |
| 38 static bool bindings_registered = false; | 39 static bool bindings_registered = false; |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 | 193 |
| 193 NOTREACHED(); | 194 NOTREACHED(); |
| 194 } | 195 } |
| 195 | 196 |
| 196 void EventBindings::HandleContextCreated(WebFrame* frame, bool content_script) { | 197 void EventBindings::HandleContextCreated(WebFrame* frame, bool content_script) { |
| 197 if (!bindings_registered) | 198 if (!bindings_registered) |
| 198 return; | 199 return; |
| 199 | 200 |
| 200 v8::HandleScope handle_scope; | 201 v8::HandleScope handle_scope; |
| 201 ContextList& contexts = GetContexts(); | 202 ContextList& contexts = GetContexts(); |
| 202 v8::Local<v8::Context> frame_context = frame->GetMainWorldScriptContext(); | 203 v8::Local<v8::Context> frame_context = frame->mainWorldScriptContext(); |
| 203 v8::Local<v8::Context> context = v8::Context::GetCurrent(); | 204 v8::Local<v8::Context> context = v8::Context::GetCurrent(); |
| 204 DCHECK(!context.IsEmpty()); | 205 DCHECK(!context.IsEmpty()); |
| 205 DCHECK(bindings_utils::FindContext(context) == contexts.end()); | 206 DCHECK(bindings_utils::FindContext(context) == contexts.end()); |
| 206 | 207 |
| 207 // Figure out the URL for the toplevel frame. If the top frame is loading, | 208 // Figure out the URL for the toplevel frame. If the top frame is loading, |
| 208 // use its provisional URL, since we get this notification before commit. | 209 // use its provisional URL, since we get this notification before commit. |
| 209 WebFrame* main_frame = frame->GetView()->GetMainFrame(); | 210 WebFrame* main_frame = frame->view()->GetMainFrame(); |
| 210 WebKit::WebDataSource* ds = main_frame->GetProvisionalDataSource(); | 211 WebKit::WebDataSource* ds = main_frame->provisionalDataSource(); |
| 211 if (!ds) | 212 if (!ds) |
| 212 ds = main_frame->GetDataSource(); | 213 ds = main_frame->dataSource(); |
| 213 GURL url = ds->request().url(); | 214 GURL url = ds->request().url(); |
| 214 std::string extension_id; | 215 std::string extension_id; |
| 215 if (url.SchemeIs(chrome::kExtensionScheme)) { | 216 if (url.SchemeIs(chrome::kExtensionScheme)) { |
| 216 extension_id = url.host(); | 217 extension_id = url.host(); |
| 217 } else if (!content_script) { | 218 } else if (!content_script) { |
| 218 // This context is a regular non-extension web page. Ignore it. We only | 219 // This context is a regular non-extension web page. Ignore it. We only |
| 219 // care about content scripts and extension frames. | 220 // care about content scripts and extension frames. |
| 220 // (Unless we're in unit tests, in which case we don't care what the URL | 221 // (Unless we're in unit tests, in which case we don't care what the URL |
| 221 // is). | 222 // is). |
| 222 DCHECK(frame_context == context); | 223 DCHECK(frame_context == context); |
| 223 if (!in_unit_tests) | 224 if (!in_unit_tests) |
| 224 return; | 225 return; |
| 225 } | 226 } |
| 226 | 227 |
| 227 v8::Persistent<v8::Context> persistent_context = | 228 v8::Persistent<v8::Context> persistent_context = |
| 228 v8::Persistent<v8::Context>::New(context); | 229 v8::Persistent<v8::Context>::New(context); |
| 229 v8::Persistent<v8::Context> parent_context; | 230 v8::Persistent<v8::Context> parent_context; |
| 230 | 231 |
| 231 if (content_script) { | 232 if (content_script) { |
| 232 DCHECK(frame_context != context); | 233 DCHECK(frame_context != context); |
| 233 | 234 |
| 234 parent_context = v8::Persistent<v8::Context>::New(frame_context); | 235 parent_context = v8::Persistent<v8::Context>::New(frame_context); |
| 235 // Content script contexts can get GCed before their frame goes away, so | 236 // Content script contexts can get GCed before their frame goes away, so |
| 236 // set up a GC callback. | 237 // set up a GC callback. |
| 237 persistent_context.MakeWeak(NULL, &ContextWeakReferenceCallback); | 238 persistent_context.MakeWeak(NULL, &ContextWeakReferenceCallback); |
| 238 } | 239 } |
| 239 | 240 |
| 240 RenderView* render_view = NULL; | 241 RenderView* render_view = NULL; |
| 241 if (frame->GetView() && frame->GetView()->GetDelegate()) | 242 if (frame->view() && frame->view()->GetDelegate()) |
| 242 render_view = static_cast<RenderView*>(frame->GetView()->GetDelegate()); | 243 render_view = static_cast<RenderView*>(frame->view()->GetDelegate()); |
| 243 | 244 |
| 244 contexts.push_back(linked_ptr<ContextInfo>( | 245 contexts.push_back(linked_ptr<ContextInfo>( |
| 245 new ContextInfo(persistent_context, extension_id, parent_context, | 246 new ContextInfo(persistent_context, extension_id, parent_context, |
| 246 render_view))); | 247 render_view))); |
| 247 | 248 |
| 248 v8::Handle<v8::Value> argv[1]; | 249 v8::Handle<v8::Value> argv[1]; |
| 249 argv[0] = v8::String::New(extension_id.c_str()); | 250 argv[0] = v8::String::New(extension_id.c_str()); |
| 250 CallFunctionInContext(context, "dispatchOnLoad", arraysize(argv), argv); | 251 CallFunctionInContext(context, "dispatchOnLoad", arraysize(argv), argv); |
| 251 } | 252 } |
| 252 | 253 |
| 253 // static | 254 // static |
| 254 void EventBindings::HandleContextDestroyed(WebFrame* frame) { | 255 void EventBindings::HandleContextDestroyed(WebFrame* frame) { |
| 255 if (!bindings_registered) | 256 if (!bindings_registered) |
| 256 return; | 257 return; |
| 257 | 258 |
| 258 v8::HandleScope handle_scope; | 259 v8::HandleScope handle_scope; |
| 259 v8::Local<v8::Context> context = frame->GetMainWorldScriptContext(); | 260 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); |
| 260 DCHECK(!context.IsEmpty()); | 261 DCHECK(!context.IsEmpty()); |
| 261 | 262 |
| 262 ContextList::iterator context_iter = bindings_utils::FindContext(context); | 263 ContextList::iterator context_iter = bindings_utils::FindContext(context); |
| 263 if (context_iter != GetContexts().end()) | 264 if (context_iter != GetContexts().end()) |
| 264 UnregisterContext(context_iter, false); | 265 UnregisterContext(context_iter, false); |
| 265 | 266 |
| 266 // Unload any content script contexts for this frame. Note that the frame | 267 // Unload any content script contexts for this frame. Note that the frame |
| 267 // itself might not be registered, but can still be a parent context. | 268 // itself might not be registered, but can still be a parent context. |
| 268 for (ContextList::iterator it = GetContexts().begin(); | 269 for (ContextList::iterator it = GetContexts().begin(); |
| 269 it != GetContexts().end(); ) { | 270 it != GetContexts().end(); ) { |
| 270 ContextList::iterator current = it++; | 271 ContextList::iterator current = it++; |
| 271 if ((*current)->parent_context == context) | 272 if ((*current)->parent_context == context) |
| 272 UnregisterContext(current, false); | 273 UnregisterContext(current, false); |
| 273 } | 274 } |
| 274 } | 275 } |
| 275 | 276 |
| 276 // static | 277 // static |
| 277 void EventBindings::CallFunction(const std::string& function_name, | 278 void EventBindings::CallFunction(const std::string& function_name, |
| 278 int argc, v8::Handle<v8::Value>* argv, | 279 int argc, v8::Handle<v8::Value>* argv, |
| 279 RenderView* render_view) { | 280 RenderView* render_view) { |
| 280 for (ContextList::iterator it = GetContexts().begin(); | 281 for (ContextList::iterator it = GetContexts().begin(); |
| 281 it != GetContexts().end(); ++it) { | 282 it != GetContexts().end(); ++it) { |
| 282 if (render_view && render_view != (*it)->render_view) | 283 if (render_view && render_view != (*it)->render_view) |
| 283 continue; | 284 continue; |
| 284 CallFunctionInContext((*it)->context, function_name, argc, argv); | 285 CallFunctionInContext((*it)->context, function_name, argc, argv); |
| 285 } | 286 } |
| 286 } | 287 } |
| OLD | NEW |