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

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

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