| 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/extension_dispatcher.h" | 5 #include "chrome/renderer/extensions/extension_dispatcher.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "chrome/common/child_process_logging.h" | 8 #include "chrome/common/child_process_logging.h" |
| 9 #include "chrome/common/chrome_switches.h" | 9 #include "chrome/common/chrome_switches.h" |
| 10 #include "chrome/common/extensions/extension.h" | 10 #include "chrome/common/extensions/extension.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h" | 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h" |
| 31 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | 31 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 32 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques
t.h" | 32 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques
t.h" |
| 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 34 #include "ui/base/resource/resource_bundle.h" | 34 #include "ui/base/resource/resource_bundle.h" |
| 35 #include "v8/include/v8.h" | 35 #include "v8/include/v8.h" |
| 36 | 36 |
| 37 namespace { | 37 namespace { |
| 38 |
| 38 static const int64 kInitialExtensionIdleHandlerDelayMs = 5*1000; | 39 static const int64 kInitialExtensionIdleHandlerDelayMs = 5*1000; |
| 39 static const int64 kMaxExtensionIdleHandlerDelayMs = 5*60*1000; | 40 static const int64 kMaxExtensionIdleHandlerDelayMs = 5*60*1000; |
| 41 |
| 42 ChromeV8Context::ContextType ExtensionGroupToContextType(int extension_group) { |
| 43 if (extension_group == EXTENSION_GROUP_CONTENT_SCRIPTS) |
| 44 return ChromeV8Context::CONTENT_SCRIPT; |
| 45 return ChromeV8Context::OTHER; |
| 46 } |
| 47 |
| 40 } | 48 } |
| 41 | 49 |
| 42 using namespace extensions; | 50 using namespace extensions; |
| 43 | 51 |
| 44 using WebKit::WebDataSource; | 52 using WebKit::WebDataSource; |
| 45 using WebKit::WebDocument; | 53 using WebKit::WebDocument; |
| 46 using WebKit::WebFrame; | 54 using WebKit::WebFrame; |
| 47 using WebKit::WebSecurityPolicy; | 55 using WebKit::WebSecurityPolicy; |
| 48 using WebKit::WebString; | 56 using WebKit::WebString; |
| 49 using WebKit::WebVector; | 57 using WebKit::WebVector; |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 active_extension_ids_.end(); | 273 active_extension_ids_.end(); |
| 266 } | 274 } |
| 267 | 275 |
| 268 bool ExtensionDispatcher::AllowScriptExtension( | 276 bool ExtensionDispatcher::AllowScriptExtension( |
| 269 WebFrame* frame, | 277 WebFrame* frame, |
| 270 const std::string& v8_extension_name, | 278 const std::string& v8_extension_name, |
| 271 int extension_group) { | 279 int extension_group) { |
| 272 return AllowScriptExtension(frame, v8_extension_name, extension_group, 0); | 280 return AllowScriptExtension(frame, v8_extension_name, extension_group, 0); |
| 273 } | 281 } |
| 274 | 282 |
| 283 namespace { |
| 284 |
| 285 // This is what the extension_group variable will be when DidCreateScriptContext |
| 286 // is called. We know because it's the same as what AllowScriptExtension gets |
| 287 // passed, and the two functions are called sequentially from WebKit. |
| 288 // |
| 289 // TODO(koz): Plumb extension_group through to AllowScriptExtension() from |
| 290 // WebKit. |
| 291 static int hack_DidCreateScriptContext_extension_group = 0; |
| 292 |
| 293 } |
| 294 |
| 275 bool ExtensionDispatcher::AllowScriptExtension( | 295 bool ExtensionDispatcher::AllowScriptExtension( |
| 276 WebFrame* frame, | 296 WebFrame* frame, |
| 277 const std::string& v8_extension_name, | 297 const std::string& v8_extension_name, |
| 278 int extension_group, | 298 int extension_group, |
| 279 int world_id) { | 299 int world_id) { |
| 300 hack_DidCreateScriptContext_extension_group = extension_group; |
| 301 |
| 280 // NULL in unit tests. | 302 // NULL in unit tests. |
| 281 if (!RenderThread::Get()) | 303 if (!RenderThread::Get()) |
| 282 return true; | 304 return true; |
| 283 | 305 |
| 284 // If we don't know about it, it was added by WebCore, so we should allow it. | 306 // If we don't know about it, it was added by WebCore, so we should allow it. |
| 285 if (!RenderThread::Get()->IsRegisteredExtension(v8_extension_name)) | 307 if (!RenderThread::Get()->IsRegisteredExtension(v8_extension_name)) |
| 286 return true; | 308 return true; |
| 287 | 309 |
| 288 // If the V8 extension is not restricted, allow it to run anywhere. | 310 // If the V8 extension is not restricted, allow it to run anywhere. |
| 289 if (!restricted_v8_extensions_.count(v8_extension_name)) | 311 if (!restricted_v8_extensions_.count(v8_extension_name)) |
| 290 return true; | 312 return true; |
| 291 | 313 |
| 292 // Extension-only bindings should be restricted to content scripts and | 314 // Extension-only bindings should be restricted to content scripts and |
| 293 // extension-blessed URLs. | 315 // extension-blessed URLs. |
| 294 if (extension_group == EXTENSION_GROUP_CONTENT_SCRIPTS || | 316 ChromeV8Context::ContextType context_type = |
| 317 ExtensionGroupToContextType(extension_group); |
| 318 |
| 319 if (context_type == ChromeV8Context::CONTENT_SCRIPT || |
| 295 extensions_.ExtensionBindingsAllowed(ExtensionURLInfo( | 320 extensions_.ExtensionBindingsAllowed(ExtensionURLInfo( |
| 296 frame->document().securityOrigin(), | 321 frame->document().securityOrigin(), |
| 297 UserScriptSlave::GetDataSourceURLForFrame(frame)))) { | 322 UserScriptSlave::GetDataSourceURLForFrame(frame)))) { |
| 298 // If the extension is a custom API binding, only allow if the extension | 323 // If the extension is a custom API binding, only allow if the extension |
| 299 // has permission to use the API. | 324 // has permission to use the API. |
| 300 std::string custom_binding_api_name = | 325 std::string custom_binding_api_name = |
| 301 custom_bindings_util::GetAPIName(v8_extension_name); | 326 custom_bindings_util::GetAPIName(v8_extension_name); |
| 302 if (!custom_binding_api_name.empty()) { | 327 if (!custom_binding_api_name.empty()) { |
| 303 std::string extension_id = GetExtensionID(frame, world_id); | 328 std::string extension_id = GetExtensionID(frame, world_id); |
| 304 const Extension* extension = extensions_.GetByID(extension_id); | 329 const Extension* extension = extensions_.GetByID(extension_id); |
| 305 if (!extension) { | 330 if (!extension) { |
| 306 // This can happen when a resource is blocked due to CSP; a valid | 331 // This can happen when a resource is blocked due to CSP; a valid |
| 307 // chrome-extension:// URL is navigated to, so it passes the initial | 332 // chrome-extension:// URL is navigated to, so it passes the initial |
| 308 // checks, but the URL gets changed to "chrome-extension://invalid" | 333 // checks, but the URL gets changed to "chrome-extension://invalid" |
| 309 // afterwards (see chrome_content_renderer_client.cc). An extension | 334 // afterwards (see chrome_content_renderer_client.cc). An extension |
| 310 // page still gets loaded, just for the extension with ID "invalid", | 335 // page still gets loaded, just for the extension with ID "invalid", |
| 311 // which of course isn't found so GetById extension will be NULL. | 336 // which of course isn't found so GetById extension will be NULL. |
| 312 // | 337 // |
| 313 // Reference: http://crbug.com/111614. | 338 // Reference: http://crbug.com/111614. |
| 314 CHECK_EQ("invalid", extension_id); | 339 CHECK_EQ("invalid", extension_id); |
| 315 return false; | 340 return false; |
| 316 } | 341 } |
| 317 return custom_bindings_util::AllowAPIInjection( | 342 return custom_bindings_util::AllowAPIInjection( |
| 318 custom_binding_api_name, *extension); | 343 custom_binding_api_name, *extension, context_type); |
| 319 } | 344 } |
| 320 | 345 |
| 321 return true; | 346 return true; |
| 322 } | 347 } |
| 323 | 348 |
| 324 return false; | 349 return false; |
| 325 } | 350 } |
| 326 | 351 |
| 327 void ExtensionDispatcher::DidCreateScriptContext( | 352 void ExtensionDispatcher::DidCreateScriptContext( |
| 328 WebFrame* frame, v8::Handle<v8::Context> v8_context, int world_id) { | 353 WebFrame* frame, v8::Handle<v8::Context> v8_context, int world_id) { |
| 329 ChromeV8Context* context = | 354 ChromeV8Context* context = |
| 330 new ChromeV8Context(v8_context, frame, GetExtensionID(frame, world_id)); | 355 new ChromeV8Context( |
| 356 v8_context, |
| 357 frame, |
| 358 GetExtensionID(frame, world_id), |
| 359 ExtensionGroupToContextType( |
| 360 hack_DidCreateScriptContext_extension_group)); |
| 331 v8_context_set_.Add(context); | 361 v8_context_set_.Add(context); |
| 332 | 362 |
| 333 const Extension* extension = extensions_.GetByID(context->extension_id()); | 363 const Extension* extension = extensions_.GetByID(context->extension_id()); |
| 334 int manifest_version = 1; | 364 int manifest_version = 1; |
| 335 if (extension) | 365 if (extension) |
| 336 manifest_version = extension->manifest_version(); | 366 manifest_version = extension->manifest_version(); |
| 337 | 367 |
| 338 context->DispatchOnLoadEvent( | 368 context->DispatchOnLoadEvent( |
| 339 is_extension_process_, | 369 is_extension_process_, |
| 340 ChromeRenderProcessObserver::is_incognito_process(), | 370 ChromeRenderProcessObserver::is_incognito_process(), |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 | 528 |
| 499 RenderThread::Get()->RegisterExtension(extension); | 529 RenderThread::Get()->RegisterExtension(extension); |
| 500 } | 530 } |
| 501 | 531 |
| 502 void ExtensionDispatcher::OnUsingWebRequestAPI( | 532 void ExtensionDispatcher::OnUsingWebRequestAPI( |
| 503 bool adblock, bool adblock_plus, bool other) { | 533 bool adblock, bool adblock_plus, bool other) { |
| 504 webrequest_adblock_ = adblock; | 534 webrequest_adblock_ = adblock; |
| 505 webrequest_adblock_plus_ = adblock_plus; | 535 webrequest_adblock_plus_ = adblock_plus; |
| 506 webrequest_other_ = other; | 536 webrequest_other_ = other; |
| 507 } | 537 } |
| OLD | NEW |