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

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: base files missing? 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
67 class ChromeHiddenNativeHandler : public NativeHandler {
68 public:
69 ChromeHiddenNativeHandler() {
70 RouteFunction("GetChromeHidden",
71 base::Bind(&ChromeHiddenNativeHandler::GetChromeHidden,
72 base::Unretained(this)));
73 }
74
75 v8::Handle<v8::Value> GetChromeHidden(const v8::Arguments& args) {
76 return ChromeV8Context::GetOrCreateChromeHidden(v8::Context::GetCurrent());
77 }
78 };
79
80 class PrintNativeHandler : public NativeHandler {
81 public:
82 PrintNativeHandler() {
83 RouteFunction("Print",
84 base::Bind(&PrintNativeHandler::Print,
85 base::Unretained(this)));
86 }
87
88 v8::Handle<v8::Value> Print(const v8::Arguments& args) {
89 if (args.Length() < 1)
90 return v8::Undefined();
91
92 std::vector<std::string> components;
93 for (int i = 0; i < args.Length(); ++i)
94 components.push_back(*v8::String::Utf8Value(args[i]->ToString()));
95
96 LOG(ERROR) << JoinString(components, ',');
97 return v8::Undefined();
98 }
99 };
100
101 class ContextInfoNativeHandler : public NativeHandler {
102 public:
103 explicit ContextInfoNativeHandler(ExtensionDispatcher* extension_dispatcher,
104 bool is_bindings_allowed,
105 WebKit::WebFrame* frame,
106 int world_id)
107 : extension_dispatcher_(extension_dispatcher),
108 is_bindings_allowed_(is_bindings_allowed),
109 frame_(frame),
110 world_id_(world_id) {
111 RouteFunction("IsBindingsAllowed",
112 base::Bind(&ContextInfoNativeHandler::IsBindingsAllowed,
113 base::Unretained(this)));
114 RouteFunction("IsAPIAllowed",
115 base::Bind(&ContextInfoNativeHandler::IsAPIAllowed,
116 base::Unretained(this)));
117 }
118
119 v8::Handle<v8::Value> IsBindingsAllowed(const v8::Arguments& args) {
120 return v8::Boolean::New(is_bindings_allowed_);
121 }
122
123 v8::Handle<v8::Value> IsAPIAllowed(const v8::Arguments& args) {
124 std::string custom_api_name = *v8::String::AsciiValue(args[0]->ToString());
125 return v8::Boolean::New(extension_dispatcher_->AllowCustomAPI(
126 frame_, custom_api_name, world_id_));
127 }
128
129 private:
130 ExtensionDispatcher* extension_dispatcher_;
131 bool is_bindings_allowed_;
132 WebKit::WebFrame* frame_;
133 int world_id_;
134 };
135
48 } 136 }
49 137
50 using namespace extensions; 138 using namespace extensions;
51 139
52 using WebKit::WebDataSource; 140 using WebKit::WebDataSource;
53 using WebKit::WebDocument; 141 using WebKit::WebDocument;
54 using WebKit::WebFrame; 142 using WebKit::WebFrame;
55 using WebKit::WebSecurityPolicy; 143 using WebKit::WebSecurityPolicy;
56 using WebKit::WebString; 144 using WebKit::WebString;
57 using WebKit::WebVector; 145 using WebKit::WebVector;
58 using WebKit::WebView; 146 using WebKit::WebView;
59 using content::RenderThread; 147 using content::RenderThread;
60 148
61 ExtensionDispatcher::ExtensionDispatcher() 149 ExtensionDispatcher::ExtensionDispatcher()
62 : is_webkit_initialized_(false), 150 : is_webkit_initialized_(false),
63 webrequest_adblock_(false), 151 webrequest_adblock_(false),
64 webrequest_adblock_plus_(false), 152 webrequest_adblock_plus_(false),
65 webrequest_other_(false) { 153 webrequest_other_(false),
154 source_map_(&ResourceBundle::GetSharedInstance()) {
66 const CommandLine& command_line = *(CommandLine::ForCurrentProcess()); 155 const CommandLine& command_line = *(CommandLine::ForCurrentProcess());
67 is_extension_process_ = 156 is_extension_process_ =
68 command_line.HasSwitch(switches::kExtensionProcess) || 157 command_line.HasSwitch(switches::kExtensionProcess) ||
69 command_line.HasSwitch(switches::kSingleProcess); 158 command_line.HasSwitch(switches::kSingleProcess);
70 159
71 if (is_extension_process_) { 160 if (is_extension_process_) {
72 RenderThread::Get()->SetIdleNotificationDelayInMs( 161 RenderThread::Get()->SetIdleNotificationDelayInMs(
73 kInitialExtensionIdleHandlerDelayMs); 162 kInitialExtensionIdleHandlerDelayMs);
74 } 163 }
75 164
76 user_script_slave_.reset(new UserScriptSlave(&extensions_)); 165 user_script_slave_.reset(new UserScriptSlave(&extensions_));
166 PopulateSourceMap();
77 } 167 }
78 168
79 ExtensionDispatcher::~ExtensionDispatcher() { 169 ExtensionDispatcher::~ExtensionDispatcher() {
80 } 170 }
81 171
82 bool ExtensionDispatcher::OnControlMessageReceived( 172 bool ExtensionDispatcher::OnControlMessageReceived(
83 const IPC::Message& message) { 173 const IPC::Message& message) {
84 bool handled = true; 174 bool handled = true;
85 IPC_BEGIN_MESSAGE_MAP(ExtensionDispatcher, message) 175 IPC_BEGIN_MESSAGE_MAP(ExtensionDispatcher, message)
86 IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnMessageInvoke) 176 IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnMessageInvoke)
(...skipping 17 matching lines...) Expand all
104 194
105 void ExtensionDispatcher::WebKitInitialized() { 195 void ExtensionDispatcher::WebKitInitialized() {
106 // For extensions, we want to ensure we call the IdleHandler every so often, 196 // For extensions, we want to ensure we call the IdleHandler every so often,
107 // even if the extension keeps up activity. 197 // even if the extension keeps up activity.
108 if (is_extension_process_) { 198 if (is_extension_process_) {
109 forced_idle_timer_.Start(FROM_HERE, 199 forced_idle_timer_.Start(FROM_HERE,
110 base::TimeDelta::FromMilliseconds(kMaxExtensionIdleHandlerDelayMs), 200 base::TimeDelta::FromMilliseconds(kMaxExtensionIdleHandlerDelayMs),
111 RenderThread::Get(), &RenderThread::IdleHandler); 201 RenderThread::Get(), &RenderThread::IdleHandler);
112 } 202 }
113 203
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 204 // Initialize host permissions for any extensions that were activated before
134 // WebKit was initialized. 205 // WebKit was initialized.
135 for (std::set<std::string>::iterator iter = active_extension_ids_.begin(); 206 for (std::set<std::string>::iterator iter = active_extension_ids_.begin();
136 iter != active_extension_ids_.end(); ++iter) { 207 iter != active_extension_ids_.end(); ++iter) {
137 const Extension* extension = extensions_.GetByID(*iter); 208 const Extension* extension = extensions_.GetByID(*iter);
138 if (extension) 209 if (extension)
139 InitOriginPermissions(extension); 210 InitOriginPermissions(extension);
140 } 211 }
141 212
142 is_webkit_initialized_ = true; 213 is_webkit_initialized_ = true;
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 static int g_hack_extension_group = 0; 355 static int g_hack_extension_group = 0;
285 356
286 } 357 }
287 358
288 bool ExtensionDispatcher::AllowScriptExtension( 359 bool ExtensionDispatcher::AllowScriptExtension(
289 WebFrame* frame, 360 WebFrame* frame,
290 const std::string& v8_extension_name, 361 const std::string& v8_extension_name,
291 int extension_group, 362 int extension_group,
292 int world_id) { 363 int world_id) {
293 g_hack_extension_group = extension_group; 364 g_hack_extension_group = extension_group;
365 return true;
366 }
294 367
295 // NULL in unit tests. 368 bool ExtensionDispatcher::AllowCustomAPI(
296 if (!RenderThread::Get()) 369 WebFrame* frame,
297 return true; 370 const std::string& custom_binding_api_name,
371 int world_id) {
372 std::string extension_id = GetExtensionID(frame, world_id);
373 const Extension* extension = extensions_.GetByID(extension_id);
374 if (!extension) {
375 // This can happen when a resource is blocked due to CSP; a valid
376 // chrome-extension:// URL is navigated to, so it passes the initial
377 // checks, but the URL gets changed to "chrome-extension://invalid"
378 // afterwards (see chrome_content_renderer_client.cc). An extension
379 // page still gets loaded, just for the extension with ID "invalid",
380 // which of course isn't found so GetById extension will be NULL.
381 //
382 // Reference: http://crbug.com/111614.
383 CHECK_EQ("invalid", extension_id);
384 return false;
385 }
386 return custom_bindings_util::AllowAPIInjection(
387 custom_binding_api_name, *extension, this);
388 }
298 389
299 // If we don't know about it, it was added by WebCore, so we should allow it. 390 void ExtensionDispatcher::RegisterNativeHandlers(ModuleSystem* module_system,
300 if (!RenderThread::Get()->IsRegisteredExtension(v8_extension_name)) 391 ChromeV8Context* context,
301 return true; 392 bool is_bindings_allowed) {
393 module_system->RegisterNativeHandler("app",
394 scoped_ptr<NativeHandler>(new AppBindings(this, context)));
395 module_system->RegisterNativeHandler("webstore",
396 scoped_ptr<NativeHandler>(new WebstoreBindings(this, context)));
397 module_system->RegisterNativeHandler("event_bindings",
398 scoped_ptr<NativeHandler>(EventBindings::Get(this)));
399 module_system->RegisterNativeHandler("miscellaneous_bindings",
400 scoped_ptr<NativeHandler>(MiscellaneousBindings::Get(this)));
401 module_system->RegisterNativeHandler("schema_generated_bindings",
402 scoped_ptr<NativeHandler>(SchemaGeneratedBindings::Get(this)));
302 403
303 // If the V8 extension is not restricted, allow it to run anywhere. 404 // Custom bindings.
304 if (!restricted_v8_extensions_.count(v8_extension_name)) 405 module_system->RegisterNativeHandler("chrome_private",
305 return true; 406 scoped_ptr<NativeHandler>(
407 new ChromePrivateCustomBindings(this)));
408 module_system->RegisterNativeHandler("context_menus",
409 scoped_ptr<NativeHandler>(new ContextMenusCustomBindings()));
410 module_system->RegisterNativeHandler("extension",
411 scoped_ptr<NativeHandler>(
412 new ExtensionCustomBindings(this)));
413 module_system->RegisterNativeHandler("experimental_socket",
414 scoped_ptr<NativeHandler>(new ExperimentalSocketCustomBindings()));
415 module_system->RegisterNativeHandler("file_browser_handler",
416 scoped_ptr<NativeHandler>(new FileBrowserHandlerCustomBindings()));
417 module_system->RegisterNativeHandler("file_browser_private",
418 scoped_ptr<NativeHandler>(new FileBrowserPrivateCustomBindings()));
419 module_system->RegisterNativeHandler("i18n",
420 scoped_ptr<NativeHandler>(new I18NCustomBindings()));
421 module_system->RegisterNativeHandler("page_actions",
422 scoped_ptr<NativeHandler>(
423 new PageActionsCustomBindings(this)));
424 module_system->RegisterNativeHandler("page_capture",
425 scoped_ptr<NativeHandler>(new PageCaptureCustomBindings()));
426 module_system->RegisterNativeHandler("tabs",
427 scoped_ptr<NativeHandler>(new TabsCustomBindings()));
428 module_system->RegisterNativeHandler("tts",
429 scoped_ptr<NativeHandler>(new TTSCustomBindings()));
430 module_system->RegisterNativeHandler("web_request",
431 scoped_ptr<NativeHandler>(new WebRequestCustomBindings()));
432 }
306 433
307 // Extension-only bindings should be restricted to content scripts and 434 void ExtensionDispatcher::PopulateSourceMap() {
308 // extension-blessed URLs. 435 source_map_.RegisterSource("app", IDR_APP_BINDINGS_JS);
309 ChromeV8Context::ContextType context_type = 436 source_map_.RegisterSource("webstore", IDR_WEBSTORE_BINDINGS_JS);
310 ExtensionGroupToContextType(extension_group); 437 source_map_.RegisterSource("event_bindings", IDR_EVENT_BINDINGS_JS);
438 source_map_.RegisterSource("miscellaneous_bindings",
439 IDR_MISCELLANEOUS_BINDINGS_JS);
440 source_map_.RegisterSource("schema_generated_bindings",
441 IDR_SCHEMA_GENERATED_BINDINGS_JS);
442 source_map_.RegisterSource("json_schema", IDR_JSON_SCHEMA_JS);
443 source_map_.RegisterSource("apitest", IDR_EXTENSION_APITEST_JS);
444 source_map_.RegisterSource("setup_bindings", IDR_SETUP_BINDINGS_JS);
311 445
312 if (context_type == ChromeV8Context::CONTENT_SCRIPT || 446 // Custom bindings.
313 extensions_.ExtensionBindingsAllowed(ExtensionURLInfo( 447 source_map_.RegisterSource("browserAction",
314 frame->document().securityOrigin(), 448 IDR_BROWSER_ACTION_CUSTOM_BINDINGS_JS);
315 UserScriptSlave::GetDataSourceURLForFrame(frame)))) { 449 source_map_.RegisterSource("chromePrivate",
316 // If the extension is a custom API binding, only allow if the extension 450 IDR_CHROME_PRIVATE_CUSTOM_BINDINGS_JS);
317 // has permission to use the API. 451 source_map_.RegisterSource("contentSettings",
318 std::string custom_binding_api_name = 452 IDR_CONTENT_SETTINGS_CUSTOM_BINDINGS_JS);
319 custom_bindings_util::GetAPIName(v8_extension_name); 453 source_map_.RegisterSource("contextMenus",
320 if (!custom_binding_api_name.empty()) { 454 IDR_CONTEXT_MENUS_CUSTOM_BINDINGS_JS);
321 std::string extension_id = GetExtensionID(frame, world_id); 455 source_map_.RegisterSource("devtools", IDR_DEVTOOLS_CUSTOM_BINDINGS_JS);
322 const Extension* extension = extensions_.GetByID(extension_id); 456 source_map_.RegisterSource("experimental.declarative",
323 if (!extension) { 457 IDR_EXPERIMENTAL_DECLARATIVE_CUSTOM_BINDINGS_JS);
324 // This can happen when a resource is blocked due to CSP; a valid 458 source_map_.RegisterSource("experimental.socket",
325 // chrome-extension:// URL is navigated to, so it passes the initial 459 IDR_EXPERIMENTAL_SOCKET_CUSTOM_BINDINGS_JS);
326 // checks, but the URL gets changed to "chrome-extension://invalid" 460 source_map_.RegisterSource("extension", IDR_EXTENSION_CUSTOM_BINDINGS_JS);
327 // afterwards (see chrome_content_renderer_client.cc). An extension 461 source_map_.RegisterSource("fileBrowserHandler",
328 // page still gets loaded, just for the extension with ID "invalid", 462 IDR_FILE_BROWSER_HANDLER_CUSTOM_BINDINGS_JS);
329 // which of course isn't found so GetById extension will be NULL. 463 source_map_.RegisterSource("fileBrowserPrivate",
330 // 464 IDR_FILE_BROWSER_PRIVATE_CUSTOM_BINDINGS_JS);
331 // Reference: http://crbug.com/111614. 465 source_map_.RegisterSource("i18n", IDR_I18N_CUSTOM_BINDINGS_JS);
332 CHECK_EQ("invalid", extension_id); 466 source_map_.RegisterSource("input.ime", IDR_INPUT_IME_CUSTOM_BINDINGS_JS);
333 return false; 467 source_map_.RegisterSource("omnibox", IDR_OMNIBOX_CUSTOM_BINDINGS_JS);
334 } 468 source_map_.RegisterSource("pageActions",
335 return custom_bindings_util::AllowAPIInjection( 469 IDR_PAGE_ACTIONS_CUSTOM_BINDINGS_JS);
336 custom_binding_api_name, *extension, this); 470 source_map_.RegisterSource("pageAction", IDR_PAGE_ACTION_CUSTOM_BINDINGS_JS);
337 } 471 source_map_.RegisterSource("pageCapture",
338 472 IDR_PAGE_CAPTURE_CUSTOM_BINDINGS_JS);
339 return true; 473 source_map_.RegisterSource("storage", IDR_STORAGE_CUSTOM_BINDINGS_JS);
340 } 474 source_map_.RegisterSource("tabs", IDR_TABS_CUSTOM_BINDINGS_JS);
341 475 source_map_.RegisterSource("tts", IDR_TTS_CUSTOM_BINDINGS_JS);
342 return false; 476 source_map_.RegisterSource("ttsEngine", IDR_TTS_ENGINE_CUSTOM_BINDINGS_JS);
477 source_map_.RegisterSource("types", IDR_TYPES_CUSTOM_BINDINGS_JS);
478 source_map_.RegisterSource("webRequest", IDR_WEB_REQUEST_CUSTOM_BINDINGS_JS);
479 source_map_.RegisterSource("windows", IDR_WINDOWS_CUSTOM_BINDINGS_JS);
343 } 480 }
344 481
345 void ExtensionDispatcher::DidCreateScriptContext( 482 void ExtensionDispatcher::DidCreateScriptContext(
346 WebFrame* frame, v8::Handle<v8::Context> v8_context, int extension_group, 483 WebFrame* frame, v8::Handle<v8::Context> v8_context, int extension_group,
347 int world_id) { 484 int world_id) {
348 // TODO(koz): If the caller didn't pass extension_group, use the last value. 485 // TODO(koz): If the caller didn't pass extension_group, use the last value.
349 if (extension_group == -1) 486 if (extension_group == -1)
350 extension_group = g_hack_extension_group; 487 extension_group = g_hack_extension_group;
351 ChromeV8Context* context = 488 ChromeV8Context* context =
352 new ChromeV8Context( 489 new ChromeV8Context(
353 v8_context, 490 v8_context,
354 frame, 491 frame,
355 GetExtensionID(frame, world_id), 492 GetExtensionID(frame, world_id),
356 ExtensionGroupToContextType(extension_group)); 493 ExtensionGroupToContextType(extension_group));
357 v8_context_set_.Add(context); 494 v8_context_set_.Add(context);
358 495
496 bool is_bindings_allowed =
497 context->context_type() == ChromeV8Context::CONTENT_SCRIPT ||
498 extensions_.ExtensionBindingsAllowed(ExtensionURLInfo(
499 frame->document().securityOrigin(),
500 UserScriptSlave::GetDataSourceURLForFrame(frame)));
501
502 scoped_ptr<ModuleSystem> module_system(new ModuleSystem(&source_map_));
503 RegisterNativeHandlers(module_system.get(), context, is_bindings_allowed);
504
505 module_system->RegisterNativeHandler("chrome_hidden",
506 scoped_ptr<NativeHandler>(new ChromeHiddenNativeHandler()));
507 module_system->RegisterNativeHandler("print",
508 scoped_ptr<NativeHandler>(new PrintNativeHandler()));
509 module_system->RegisterNativeHandler("context_info",
510 scoped_ptr<NativeHandler>(new ContextInfoNativeHandler(
511 this,
512 is_bindings_allowed,
513 frame,
514 world_id)));
515
516 context->set_module_system(module_system.Pass());
517
359 const Extension* extension = extensions_.GetByID(context->extension_id()); 518 const Extension* extension = extensions_.GetByID(context->extension_id());
360 int manifest_version = 1; 519 int manifest_version = 1;
361 if (extension) 520 if (extension)
362 manifest_version = extension->manifest_version(); 521 manifest_version = extension->manifest_version();
363 522
523 module_system->Require("setup_bindings");
524
364 context->DispatchOnLoadEvent( 525 context->DispatchOnLoadEvent(
365 is_extension_process_, 526 is_extension_process_,
366 ChromeRenderProcessObserver::is_incognito_process(), 527 ChromeRenderProcessObserver::is_incognito_process(),
367 manifest_version); 528 manifest_version);
368 529
369 VLOG(1) << "Num tracked contexts: " << v8_context_set_.size(); 530 VLOG(1) << "Num tracked contexts: " << v8_context_set_.size();
370 } 531 }
371 532
372 std::string ExtensionDispatcher::GetExtensionID(WebFrame* frame, int world_id) { 533 std::string ExtensionDispatcher::GetExtensionID(WebFrame* frame, int world_id) {
373 if (!test_extension_id_.empty()) { 534 if (!test_extension_id_.empty()) {
(...skipping 10 matching lines...) Expand all
384 } 545 }
385 546
386 void ExtensionDispatcher::WillReleaseScriptContext( 547 void ExtensionDispatcher::WillReleaseScriptContext(
387 WebFrame* frame, v8::Handle<v8::Context> v8_context, int world_id) { 548 WebFrame* frame, v8::Handle<v8::Context> v8_context, int world_id) {
388 ChromeV8Context* context = v8_context_set_.GetByV8Context(v8_context); 549 ChromeV8Context* context = v8_context_set_.GetByV8Context(v8_context);
389 if (!context) 550 if (!context)
390 return; 551 return;
391 552
392 context->DispatchOnUnloadEvent(); 553 context->DispatchOnUnloadEvent();
393 554
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); 555 v8_context_set_.Remove(context);
401 VLOG(1) << "Num tracked contexts: " << v8_context_set_.size(); 556 VLOG(1) << "Num tracked contexts: " << v8_context_set_.size();
402 } 557 }
403 558
404 void ExtensionDispatcher::SetTestExtensionId(const std::string& id) { 559 void ExtensionDispatcher::SetTestExtensionId(const std::string& id) {
405 test_extension_id_ = id; 560 test_extension_id_ = id;
406 } 561 }
407 562
408 bool ExtensionDispatcher::IsTestExtensionId(const std::string& id) { 563 bool ExtensionDispatcher::IsTestExtensionId(const std::string& id) {
409 return id == test_extension_id_; 564 return id == test_extension_id_;
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 webrequest_adblock_ = adblock; 689 webrequest_adblock_ = adblock;
535 webrequest_adblock_plus_ = adblock_plus; 690 webrequest_adblock_plus_ = adblock_plus;
536 webrequest_other_ = other; 691 webrequest_other_ = other;
537 } 692 }
538 693
539 void ExtensionDispatcher::OnShouldClose(const std::string& extension_id, 694 void ExtensionDispatcher::OnShouldClose(const std::string& extension_id,
540 int sequence_id) { 695 int sequence_id) {
541 RenderThread::Get()->Send( 696 RenderThread::Get()->Send(
542 new ExtensionHostMsg_ShouldCloseAck(extension_id, sequence_id)); 697 new ExtensionHostMsg_ShouldCloseAck(extension_id, sequence_id));
543 } 698 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698