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

Side by Side Diff: extensions/renderer/dispatcher.cc

Issue 1010473002: Revert of Move Extension ScriptContext creation into ScriptContextSet. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 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
« no previous file with comments | « extensions/renderer/dispatcher.h ('k') | extensions/renderer/document_custom_bindings.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "extensions/renderer/dispatcher.h" 5 #include "extensions/renderer/dispatcher.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/debug/alias.h" 10 #include "base/debug/alias.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/metrics/histogram_macros.h" 12 #include "base/metrics/histogram_macros.h"
13 #include "base/metrics/user_metrics_action.h" 13 #include "base/metrics/user_metrics_action.h"
14 #include "base/strings/string_piece.h" 14 #include "base/strings/string_piece.h"
15 #include "base/strings/string_split.h" 15 #include "base/strings/string_split.h"
16 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
17 #include "base/time/time.h" 17 #include "base/time/time.h"
18 #include "base/values.h" 18 #include "base/values.h"
19 #include "content/grit/content_resources.h" 19 #include "content/grit/content_resources.h"
20 #include "content/public/child/v8_value_converter.h" 20 #include "content/public/child/v8_value_converter.h"
21 #include "content/public/common/content_switches.h" 21 #include "content/public/common/content_switches.h"
22 #include "content/public/common/url_constants.h" 22 #include "content/public/common/url_constants.h"
23 #include "content/public/renderer/render_thread.h" 23 #include "content/public/renderer/render_thread.h"
24 #include "content/public/renderer/render_view.h" 24 #include "content/public/renderer/render_view.h"
25 #include "extensions/common/api/messaging/message.h" 25 #include "extensions/common/api/messaging/message.h"
26 #include "extensions/common/constants.h" 26 #include "extensions/common/constants.h"
27 #include "extensions/common/extension.h"
27 #include "extensions/common/extension_api.h" 28 #include "extensions/common/extension_api.h"
28 #include "extensions/common/extension_messages.h" 29 #include "extensions/common/extension_messages.h"
29 #include "extensions/common/extension_urls.h" 30 #include "extensions/common/extension_urls.h"
30 #include "extensions/common/feature_switch.h" 31 #include "extensions/common/feature_switch.h"
31 #include "extensions/common/features/feature.h" 32 #include "extensions/common/features/feature.h"
32 #include "extensions/common/features/feature_provider.h" 33 #include "extensions/common/features/feature_provider.h"
33 #include "extensions/common/manifest.h" 34 #include "extensions/common/manifest.h"
34 #include "extensions/common/manifest_constants.h" 35 #include "extensions/common/manifest_constants.h"
35 #include "extensions/common/manifest_handlers/background_info.h" 36 #include "extensions/common/manifest_handlers/background_info.h"
36 #include "extensions/common/manifest_handlers/content_capabilities_handler.h" 37 #include "extensions/common/manifest_handlers/content_capabilities_handler.h"
37 #include "extensions/common/manifest_handlers/externally_connectable.h" 38 #include "extensions/common/manifest_handlers/externally_connectable.h"
38 #include "extensions/common/manifest_handlers/options_page_info.h" 39 #include "extensions/common/manifest_handlers/options_page_info.h"
40 #include "extensions/common/manifest_handlers/sandboxed_page_info.h"
39 #include "extensions/common/message_bundle.h" 41 #include "extensions/common/message_bundle.h"
40 #include "extensions/common/permissions/permission_set.h" 42 #include "extensions/common/permissions/permission_set.h"
41 #include "extensions/common/permissions/permissions_data.h" 43 #include "extensions/common/permissions/permissions_data.h"
42 #include "extensions/common/switches.h" 44 #include "extensions/common/switches.h"
43 #include "extensions/common/view_type.h" 45 #include "extensions/common/view_type.h"
44 #include "extensions/renderer/api_activity_logger.h" 46 #include "extensions/renderer/api_activity_logger.h"
45 #include "extensions/renderer/api_definitions_natives.h" 47 #include "extensions/renderer/api_definitions_natives.h"
46 #include "extensions/renderer/app_runtime_custom_bindings.h" 48 #include "extensions/renderer/app_runtime_custom_bindings.h"
47 #include "extensions/renderer/app_window_custom_bindings.h" 49 #include "extensions/renderer/app_window_custom_bindings.h"
48 #include "extensions/renderer/binding_generating_native_handler.h" 50 #include "extensions/renderer/binding_generating_native_handler.h"
49 #include "extensions/renderer/blob_native_handler.h" 51 #include "extensions/renderer/blob_native_handler.h"
50 #include "extensions/renderer/content_watcher.h" 52 #include "extensions/renderer/content_watcher.h"
51 #include "extensions/renderer/context_menus_custom_bindings.h" 53 #include "extensions/renderer/context_menus_custom_bindings.h"
52 #include "extensions/renderer/css_native_handler.h" 54 #include "extensions/renderer/css_native_handler.h"
53 #include "extensions/renderer/dispatcher_delegate.h" 55 #include "extensions/renderer/dispatcher_delegate.h"
54 #include "extensions/renderer/document_custom_bindings.h" 56 #include "extensions/renderer/document_custom_bindings.h"
55 #include "extensions/renderer/dom_activity_logger.h" 57 #include "extensions/renderer/dom_activity_logger.h"
56 #include "extensions/renderer/event_bindings.h" 58 #include "extensions/renderer/event_bindings.h"
59 #include "extensions/renderer/extension_groups.h"
57 #include "extensions/renderer/extension_helper.h" 60 #include "extensions/renderer/extension_helper.h"
58 #include "extensions/renderer/extensions_renderer_client.h" 61 #include "extensions/renderer/extensions_renderer_client.h"
59 #include "extensions/renderer/file_system_natives.h" 62 #include "extensions/renderer/file_system_natives.h"
60 #include "extensions/renderer/guest_view/guest_view_internal_custom_bindings.h" 63 #include "extensions/renderer/guest_view/guest_view_internal_custom_bindings.h"
61 #include "extensions/renderer/i18n_custom_bindings.h" 64 #include "extensions/renderer/i18n_custom_bindings.h"
62 #include "extensions/renderer/id_generator_custom_bindings.h" 65 #include "extensions/renderer/id_generator_custom_bindings.h"
63 #include "extensions/renderer/lazy_background_page_native_handler.h" 66 #include "extensions/renderer/lazy_background_page_native_handler.h"
64 #include "extensions/renderer/logging_native_handler.h" 67 #include "extensions/renderer/logging_native_handler.h"
65 #include "extensions/renderer/messaging_bindings.h" 68 #include "extensions/renderer/messaging_bindings.h"
66 #include "extensions/renderer/module_system.h" 69 #include "extensions/renderer/module_system.h"
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 command_line.HasSwitch(switches::kExtensionProcess) || 198 command_line.HasSwitch(switches::kExtensionProcess) ||
196 command_line.HasSwitch(::switches::kSingleProcess); 199 command_line.HasSwitch(::switches::kSingleProcess);
197 200
198 if (set_idle_notifications_) { 201 if (set_idle_notifications_) {
199 RenderThread::Get()->SetIdleNotificationDelayInMs( 202 RenderThread::Get()->SetIdleNotificationDelayInMs(
200 kInitialExtensionIdleHandlerDelayMs); 203 kInitialExtensionIdleHandlerDelayMs);
201 } 204 }
202 205
203 RenderThread::Get()->RegisterExtension(SafeBuiltins::CreateV8Extension()); 206 RenderThread::Get()->RegisterExtension(SafeBuiltins::CreateV8Extension());
204 207
205 script_context_set_.reset(
206 new ScriptContextSet(&extensions_, &active_extension_ids_));
207 user_script_set_manager_.reset(new UserScriptSetManager(&extensions_)); 208 user_script_set_manager_.reset(new UserScriptSetManager(&extensions_));
208 script_injection_manager_.reset( 209 script_injection_manager_.reset(
209 new ScriptInjectionManager(&extensions_, user_script_set_manager_.get())); 210 new ScriptInjectionManager(&extensions_, user_script_set_manager_.get()));
210 user_script_set_manager_observer_.Add(user_script_set_manager_.get()); 211 user_script_set_manager_observer_.Add(user_script_set_manager_.get());
211 request_sender_.reset(new RequestSender(this)); 212 request_sender_.reset(new RequestSender(this));
212 PopulateSourceMap(); 213 PopulateSourceMap();
213 } 214 }
214 215
215 Dispatcher::~Dispatcher() { 216 Dispatcher::~Dispatcher() {
216 } 217 }
217 218
218 void Dispatcher::OnRenderViewCreated(content::RenderView* render_view) { 219 void Dispatcher::OnRenderViewCreated(content::RenderView* render_view) {
219 script_injection_manager_->OnRenderViewCreated(render_view); 220 script_injection_manager_->OnRenderViewCreated(render_view);
220 } 221 }
221 222
222 bool Dispatcher::IsExtensionActive(const std::string& extension_id) const { 223 bool Dispatcher::IsExtensionActive(const std::string& extension_id) const {
223 bool is_active = 224 bool is_active =
224 active_extension_ids_.find(extension_id) != active_extension_ids_.end(); 225 active_extension_ids_.find(extension_id) != active_extension_ids_.end();
225 if (is_active) 226 if (is_active)
226 CHECK(extensions_.Contains(extension_id)); 227 CHECK(extensions_.Contains(extension_id));
227 return is_active; 228 return is_active;
228 } 229 }
229 230
231 const Extension* Dispatcher::GetExtensionFromFrameAndWorld(
232 const blink::WebFrame* frame,
233 int world_id,
234 bool use_effective_url) {
235 std::string extension_id;
236 if (world_id != 0) {
237 // Isolated worlds (content script).
238 extension_id = ScriptInjection::GetHostIdForIsolatedWorld(world_id);
239 } else if (!frame->document().securityOrigin().isUnique()) {
240 // TODO(kalman): Delete the above check.
241
242 // Extension pages (chrome-extension:// URLs).
243 GURL frame_url = ScriptContext::GetDataSourceURLForFrame(frame);
244 frame_url = ScriptContext::GetEffectiveDocumentURL(
245 frame, frame_url, use_effective_url);
246 extension_id = extensions_.GetExtensionOrAppIDByURL(frame_url);
247 }
248
249 const Extension* extension = extensions_.GetByID(extension_id);
250 if (!extension && !extension_id.empty()) {
251 // There are conditions where despite a context being associated with an
252 // extension, no extension actually gets found. Ignore "invalid" because
253 // CSP blocks extension page loading by switching the extension ID to
254 // "invalid". This isn't interesting.
255 if (extension_id != "invalid") {
256 LOG(ERROR) << "Extension \"" << extension_id << "\" not found";
257 RenderThread::Get()->RecordAction(
258 UserMetricsAction("ExtensionNotFound_ED"));
259 }
260 }
261 return extension;
262 }
263
230 void Dispatcher::DidCreateScriptContext( 264 void Dispatcher::DidCreateScriptContext(
231 blink::WebLocalFrame* frame, 265 blink::WebLocalFrame* frame,
232 const v8::Handle<v8::Context>& v8_context, 266 const v8::Handle<v8::Context>& v8_context,
233 int extension_group, 267 int extension_group,
234 int world_id) { 268 int world_id) {
235 const base::TimeTicks start_time = base::TimeTicks::Now(); 269 const base::TimeTicks start_time = base::TimeTicks::Now();
236 270
237 ScriptContext* context = script_context_set_->Register( 271 const Extension* extension =
238 frame, v8_context, extension_group, world_id); 272 GetExtensionFromFrameAndWorld(frame, world_id, false);
273 const Extension* effective_extension =
274 GetExtensionFromFrameAndWorld(frame, world_id, true);
275
276 GURL frame_url = ScriptContext::GetDataSourceURLForFrame(frame);
277 Feature::Context context_type =
278 ClassifyJavaScriptContext(extension,
279 extension_group,
280 frame_url,
281 frame->document().securityOrigin());
282 Feature::Context effective_context_type = ClassifyJavaScriptContext(
283 effective_extension,
284 extension_group,
285 ScriptContext::GetEffectiveDocumentURL(frame, frame_url, true),
286 frame->document().securityOrigin());
287
288 ScriptContext* context =
289 new ScriptContext(v8_context, frame, extension, context_type,
290 effective_extension, effective_context_type);
291 script_context_set_.Add(context);
239 292
240 // Initialize origin permissions for content scripts, which can't be 293 // Initialize origin permissions for content scripts, which can't be
241 // initialized in |OnActivateExtension|. 294 // initialized in |OnActivateExtension|.
242 if (context->context_type() == Feature::CONTENT_SCRIPT_CONTEXT) 295 if (context_type == Feature::CONTENT_SCRIPT_CONTEXT)
243 InitOriginPermissions(context->extension()); 296 InitOriginPermissions(extension);
244 297
245 { 298 {
246 scoped_ptr<ModuleSystem> module_system( 299 scoped_ptr<ModuleSystem> module_system(
247 new ModuleSystem(context, &source_map_)); 300 new ModuleSystem(context, &source_map_));
248 context->set_module_system(module_system.Pass()); 301 context->set_module_system(module_system.Pass());
249 } 302 }
250 ModuleSystem* module_system = context->module_system(); 303 ModuleSystem* module_system = context->module_system();
251 304
252 // Enable natives in startup. 305 // Enable natives in startup.
253 ModuleSystem::NativesEnabledScope natives_enabled_scope(module_system); 306 ModuleSystem::NativesEnabledScope natives_enabled_scope(module_system);
(...skipping 14 matching lines...) Expand all
268 bool is_within_platform_app = IsWithinPlatformApp(); 321 bool is_within_platform_app = IsWithinPlatformApp();
269 // Inject custom JS into the platform app context. 322 // Inject custom JS into the platform app context.
270 if (is_within_platform_app) { 323 if (is_within_platform_app) {
271 module_system->Require("platformApp"); 324 module_system->Require("platformApp");
272 } 325 }
273 326
274 RequireGuestViewModules(context); 327 RequireGuestViewModules(context);
275 delegate_->RequireAdditionalModules(context, is_within_platform_app); 328 delegate_->RequireAdditionalModules(context, is_within_platform_app);
276 329
277 const base::TimeDelta elapsed = base::TimeTicks::Now() - start_time; 330 const base::TimeDelta elapsed = base::TimeTicks::Now() - start_time;
278 switch (context->context_type()) { 331 switch (context_type) {
279 case Feature::UNSPECIFIED_CONTEXT: 332 case Feature::UNSPECIFIED_CONTEXT:
280 UMA_HISTOGRAM_TIMES("Extensions.DidCreateScriptContext_Unspecified", 333 UMA_HISTOGRAM_TIMES("Extensions.DidCreateScriptContext_Unspecified",
281 elapsed); 334 elapsed);
282 break; 335 break;
283 case Feature::BLESSED_EXTENSION_CONTEXT: 336 case Feature::BLESSED_EXTENSION_CONTEXT:
284 UMA_HISTOGRAM_TIMES("Extensions.DidCreateScriptContext_Blessed", elapsed); 337 UMA_HISTOGRAM_TIMES("Extensions.DidCreateScriptContext_Blessed", elapsed);
285 break; 338 break;
286 case Feature::UNBLESSED_EXTENSION_CONTEXT: 339 case Feature::UNBLESSED_EXTENSION_CONTEXT:
287 UMA_HISTOGRAM_TIMES("Extensions.DidCreateScriptContext_Unblessed", 340 UMA_HISTOGRAM_TIMES("Extensions.DidCreateScriptContext_Unblessed",
288 elapsed); 341 elapsed);
289 break; 342 break;
290 case Feature::CONTENT_SCRIPT_CONTEXT: 343 case Feature::CONTENT_SCRIPT_CONTEXT:
291 UMA_HISTOGRAM_TIMES("Extensions.DidCreateScriptContext_ContentScript", 344 UMA_HISTOGRAM_TIMES("Extensions.DidCreateScriptContext_ContentScript",
292 elapsed); 345 elapsed);
293 break; 346 break;
294 case Feature::WEB_PAGE_CONTEXT: 347 case Feature::WEB_PAGE_CONTEXT:
295 UMA_HISTOGRAM_TIMES("Extensions.DidCreateScriptContext_WebPage", elapsed); 348 UMA_HISTOGRAM_TIMES("Extensions.DidCreateScriptContext_WebPage", elapsed);
296 break; 349 break;
297 case Feature::BLESSED_WEB_PAGE_CONTEXT: 350 case Feature::BLESSED_WEB_PAGE_CONTEXT:
298 UMA_HISTOGRAM_TIMES("Extensions.DidCreateScriptContext_BlessedWebPage", 351 UMA_HISTOGRAM_TIMES("Extensions.DidCreateScriptContext_BlessedWebPage",
299 elapsed); 352 elapsed);
300 break; 353 break;
301 case Feature::WEBUI_CONTEXT: 354 case Feature::WEBUI_CONTEXT:
302 UMA_HISTOGRAM_TIMES("Extensions.DidCreateScriptContext_WebUI", elapsed); 355 UMA_HISTOGRAM_TIMES("Extensions.DidCreateScriptContext_WebUI", elapsed);
303 break; 356 break;
304 } 357 }
305 358
306 VLOG(1) << "Num tracked contexts: " << script_context_set_->size(); 359 VLOG(1) << "Num tracked contexts: " << script_context_set_.size();
307 } 360 }
308 361
309 void Dispatcher::WillReleaseScriptContext( 362 void Dispatcher::WillReleaseScriptContext(
310 blink::WebLocalFrame* frame, 363 blink::WebLocalFrame* frame,
311 const v8::Handle<v8::Context>& v8_context, 364 const v8::Handle<v8::Context>& v8_context,
312 int world_id) { 365 int world_id) {
313 ScriptContext* context = script_context_set_->GetByV8Context(v8_context); 366 ScriptContext* context = script_context_set_.GetByV8Context(v8_context);
314 if (!context) 367 if (!context)
315 return; 368 return;
316 369
317 context->DispatchOnUnloadEvent(); 370 context->DispatchOnUnloadEvent();
318 // TODO(kalman): add an invalidation observer interface to ScriptContext. 371 // TODO(kalman): add an invalidation observer interface to ScriptContext.
319 request_sender_->InvalidateSource(context); 372 request_sender_->InvalidateSource(context);
320 373
321 script_context_set_->Remove(context); 374 script_context_set_.Remove(context);
322 VLOG(1) << "Num tracked contexts: " << script_context_set_->size(); 375 VLOG(1) << "Num tracked contexts: " << script_context_set_.size();
323 } 376 }
324 377
325 void Dispatcher::DidCreateDocumentElement(blink::WebFrame* frame) { 378 void Dispatcher::DidCreateDocumentElement(blink::WebFrame* frame) {
326 // Note: use GetEffectiveDocumentURL not just frame->document()->url() 379 // Note: use GetEffectiveDocumentURL not just frame->document()->url()
327 // so that this also injects the stylesheet on about:blank frames that 380 // so that this also injects the stylesheet on about:blank frames that
328 // are hosted in the extension process. 381 // are hosted in the extension process.
329 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( 382 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL(
330 frame, frame->document().url(), true /* match_about_blank */); 383 frame, frame->document().url(), true /* match_about_blank */);
331 384
332 const Extension* extension = 385 const Extension* extension =
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 const std::string& function_name, 436 const std::string& function_name,
384 ScriptContext* context) const { 437 ScriptContext* context) const {
385 if (!context) { 438 if (!context) {
386 DLOG(ERROR) << "Not in a v8::Context"; 439 DLOG(ERROR) << "Not in a v8::Context";
387 return false; 440 return false;
388 } 441 }
389 442
390 // Theoretically we could end up with bindings being injected into sandboxed 443 // Theoretically we could end up with bindings being injected into sandboxed
391 // frames, for example content scripts. Don't let them execute API functions. 444 // frames, for example content scripts. Don't let them execute API functions.
392 blink::WebFrame* frame = context->web_frame(); 445 blink::WebFrame* frame = context->web_frame();
393 if (ScriptContext::IsSandboxedPage( 446 if (IsSandboxedPage(ScriptContext::GetDataSourceURLForFrame(frame))) {
394 extensions_, ScriptContext::GetDataSourceURLForFrame(frame))) {
395 static const char kMessage[] = 447 static const char kMessage[] =
396 "%s cannot be used within a sandboxed frame."; 448 "%s cannot be used within a sandboxed frame.";
397 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str()); 449 std::string error_msg = base::StringPrintf(kMessage, function_name.c_str());
398 context->isolate()->ThrowException(v8::Exception::Error( 450 context->isolate()->ThrowException(v8::Exception::Error(
399 v8::String::NewFromUtf8(context->isolate(), error_msg.c_str()))); 451 v8::String::NewFromUtf8(context->isolate(), error_msg.c_str())));
400 return false; 452 return false;
401 } 453 }
402 454
403 Feature::Availability availability = context->GetAvailability(function_name); 455 Feature::Availability availability = context->GetAvailability(function_name);
404 if (!availability.is_available()) { 456 if (!availability.is_available()) {
405 context->isolate()->ThrowException( 457 context->isolate()->ThrowException(
406 v8::Exception::Error(v8::String::NewFromUtf8( 458 v8::Exception::Error(v8::String::NewFromUtf8(
407 context->isolate(), availability.message().c_str()))); 459 context->isolate(), availability.message().c_str())));
408 } 460 }
409 461
410 return availability.is_available(); 462 return availability.is_available();
411 } 463 }
412 464
413 void Dispatcher::DispatchEvent(const std::string& extension_id, 465 void Dispatcher::DispatchEvent(const std::string& extension_id,
414 const std::string& event_name) const { 466 const std::string& event_name) const {
415 base::ListValue args; 467 base::ListValue args;
416 args.Set(0, new base::StringValue(event_name)); 468 args.Set(0, new base::StringValue(event_name));
417 args.Set(1, new base::ListValue()); 469 args.Set(1, new base::ListValue());
418 470
419 // Needed for Windows compilation, since kEventBindings is declared extern. 471 // Needed for Windows compilation, since kEventBindings is declared extern.
420 const char* local_event_bindings = kEventBindings; 472 const char* local_event_bindings = kEventBindings;
421 script_context_set_->ForEach( 473 script_context_set_.ForEach(extension_id,
422 extension_id, base::Bind(&CallModuleMethod, local_event_bindings, 474 base::Bind(&CallModuleMethod,
423 kEventDispatchFunction, &args)); 475 local_event_bindings,
476 kEventDispatchFunction,
477 &args));
424 } 478 }
425 479
426 void Dispatcher::InvokeModuleSystemMethod(content::RenderView* render_view, 480 void Dispatcher::InvokeModuleSystemMethod(content::RenderView* render_view,
427 const std::string& extension_id, 481 const std::string& extension_id,
428 const std::string& module_name, 482 const std::string& module_name,
429 const std::string& function_name, 483 const std::string& function_name,
430 const base::ListValue& args, 484 const base::ListValue& args,
431 bool user_gesture) { 485 bool user_gesture) {
432 scoped_ptr<WebScopedUserGesture> web_user_gesture; 486 scoped_ptr<WebScopedUserGesture> web_user_gesture;
433 if (user_gesture) 487 if (user_gesture)
434 web_user_gesture.reset(new WebScopedUserGesture); 488 web_user_gesture.reset(new WebScopedUserGesture);
435 489
436 script_context_set_->ForEach( 490 script_context_set_.ForEach(
437 extension_id, render_view, 491 extension_id,
492 render_view,
438 base::Bind(&CallModuleMethod, module_name, function_name, &args)); 493 base::Bind(&CallModuleMethod, module_name, function_name, &args));
439 494
440 // Reset the idle handler each time there's any activity like event or message 495 // Reset the idle handler each time there's any activity like event or message
441 // dispatch, for which Invoke is the chokepoint. 496 // dispatch, for which Invoke is the chokepoint.
442 if (set_idle_notifications_) { 497 if (set_idle_notifications_) {
443 RenderThread::Get()->ScheduleIdleHandler( 498 RenderThread::Get()->ScheduleIdleHandler(
444 kInitialExtensionIdleHandlerDelayMs); 499 kInitialExtensionIdleHandlerDelayMs);
445 } 500 }
446 501
447 // Tell the browser process when an event has been dispatched with a lazy 502 // Tell the browser process when an event has been dispatched with a lazy
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after
834 889
835 void Dispatcher::OnDeliverMessage(int target_port_id, const Message& message) { 890 void Dispatcher::OnDeliverMessage(int target_port_id, const Message& message) {
836 scoped_ptr<RequestSender::ScopedTabID> scoped_tab_id; 891 scoped_ptr<RequestSender::ScopedTabID> scoped_tab_id;
837 std::map<int, int>::const_iterator it = 892 std::map<int, int>::const_iterator it =
838 port_to_tab_id_map_.find(target_port_id); 893 port_to_tab_id_map_.find(target_port_id);
839 if (it != port_to_tab_id_map_.end()) { 894 if (it != port_to_tab_id_map_.end()) {
840 scoped_tab_id.reset( 895 scoped_tab_id.reset(
841 new RequestSender::ScopedTabID(request_sender(), it->second)); 896 new RequestSender::ScopedTabID(request_sender(), it->second));
842 } 897 }
843 898
844 MessagingBindings::DeliverMessage(*script_context_set_, target_port_id, 899 MessagingBindings::DeliverMessage(script_context_set_,
900 target_port_id,
845 message, 901 message,
846 NULL); // All render frames. 902 NULL); // All render frames.
847 } 903 }
848 904
849 void Dispatcher::OnDispatchOnConnect( 905 void Dispatcher::OnDispatchOnConnect(
850 int target_port_id, 906 int target_port_id,
851 const std::string& channel_name, 907 const std::string& channel_name,
852 const ExtensionMsg_TabConnectionInfo& source, 908 const ExtensionMsg_TabConnectionInfo& source,
853 const ExtensionMsg_ExternalConnectionInfo& info, 909 const ExtensionMsg_ExternalConnectionInfo& info,
854 const std::string& tls_channel_id) { 910 const std::string& tls_channel_id) {
855 DCHECK(!ContainsKey(port_to_tab_id_map_, target_port_id)); 911 DCHECK(!ContainsKey(port_to_tab_id_map_, target_port_id));
856 DCHECK_EQ(1, target_port_id % 2); // target renderer ports have odd IDs. 912 DCHECK_EQ(1, target_port_id % 2); // target renderer ports have odd IDs.
857 int sender_tab_id = -1; 913 int sender_tab_id = -1;
858 source.tab.GetInteger("id", &sender_tab_id); 914 source.tab.GetInteger("id", &sender_tab_id);
859 port_to_tab_id_map_[target_port_id] = sender_tab_id; 915 port_to_tab_id_map_[target_port_id] = sender_tab_id;
860 916
861 MessagingBindings::DispatchOnConnect(*script_context_set_, target_port_id, 917 MessagingBindings::DispatchOnConnect(script_context_set_,
862 channel_name, source, info, 918 target_port_id,
919 channel_name,
920 source,
921 info,
863 tls_channel_id, 922 tls_channel_id,
864 NULL); // All render frames. 923 NULL); // All render frames.
865 } 924 }
866 925
867 void Dispatcher::OnDispatchOnDisconnect(int port_id, 926 void Dispatcher::OnDispatchOnDisconnect(int port_id,
868 const std::string& error_message) { 927 const std::string& error_message) {
869 MessagingBindings::DispatchOnDisconnect(*script_context_set_, port_id, 928 MessagingBindings::DispatchOnDisconnect(script_context_set_,
929 port_id,
870 error_message, 930 error_message,
871 NULL); // All render frames. 931 NULL); // All render frames.
872 } 932 }
873 933
874 void Dispatcher::OnLoaded( 934 void Dispatcher::OnLoaded(
875 const std::vector<ExtensionMsg_Loaded_Params>& loaded_extensions) { 935 const std::vector<ExtensionMsg_Loaded_Params>& loaded_extensions) {
876 std::vector<ExtensionMsg_Loaded_Params>::const_iterator i; 936 std::vector<ExtensionMsg_Loaded_Params>::const_iterator i;
877 for (i = loaded_extensions.begin(); i != loaded_extensions.end(); ++i) { 937 for (i = loaded_extensions.begin(); i != loaded_extensions.end(); ++i) {
878 std::string error; 938 std::string error;
879 scoped_refptr<const Extension> extension = i->ConvertToExtension(&error); 939 scoped_refptr<const Extension> extension = i->ConvertToExtension(&error);
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
949 1009
950 script_injection_manager_->OnExtensionUnloaded(id); 1010 script_injection_manager_->OnExtensionUnloaded(id);
951 1011
952 // If the extension is later reloaded with a different set of permissions, 1012 // If the extension is later reloaded with a different set of permissions,
953 // we'd like it to get a new isolated world ID, so that it can pick up the 1013 // we'd like it to get a new isolated world ID, so that it can pick up the
954 // changed origin whitelist. 1014 // changed origin whitelist.
955 ScriptInjection::RemoveIsolatedWorld(id); 1015 ScriptInjection::RemoveIsolatedWorld(id);
956 1016
957 // Invalidate all of the contexts that were removed. 1017 // Invalidate all of the contexts that were removed.
958 // TODO(kalman): add an invalidation observer interface to ScriptContext. 1018 // TODO(kalman): add an invalidation observer interface to ScriptContext.
959 std::set<ScriptContext*> removed_contexts = 1019 ScriptContextSet::ContextSet removed_contexts =
960 script_context_set_->OnExtensionUnloaded(id); 1020 script_context_set_.OnExtensionUnloaded(id);
961 for (ScriptContext* context : removed_contexts) { 1021 for (ScriptContextSet::ContextSet::iterator it = removed_contexts.begin();
962 request_sender_->InvalidateSource(context); 1022 it != removed_contexts.end();
1023 ++it) {
1024 request_sender_->InvalidateSource(*it);
963 } 1025 }
964 1026
965 // Update the available bindings for the remaining contexts. These may have 1027 // Update the available bindings for the remaining contexts. These may have
966 // changed if an externally_connectable extension is unloaded and a webpage 1028 // changed if an externally_connectable extension is unloaded and a webpage
967 // is no longer accessible. 1029 // is no longer accessible.
968 UpdateBindings(""); 1030 UpdateBindings("");
969 1031
970 // Invalidates the messages map for the extension in case the extension is 1032 // Invalidates the messages map for the extension in case the extension is
971 // reloaded with a new messages map. 1033 // reloaded with a new messages map.
972 EraseL10nMessagesMap(id); 1034 EraseL10nMessagesMap(id);
(...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after
1317 for (std::set<std::string>::iterator iter = active_extension_ids_.begin(); 1379 for (std::set<std::string>::iterator iter = active_extension_ids_.begin();
1318 iter != active_extension_ids_.end(); 1380 iter != active_extension_ids_.end();
1319 ++iter) { 1381 ++iter) {
1320 const Extension* extension = extensions_.GetByID(*iter); 1382 const Extension* extension = extensions_.GetByID(*iter);
1321 if (extension && extension->is_platform_app()) 1383 if (extension && extension->is_platform_app())
1322 return true; 1384 return true;
1323 } 1385 }
1324 return false; 1386 return false;
1325 } 1387 }
1326 1388
1389 // TODO(kalman): This is checking for the wrong thing, it should be checking if
1390 // the frame's security origin is unique. The extension sandbox directive is
1391 // checked for in extensions/common/manifest_handlers/csp_info.cc.
1392 bool Dispatcher::IsSandboxedPage(const GURL& url) const {
1393 if (url.SchemeIs(kExtensionScheme)) {
1394 const Extension* extension = extensions_.GetByID(url.host());
1395 if (extension) {
1396 return SandboxedPageInfo::IsSandboxedPage(extension, url.path());
1397 }
1398 }
1399 return false;
1400 }
1401
1402 Feature::Context Dispatcher::ClassifyJavaScriptContext(
1403 const Extension* extension,
1404 int extension_group,
1405 const GURL& url,
1406 const blink::WebSecurityOrigin& origin) {
1407 // WARNING: This logic must match ProcessMap::GetContextType, as much as
1408 // possible.
1409
1410 DCHECK_GE(extension_group, 0);
1411 if (extension_group == EXTENSION_GROUP_CONTENT_SCRIPTS) {
1412 return extension ? // TODO(kalman): when does this happen?
1413 Feature::CONTENT_SCRIPT_CONTEXT
1414 : Feature::UNSPECIFIED_CONTEXT;
1415 }
1416
1417 // We have an explicit check for sandboxed pages before checking whether the
1418 // extension is active in this process because:
1419 // 1. Sandboxed pages run in the same process as regular extension pages, so
1420 // the extension is considered active.
1421 // 2. ScriptContext creation (which triggers bindings injection) happens
1422 // before the SecurityContext is updated with the sandbox flags (after
1423 // reading the CSP header), so the caller can't check if the context's
1424 // security origin is unique yet.
1425 if (IsSandboxedPage(url))
1426 return Feature::WEB_PAGE_CONTEXT;
1427
1428 if (extension && IsExtensionActive(extension->id())) {
1429 // |extension| is active in this process, but it could be either a true
1430 // extension process or within the extent of a hosted app. In the latter
1431 // case this would usually be considered a (blessed) web page context,
1432 // unless the extension in question is a component extension, in which case
1433 // we cheat and call it blessed.
1434 return (extension->is_hosted_app() &&
1435 extension->location() != Manifest::COMPONENT)
1436 ? Feature::BLESSED_WEB_PAGE_CONTEXT
1437 : Feature::BLESSED_EXTENSION_CONTEXT;
1438 }
1439
1440 // TODO(kalman): This isUnique() check is wrong, it should be performed as
1441 // part of IsSandboxedPage().
1442 if (!origin.isUnique() && extensions_.ExtensionBindingsAllowed(url)) {
1443 if (!extension) // TODO(kalman): when does this happen?
1444 return Feature::UNSPECIFIED_CONTEXT;
1445 return extension->is_hosted_app() ? Feature::BLESSED_WEB_PAGE_CONTEXT
1446 : Feature::UNBLESSED_EXTENSION_CONTEXT;
1447 }
1448
1449 if (!url.is_valid())
1450 return Feature::UNSPECIFIED_CONTEXT;
1451
1452 if (url.SchemeIs(content::kChromeUIScheme))
1453 return Feature::WEBUI_CONTEXT;
1454
1455 return Feature::WEB_PAGE_CONTEXT;
1456 }
1457
1327 v8::Handle<v8::Object> Dispatcher::GetOrCreateObject( 1458 v8::Handle<v8::Object> Dispatcher::GetOrCreateObject(
1328 const v8::Handle<v8::Object>& object, 1459 const v8::Handle<v8::Object>& object,
1329 const std::string& field, 1460 const std::string& field,
1330 v8::Isolate* isolate) { 1461 v8::Isolate* isolate) {
1331 v8::Handle<v8::String> key = v8::String::NewFromUtf8(isolate, field.c_str()); 1462 v8::Handle<v8::String> key = v8::String::NewFromUtf8(isolate, field.c_str());
1332 // If the object has a callback property, it is assumed it is an unavailable 1463 // If the object has a callback property, it is assumed it is an unavailable
1333 // API, so it is safe to delete. This is checked before GetOrCreateObject is 1464 // API, so it is safe to delete. This is checked before GetOrCreateObject is
1334 // called. 1465 // called.
1335 if (object->HasRealNamedCallbackProperty(key)) { 1466 if (object->HasRealNamedCallbackProperty(key)) {
1336 object->Delete(key); 1467 object->Delete(key);
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
1437 // The "guestViewDeny" module must always be loaded last. It registers 1568 // The "guestViewDeny" module must always be loaded last. It registers
1438 // error-providing custom elements for the GuestView types that are not 1569 // error-providing custom elements for the GuestView types that are not
1439 // available, and thus all of those types must have been checked and loaded 1570 // available, and thus all of those types must have been checked and loaded
1440 // (or not loaded) beforehand. 1571 // (or not loaded) beforehand.
1441 if (context_type == Feature::BLESSED_EXTENSION_CONTEXT) { 1572 if (context_type == Feature::BLESSED_EXTENSION_CONTEXT) {
1442 module_system->Require("guestViewDeny"); 1573 module_system->Require("guestViewDeny");
1443 } 1574 }
1444 } 1575 }
1445 1576
1446 } // namespace extensions 1577 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/dispatcher.h ('k') | extensions/renderer/document_custom_bindings.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698