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

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: make test extension ID tests pass 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/callback.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/string_piece.h" 10 #include "base/string_piece.h"
11 #include "chrome/common/child_process_logging.h" 11 #include "chrome/common/child_process_logging.h"
12 #include "chrome/common/chrome_switches.h" 12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/common/extensions/api/extension_api.h"
13 #include "chrome/common/extensions/extension.h" 14 #include "chrome/common/extensions/extension.h"
14 #include "chrome/common/extensions/extension_messages.h" 15 #include "chrome/common/extensions/extension_messages.h"
15 #include "chrome/common/extensions/extension_permission_set.h" 16 #include "chrome/common/extensions/extension_permission_set.h"
16 #include "chrome/common/url_constants.h" 17 #include "chrome/common/url_constants.h"
17 #include "chrome/renderer/chrome_render_process_observer.h" 18 #include "chrome/renderer/chrome_render_process_observer.h"
18 #include "chrome/renderer/extensions/app_bindings.h" 19 #include "chrome/renderer/extensions/app_bindings.h"
19 #include "chrome/renderer/extensions/chrome_v8_context.h" 20 #include "chrome/renderer/extensions/chrome_v8_context.h"
20 #include "chrome/renderer/extensions/chrome_v8_extension.h" 21 #include "chrome/renderer/extensions/chrome_v8_extension.h"
21 #include "chrome/renderer/extensions/custom_bindings_util.h" 22 #include "chrome/renderer/extensions/chrome_private_custom_bindings.h"
23 #include "chrome/renderer/extensions/context_menus_custom_bindings.h"
22 #include "chrome/renderer/extensions/event_bindings.h" 24 #include "chrome/renderer/extensions/event_bindings.h"
25 #include "chrome/renderer/extensions/experimental.socket_custom_bindings.h"
26 #include "chrome/renderer/extensions/extension_custom_bindings.h"
23 #include "chrome/renderer/extensions/extension_groups.h" 27 #include "chrome/renderer/extensions/extension_groups.h"
28 #include "chrome/renderer/extensions/file_browser_handler_custom_bindings.h"
29 #include "chrome/renderer/extensions/file_browser_private_custom_bindings.h"
30 #include "chrome/renderer/extensions/i18n_custom_bindings.h"
24 #include "chrome/renderer/extensions/miscellaneous_bindings.h" 31 #include "chrome/renderer/extensions/miscellaneous_bindings.h"
32 #include "chrome/renderer/extensions/page_actions_custom_bindings.h"
33 #include "chrome/renderer/extensions/page_capture_custom_bindings.h"
25 #include "chrome/renderer/extensions/schema_generated_bindings.h" 34 #include "chrome/renderer/extensions/schema_generated_bindings.h"
35 #include "chrome/renderer/extensions/tabs_custom_bindings.h"
36 #include "chrome/renderer/extensions/tts_custom_bindings.h"
26 #include "chrome/renderer/extensions/user_script_slave.h" 37 #include "chrome/renderer/extensions/user_script_slave.h"
38 #include "chrome/renderer/extensions/web_request_custom_bindings.h"
27 #include "chrome/renderer/extensions/webstore_bindings.h" 39 #include "chrome/renderer/extensions/webstore_bindings.h"
28 #include "chrome/renderer/module_system.h" 40 #include "chrome/renderer/module_system.h"
29 #include "chrome/renderer/native_handler.h" 41 #include "chrome/renderer/native_handler.h"
30 #include "chrome/renderer/resource_bundle_source_map.h" 42 #include "chrome/renderer/resource_bundle_source_map.h"
31 #include "content/public/renderer/render_thread.h" 43 #include "content/public/renderer/render_thread.h"
32 #include "grit/renderer_resources.h" 44 #include "grit/renderer_resources.h"
33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" 45 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h"
34 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" 46 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
35 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 47 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
48 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedUserGesture. h"
36 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h" 49 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h"
50 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
37 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" 51 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
38 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques t.h" 52 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLReques t.h"
39 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScopedUserGesture. h"
40 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
41 #include "ui/base/resource/resource_bundle.h" 53 #include "ui/base/resource/resource_bundle.h"
42 #include "v8/include/v8.h" 54 #include "v8/include/v8.h"
43 55
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 using content::RenderThread; 56 using content::RenderThread;
58 using extensions::ChromePrivateCustomBindings; 57 using extensions::ChromePrivateCustomBindings;
59 using extensions::ContextMenusCustomBindings; 58 using extensions::ContextMenusCustomBindings;
60 using extensions::ExperimentalSocketCustomBindings; 59 using extensions::ExperimentalSocketCustomBindings;
60 using extensions::ExtensionAPI;
61 using extensions::ExtensionCustomBindings; 61 using extensions::ExtensionCustomBindings;
62 using extensions::Feature;
62 using extensions::FileBrowserHandlerCustomBindings; 63 using extensions::FileBrowserHandlerCustomBindings;
63 using extensions::FileBrowserPrivateCustomBindings; 64 using extensions::FileBrowserPrivateCustomBindings;
64 using extensions::I18NCustomBindings; 65 using extensions::I18NCustomBindings;
65 using extensions::MiscellaneousBindings; 66 using extensions::MiscellaneousBindings;
66 using extensions::PageActionsCustomBindings; 67 using extensions::PageActionsCustomBindings;
67 using extensions::PageCaptureCustomBindings; 68 using extensions::PageCaptureCustomBindings;
68 using extensions::SchemaGeneratedBindings; 69 using extensions::SchemaGeneratedBindings;
69 using extensions::TTSCustomBindings; 70 using extensions::TTSCustomBindings;
70 using extensions::TabsCustomBindings; 71 using extensions::TabsCustomBindings;
71 using extensions::WebRequestCustomBindings; 72 using extensions::WebRequestCustomBindings;
72 using WebKit::WebDataSource; 73 using WebKit::WebDataSource;
73 using WebKit::WebDocument; 74 using WebKit::WebDocument;
74 using WebKit::WebFrame; 75 using WebKit::WebFrame;
75 using WebKit::WebSecurityPolicy; 76 using WebKit::WebSecurityPolicy;
76 using WebKit::WebString; 77 using WebKit::WebString;
77 using WebKit::WebScopedUserGesture; 78 using WebKit::WebScopedUserGesture;
78 using WebKit::WebVector; 79 using WebKit::WebVector;
79 using WebKit::WebView; 80 using WebKit::WebView;
80 81
81 namespace util = extensions::custom_bindings_util;
82
83 namespace { 82 namespace {
84 83
85 static const int64 kInitialExtensionIdleHandlerDelayMs = 5*1000; 84 static const int64 kInitialExtensionIdleHandlerDelayMs = 5*1000;
86 static const int64 kMaxExtensionIdleHandlerDelayMs = 5*60*1000; 85 static const int64 kMaxExtensionIdleHandlerDelayMs = 5*60*1000;
87 86
88 ChromeV8Context::ContextType ExtensionGroupToContextType(int extension_group) {
89 if (extension_group == EXTENSION_GROUP_CONTENT_SCRIPTS)
90 return ChromeV8Context::CONTENT_SCRIPT;
91 return ChromeV8Context::OTHER;
92 }
93
94 class ChromeHiddenNativeHandler : public NativeHandler { 87 class ChromeHiddenNativeHandler : public NativeHandler {
95 public: 88 public:
96 ChromeHiddenNativeHandler() { 89 ChromeHiddenNativeHandler() {
97 RouteFunction("GetChromeHidden", 90 RouteFunction("GetChromeHidden",
98 base::Bind(&ChromeHiddenNativeHandler::GetChromeHidden, 91 base::Bind(&ChromeHiddenNativeHandler::GetChromeHidden,
99 base::Unretained(this))); 92 base::Unretained(this)));
100 } 93 }
101 94
102 v8::Handle<v8::Value> GetChromeHidden(const v8::Arguments& args) { 95 v8::Handle<v8::Value> GetChromeHidden(const v8::Arguments& args) {
103 return ChromeV8Context::GetOrCreateChromeHidden(v8::Context::GetCurrent()); 96 return ChromeV8Context::GetOrCreateChromeHidden(v8::Context::GetCurrent());
(...skipping 14 matching lines...) Expand all
118 111
119 std::vector<std::string> components; 112 std::vector<std::string> components;
120 for (int i = 0; i < args.Length(); ++i) 113 for (int i = 0; i < args.Length(); ++i)
121 components.push_back(*v8::String::Utf8Value(args[i]->ToString())); 114 components.push_back(*v8::String::Utf8Value(args[i]->ToString()));
122 115
123 LOG(ERROR) << JoinString(components, ','); 116 LOG(ERROR) << JoinString(components, ',');
124 return v8::Undefined(); 117 return v8::Undefined();
125 } 118 }
126 }; 119 };
127 120
128 class ContextInfoNativeHandler : public NativeHandler {
129 public:
130 explicit ContextInfoNativeHandler(ExtensionDispatcher* extension_dispatcher,
131 bool is_bindings_allowed,
132 WebKit::WebFrame* frame,
133 int world_id)
134 : extension_dispatcher_(extension_dispatcher),
135 is_bindings_allowed_(is_bindings_allowed),
136 frame_(frame),
137 world_id_(world_id) {
138 RouteFunction("IsBindingsAllowed",
139 base::Bind(&ContextInfoNativeHandler::IsBindingsAllowed,
140 base::Unretained(this)));
141 RouteFunction("IsAPIAllowed",
142 base::Bind(&ContextInfoNativeHandler::IsAPIAllowed,
143 base::Unretained(this)));
144 }
145
146 v8::Handle<v8::Value> IsBindingsAllowed(const v8::Arguments& args) {
147 return v8::Boolean::New(is_bindings_allowed_);
148 }
149
150 v8::Handle<v8::Value> IsAPIAllowed(const v8::Arguments& args) {
151 std::string custom_api_name = *v8::String::AsciiValue(args[0]->ToString());
152 return v8::Boolean::New(extension_dispatcher_->AllowCustomAPI(
153 frame_, custom_api_name, world_id_));
154 }
155
156 private:
157 ExtensionDispatcher* extension_dispatcher_;
158 bool is_bindings_allowed_;
159 WebKit::WebFrame* frame_;
160 int world_id_;
161 };
162
163 } 121 }
164 122
165 ExtensionDispatcher::ExtensionDispatcher() 123 ExtensionDispatcher::ExtensionDispatcher()
166 : is_webkit_initialized_(false), 124 : is_webkit_initialized_(false),
167 webrequest_adblock_(false), 125 webrequest_adblock_(false),
168 webrequest_adblock_plus_(false), 126 webrequest_adblock_plus_(false),
169 webrequest_other_(false), 127 webrequest_other_(false),
170 source_map_(&ResourceBundle::GetSharedInstance()) { 128 source_map_(&ResourceBundle::GetSharedInstance()) {
171 const CommandLine& command_line = *(CommandLine::ForCurrentProcess()); 129 const CommandLine& command_line = *(CommandLine::ForCurrentProcess());
172 is_extension_process_ = 130 is_extension_process_ =
(...skipping 17 matching lines...) Expand all
190 bool handled = true; 148 bool handled = true;
191 IPC_BEGIN_MESSAGE_MAP(ExtensionDispatcher, message) 149 IPC_BEGIN_MESSAGE_MAP(ExtensionDispatcher, message)
192 IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnMessageInvoke) 150 IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnMessageInvoke)
193 IPC_MESSAGE_HANDLER(ExtensionMsg_DeliverMessage, OnDeliverMessage) 151 IPC_MESSAGE_HANDLER(ExtensionMsg_DeliverMessage, OnDeliverMessage)
194 IPC_MESSAGE_HANDLER(ExtensionMsg_SetFunctionNames, OnSetFunctionNames) 152 IPC_MESSAGE_HANDLER(ExtensionMsg_SetFunctionNames, OnSetFunctionNames)
195 IPC_MESSAGE_HANDLER(ExtensionMsg_Loaded, OnLoaded) 153 IPC_MESSAGE_HANDLER(ExtensionMsg_Loaded, OnLoaded)
196 IPC_MESSAGE_HANDLER(ExtensionMsg_Unloaded, OnUnloaded) 154 IPC_MESSAGE_HANDLER(ExtensionMsg_Unloaded, OnUnloaded)
197 IPC_MESSAGE_HANDLER(ExtensionMsg_SetScriptingWhitelist, 155 IPC_MESSAGE_HANDLER(ExtensionMsg_SetScriptingWhitelist,
198 OnSetScriptingWhitelist) 156 OnSetScriptingWhitelist)
199 IPC_MESSAGE_HANDLER(ExtensionMsg_ActivateExtension, OnActivateExtension) 157 IPC_MESSAGE_HANDLER(ExtensionMsg_ActivateExtension, OnActivateExtension)
200 IPC_MESSAGE_HANDLER(ExtensionMsg_ActivateApplication, OnActivateApplication)
201 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdatePermissions, OnUpdatePermissions) 158 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdatePermissions, OnUpdatePermissions)
202 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateUserScripts, OnUpdateUserScripts) 159 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateUserScripts, OnUpdateUserScripts)
203 IPC_MESSAGE_HANDLER(ExtensionMsg_UsingWebRequestAPI, OnUsingWebRequestAPI) 160 IPC_MESSAGE_HANDLER(ExtensionMsg_UsingWebRequestAPI, OnUsingWebRequestAPI)
204 IPC_MESSAGE_HANDLER(ExtensionMsg_ShouldClose, OnShouldClose) 161 IPC_MESSAGE_HANDLER(ExtensionMsg_ShouldClose, OnShouldClose)
205 IPC_MESSAGE_UNHANDLED(handled = false) 162 IPC_MESSAGE_UNHANDLED(handled = false)
206 IPC_END_MESSAGE_MAP() 163 IPC_END_MESSAGE_MAP()
207 164
208 return handled; 165 return handled;
209 } 166 }
210 167
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 // We don't do anything with existing platform-app stylesheets. They will 297 // We don't do anything with existing platform-app stylesheets. They will
341 // stay resident, but the URL pattern corresponding to the unloaded 298 // stay resident, but the URL pattern corresponding to the unloaded
342 // extension's URL just won't match anything anymore. 299 // extension's URL just won't match anything anymore.
343 } 300 }
344 301
345 void ExtensionDispatcher::OnSetScriptingWhitelist( 302 void ExtensionDispatcher::OnSetScriptingWhitelist(
346 const Extension::ScriptingWhitelist& extension_ids) { 303 const Extension::ScriptingWhitelist& extension_ids) {
347 Extension::SetScriptingWhitelist(extension_ids); 304 Extension::SetScriptingWhitelist(extension_ids);
348 } 305 }
349 306
350 bool ExtensionDispatcher::IsApplicationActive(
351 const std::string& extension_id) const {
352 return active_application_ids_.find(extension_id) !=
353 active_application_ids_.end();
354 }
355
356 bool ExtensionDispatcher::IsExtensionActive( 307 bool ExtensionDispatcher::IsExtensionActive(
357 const std::string& extension_id) const { 308 const std::string& extension_id) const {
358 return active_extension_ids_.find(extension_id) != 309 return active_extension_ids_.find(extension_id) !=
359 active_extension_ids_.end(); 310 active_extension_ids_.end();
360 } 311 }
361 312
362 bool ExtensionDispatcher::AllowScriptExtension( 313 bool ExtensionDispatcher::AllowScriptExtension(
363 WebFrame* frame, 314 WebFrame* frame,
364 const std::string& v8_extension_name, 315 const std::string& v8_extension_name,
365 int extension_group) { 316 int extension_group) {
(...skipping 14 matching lines...) Expand all
380 331
381 bool ExtensionDispatcher::AllowScriptExtension( 332 bool ExtensionDispatcher::AllowScriptExtension(
382 WebFrame* frame, 333 WebFrame* frame,
383 const std::string& v8_extension_name, 334 const std::string& v8_extension_name,
384 int extension_group, 335 int extension_group,
385 int world_id) { 336 int world_id) {
386 g_hack_extension_group = extension_group; 337 g_hack_extension_group = extension_group;
387 return true; 338 return true;
388 } 339 }
389 340
390 bool ExtensionDispatcher::AllowCustomAPI(
391 WebFrame* frame,
392 const std::string& custom_binding_api_name,
393 int world_id) {
394 std::string extension_id = GetExtensionID(frame, world_id);
395 if (IsTestExtensionId(extension_id))
396 return true;
397 const Extension* extension = extensions_.GetByID(extension_id);
398 if (!extension) {
399 // This can happen when a resource is blocked due to CSP; a valid
400 // chrome-extension:// URL is navigated to, so it passes the initial
401 // checks, but the URL gets changed to "chrome-extension://invalid"
402 // afterwards (see chrome_content_renderer_client.cc). An extension
403 // page still gets loaded, just for the extension with ID "invalid",
404 // which of course isn't found so GetById extension will be NULL.
405 //
406 // Reference: http://crbug.com/111614.
407 CHECK_EQ("invalid", extension_id);
408 return false;
409 }
410 return util::AllowAPIInjection(
411 custom_binding_api_name, *extension, this);
412 }
413
414 void ExtensionDispatcher::RegisterNativeHandlers(ModuleSystem* module_system, 341 void ExtensionDispatcher::RegisterNativeHandlers(ModuleSystem* module_system,
415 ChromeV8Context* context) { 342 ChromeV8Context* context) {
416 module_system->RegisterNativeHandler("app",
417 scoped_ptr<NativeHandler>(new AppBindings(this, context)));
418 module_system->RegisterNativeHandler("webstore", 343 module_system->RegisterNativeHandler("webstore",
419 scoped_ptr<NativeHandler>(new WebstoreBindings(this, context))); 344 scoped_ptr<NativeHandler>(new WebstoreBindings(this, context)));
420 module_system->RegisterNativeHandler("event_bindings", 345 module_system->RegisterNativeHandler("event_bindings",
421 scoped_ptr<NativeHandler>(EventBindings::Get(this))); 346 scoped_ptr<NativeHandler>(EventBindings::Get(this)));
422 module_system->RegisterNativeHandler("miscellaneous_bindings", 347 module_system->RegisterNativeHandler("miscellaneous_bindings",
423 scoped_ptr<NativeHandler>(MiscellaneousBindings::Get(this))); 348 scoped_ptr<NativeHandler>(MiscellaneousBindings::Get(this)));
424 module_system->RegisterNativeHandler("schema_generated_bindings", 349 module_system->RegisterNativeHandler("schema_generated_bindings",
425 scoped_ptr<NativeHandler>(SchemaGeneratedBindings::Get(this))); 350 scoped_ptr<NativeHandler>(SchemaGeneratedBindings::Get(this)));
426 351
427 // Custom bindings. 352 // Custom bindings.
353 module_system->RegisterNativeHandler("app",
354 scoped_ptr<NativeHandler>(new AppBindings(this, context)));
428 module_system->RegisterNativeHandler("chrome_private", 355 module_system->RegisterNativeHandler("chrome_private",
429 scoped_ptr<NativeHandler>( 356 scoped_ptr<NativeHandler>(
430 new ChromePrivateCustomBindings(this))); 357 new ChromePrivateCustomBindings(this)));
431 module_system->RegisterNativeHandler("context_menus", 358 module_system->RegisterNativeHandler("context_menus",
432 scoped_ptr<NativeHandler>(new ContextMenusCustomBindings())); 359 scoped_ptr<NativeHandler>(new ContextMenusCustomBindings()));
433 module_system->RegisterNativeHandler("extension", 360 module_system->RegisterNativeHandler("extension",
434 scoped_ptr<NativeHandler>( 361 scoped_ptr<NativeHandler>(
435 new ExtensionCustomBindings(this))); 362 new ExtensionCustomBindings(this)));
436 module_system->RegisterNativeHandler("experimental_socket", 363 module_system->RegisterNativeHandler("experimental_socket",
437 scoped_ptr<NativeHandler>(new ExperimentalSocketCustomBindings())); 364 scoped_ptr<NativeHandler>(new ExperimentalSocketCustomBindings()));
(...skipping 10 matching lines...) Expand all
448 scoped_ptr<NativeHandler>(new PageCaptureCustomBindings())); 375 scoped_ptr<NativeHandler>(new PageCaptureCustomBindings()));
449 module_system->RegisterNativeHandler("tabs", 376 module_system->RegisterNativeHandler("tabs",
450 scoped_ptr<NativeHandler>(new TabsCustomBindings())); 377 scoped_ptr<NativeHandler>(new TabsCustomBindings()));
451 module_system->RegisterNativeHandler("tts", 378 module_system->RegisterNativeHandler("tts",
452 scoped_ptr<NativeHandler>(new TTSCustomBindings())); 379 scoped_ptr<NativeHandler>(new TTSCustomBindings()));
453 module_system->RegisterNativeHandler("web_request", 380 module_system->RegisterNativeHandler("web_request",
454 scoped_ptr<NativeHandler>(new WebRequestCustomBindings())); 381 scoped_ptr<NativeHandler>(new WebRequestCustomBindings()));
455 } 382 }
456 383
457 void ExtensionDispatcher::PopulateSourceMap() { 384 void ExtensionDispatcher::PopulateSourceMap() {
458 source_map_.RegisterSource("app", IDR_APP_BINDINGS_JS);
459 source_map_.RegisterSource("webstore", IDR_WEBSTORE_BINDINGS_JS); 385 source_map_.RegisterSource("webstore", IDR_WEBSTORE_BINDINGS_JS);
460 source_map_.RegisterSource("event_bindings", IDR_EVENT_BINDINGS_JS); 386 source_map_.RegisterSource("event_bindings", IDR_EVENT_BINDINGS_JS);
461 source_map_.RegisterSource("miscellaneous_bindings", 387 source_map_.RegisterSource("miscellaneous_bindings",
462 IDR_MISCELLANEOUS_BINDINGS_JS); 388 IDR_MISCELLANEOUS_BINDINGS_JS);
463 source_map_.RegisterSource("schema_generated_bindings", 389 source_map_.RegisterSource("schema_generated_bindings",
464 IDR_SCHEMA_GENERATED_BINDINGS_JS); 390 IDR_SCHEMA_GENERATED_BINDINGS_JS);
465 source_map_.RegisterSource("json_schema", IDR_JSON_SCHEMA_JS); 391 source_map_.RegisterSource("json_schema", IDR_JSON_SCHEMA_JS);
466 source_map_.RegisterSource("apitest", IDR_EXTENSION_APITEST_JS); 392 source_map_.RegisterSource("apitest", IDR_EXTENSION_APITEST_JS);
467 source_map_.RegisterSource("setup_bindings", IDR_SETUP_BINDINGS_JS);
468 393
469 // Custom bindings. 394 // Custom bindings.
395 source_map_.RegisterSource("app", IDR_APP_CUSTOM_BINDINGS_JS);
470 source_map_.RegisterSource("browserAction", 396 source_map_.RegisterSource("browserAction",
471 IDR_BROWSER_ACTION_CUSTOM_BINDINGS_JS); 397 IDR_BROWSER_ACTION_CUSTOM_BINDINGS_JS);
472 source_map_.RegisterSource("chromePrivate", 398 source_map_.RegisterSource("chromePrivate",
473 IDR_CHROME_PRIVATE_CUSTOM_BINDINGS_JS); 399 IDR_CHROME_PRIVATE_CUSTOM_BINDINGS_JS);
474 source_map_.RegisterSource("contentSettings", 400 source_map_.RegisterSource("contentSettings",
475 IDR_CONTENT_SETTINGS_CUSTOM_BINDINGS_JS); 401 IDR_CONTENT_SETTINGS_CUSTOM_BINDINGS_JS);
476 source_map_.RegisterSource("contextMenus", 402 source_map_.RegisterSource("contextMenus",
477 IDR_CONTEXT_MENUS_CUSTOM_BINDINGS_JS); 403 IDR_CONTEXT_MENUS_CUSTOM_BINDINGS_JS);
478 source_map_.RegisterSource("devtools", IDR_DEVTOOLS_CUSTOM_BINDINGS_JS); 404 source_map_.RegisterSource("devtools", IDR_DEVTOOLS_CUSTOM_BINDINGS_JS);
479 source_map_.RegisterSource("experimental.declarative", 405 source_map_.RegisterSource("experimental.declarative",
(...skipping 22 matching lines...) Expand all
502 } 428 }
503 429
504 void ExtensionDispatcher::DidCreateScriptContext( 430 void ExtensionDispatcher::DidCreateScriptContext(
505 WebFrame* frame, v8::Handle<v8::Context> v8_context, int extension_group, 431 WebFrame* frame, v8::Handle<v8::Context> v8_context, int extension_group,
506 int world_id) { 432 int world_id) {
507 // TODO(koz): If the caller didn't pass extension_group, use the last value. 433 // TODO(koz): If the caller didn't pass extension_group, use the last value.
508 if (extension_group == -1) 434 if (extension_group == -1)
509 extension_group = g_hack_extension_group; 435 extension_group = g_hack_extension_group;
510 436
511 std::string extension_id = GetExtensionID(frame, world_id); 437 std::string extension_id = GetExtensionID(frame, world_id);
438
439 const Extension* extension = extensions_.GetByID(extension_id);
440 if (!extension && !extension_id.empty() && !IsTestExtensionId(extension_id)) {
441 // There are conditions where despite a context being associated with an
442 // extension, no extension actually gets found. Ignore "invalid" because
443 // CSP blocks extension page loading by switching the extension ID to
444 // "invalid". This isn't interesting.
445 if (extension_id != "invalid") {
446 LOG(ERROR) << "Extension \"" << extension_id << "\" not found";
447 RenderThread::Get()->RecordUserMetrics("ExtensionNotFound_ED");
448 }
449 }
450
451 ExtensionURLInfo url_info(frame->document().securityOrigin(),
452 UserScriptSlave::GetDataSourceURLForFrame(frame));
453
454 Feature::Context context_type =
455 ClassifyJavaScriptContext(extension_id, extension_group, url_info);
456
512 ChromeV8Context* context = 457 ChromeV8Context* context =
513 new ChromeV8Context( 458 new ChromeV8Context(v8_context, frame, extension_id, context_type);
514 v8_context,
515 frame,
516 extension_id,
517 ExtensionGroupToContextType(extension_group));
518 v8_context_set_.Add(context); 459 v8_context_set_.Add(context);
519 460
520 scoped_ptr<ModuleSystem> module_system(new ModuleSystem(&source_map_)); 461 scoped_ptr<ModuleSystem> module_system(new ModuleSystem(&source_map_));
521 RegisterNativeHandlers(module_system.get(), context); 462 RegisterNativeHandlers(module_system.get(), context);
522 463
523 bool is_bindings_allowed =
524 IsTestExtensionId(extension_id) ||
525 context->context_type() == ChromeV8Context::CONTENT_SCRIPT ||
526 extensions_.ExtensionBindingsAllowed(ExtensionURLInfo(
527 frame->document().securityOrigin(),
528 UserScriptSlave::GetDataSourceURLForFrame(frame)));
529
530 module_system->RegisterNativeHandler("chrome_hidden", 464 module_system->RegisterNativeHandler("chrome_hidden",
531 scoped_ptr<NativeHandler>(new ChromeHiddenNativeHandler())); 465 scoped_ptr<NativeHandler>(new ChromeHiddenNativeHandler()));
532 module_system->RegisterNativeHandler("context_info",
533 scoped_ptr<NativeHandler>(new ContextInfoNativeHandler(
534 this,
535 is_bindings_allowed,
536 frame,
537 world_id)));
538 module_system->RegisterNativeHandler("print", 466 module_system->RegisterNativeHandler("print",
539 scoped_ptr<NativeHandler>(new PrintNativeHandler())); 467 scoped_ptr<NativeHandler>(new PrintNativeHandler()));
540 468
541 const Extension* extension = extensions_.GetByID(context->extension_id());
542 int manifest_version = 1; 469 int manifest_version = 1;
543 if (extension) 470 if (extension)
544 manifest_version = extension->manifest_version(); 471 manifest_version = extension->manifest_version();
545 472
546 // Create the 'chrome' variable if it doesn't already exist. 473 // Create the 'chrome' variable if it doesn't already exist.
547 { 474 {
548 v8::HandleScope handle_scope; 475 v8::HandleScope handle_scope;
549 v8::Handle<v8::String> chrome_string(v8::String::New("chrome")); 476 v8::Handle<v8::String> chrome_string(v8::String::New("chrome"));
550 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global()); 477 v8::Handle<v8::Object> global(v8::Context::GetCurrent()->Global());
551 if (global->Get(chrome_string)->IsUndefined()) 478 if (global->Get(chrome_string)->IsUndefined())
552 global->Set(chrome_string, v8::Object::New()); 479 global->Set(chrome_string, v8::Object::New());
553 } 480 }
554 module_system->Require("app"); 481
555 module_system->Require("webstore"); 482 // Loading JavaScript is expensive, so only run the full API bindings
556 if (is_bindings_allowed) { 483 // generation mechanisms in extension pages (NOT all web pages).
557 module_system->Require("setup_bindings"); 484 switch (context_type) {
485 case Feature::UNSPECIFIED_CONTEXT:
486 case Feature::WEB_PAGE_CONTEXT:
487 break;
488
489 case Feature::PRIVILEGED_CONTEXT:
490 case Feature::UNPRIVILEGED_CONTEXT:
491 case Feature::CONTENT_SCRIPT_CONTEXT:
492 module_system->Require("json_schema");
493 module_system->Require("event_bindings");
494 module_system->Require("miscellaneous_bindings");
495 module_system->Require("schema_generated_bindings");
496 module_system->Require("apitest");
497 break;
558 } 498 }
499
500 scoped_ptr<std::set<std::string> > apis =
501 ExtensionAPI::GetInstance()->GetAPIsForContext(
502 context_type, extension, url_info.url());
503
504 // TODO(kalman): include this in the APIs returned from GetAPIsForContext.
505 apis->insert("webstore");
506
507 // TODO(kalman): this is probably the most unfortunate thing I've ever had
508 // to write. We really need to factor things differently to delete the
509 // concept of a test extension ID.
510 if (IsTestExtensionId(extension_id)) {
511 module_system->Require("miscellaneous_bindings");
512 module_system->Require("schema_generated_bindings");
513 apis->insert("extension");
514 }
515
516 for (std::set<std::string>::iterator i = apis->begin(); i != apis->end();
517 ++i) {
518 module_system->Require(*i);
519 }
520
559 module_system->set_natives_enabled(false); 521 module_system->set_natives_enabled(false);
560 522
561 context->set_module_system(module_system.Pass()); 523 context->set_module_system(module_system.Pass());
562 524
563 context->DispatchOnLoadEvent( 525 context->DispatchOnLoadEvent(
564 is_extension_process_, 526 is_extension_process_,
565 ChromeRenderProcessObserver::is_incognito_process(), 527 ChromeRenderProcessObserver::is_incognito_process(),
566 manifest_version); 528 manifest_version);
567 529
568 VLOG(1) << "Num tracked contexts: " << v8_context_set_.size(); 530 VLOG(1) << "Num tracked contexts: " << v8_context_set_.size();
(...skipping 19 matching lines...) Expand all
588 if (!context) 550 if (!context)
589 return; 551 return;
590 552
591 context->DispatchOnUnloadEvent(); 553 context->DispatchOnUnloadEvent();
592 554
593 v8_context_set_.Remove(context); 555 v8_context_set_.Remove(context);
594 VLOG(1) << "Num tracked contexts: " << v8_context_set_.size(); 556 VLOG(1) << "Num tracked contexts: " << v8_context_set_.size();
595 } 557 }
596 558
597 void ExtensionDispatcher::SetTestExtensionId(const std::string& id) { 559 void ExtensionDispatcher::SetTestExtensionId(const std::string& id) {
560 CHECK(!id.empty());
598 test_extension_id_ = id; 561 test_extension_id_ = id;
599 } 562 }
600 563
601 bool ExtensionDispatcher::IsTestExtensionId(const std::string& id) { 564 bool ExtensionDispatcher::IsTestExtensionId(const std::string& id) {
602 return !test_extension_id_.empty() && id == test_extension_id_; 565 return !test_extension_id_.empty() && id == test_extension_id_;
603 } 566 }
604 567
605 void ExtensionDispatcher::OnActivateApplication(
606 const std::string& extension_id) {
607 active_application_ids_.insert(extension_id);
608 }
609
610 void ExtensionDispatcher::OnActivateExtension( 568 void ExtensionDispatcher::OnActivateExtension(
611 const std::string& extension_id) { 569 const std::string& extension_id) {
612 active_extension_ids_.insert(extension_id); 570 active_extension_ids_.insert(extension_id);
613 571
614 // This is called when starting a new extension page, so start the idle 572 // This is called when starting a new extension page, so start the idle
615 // handler ticking. 573 // handler ticking.
616 RenderThread::Get()->ScheduleIdleHandler(kInitialExtensionIdleHandlerDelayMs); 574 RenderThread::Get()->ScheduleIdleHandler(kInitialExtensionIdleHandlerDelayMs);
617 575
618 UpdateActiveExtensions(); 576 UpdateActiveExtensions();
619 577
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
727 webrequest_adblock_ = adblock; 685 webrequest_adblock_ = adblock;
728 webrequest_adblock_plus_ = adblock_plus; 686 webrequest_adblock_plus_ = adblock_plus;
729 webrequest_other_ = other; 687 webrequest_other_ = other;
730 } 688 }
731 689
732 void ExtensionDispatcher::OnShouldClose(const std::string& extension_id, 690 void ExtensionDispatcher::OnShouldClose(const std::string& extension_id,
733 int sequence_id) { 691 int sequence_id) {
734 RenderThread::Get()->Send( 692 RenderThread::Get()->Send(
735 new ExtensionHostMsg_ShouldCloseAck(extension_id, sequence_id)); 693 new ExtensionHostMsg_ShouldCloseAck(extension_id, sequence_id));
736 } 694 }
695
696 Feature::Context ExtensionDispatcher::ClassifyJavaScriptContext(
697 const std::string& extension_id,
698 int extension_group,
699 const ExtensionURLInfo& url_info) {
700 if (extension_group == EXTENSION_GROUP_CONTENT_SCRIPTS)
701 return Feature::CONTENT_SCRIPT_CONTEXT;
702
703 if (IsExtensionActive(extension_id))
704 return Feature::PRIVILEGED_CONTEXT;
705
706 if (extensions_.ExtensionBindingsAllowed(url_info))
707 return Feature::UNPRIVILEGED_CONTEXT;
708
709 if (url_info.url().is_valid())
710 return Feature::WEB_PAGE_CONTEXT;
711
712 return Feature::UNSPECIFIED_CONTEXT;
713 }
OLDNEW
« no previous file with comments | « chrome/renderer/extensions/extension_dispatcher.h ('k') | chrome/renderer/extensions/schema_generated_bindings.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698