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

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

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

Powered by Google App Engine
This is Rietveld 408576698