Chromium Code Reviews| Index: chrome/browser/extensions/declarative_user_script_master.cc |
| diff --git a/chrome/browser/extensions/declarative_user_script_master.cc b/chrome/browser/extensions/declarative_user_script_master.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6f8a6f25a21f6f51c96979f56b560b0e765a4eb5 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/declarative_user_script_master.cc |
| @@ -0,0 +1,129 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/extensions/declarative_user_script_master.h" |
| + |
| +#include "chrome/browser/chrome_notification_types.h" |
| +#include "chrome/browser/extensions/extension_util.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/common/extensions/api/i18n/default_locale_handler.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "content/public/browser/render_process_host.h" |
| +#include "extensions/browser/content_verifier.h" |
| +#include "extensions/browser/extension_registry.h" |
| +#include "extensions/browser/extension_system.h" |
| + |
| +namespace extensions { |
| + |
| +DeclarativeUserScriptMaster::DeclarativeUserScriptMaster( |
| + Profile* profile, |
| + const ExtensionId& extension_id) |
| + : UserScriptMaster(profile, extension_id), |
| + user_scripts_(new UserScriptList()) { |
| + owner_extension_as_set_.insert(extension_id); |
| + const Extension* extension = |
| + ExtensionRegistry::Get(this->profile()) |
| + ->GetExtensionById(owner_extension_id(), ExtensionRegistry::ENABLED); |
| + DCHECK(extension); |
| + extensions_info_[owner_extension_id()] = |
| + ExtensionSet::ExtensionPathAndDefaultLocale( |
| + extension->path(), LocaleInfo::GetDefaultLocale(extension)); |
| +} |
| + |
| +DeclarativeUserScriptMaster::~DeclarativeUserScriptMaster() { |
| +} |
| + |
| +void DeclarativeUserScriptMaster::AddScript(const UserScript& script) { |
| + scripts_to_add_.insert(script); |
| + AttemptLoad(); |
| +} |
| + |
| +void DeclarativeUserScriptMaster::RemoveScript(const UserScript& script) { |
| + if (scripts_to_add_.count(script) != 0) |
| + scripts_to_add_.erase(script); |
| + else |
| + scripts_to_remove_.insert(script); |
| + AttemptLoad(); |
|
Devlin
2014/07/31 20:32:23
Seems to me there's no reason to attempt the load
|
| +} |
| + |
| +void DeclarativeUserScriptMaster::ClearScripts() { |
| + clear_scripts_ = true; |
| + scripts_to_add_.clear(); |
| + scripts_to_remove_.clear(); |
| + AttemptLoad(); |
| +} |
| + |
| +void DeclarativeUserScriptMaster::StartLoad() { |
| + if (clear_scripts_) { |
| + user_scripts_->clear(); |
| + } |
| + bool incognito_enabled = |
| + util::IsIncognitoEnabled(owner_extension_id(), profile()); |
| + std::set<UserScript> new_user_scripts; |
| + new_user_scripts.insert(user_scripts_->begin(), user_scripts_->end()); |
|
Devlin
2014/07/31 20:32:23
If we wanted to be cool, we would do:
user_scripts
|
| + for (std::set<UserScript>::iterator it = scripts_to_remove_.begin(); |
| + it != scripts_to_remove_.end(); |
| + ++it) { |
| + new_user_scripts.erase(*it); |
| + } |
| + for (std::set<UserScript>::const_iterator it = scripts_to_add_.begin(); |
| + it != scripts_to_add_.end(); |
| + ++it) { |
| + UserScript script = *it; |
| + script.set_incognito_enabled(incognito_enabled); |
| + new_user_scripts.insert(script); |
| + } |
| + user_scripts_->assign(new_user_scripts.begin(), new_user_scripts.end()); |
| + |
| + LoadScripts(user_scripts_.Pass(), extensions_info_); |
| + |
| + user_scripts_.reset(NULL); |
| + scripts_to_add_.clear(); |
| + scripts_to_remove_.clear(); |
| + clear_scripts_ = false; |
| +} |
| + |
| +void DeclarativeUserScriptMaster::AcquireUserScripts( |
| + scoped_ptr<UserScriptList> scripts) { |
| + user_scripts_.reset(scripts.release()); |
| +} |
| + |
| +ExtensionsInfo DeclarativeUserScriptMaster::GetExtensionsInfo() { |
| + return extensions_info_; |
| +} |
| + |
| +std::set<ExtensionId> DeclarativeUserScriptMaster::GetAddedExtensions() { |
| + // If loading from the file thread has been triggered, then reload scripts |
| + // for the one extension that we manage. |
| + return owner_extension_as_set_; |
| +} |
| + |
| +std::set<ExtensionId> DeclarativeUserScriptMaster::GetAllManagedExtensions() { |
| + return owner_extension_as_set_; |
| +} |
| + |
| +std::set<ExtensionId> DeclarativeUserScriptMaster::GetChangedExtensions() { |
| + // In any context where we need to know which extensions have changed, assume |
| + // that the extension we manage may have changed. |
| + return owner_extension_as_set_; |
| +} |
| + |
| +void DeclarativeUserScriptMaster::ResetChangedExtensions() { |
| + // No extra bookkeeping for tracking changed extensions. |
| +} |
| + |
| +void DeclarativeUserScriptMaster::AttemptLoad() { |
| + if (is_loading()) |
| + MaybeSignalPendingLoad(); |
| + else |
| + StartLoad(); |
| +} |
| + |
| +void DeclarativeUserScriptMaster::MaybeSignalPendingLoad() { |
| + if (clear_scripts_ || scripts_to_add_.size() > 0 || |
| + scripts_to_remove_.size() > 0) |
| + SignalPendingLoad(); |
| +} |
| + |
| +} // namespace extensions |