diff options
author | mihaip@chromium.org <mihaip@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-02 16:53:18 +0000 |
---|---|---|
committer | mihaip@chromium.org <mihaip@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-06-02 16:53:18 +0000 |
commit | 4c14ce487f93032da0ccc0d93ef42dd31303e0e6 (patch) | |
tree | 5e1f35c0846ae5b417418f4d8e32b136978d9503 /chrome/browser/extensions/user_script_master.cc | |
parent | afb77faa1a2d3c98176c90e5ca76061b23cf8c13 (diff) | |
download | chromium_src-4c14ce487f93032da0ccc0d93ef42dd31303e0e6.zip chromium_src-4c14ce487f93032da0ccc0d93ef42dd31303e0e6.tar.gz chromium_src-4c14ce487f93032da0ccc0d93ef42dd31303e0e6.tar.bz2 |
Remove obsolete code from UserScriptMaster.
Remove user script directory support from UserScriptMaster and other classes
since it has been disabled since r32987.
Remove support for the EXTENSION_USER_SCRIPTS_UPDATED notification, since it's
no longer being sent (as of r80213).
BUG=none
TEST=no
R=mpcomplete@chromium.org
Review URL: http://codereview.chromium.org/7027019
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@87618 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/user_script_master.cc')
-rw-r--r-- | chrome/browser/extensions/user_script_master.cc | 142 |
1 files changed, 41 insertions, 101 deletions
diff --git a/chrome/browser/extensions/user_script_master.cc b/chrome/browser/extensions/user_script_master.cc index ba772b6..797fe59 100644 --- a/chrome/browser/extensions/user_script_master.cc +++ b/chrome/browser/extensions/user_script_master.cc @@ -9,7 +9,6 @@ #include "base/file_path.h" #include "base/file_util.h" -#include "base/path_service.h" #include "base/pickle.h" #include "base/stl_util-inl.h" #include "base/string_util.h" @@ -20,11 +19,8 @@ #include "chrome/common/extensions/extension.h" #include "chrome/common/extensions/extension_messages.h" #include "chrome/common/extensions/extension_resource.h" -#include "chrome/common/url_constants.h" #include "content/browser/renderer_host/render_process_host.h" #include "content/common/notification_service.h" -#include "net/base/net_util.h" - // Helper function to parse greasesmonkey headers static bool GetDeclarationValue(const base::StringPiece& line, @@ -139,16 +135,15 @@ bool UserScriptMaster::ScriptReloader::ParseMetadataHeader( return true; } -void UserScriptMaster::ScriptReloader::StartScan( - const FilePath& script_dir, const UserScriptList& lone_scripts) { +void UserScriptMaster::ScriptReloader::StartLoad( + const UserScriptList& user_scripts) { // Add a reference to ourselves to keep ourselves alive while we're running. // Balanced by NotifyMaster(). AddRef(); BrowserThread::PostTask( BrowserThread::FILE, FROM_HERE, NewRunnableMethod( - this, &UserScriptMaster::ScriptReloader::RunScan, script_dir, - lone_scripts)); + this, &UserScriptMaster::ScriptReloader::RunLoad, user_scripts)); } void UserScriptMaster::ScriptReloader::NotifyMaster( @@ -160,7 +155,7 @@ void UserScriptMaster::ScriptReloader::NotifyMaster( master_->NewScriptsAvailable(memory); // Drop our self-reference. - // Balances StartScan(). + // Balances StartLoad(). Release(); } @@ -185,46 +180,10 @@ static bool LoadScriptContent(UserScript::File* script_file) { } // static -void UserScriptMaster::ScriptReloader::LoadScriptsFromDirectory( - const FilePath& script_dir, UserScriptList* result) { - // Clear the list. We will populate it with the scripts found in script_dir. - result->clear(); - - // Find all the scripts in |script_dir|. - if (!script_dir.value().empty()) { - // Create the "<Profile>/User Scripts" directory if it doesn't exist - if (!file_util::DirectoryExists(script_dir)) - file_util::CreateDirectory(script_dir); - - file_util::FileEnumerator enumerator(script_dir, false, - file_util::FileEnumerator::FILES, - FILE_PATH_LITERAL("*.user.js")); - for (FilePath file = enumerator.Next(); !file.value().empty(); - file = enumerator.Next()) { - result->push_back(UserScript()); - UserScript& user_script = result->back(); - - // We default standalone user scripts to document-end for better - // Greasemonkey compatibility. - user_script.set_run_location(UserScript::DOCUMENT_END); - - // Push single js file in this UserScript. - GURL url(std::string(chrome::kUserScriptScheme) + ":/" + - net::FilePathToFileURL(file).ExtractFileName()); - user_script.js_scripts().push_back(UserScript::File( - script_dir, file.BaseName(), url)); - UserScript::File& script_file = user_script.js_scripts().back(); - if (!LoadScriptContent(&script_file)) - result->pop_back(); - else - ParseMetadataHeader(script_file.GetContent(), &user_script); - } - } -} - -static void LoadLoneScripts(UserScriptList* lone_scripts) { - for (size_t i = 0; i < lone_scripts->size(); ++i) { - UserScript& script = lone_scripts->at(i); +void UserScriptMaster::ScriptReloader::LoadUserScripts( + UserScriptList* user_scripts) { + for (size_t i = 0; i < user_scripts->size(); ++i) { + UserScript& script = user_scripts->at(i); for (size_t k = 0; k < script.js_scripts().size(); ++k) { UserScript::File& script_file = script.js_scripts()[k]; if (script_file.GetContent().empty()) @@ -273,19 +232,8 @@ static base::SharedMemory* Serialize(const UserScriptList& scripts) { } // This method will be called from the file thread -void UserScriptMaster::ScriptReloader::RunScan( - const FilePath script_dir, UserScriptList lone_script) { - UserScriptList scripts; - // Get list of user scripts. - if (!script_dir.empty()) - LoadScriptsFromDirectory(script_dir, &scripts); - - LoadLoneScripts(&lone_script); - - // Merge with the explicit scripts - scripts.reserve(scripts.size() + lone_script.size()); - scripts.insert(scripts.end(), - lone_script.begin(), lone_script.end()); +void UserScriptMaster::ScriptReloader::RunLoad(UserScriptList user_scripts) { + LoadUserScripts(&user_scripts); // Scripts now contains list of up-to-date scripts. Load the content in the // shared memory and let the master know it's ready. We need to post the task @@ -293,14 +241,13 @@ void UserScriptMaster::ScriptReloader::RunScan( BrowserThread::PostTask( master_thread_id_, FROM_HERE, NewRunnableMethod( - this, &ScriptReloader::NotifyMaster, Serialize(scripts))); + this, &ScriptReloader::NotifyMaster, Serialize(user_scripts))); } -UserScriptMaster::UserScriptMaster(const FilePath& script_dir, Profile* profile) - : user_script_dir_(script_dir), - extensions_service_ready_(false), - pending_scan_(false), +UserScriptMaster::UserScriptMaster(Profile* profile) + : extensions_service_ready_(false), + pending_load_(false), profile_(profile) { registrar_.Add(this, NotificationType::EXTENSIONS_READY, Source<Profile>(profile_)); @@ -308,8 +255,6 @@ UserScriptMaster::UserScriptMaster(const FilePath& script_dir, Profile* profile) Source<Profile>(profile_)); registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, Source<Profile>(profile_)); - registrar_.Add(this, NotificationType::EXTENSION_USER_SCRIPTS_UPDATED, - Source<Profile>(profile_)); registrar_.Add(this, NotificationType::RENDERER_PROCESS_CREATED, NotificationService::AllSources()); } @@ -323,13 +268,13 @@ void UserScriptMaster::NewScriptsAvailable(base::SharedMemory* handle) { // Ensure handle is deleted or released. scoped_ptr<base::SharedMemory> handle_deleter(handle); - if (pending_scan_) { - // While we were scanning, there were further changes. Don't bother - // notifying about these scripts and instead just immediately rescan. - pending_scan_ = false; - StartScan(); + if (pending_load_) { + // While we were loading, there were further changes. Don't bother + // notifying about these scripts and instead just immediately reload. + pending_load_ = false; + StartLoad(); } else { - // We're no longer scanning. + // We're no longer loading. script_reloader_ = NULL; // We've got scripts ready to go. shared_memory_.swap(handle_deleter); @@ -349,10 +294,11 @@ void UserScriptMaster::NewScriptsAvailable(base::SharedMemory* handle) { void UserScriptMaster::Observe(NotificationType type, const NotificationSource& source, const NotificationDetails& details) { + bool should_start_load = false; switch (type.value) { case NotificationType::EXTENSIONS_READY: extensions_service_ready_ = true; - StartScan(); + should_start_load = true; break; case NotificationType::EXTENSION_LOADED: { // Add any content scripts inside the extension. @@ -362,45 +308,31 @@ void UserScriptMaster::Observe(NotificationType type, const UserScriptList& scripts = extension->content_scripts(); for (UserScriptList::const_iterator iter = scripts.begin(); iter != scripts.end(); ++iter) { - lone_scripts_.push_back(*iter); - lone_scripts_.back().set_incognito_enabled(incognito_enabled); + user_scripts_.push_back(*iter); + user_scripts_.back().set_incognito_enabled(incognito_enabled); } if (extensions_service_ready_) - StartScan(); + should_start_load = true; break; } case NotificationType::EXTENSION_UNLOADED: { // Remove any content scripts. const Extension* extension = Details<UnloadedExtensionInfo>(details)->extension; - UserScriptList new_lone_scripts; - for (UserScriptList::iterator iter = lone_scripts_.begin(); - iter != lone_scripts_.end(); ++iter) { + UserScriptList new_user_scripts; + for (UserScriptList::iterator iter = user_scripts_.begin(); + iter != user_scripts_.end(); ++iter) { if (iter->extension_id() != extension->id()) - new_lone_scripts.push_back(*iter); + new_user_scripts.push_back(*iter); } - lone_scripts_ = new_lone_scripts; - StartScan(); + user_scripts_ = new_user_scripts; + should_start_load = true; // TODO(aa): Do we want to do something smarter for the scripts that have // already been injected? break; } - case NotificationType::EXTENSION_USER_SCRIPTS_UPDATED: { - const Extension* extension = Details<const Extension>(details).ptr(); - UserScriptList new_lone_scripts; - bool incognito_enabled = profile_->GetExtensionService()-> - IsIncognitoEnabled(extension->id()); - for (UserScriptList::iterator iter = lone_scripts_.begin(); - iter != lone_scripts_.end(); ++iter) { - if (iter->extension_id() == extension->id()) { - iter->set_incognito_enabled(incognito_enabled); - } - } - StartScan(); - break; - } case NotificationType::RENDERER_PROCESS_CREATED: { RenderProcessHost* process = Source<RenderProcessHost>(source).ptr(); if (ScriptsReady()) @@ -410,13 +342,21 @@ void UserScriptMaster::Observe(NotificationType type, default: DCHECK(false); } + + if (should_start_load) { + if (script_reloader_) { + pending_load_ = true; + } else { + StartLoad(); + } + } } -void UserScriptMaster::StartScan() { +void UserScriptMaster::StartLoad() { if (!script_reloader_) script_reloader_ = new ScriptReloader(this); - script_reloader_->StartScan(user_script_dir_, lone_scripts_); + script_reloader_->StartLoad(user_scripts_); } void UserScriptMaster::SendUpdate(RenderProcessHost* process, |