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

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

Issue 9386001: Implement a module system for the extension bindings JS. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix compile errors 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/callback.h"
7 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/string_piece.h"
8 #include "chrome/common/child_process_logging.h" 11 #include "chrome/common/child_process_logging.h"
9 #include "chrome/common/chrome_switches.h" 12 #include "chrome/common/chrome_switches.h"
10 #include "chrome/common/extensions/extension.h" 13 #include "chrome/common/extensions/extension.h"
11 #include "chrome/common/extensions/extension_messages.h" 14 #include "chrome/common/extensions/extension_messages.h"
12 #include "chrome/common/extensions/extension_permission_set.h" 15 #include "chrome/common/extensions/extension_permission_set.h"
13 #include "chrome/common/url_constants.h" 16 #include "chrome/common/url_constants.h"
14 #include "chrome/renderer/chrome_render_process_observer.h" 17 #include "chrome/renderer/chrome_render_process_observer.h"
15 #include "chrome/renderer/extensions/app_bindings.h" 18 #include "chrome/renderer/extensions/app_bindings.h"
16 #include "chrome/renderer/extensions/chrome_v8_context.h" 19 #include "chrome/renderer/extensions/chrome_v8_context.h"
17 #include "chrome/renderer/extensions/chrome_v8_extension.h" 20 #include "chrome/renderer/extensions/chrome_v8_extension.h"
18 #include "chrome/renderer/extensions/custom_bindings_util.h" 21 #include "chrome/renderer/extensions/custom_bindings_util.h"
19 #include "chrome/renderer/extensions/event_bindings.h" 22 #include "chrome/renderer/extensions/event_bindings.h"
20 #include "chrome/renderer/extensions/extension_groups.h" 23 #include "chrome/renderer/extensions/extension_groups.h"
21 #include "chrome/renderer/extensions/miscellaneous_bindings.h" 24 #include "chrome/renderer/extensions/miscellaneous_bindings.h"
22 #include "chrome/renderer/extensions/schema_generated_bindings.h" 25 #include "chrome/renderer/extensions/schema_generated_bindings.h"
23 #include "chrome/renderer/extensions/user_script_slave.h" 26 #include "chrome/renderer/extensions/user_script_slave.h"
24 #include "chrome/renderer/extensions/webstore_bindings.h" 27 #include "chrome/renderer/extensions/webstore_bindings.h"
28 #include "chrome/renderer/module_system.h"
29 #include "chrome/renderer/native_handler.h"
30 #include "chrome/renderer/resource_bundle_source_map.h"
25 #include "content/public/renderer/render_thread.h" 31 #include "content/public/renderer/render_thread.h"
26 #include "grit/renderer_resources.h" 32 #include "grit/renderer_resources.h"
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h"
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" 34 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 35 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h" 36 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h"
31 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" 37 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
32 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques t.h" 38 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques t.h"
33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 39 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
34 #include "ui/base/resource/resource_bundle.h" 40 #include "ui/base/resource/resource_bundle.h"
35 #include "v8/include/v8.h" 41 #include "v8/include/v8.h"
36 42
43 #include "chrome/renderer/extensions/chrome_private_custom_bindings.h"
44 #include "chrome/renderer/extensions/context_menus_custom_bindings.h"
45 #include "chrome/renderer/extensions/experimental.socket_custom_bindings.h"
46 #include "chrome/renderer/extensions/extension_custom_bindings.h"
47 #include "chrome/renderer/extensions/file_browser_handler_custom_bindings.h"
48 #include "chrome/renderer/extensions/file_browser_private_custom_bindings.h"
49 #include "chrome/renderer/extensions/i18n_custom_bindings.h"
50 #include "chrome/renderer/extensions/page_actions_custom_bindings.h"
51 #include "chrome/renderer/extensions/page_capture_custom_bindings.h"
52 #include "chrome/renderer/extensions/tabs_custom_bindings.h"
53 #include "chrome/renderer/extensions/tts_custom_bindings.h"
54 #include "chrome/renderer/extensions/web_request_custom_bindings.h"
55
37 namespace { 56 namespace {
38 57
39 static const int64 kInitialExtensionIdleHandlerDelayMs = 5*1000; 58 static const int64 kInitialExtensionIdleHandlerDelayMs = 5*1000;
40 static const int64 kMaxExtensionIdleHandlerDelayMs = 5*60*1000; 59 static const int64 kMaxExtensionIdleHandlerDelayMs = 5*60*1000;
41 60
42 ChromeV8Context::ContextType ExtensionGroupToContextType(int extension_group) { 61 ChromeV8Context::ContextType ExtensionGroupToContextType(int extension_group) {
43 if (extension_group == EXTENSION_GROUP_CONTENT_SCRIPTS) 62 if (extension_group == EXTENSION_GROUP_CONTENT_SCRIPTS)
44 return ChromeV8Context::CONTENT_SCRIPT; 63 return ChromeV8Context::CONTENT_SCRIPT;
45 return ChromeV8Context::OTHER; 64 return ChromeV8Context::OTHER;
46 } 65 }
47 66
48 } 67 }
49 68
50 using namespace extensions; 69 using namespace extensions;
51 70
52 using WebKit::WebDataSource; 71 using WebKit::WebDataSource;
53 using WebKit::WebDocument; 72 using WebKit::WebDocument;
54 using WebKit::WebFrame; 73 using WebKit::WebFrame;
55 using WebKit::WebSecurityPolicy; 74 using WebKit::WebSecurityPolicy;
56 using WebKit::WebString; 75 using WebKit::WebString;
57 using WebKit::WebVector; 76 using WebKit::WebVector;
58 using WebKit::WebView; 77 using WebKit::WebView;
59 using content::RenderThread; 78 using content::RenderThread;
60 79
61 ExtensionDispatcher::ExtensionDispatcher() 80 ExtensionDispatcher::ExtensionDispatcher()
62 : is_webkit_initialized_(false), 81 : is_webkit_initialized_(false),
63 webrequest_adblock_(false), 82 webrequest_adblock_(false),
64 webrequest_adblock_plus_(false), 83 webrequest_adblock_plus_(false),
65 webrequest_other_(false) { 84 webrequest_other_(false),
85 source_map_(&ResourceBundle::GetSharedInstance()) {
66 const CommandLine& command_line = *(CommandLine::ForCurrentProcess()); 86 const CommandLine& command_line = *(CommandLine::ForCurrentProcess());
67 is_extension_process_ = 87 is_extension_process_ =
68 command_line.HasSwitch(switches::kExtensionProcess) || 88 command_line.HasSwitch(switches::kExtensionProcess) ||
69 command_line.HasSwitch(switches::kSingleProcess); 89 command_line.HasSwitch(switches::kSingleProcess);
70 90
71 if (is_extension_process_) { 91 if (is_extension_process_) {
72 RenderThread::Get()->SetIdleNotificationDelayInMs( 92 RenderThread::Get()->SetIdleNotificationDelayInMs(
73 kInitialExtensionIdleHandlerDelayMs); 93 kInitialExtensionIdleHandlerDelayMs);
74 } 94 }
75 95
76 user_script_slave_.reset(new UserScriptSlave(&extensions_)); 96 user_script_slave_.reset(new UserScriptSlave(&extensions_));
97 PopulateSourceMap();
77 } 98 }
78 99
79 ExtensionDispatcher::~ExtensionDispatcher() { 100 ExtensionDispatcher::~ExtensionDispatcher() {
80 } 101 }
81 102
82 bool ExtensionDispatcher::OnControlMessageReceived( 103 bool ExtensionDispatcher::OnControlMessageReceived(
83 const IPC::Message& message) { 104 const IPC::Message& message) {
84 bool handled = true; 105 bool handled = true;
85 IPC_BEGIN_MESSAGE_MAP(ExtensionDispatcher, message) 106 IPC_BEGIN_MESSAGE_MAP(ExtensionDispatcher, message)
86 IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnMessageInvoke) 107 IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnMessageInvoke)
(...skipping 17 matching lines...) Expand all
104 125
105 void ExtensionDispatcher::WebKitInitialized() { 126 void ExtensionDispatcher::WebKitInitialized() {
106 // For extensions, we want to ensure we call the IdleHandler every so often, 127 // For extensions, we want to ensure we call the IdleHandler every so often,
107 // even if the extension keeps up activity. 128 // even if the extension keeps up activity.
108 if (is_extension_process_) { 129 if (is_extension_process_) {
109 forced_idle_timer_.Start(FROM_HERE, 130 forced_idle_timer_.Start(FROM_HERE,
110 base::TimeDelta::FromMilliseconds(kMaxExtensionIdleHandlerDelayMs), 131 base::TimeDelta::FromMilliseconds(kMaxExtensionIdleHandlerDelayMs),
111 RenderThread::Get(), &RenderThread::IdleHandler); 132 RenderThread::Get(), &RenderThread::IdleHandler);
112 } 133 }
113 134
114 RegisterExtension(new AppBindings(this), false);
115 RegisterExtension(new WebstoreBindings(this), false);
116
117 // Add v8 extensions related to chrome extensions.
118 RegisterExtension(new ChromeV8Extension(
119 "extensions/json_schema.js", IDR_JSON_SCHEMA_JS, NULL), true);
120 RegisterExtension(EventBindings::Get(this), true);
121 RegisterExtension(MiscellaneousBindings::Get(this), true);
122 RegisterExtension(SchemaGeneratedBindings::Get(this), true);
123 RegisterExtension(new ChromeV8Extension(
124 "extensions/apitest.js", IDR_EXTENSION_APITEST_JS, NULL), true);
125
126 std::vector<v8::Extension*> custom_bindings =
127 custom_bindings_util::GetAll(this);
128 for (std::vector<v8::Extension*>::iterator it = custom_bindings.begin();
129 it != custom_bindings.end(); ++it) {
130 RegisterExtension(*it, true);
131 }
132
133 // Initialize host permissions for any extensions that were activated before 135 // Initialize host permissions for any extensions that were activated before
134 // WebKit was initialized. 136 // WebKit was initialized.
135 for (std::set<std::string>::iterator iter = active_extension_ids_.begin(); 137 for (std::set<std::string>::iterator iter = active_extension_ids_.begin();
136 iter != active_extension_ids_.end(); ++iter) { 138 iter != active_extension_ids_.end(); ++iter) {
137 const Extension* extension = extensions_.GetByID(*iter); 139 const Extension* extension = extensions_.GetByID(*iter);
138 if (extension) 140 if (extension)
139 InitOriginPermissions(extension); 141 InitOriginPermissions(extension);
140 } 142 }
141 143
142 is_webkit_initialized_ = true; 144 is_webkit_initialized_ = true;
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 static int g_hack_extension_group = 0; 286 static int g_hack_extension_group = 0;
285 287
286 } 288 }
287 289
288 bool ExtensionDispatcher::AllowScriptExtension( 290 bool ExtensionDispatcher::AllowScriptExtension(
289 WebFrame* frame, 291 WebFrame* frame,
290 const std::string& v8_extension_name, 292 const std::string& v8_extension_name,
291 int extension_group, 293 int extension_group,
292 int world_id) { 294 int world_id) {
293 g_hack_extension_group = extension_group; 295 g_hack_extension_group = extension_group;
294 296 return true;
295 // NULL in unit tests. 297 }
296 if (!RenderThread::Get()) 298
297 return true; 299 bool ExtensionDispatcher::AllowCustomAPI(WebFrame* frame,
298 300 const std::string& custom_binding_api_name,
Matt Perry 2012/03/01 23:23:06 indent is wonky here
koz (OOO until 15th September) 2012/03/02 01:13:58 Done.
299 // If we don't know about it, it was added by WebCore, so we should allow it. 301 int world_id) {
300 if (!RenderThread::Get()->IsRegisteredExtension(v8_extension_name)) 302 std::string extension_id = GetExtensionID(frame, world_id);
301 return true; 303 const Extension* extension = extensions_.GetByID(extension_id);
302 304 if (!extension) {
303 // If the V8 extension is not restricted, allow it to run anywhere. 305 // This can happen when a resource is blocked due to CSP; a valid
304 if (!restricted_v8_extensions_.count(v8_extension_name)) 306 // chrome-extension:// URL is navigated to, so it passes the initial
305 return true; 307 // checks, but the URL gets changed to "chrome-extension://invalid"
306 308 // afterwards (see chrome_content_renderer_client.cc). An extension
307 // Extension-only bindings should be restricted to content scripts and 309 // page still gets loaded, just for the extension with ID "invalid",
308 // extension-blessed URLs. 310 // which of course isn't found so GetById extension will be NULL.
309 ChromeV8Context::ContextType context_type = 311 //
310 ExtensionGroupToContextType(extension_group); 312 // Reference: http://crbug.com/111614.
311 313 CHECK_EQ("invalid", extension_id);
312 if (context_type == ChromeV8Context::CONTENT_SCRIPT || 314 return false;
313 extensions_.ExtensionBindingsAllowed(ExtensionURLInfo( 315 }
314 frame->document().securityOrigin(), 316 return custom_bindings_util::AllowAPIInjection(
315 UserScriptSlave::GetDataSourceURLForFrame(frame)))) { 317 custom_binding_api_name, *extension, this);
316 // If the extension is a custom API binding, only allow if the extension 318 }
317 // has permission to use the API. 319
318 std::string custom_binding_api_name = 320 base::StringPiece GetResource(int resource_id) {
Aaron Boodman 2012/03/01 07:24:26 Dead code?
koz (OOO until 15th September) 2012/03/02 01:13:58 Done.
319 custom_bindings_util::GetAPIName(v8_extension_name); 321 return ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id);
320 if (!custom_binding_api_name.empty()) { 322 }
321 std::string extension_id = GetExtensionID(frame, world_id); 323
322 const Extension* extension = extensions_.GetByID(extension_id); 324 SourceMap* GetOrCreateSourceMap() {
Aaron Boodman 2012/03/01 07:24:26 Dead code.
koz (OOO until 15th September) 2012/03/02 01:13:58 Done.
323 if (!extension) { 325 static ResourceBundleSourceMap* source_map;
324 // This can happen when a resource is blocked due to CSP; a valid 326 if (source_map == NULL) {
325 // chrome-extension:// URL is navigated to, so it passes the initial 327 source_map =
326 // checks, but the URL gets changed to "chrome-extension://invalid" 328 new ResourceBundleSourceMap(&ResourceBundle::GetSharedInstance());
327 // afterwards (see chrome_content_renderer_client.cc). An extension 329 }
328 // page still gets loaded, just for the extension with ID "invalid", 330
329 // which of course isn't found so GetById extension will be NULL. 331 return source_map;
330 // 332 }
331 // Reference: http://crbug.com/111614. 333
332 CHECK_EQ("invalid", extension_id); 334 class ChromeHiddenNativeHandler : public NativeHandler {
Aaron Boodman 2012/03/01 07:24:26 Global definitions in a cc file should be in an an
koz (OOO until 15th September) 2012/03/02 01:13:58 Done.
333 return false; 335 public:
334 } 336 ChromeHiddenNativeHandler() {
335 return custom_bindings_util::AllowAPIInjection( 337 RouteFunction("GetChromeHidden",
336 custom_binding_api_name, *extension, this); 338 base::Bind(&ChromeHiddenNativeHandler::GetChromeHidden,
337 } 339 base::Unretained(this)));
338 340 }
339 return true; 341
340 } 342 v8::Handle<v8::Value> GetChromeHidden(const v8::Arguments& args) {
341 343 return ChromeV8Context::GetOrCreateChromeHidden(v8::Context::GetCurrent());
342 return false; 344 }
345 };
346
347 class PrintNativeHandler : public NativeHandler {
348 public:
349 PrintNativeHandler() {
350 RouteFunction("Print",
351 base::Bind(&PrintNativeHandler::Print,
352 base::Unretained(this)));
353 }
354
355 v8::Handle<v8::Value> Print(const v8::Arguments& args) {
356 if (args.Length() < 1)
357 return v8::Undefined();
358
359 std::vector<std::string> components;
360 for (int i = 0; i < args.Length(); ++i)
361 components.push_back(*v8::String::Utf8Value(args[i]->ToString()));
362
363 LOG(ERROR) << JoinString(components, ',');
364 return v8::Undefined();
365 }
366 };
367
368 class ContextInfoNativeHandler : public NativeHandler {
369 public:
370 explicit ContextInfoNativeHandler(ExtensionDispatcher* extension_dispatcher,
371 bool is_bindings_allowed,
372 WebFrame* frame,
373 int world_id)
374 : extension_dispatcher_(extension_dispatcher),
375 is_bindings_allowed_(is_bindings_allowed),
376 frame_(frame),
377 world_id_(world_id) {
378 RouteFunction("IsBindingsAllowed",
379 base::Bind(&ContextInfoNativeHandler::IsBindingsAllowed,
380 base::Unretained(this)));
381 RouteFunction("IsAPIAllowed",
382 base::Bind(&ContextInfoNativeHandler::IsAPIAllowed,
383 base::Unretained(this)));
384 }
385
386 v8::Handle<v8::Value> IsBindingsAllowed(const v8::Arguments& args) {
387 return v8::Boolean::New(is_bindings_allowed_);
388 }
389
390 v8::Handle<v8::Value> IsAPIAllowed(const v8::Arguments& args) {
391 std::string custom_api_name = *v8::String::AsciiValue(args[0]->ToString());
392 return v8::Boolean::New(extension_dispatcher_->AllowCustomAPI(
393 frame_, custom_api_name, world_id_));
394 }
395
396 private:
397 ExtensionDispatcher* extension_dispatcher_;
398 bool is_bindings_allowed_;
399 WebFrame* frame_;
400 int world_id_;
401 };
402
403 void ExtensionDispatcher::RegisterNativeHandlers(ModuleSystem* module_system,
404 ChromeV8Context* context,
405 bool is_bindings_allowed) {
406 module_system->RegisterNativeHandler("app",
407 scoped_ptr<NativeHandler>(new AppBindings(this, context)));
408 module_system->RegisterNativeHandler("webstore",
409 scoped_ptr<NativeHandler>(new WebstoreBindings(this, context)));
410 module_system->RegisterNativeHandler("event_bindings",
411 scoped_ptr<NativeHandler>(EventBindings::Get(this)));
412 module_system->RegisterNativeHandler("miscellaneous_bindings",
413 scoped_ptr<NativeHandler>(MiscellaneousBindings::Get(this)));
414 module_system->RegisterNativeHandler("schema_generated_bindings",
415 scoped_ptr<NativeHandler>(SchemaGeneratedBindings::Get(this)));
416
417 // Custom bindings.
418 module_system->RegisterNativeHandler("chrome_private",
419 scoped_ptr<NativeHandler>(
420 new ChromePrivateCustomBindings(this)));
421 module_system->RegisterNativeHandler("context_menus",
422 scoped_ptr<NativeHandler>(new ContextMenusCustomBindings()));
423 module_system->RegisterNativeHandler("extension",
424 scoped_ptr<NativeHandler>(
425 new ExtensionCustomBindings(this)));
426 module_system->RegisterNativeHandler("experimental_socket",
427 scoped_ptr<NativeHandler>(new ExperimentalSocketCustomBindings()));
428 module_system->RegisterNativeHandler("file_browser_handler",
429 scoped_ptr<NativeHandler>(new FileBrowserHandlerCustomBindings()));
430 module_system->RegisterNativeHandler("file_browser_private",
431 scoped_ptr<NativeHandler>(new FileBrowserPrivateCustomBindings()));
432 module_system->RegisterNativeHandler("i18n",
433 scoped_ptr<NativeHandler>(new I18NCustomBindings()));
434 module_system->RegisterNativeHandler("page_actions",
435 scoped_ptr<NativeHandler>(
436 new PageActionsCustomBindings(this)));
437 module_system->RegisterNativeHandler("page_capture",
438 scoped_ptr<NativeHandler>(new PageCaptureCustomBindings()));
439 module_system->RegisterNativeHandler("tabs",
440 scoped_ptr<NativeHandler>(new TabsCustomBindings()));
441 module_system->RegisterNativeHandler("tts",
442 scoped_ptr<NativeHandler>(new TTSCustomBindings()));
443 module_system->RegisterNativeHandler("web_request",
444 scoped_ptr<NativeHandler>(new WebRequestCustomBindings()));
445 }
446
447 void ExtensionDispatcher::PopulateSourceMap() {
448 source_map_.RegisterSource("app", IDR_APP_BINDINGS_JS);
449 source_map_.RegisterSource("webstore", IDR_WEBSTORE_BINDINGS_JS);
450 source_map_.RegisterSource("event_bindings", IDR_EVENT_BINDINGS_JS);
451 source_map_.RegisterSource("miscellaneous_bindings",
452 IDR_MISCELLANEOUS_BINDINGS_JS);
453 source_map_.RegisterSource("schema_generated_bindings",
454 IDR_SCHEMA_GENERATED_BINDINGS_JS);
455 source_map_.RegisterSource("json_schema", IDR_JSON_SCHEMA_JS);
456 source_map_.RegisterSource("apitest", IDR_EXTENSION_APITEST_JS);
457 source_map_.RegisterSource("setup_bindings", IDR_SETUP_BINDINGS_JS);
458
459 // Custom bindings.
460 source_map_.RegisterSource("browserAction",
461 IDR_BROWSER_ACTION_CUSTOM_BINDINGS_JS);
462 source_map_.RegisterSource("chromePrivate",
463 IDR_CHROME_PRIVATE_CUSTOM_BINDINGS_JS);
464 source_map_.RegisterSource("contentSettings",
465 IDR_CONTENT_SETTINGS_CUSTOM_BINDINGS_JS);
466 source_map_.RegisterSource("contextMenus",
467 IDR_CONTEXT_MENUS_CUSTOM_BINDINGS_JS);
468 source_map_.RegisterSource("devtools", IDR_DEVTOOLS_CUSTOM_BINDINGS_JS);
469 source_map_.RegisterSource("experimental.declarative",
470 IDR_EXPERIMENTAL_DECLARATIVE_CUSTOM_BINDINGS_JS);
471 source_map_.RegisterSource("experimental.socket",
472 IDR_EXPERIMENTAL_SOCKET_CUSTOM_BINDINGS_JS);
473 source_map_.RegisterSource("extension", IDR_EXTENSION_CUSTOM_BINDINGS_JS);
474 source_map_.RegisterSource("fileBrowserHandler",
475 IDR_FILE_BROWSER_HANDLER_CUSTOM_BINDINGS_JS);
476 source_map_.RegisterSource("fileBrowserPrivate",
477 IDR_FILE_BROWSER_PRIVATE_CUSTOM_BINDINGS_JS);
478 source_map_.RegisterSource("i18n", IDR_I18N_CUSTOM_BINDINGS_JS);
479 source_map_.RegisterSource("input.ime", IDR_INPUT_IME_CUSTOM_BINDINGS_JS);
480 source_map_.RegisterSource("omnibox", IDR_OMNIBOX_CUSTOM_BINDINGS_JS);
481 source_map_.RegisterSource("pageActions",
482 IDR_PAGE_ACTIONS_CUSTOM_BINDINGS_JS);
483 source_map_.RegisterSource("pageAction", IDR_PAGE_ACTION_CUSTOM_BINDINGS_JS);
484 source_map_.RegisterSource("pageCapture",
485 IDR_PAGE_CAPTURE_CUSTOM_BINDINGS_JS);
486 source_map_.RegisterSource("storage", IDR_STORAGE_CUSTOM_BINDINGS_JS);
487 source_map_.RegisterSource("tabs", IDR_TABS_CUSTOM_BINDINGS_JS);
488 source_map_.RegisterSource("tts", IDR_TTS_CUSTOM_BINDINGS_JS);
489 source_map_.RegisterSource("ttsEngine", IDR_TTS_ENGINE_CUSTOM_BINDINGS_JS);
490 source_map_.RegisterSource("types", IDR_TYPES_CUSTOM_BINDINGS_JS);
491 source_map_.RegisterSource("webRequest", IDR_WEB_REQUEST_CUSTOM_BINDINGS_JS);
492 source_map_.RegisterSource("windows", IDR_WINDOWS_CUSTOM_BINDINGS_JS);
343 } 493 }
344 494
345 void ExtensionDispatcher::DidCreateScriptContext( 495 void ExtensionDispatcher::DidCreateScriptContext(
346 WebFrame* frame, v8::Handle<v8::Context> v8_context, int extension_group, 496 WebFrame* frame, v8::Handle<v8::Context> v8_context, int extension_group,
347 int world_id) { 497 int world_id) {
348 // TODO(koz): If the caller didn't pass extension_group, use the last value. 498 // TODO(koz): If the caller didn't pass extension_group, use the last value.
349 if (extension_group == -1) 499 if (extension_group == -1)
350 extension_group = g_hack_extension_group; 500 extension_group = g_hack_extension_group;
351 ChromeV8Context* context = 501 ChromeV8Context* context =
352 new ChromeV8Context( 502 new ChromeV8Context(
353 v8_context, 503 v8_context,
354 frame, 504 frame,
355 GetExtensionID(frame, world_id), 505 GetExtensionID(frame, world_id),
356 ExtensionGroupToContextType(extension_group)); 506 ExtensionGroupToContextType(extension_group));
357 v8_context_set_.Add(context); 507 v8_context_set_.Add(context);
358 508
509 bool is_bindings_allowed =
510 context->context_type() == ChromeV8Context::CONTENT_SCRIPT ||
511 extensions_.ExtensionBindingsAllowed(ExtensionURLInfo(
512 frame->document().securityOrigin(),
513 UserScriptSlave::GetDataSourceURLForFrame(frame)));
514
515 ModuleSystem* module_system = new ModuleSystem(&source_map_);
Aaron Boodman 2012/03/01 07:24:26 Make this a scoped_ptr, and use module_system.Pass
koz (OOO until 15th September) 2012/03/02 01:13:58 Done.
516 RegisterNativeHandlers(module_system, context, is_bindings_allowed);
517
518 module_system->RegisterNativeHandler("chrome_hidden",
519 scoped_ptr<NativeHandler>(new ChromeHiddenNativeHandler()));
520 module_system->RegisterNativeHandler("print",
521 scoped_ptr<NativeHandler>(new PrintNativeHandler()));
522 module_system->RegisterNativeHandler("context_info",
523 scoped_ptr<NativeHandler>(new ContextInfoNativeHandler(
524 this,
525 is_bindings_allowed,
526 frame,
527 world_id)));
528
529 context->set_module_system(scoped_ptr<ModuleSystem>(module_system));
530
359 const Extension* extension = extensions_.GetByID(context->extension_id()); 531 const Extension* extension = extensions_.GetByID(context->extension_id());
360 int manifest_version = 1; 532 int manifest_version = 1;
361 if (extension) 533 if (extension)
362 manifest_version = extension->manifest_version(); 534 manifest_version = extension->manifest_version();
363 535
536 module_system->Require("setup_bindings");
537
364 context->DispatchOnLoadEvent( 538 context->DispatchOnLoadEvent(
365 is_extension_process_, 539 is_extension_process_,
366 ChromeRenderProcessObserver::is_incognito_process(), 540 ChromeRenderProcessObserver::is_incognito_process(),
367 manifest_version); 541 manifest_version);
368 542
369 VLOG(1) << "Num tracked contexts: " << v8_context_set_.size(); 543 VLOG(1) << "Num tracked contexts: " << v8_context_set_.size();
370 } 544 }
371 545
372 std::string ExtensionDispatcher::GetExtensionID(WebFrame* frame, int world_id) { 546 std::string ExtensionDispatcher::GetExtensionID(WebFrame* frame, int world_id) {
373 if (!test_extension_id_.empty()) { 547 if (!test_extension_id_.empty()) {
(...skipping 10 matching lines...) Expand all
384 } 558 }
385 559
386 void ExtensionDispatcher::WillReleaseScriptContext( 560 void ExtensionDispatcher::WillReleaseScriptContext(
387 WebFrame* frame, v8::Handle<v8::Context> v8_context, int world_id) { 561 WebFrame* frame, v8::Handle<v8::Context> v8_context, int world_id) {
388 ChromeV8Context* context = v8_context_set_.GetByV8Context(v8_context); 562 ChromeV8Context* context = v8_context_set_.GetByV8Context(v8_context);
389 if (!context) 563 if (!context)
390 return; 564 return;
391 565
392 context->DispatchOnUnloadEvent(); 566 context->DispatchOnUnloadEvent();
393 567
394 ChromeV8Extension::InstanceSet extensions = ChromeV8Extension::GetAll();
395 for (ChromeV8Extension::InstanceSet::const_iterator iter = extensions.begin();
396 iter != extensions.end(); ++iter) {
397 (*iter)->ContextWillBeReleased(context);
398 }
399
400 v8_context_set_.Remove(context); 568 v8_context_set_.Remove(context);
401 VLOG(1) << "Num tracked contexts: " << v8_context_set_.size(); 569 VLOG(1) << "Num tracked contexts: " << v8_context_set_.size();
402 } 570 }
403 571
404 void ExtensionDispatcher::SetTestExtensionId(const std::string& id) { 572 void ExtensionDispatcher::SetTestExtensionId(const std::string& id) {
405 test_extension_id_ = id; 573 test_extension_id_ = id;
406 } 574 }
407 575
408 bool ExtensionDispatcher::IsTestExtensionId(const std::string& id) { 576 bool ExtensionDispatcher::IsTestExtensionId(const std::string& id) {
409 return id == test_extension_id_; 577 return id == test_extension_id_;
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 webrequest_adblock_ = adblock; 702 webrequest_adblock_ = adblock;
535 webrequest_adblock_plus_ = adblock_plus; 703 webrequest_adblock_plus_ = adblock_plus;
536 webrequest_other_ = other; 704 webrequest_other_ = other;
537 } 705 }
538 706
539 void ExtensionDispatcher::OnShouldClose(const std::string& extension_id, 707 void ExtensionDispatcher::OnShouldClose(const std::string& extension_id,
540 int sequence_id) { 708 int sequence_id) {
541 RenderThread::Get()->Send( 709 RenderThread::Get()->Send(
542 new ExtensionHostMsg_ShouldCloseAck(extension_id, sequence_id)); 710 new ExtensionHostMsg_ShouldCloseAck(extension_id, sequence_id));
543 } 711 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698