diff options
Diffstat (limited to 'chrome/browser/extensions/user_script_master.cc')
-rw-r--r-- | chrome/browser/extensions/user_script_master.cc | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/chrome/browser/extensions/user_script_master.cc b/chrome/browser/extensions/user_script_master.cc index 8b5e4d9..e1ed5db 100644 --- a/chrome/browser/extensions/user_script_master.cc +++ b/chrome/browser/extensions/user_script_master.cc @@ -14,6 +14,8 @@ #include "base/string_util.h" #include "base/thread.h" #include "chrome/browser/browser_process.h" +#include "chrome/browser/extensions/extension.h" +#include "chrome/browser/extensions/extensions_service.h" #include "chrome/common/notification_service.h" #include "chrome/common/url_constants.h" #include "net/base/net_util.h" @@ -257,6 +259,11 @@ UserScriptMaster::UserScriptMaster(MessageLoop* worker_loop, pending_scan_(false) { if (!user_script_dir_.value().empty()) AddWatchedPath(script_dir); + + registrar_.Add(this, NotificationType::EXTENSIONS_LOADED, + NotificationService::AllSources()); + registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, + NotificationService::AllSources()); } UserScriptMaster::~UserScriptMaster() { @@ -313,6 +320,55 @@ void UserScriptMaster::OnDirectoryChanged(const FilePath& path) { StartScan(); } +void UserScriptMaster::Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + switch (type.value) { + case NotificationType::EXTENSIONS_LOADED: { + // TODO(aa): Fix race here. A page could need a content script on startup, + // before the extension has loaded. We need to freeze the renderer in + // that case. + // See: http://code.google.com/p/chromium/issues/detail?id=11547. + + // Add any content scripts inside the extension. + ExtensionList* extensions = Details<ExtensionList>(details).ptr(); + for (ExtensionList::iterator extension_iterator = extensions->begin(); + extension_iterator != extensions->end(); ++extension_iterator) { + Extension* extension = *extension_iterator; + const UserScriptList& scripts = extension->content_scripts(); + for (UserScriptList::const_iterator iter = scripts.begin(); + iter != scripts.end(); ++iter) { + lone_scripts_.push_back(*iter); + } + } + StartScan(); + break; + } + + case NotificationType::EXTENSION_UNLOADED: { + // Remove any content scripts. + Extension* extension = Details<Extension>(details).ptr(); + UserScriptList new_lone_scripts; + for (UserScriptList::iterator iter = lone_scripts_.begin(); + iter != lone_scripts_.end(); ++iter) { + if (iter->extension_id() != extension->id()) { + new_lone_scripts.push_back(*iter); + } + } + lone_scripts_ = new_lone_scripts; + StartScan(); + + // TODO(aa): Do we want to do something smarter for the scripts that have + // already been injected? + + break; + } + + default: + DCHECK(false); + } +} + void UserScriptMaster::StartScan() { if (!script_reloader_) script_reloader_ = new ScriptReloader(this); |