OLD | NEW |
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/browser/extensions/user_script_master.h" | 5 #include "chrome/browser/extensions/user_script_master.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/memory/shared_memory.h" | 13 #include "base/memory/shared_memory.h" |
14 #include "base/version.h" | 14 #include "base/version.h" |
15 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
16 #include "chrome/browser/extensions/extension_util.h" | 16 #include "chrome/browser/extensions/extension_util.h" |
17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/common/extensions/api/i18n/default_locale_handler.h" | 18 #include "chrome/common/extensions/api/i18n/default_locale_handler.h" |
19 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h" | 19 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h" |
20 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
21 #include "content/public/browser/render_process_host.h" | 21 #include "content/public/browser/render_process_host.h" |
22 #include "extensions/browser/component_extension_resource_manager.h" | 22 #include "extensions/browser/component_extension_resource_manager.h" |
23 #include "extensions/browser/content_verifier.h" | 23 #include "extensions/browser/content_verifier.h" |
24 #include "extensions/browser/extension_registry.h" | 24 #include "extensions/browser/extension_registry.h" |
25 #include "extensions/browser/extension_system.h" | 25 #include "extensions/browser/extension_system.h" |
26 #include "extensions/browser/extensions_browser_client.h" | 26 #include "extensions/browser/extensions_browser_client.h" |
27 #include "extensions/common/file_util.h" | 27 #include "extensions/common/file_util.h" |
| 28 #include "extensions/common/one_shot_event.h" |
28 #include "extensions/common/message_bundle.h" | 29 #include "extensions/common/message_bundle.h" |
29 #include "ui/base/resource/resource_bundle.h" | 30 #include "ui/base/resource/resource_bundle.h" |
30 | 31 |
31 using content::BrowserThread; | 32 using content::BrowserThread; |
32 using extensions::ExtensionsBrowserClient; | 33 using extensions::ExtensionsBrowserClient; |
33 | 34 |
34 namespace extensions { | 35 namespace extensions { |
35 | 36 |
36 namespace { | 37 namespace { |
37 | 38 |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 } | 344 } |
344 | 345 |
345 UserScriptMaster::UserScriptMaster(Profile* profile) | 346 UserScriptMaster::UserScriptMaster(Profile* profile) |
346 : user_scripts_(new UserScriptList()), | 347 : user_scripts_(new UserScriptList()), |
347 extensions_service_ready_(false), | 348 extensions_service_ready_(false), |
348 pending_load_(false), | 349 pending_load_(false), |
349 profile_(profile), | 350 profile_(profile), |
350 extension_registry_observer_(this), | 351 extension_registry_observer_(this), |
351 weak_factory_(this) { | 352 weak_factory_(this) { |
352 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); | 353 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); |
353 registrar_.Add(this, | |
354 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED, | |
355 content::Source<Profile>(profile_)); | |
356 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 354 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
357 content::NotificationService::AllBrowserContextsAndSources()); | 355 content::NotificationService::AllBrowserContextsAndSources()); |
| 356 ExtensionSystem::Get(profile)->ready().Post( |
| 357 FROM_HERE, |
| 358 base::Bind(&UserScriptMaster::OnExtensionsReady, |
| 359 weak_factory_.GetWeakPtr())); |
358 } | 360 } |
359 | 361 |
360 UserScriptMaster::~UserScriptMaster() { | 362 UserScriptMaster::~UserScriptMaster() { |
361 } | 363 } |
362 | 364 |
363 void UserScriptMaster::OnScriptsLoaded( | 365 void UserScriptMaster::OnScriptsLoaded( |
364 scoped_ptr<UserScriptList> user_scripts, | 366 scoped_ptr<UserScriptList> user_scripts, |
365 scoped_ptr<base::SharedMemory> shared_memory) { | 367 scoped_ptr<base::SharedMemory> shared_memory) { |
366 user_scripts_.reset(user_scripts.release()); | 368 user_scripts_.reset(user_scripts.release()); |
367 if (pending_load_) { | 369 if (pending_load_) { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
430 changed_extensions_.insert(extension->id()); | 432 changed_extensions_.insert(extension->id()); |
431 if (is_loading()) | 433 if (is_loading()) |
432 pending_load_ = true; | 434 pending_load_ = true; |
433 else | 435 else |
434 StartLoad(); | 436 StartLoad(); |
435 } | 437 } |
436 | 438 |
437 void UserScriptMaster::Observe(int type, | 439 void UserScriptMaster::Observe(int type, |
438 const content::NotificationSource& source, | 440 const content::NotificationSource& source, |
439 const content::NotificationDetails& details) { | 441 const content::NotificationDetails& details) { |
440 bool should_start_load = false; | 442 DCHECK_EQ(type, content::NOTIFICATION_RENDERER_PROCESS_CREATED); |
441 switch (type) { | 443 content::RenderProcessHost* process = |
442 case extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED: | 444 content::Source<content::RenderProcessHost>(source).ptr(); |
443 extensions_service_ready_ = true; | 445 Profile* profile = Profile::FromBrowserContext( |
444 should_start_load = true; | 446 process->GetBrowserContext()); |
445 break; | 447 if (!profile_->IsSameProfile(profile)) |
446 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: { | 448 return; |
447 content::RenderProcessHost* process = | 449 if (ScriptsReady()) { |
448 content::Source<content::RenderProcessHost>(source).ptr(); | 450 SendUpdate(process, |
449 Profile* profile = Profile::FromBrowserContext( | 451 GetSharedMemory(), |
450 process->GetBrowserContext()); | 452 std::set<std::string>()); // Include all extensions. |
451 if (!profile_->IsSameProfile(profile)) | |
452 return; | |
453 if (ScriptsReady()) { | |
454 SendUpdate(process, | |
455 GetSharedMemory(), | |
456 std::set<std::string>()); // Include all extensions. | |
457 } | |
458 break; | |
459 } | |
460 default: | |
461 DCHECK(false); | |
462 } | |
463 | |
464 if (should_start_load) { | |
465 if (is_loading()) | |
466 pending_load_ = true; | |
467 else | |
468 StartLoad(); | |
469 } | 453 } |
470 } | 454 } |
471 | 455 |
| 456 void UserScriptMaster::OnExtensionsReady() { |
| 457 extensions_service_ready_ = true; |
| 458 if (is_loading()) |
| 459 pending_load_ = true; |
| 460 else |
| 461 StartLoad(); |
| 462 } |
| 463 |
472 void UserScriptMaster::StartLoad() { | 464 void UserScriptMaster::StartLoad() { |
473 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 465 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
474 DCHECK(!is_loading()); | 466 DCHECK(!is_loading()); |
475 | 467 |
476 // Remove any user scripts belonging to any extension that was updated or | 468 // Remove any user scripts belonging to any extension that was updated or |
477 // removed. | 469 // removed. |
478 for (UserScriptList::iterator iter = user_scripts_->begin(); | 470 for (UserScriptList::iterator iter = user_scripts_->begin(); |
479 iter != user_scripts_->end();) { | 471 iter != user_scripts_->end();) { |
480 if (removed_extensions_.count(iter->extension_id()) > 0 || | 472 if (removed_extensions_.count(iter->extension_id()) > 0 || |
481 added_extensions_.count(iter->extension_id()) > 0) { | 473 added_extensions_.count(iter->extension_id()) > 0) { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
544 if (!shared_memory->ShareToProcess(handle, &handle_for_process)) | 536 if (!shared_memory->ShareToProcess(handle, &handle_for_process)) |
545 return; // This can legitimately fail if the renderer asserts at startup. | 537 return; // This can legitimately fail if the renderer asserts at startup. |
546 | 538 |
547 if (base::SharedMemory::IsHandleValid(handle_for_process)) { | 539 if (base::SharedMemory::IsHandleValid(handle_for_process)) { |
548 process->Send(new ExtensionMsg_UpdateUserScripts( | 540 process->Send(new ExtensionMsg_UpdateUserScripts( |
549 handle_for_process, "" /* owner */, changed_extensions)); | 541 handle_for_process, "" /* owner */, changed_extensions)); |
550 } | 542 } |
551 } | 543 } |
552 | 544 |
553 } // namespace extensions | 545 } // namespace extensions |
OLD | NEW |