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

Side by Side Diff: chrome/renderer/extensions/extension_dispatcher.cc

Issue 9460002: Convert app_bindings.js to the schema_generated_bindings.js infrastructure. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: aa comments Created 8 years, 9 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/api/extension_api.h"
10 #include "chrome/common/extensions/extension.h" 11 #include "chrome/common/extensions/extension.h"
11 #include "chrome/common/extensions/extension_messages.h" 12 #include "chrome/common/extensions/extension_messages.h"
12 #include "chrome/common/extensions/extension_permission_set.h" 13 #include "chrome/common/extensions/extension_permission_set.h"
13 #include "chrome/common/url_constants.h" 14 #include "chrome/common/url_constants.h"
14 #include "chrome/renderer/chrome_render_process_observer.h" 15 #include "chrome/renderer/chrome_render_process_observer.h"
15 #include "chrome/renderer/extensions/app_bindings.h"
16 #include "chrome/renderer/extensions/chrome_v8_context.h" 16 #include "chrome/renderer/extensions/chrome_v8_context.h"
17 #include "chrome/renderer/extensions/chrome_v8_extension.h" 17 #include "chrome/renderer/extensions/chrome_v8_extension.h"
18 #include "chrome/renderer/extensions/custom_bindings_util.h" 18 #include "chrome/renderer/extensions/custom_bindings_util.h"
19 #include "chrome/renderer/extensions/event_bindings.h" 19 #include "chrome/renderer/extensions/event_bindings.h"
20 #include "chrome/renderer/extensions/extension_groups.h" 20 #include "chrome/renderer/extensions/extension_groups.h"
21 #include "chrome/renderer/extensions/miscellaneous_bindings.h" 21 #include "chrome/renderer/extensions/miscellaneous_bindings.h"
22 #include "chrome/renderer/extensions/schema_generated_bindings.h" 22 #include "chrome/renderer/extensions/schema_generated_bindings.h"
23 #include "chrome/renderer/extensions/user_script_slave.h" 23 #include "chrome/renderer/extensions/user_script_slave.h"
24 #include "chrome/renderer/extensions/webstore_bindings.h" 24 #include "chrome/renderer/extensions/webstore_bindings.h"
25 #include "content/public/renderer/render_thread.h" 25 #include "content/public/renderer/render_thread.h"
26 #include "grit/renderer_resources.h" 26 #include "grit/renderer_resources.h"
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h"
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
39 static const int64 kInitialExtensionIdleHandlerDelayMs = 5*1000; 39 static const int64 kInitialExtensionIdleHandlerDelayMs = 5*1000;
40 static const int64 kMaxExtensionIdleHandlerDelayMs = 5*60*1000; 40 static const int64 kMaxExtensionIdleHandlerDelayMs = 5*60*1000;
41 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
48 } 42 }
49 43
50 using namespace extensions; 44 using namespace extensions;
51 45
52 using WebKit::WebDataSource; 46 using WebKit::WebDataSource;
53 using WebKit::WebDocument; 47 using WebKit::WebDocument;
54 using WebKit::WebFrame; 48 using WebKit::WebFrame;
55 using WebKit::WebSecurityPolicy; 49 using WebKit::WebSecurityPolicy;
56 using WebKit::WebString; 50 using WebKit::WebString;
57 using WebKit::WebVector; 51 using WebKit::WebVector;
(...skipping 26 matching lines...) Expand all
84 bool handled = true; 78 bool handled = true;
85 IPC_BEGIN_MESSAGE_MAP(ExtensionDispatcher, message) 79 IPC_BEGIN_MESSAGE_MAP(ExtensionDispatcher, message)
86 IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnMessageInvoke) 80 IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnMessageInvoke)
87 IPC_MESSAGE_HANDLER(ExtensionMsg_DeliverMessage, OnDeliverMessage) 81 IPC_MESSAGE_HANDLER(ExtensionMsg_DeliverMessage, OnDeliverMessage)
88 IPC_MESSAGE_HANDLER(ExtensionMsg_SetFunctionNames, OnSetFunctionNames) 82 IPC_MESSAGE_HANDLER(ExtensionMsg_SetFunctionNames, OnSetFunctionNames)
89 IPC_MESSAGE_HANDLER(ExtensionMsg_Loaded, OnLoaded) 83 IPC_MESSAGE_HANDLER(ExtensionMsg_Loaded, OnLoaded)
90 IPC_MESSAGE_HANDLER(ExtensionMsg_Unloaded, OnUnloaded) 84 IPC_MESSAGE_HANDLER(ExtensionMsg_Unloaded, OnUnloaded)
91 IPC_MESSAGE_HANDLER(ExtensionMsg_SetScriptingWhitelist, 85 IPC_MESSAGE_HANDLER(ExtensionMsg_SetScriptingWhitelist,
92 OnSetScriptingWhitelist) 86 OnSetScriptingWhitelist)
93 IPC_MESSAGE_HANDLER(ExtensionMsg_ActivateExtension, OnActivateExtension) 87 IPC_MESSAGE_HANDLER(ExtensionMsg_ActivateExtension, OnActivateExtension)
94 IPC_MESSAGE_HANDLER(ExtensionMsg_ActivateApplication, OnActivateApplication)
95 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdatePermissions, OnUpdatePermissions) 88 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdatePermissions, OnUpdatePermissions)
96 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateUserScripts, OnUpdateUserScripts) 89 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateUserScripts, OnUpdateUserScripts)
97 IPC_MESSAGE_HANDLER(ExtensionMsg_UsingWebRequestAPI, OnUsingWebRequestAPI) 90 IPC_MESSAGE_HANDLER(ExtensionMsg_UsingWebRequestAPI, OnUsingWebRequestAPI)
98 IPC_MESSAGE_UNHANDLED(handled = false) 91 IPC_MESSAGE_UNHANDLED(handled = false)
99 IPC_END_MESSAGE_MAP() 92 IPC_END_MESSAGE_MAP()
100 93
101 return handled; 94 return handled;
102 } 95 }
103 96
104 void ExtensionDispatcher::WebKitInitialized() { 97 void ExtensionDispatcher::WebKitInitialized() {
105 // For extensions, we want to ensure we call the IdleHandler every so often, 98 // For extensions, we want to ensure we call the IdleHandler every so often,
106 // even if the extension keeps up activity. 99 // even if the extension keeps up activity.
107 if (is_extension_process_) { 100 if (is_extension_process_) {
108 forced_idle_timer_.Start(FROM_HERE, 101 forced_idle_timer_.Start(FROM_HERE,
109 base::TimeDelta::FromMilliseconds(kMaxExtensionIdleHandlerDelayMs), 102 base::TimeDelta::FromMilliseconds(kMaxExtensionIdleHandlerDelayMs),
110 RenderThread::Get(), &RenderThread::IdleHandler); 103 RenderThread::Get(), &RenderThread::IdleHandler);
111 } 104 }
112 105
113 RegisterExtension(new AppBindings(this), false); 106 // Unrestricted extension-related v8-extensions.
107 RegisterExtension(EventBindings::Get(this), false);
108 RegisterExtension(SchemaGeneratedBindings::Get(this), false);
109 RegisterExtension(new ChromeV8Extension(
110 "extensions/json_schema.js", IDR_JSON_SCHEMA_JS, NULL), false);
111 // TODO(kalman): move this to the custom_bindings infrastructure.
114 RegisterExtension(new WebstoreBindings(this), false); 112 RegisterExtension(new WebstoreBindings(this), false);
115 113
116 // Add v8 extensions related to chrome extensions. 114 // Permissions-checked extension-related v8-extensions.
117 RegisterExtension(new ChromeV8Extension(
118 "extensions/json_schema.js", IDR_JSON_SCHEMA_JS, NULL), true);
119 RegisterExtension(EventBindings::Get(this), true);
120 RegisterExtension(MiscellaneousBindings::Get(this), true); 115 RegisterExtension(MiscellaneousBindings::Get(this), true);
121 RegisterExtension(SchemaGeneratedBindings::Get(this), true);
122 RegisterExtension(new ChromeV8Extension( 116 RegisterExtension(new ChromeV8Extension(
123 "extensions/apitest.js", IDR_EXTENSION_APITEST_JS, NULL), true); 117 "extensions/apitest.js", IDR_EXTENSION_APITEST_JS, NULL), true);
124 118
125 std::vector<v8::Extension*> custom_bindings = 119 std::vector<v8::Extension*> custom_bindings =
126 custom_bindings_util::GetAll(this); 120 custom_bindings_util::GetAll(this);
127 for (std::vector<v8::Extension*>::iterator it = custom_bindings.begin(); 121 for (std::vector<v8::Extension*>::iterator it = custom_bindings.begin();
128 it != custom_bindings.end(); ++it) { 122 it != custom_bindings.end(); ++it) {
129 RegisterExtension(*it, true); 123 RegisterExtension(*it, true);
130 } 124 }
131 125
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 // We don't do anything with existing platform-app stylesheets. They will 248 // We don't do anything with existing platform-app stylesheets. They will
255 // stay resident, but the URL pattern corresponding to the unloaded 249 // stay resident, but the URL pattern corresponding to the unloaded
256 // extension's URL just won't match anything anymore. 250 // extension's URL just won't match anything anymore.
257 } 251 }
258 252
259 void ExtensionDispatcher::OnSetScriptingWhitelist( 253 void ExtensionDispatcher::OnSetScriptingWhitelist(
260 const Extension::ScriptingWhitelist& extension_ids) { 254 const Extension::ScriptingWhitelist& extension_ids) {
261 Extension::SetScriptingWhitelist(extension_ids); 255 Extension::SetScriptingWhitelist(extension_ids);
262 } 256 }
263 257
264 bool ExtensionDispatcher::IsApplicationActive(
265 const std::string& extension_id) const {
266 return active_application_ids_.find(extension_id) !=
267 active_application_ids_.end();
268 }
269
270 bool ExtensionDispatcher::IsExtensionActive( 258 bool ExtensionDispatcher::IsExtensionActive(
271 const std::string& extension_id) const { 259 const std::string& extension_id) const {
272 return active_extension_ids_.find(extension_id) != 260 return active_extension_ids_.find(extension_id) !=
273 active_extension_ids_.end(); 261 active_extension_ids_.end();
274 } 262 }
275 263
276 bool ExtensionDispatcher::AllowScriptExtension( 264 bool ExtensionDispatcher::AllowScriptExtension(
277 WebFrame* frame, 265 WebFrame* frame,
278 const std::string& v8_extension_name, 266 const std::string& v8_extension_name,
279 int extension_group) { 267 int extension_group) {
(...skipping 24 matching lines...) Expand all
304 return true; 292 return true;
305 293
306 // If we don't know about it, it was added by WebCore, so we should allow it. 294 // If we don't know about it, it was added by WebCore, so we should allow it.
307 if (!RenderThread::Get()->IsRegisteredExtension(v8_extension_name)) 295 if (!RenderThread::Get()->IsRegisteredExtension(v8_extension_name))
308 return true; 296 return true;
309 297
310 // If the V8 extension is not restricted, allow it to run anywhere. 298 // If the V8 extension is not restricted, allow it to run anywhere.
311 if (!restricted_v8_extensions_.count(v8_extension_name)) 299 if (!restricted_v8_extensions_.count(v8_extension_name))
312 return true; 300 return true;
313 301
314 // Extension-only bindings should be restricted to content scripts and
315 // extension-blessed URLs.
316 ChromeV8Context::ContextType context_type = 302 ChromeV8Context::ContextType context_type =
317 ExtensionGroupToContextType(extension_group); 303 GetContextType(extension_group, frame);
318 304
319 if (context_type == ChromeV8Context::CONTENT_SCRIPT || 305 std::string custom_binding_api_name =
Aaron Boodman 2012/02/29 01:03:58 If you make the change I suggested in schema_gener
not at google - send to devlin 2012/03/05 07:46:54 Done... kinda. I consolidated all the logic in Ex
320 extensions_.ExtensionBindingsAllowed(ExtensionURLInfo( 306 custom_bindings_util::GetAPIName(v8_extension_name);
321 frame->document().securityOrigin(), 307 if (!custom_binding_api_name.empty()) {
322 UserScriptSlave::GetDataSourceURLForFrame(frame)))) { 308 switch (context_type) {
323 // If the extension is a custom API binding, only allow if the extension 309 case ChromeV8Context::PRIVILEGED:
324 // has permission to use the API. 310 case ChromeV8Context::CONTENT_SCRIPT:
325 std::string custom_binding_api_name = 311 case ChromeV8Context::UNPRIVILEGED: {
326 custom_bindings_util::GetAPIName(v8_extension_name); 312 std::string extension_id = GetExtensionID(frame, world_id);
327 if (!custom_binding_api_name.empty()) { 313 const Extension* extension = extensions_.GetByID(extension_id);
328 std::string extension_id = GetExtensionID(frame, world_id); 314 if (!extension) {
329 const Extension* extension = extensions_.GetByID(extension_id); 315 // This can happen when a resource is blocked due to CSP; a valid
330 if (!extension) { 316 // chrome-extension:// URL is navigated to, so it passes the initial
331 // This can happen when a resource is blocked due to CSP; a valid 317 // checks, but the URL gets changed to "chrome-extension://invalid"
332 // chrome-extension:// URL is navigated to, so it passes the initial 318 // afterwards (see chrome_content_renderer_client.cc). An extension
333 // checks, but the URL gets changed to "chrome-extension://invalid" 319 // page still gets loaded, just for the extension with ID "invalid",
334 // afterwards (see chrome_content_renderer_client.cc). An extension 320 // which of course isn't found so GetById extension will be NULL.
335 // page still gets loaded, just for the extension with ID "invalid", 321 //
336 // which of course isn't found so GetById extension will be NULL. 322 // Reference: http://crbug.com/111614.
337 // 323 CHECK_EQ("invalid", extension_id);
338 // Reference: http://crbug.com/111614. 324 return false;
339 CHECK_EQ("invalid", extension_id); 325 }
340 return false; 326 return custom_bindings_util::AllowAPIInjection(
327 custom_binding_api_name, *extension, context_type);
341 } 328 }
342 return custom_bindings_util::AllowAPIInjection( 329
343 custom_binding_api_name, *extension, this); 330 case ChromeV8Context::WEB_PAGE:
331 return ExtensionAPI::GetInstance()->MatchesURL(
332 custom_binding_api_name,
333 UserScriptSlave::GetDataSourceURLForFrame(frame));
344 } 334 }
345
346 return true;
347 } 335 }
348 336
349 return false; 337 return context_type != ChromeV8Context::WEB_PAGE;
350 } 338 }
351 339
352 void ExtensionDispatcher::DidCreateScriptContext( 340 void ExtensionDispatcher::DidCreateScriptContext(
353 WebFrame* frame, v8::Handle<v8::Context> v8_context, int world_id) { 341 WebFrame* frame, v8::Handle<v8::Context> v8_context, int world_id) {
354 ChromeV8Context* context = 342 ChromeV8Context* context =
355 new ChromeV8Context( 343 new ChromeV8Context(
356 v8_context, 344 v8_context,
357 frame, 345 frame,
358 GetExtensionID(frame, world_id), 346 GetExtensionID(frame, world_id),
359 ExtensionGroupToContextType( 347 GetContextType(hack_DidCreateScriptContext_extension_group, frame));
360 hack_DidCreateScriptContext_extension_group));
361 v8_context_set_.Add(context); 348 v8_context_set_.Add(context);
362 349
363 const Extension* extension = extensions_.GetByID(context->extension_id()); 350 const Extension* extension = extensions_.GetByID(context->extension_id());
364 int manifest_version = 1; 351 int manifest_version = 1;
365 if (extension) 352 if (extension)
366 manifest_version = extension->manifest_version(); 353 manifest_version = extension->manifest_version();
367 354
368 context->DispatchOnLoadEvent( 355 context->DispatchOnLoadEvent(
369 is_extension_process_, 356 is_extension_process_,
370 ChromeRenderProcessObserver::is_incognito_process(), 357 ChromeRenderProcessObserver::is_incognito_process(),
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 } 393 }
407 394
408 void ExtensionDispatcher::SetTestExtensionId(const std::string& id) { 395 void ExtensionDispatcher::SetTestExtensionId(const std::string& id) {
409 test_extension_id_ = id; 396 test_extension_id_ = id;
410 } 397 }
411 398
412 bool ExtensionDispatcher::IsTestExtensionId(const std::string& id) { 399 bool ExtensionDispatcher::IsTestExtensionId(const std::string& id) {
413 return id == test_extension_id_; 400 return id == test_extension_id_;
414 } 401 }
415 402
416 void ExtensionDispatcher::OnActivateApplication(
417 const std::string& extension_id) {
418 active_application_ids_.insert(extension_id);
419 }
420
421 void ExtensionDispatcher::OnActivateExtension( 403 void ExtensionDispatcher::OnActivateExtension(
422 const std::string& extension_id) { 404 const std::string& extension_id) {
423 active_extension_ids_.insert(extension_id); 405 active_extension_ids_.insert(extension_id);
424 406
425 // This is called when starting a new extension page, so start the idle 407 // This is called when starting a new extension page, so start the idle
426 // handler ticking. 408 // handler ticking.
427 RenderThread::Get()->ScheduleIdleHandler(kInitialExtensionIdleHandlerDelayMs); 409 RenderThread::Get()->ScheduleIdleHandler(kInitialExtensionIdleHandlerDelayMs);
428 410
429 UpdateActiveExtensions(); 411 UpdateActiveExtensions();
430 412
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 514
533 RenderThread::Get()->RegisterExtension(extension); 515 RenderThread::Get()->RegisterExtension(extension);
534 } 516 }
535 517
536 void ExtensionDispatcher::OnUsingWebRequestAPI( 518 void ExtensionDispatcher::OnUsingWebRequestAPI(
537 bool adblock, bool adblock_plus, bool other) { 519 bool adblock, bool adblock_plus, bool other) {
538 webrequest_adblock_ = adblock; 520 webrequest_adblock_ = adblock;
539 webrequest_adblock_plus_ = adblock_plus; 521 webrequest_adblock_plus_ = adblock_plus;
540 webrequest_other_ = other; 522 webrequest_other_ = other;
541 } 523 }
524
525 ChromeV8Context::ContextType ExtensionDispatcher::GetContextType(
Aaron Boodman 2012/02/29 01:03:58 Naming suggestion: ClassifyJavaScriptContext ?
not at google - send to devlin 2012/03/05 07:46:54 Done.
526 int extension_group, WebFrame* frame) {
527 if (extension_group == EXTENSION_GROUP_CONTENT_SCRIPTS)
528 return ChromeV8Context::CONTENT_SCRIPT;
529
530 ExtensionURLInfo url_info(frame->document().securityOrigin(),
531 UserScriptSlave::GetDataSourceURLForFrame(frame));
532
533 const Extension* extension = extensions_.GetExtensionOrAppByURL(url_info);
534 if (extension && IsExtensionActive(extension->id()))
535 return ChromeV8Context::PRIVILEGED;
536
537 if (extensions_.ExtensionBindingsAllowed(url_info))
538 return ChromeV8Context::UNPRIVILEGED;
539
540 return ChromeV8Context::WEB_PAGE;
541 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698