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..a9602f61d62782c0fb6723345fe7c90908a0f3d8 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/declarative_user_script_master.cc |
| @@ -0,0 +1,128 @@ |
| +// Copyright (c) 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_system.h" |
| + |
| +namespace extensions { |
| + |
| +DeclarativeUserScriptMaster::DeclarativeUserScriptMaster( |
| + Profile* profile, |
| + const ExtensionId& extension_id) |
| + : UserScriptMaster(profile) { |
| + owner_extension_id_ = extension_id; |
|
Devlin
2014/07/30 21:20:38
init this in the initialization list.
Mark Dittmer
2014/07/31 18:37:33
Done.
|
| + registrar_.Add(this, |
| + content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
| + content::NotificationService::AllBrowserContextsAndSources()); |
| +} |
| + |
| +DeclarativeUserScriptMaster::~DeclarativeUserScriptMaster() { |
| +} |
| + |
| +void DeclarativeUserScriptMaster::Observe( |
| + int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + switch (type) { |
|
Devlin
2014/07/30 21:20:38
If you only expect one notification, it's cleaner
Mark Dittmer
2014/07/31 18:37:33
Done.
|
| + case content::NOTIFICATION_RENDERER_PROCESS_CREATED: { |
| + content::RenderProcessHost* process = |
| + content::Source<content::RenderProcessHost>(source).ptr(); |
| + Profile* profile = |
| + Profile::FromBrowserContext(process->GetBrowserContext()); |
| + if (!profile_->IsSameProfile(profile)) |
| + return; |
| + if (ScriptsReady()) { |
| + std::set<ExtensionId> changed_extensions; |
| + changed_extensions.insert(owner_extension_id_); |
| + SendUpdate(process, GetSharedMemory(), changed_extensions); |
| + } |
| + break; |
| + } |
| + default: |
| + DCHECK(false); |
|
Devlin
2014/07/30 21:20:38
We usually spell this NOTREACHED(). (though moot w
Mark Dittmer
2014/07/31 18:37:33
Acknowledged.
|
| + } |
| +} |
| + |
| +void DeclarativeUserScriptMaster::AddScript(const UserScript& script) { |
| + scripts_to_add_.insert(script); |
| + if (is_loading()) { |
|
Devlin
2014/07/30 21:20:38
if (IsSingleLineIfStatement())
RemoveBrackets();
|
| + ResetPendingLoad(); |
| + } else { |
| + StartLoad(); |
| + } |
| +} |
| + |
| +void DeclarativeUserScriptMaster::RemoveScript(const UserScript& script) { |
| + if (scripts_to_add_.find(script) != scripts_to_add_.end()) { |
|
Devlin
2014/07/30 21:20:38
brackets
Devlin
2014/07/30 21:20:38
For sets, prefer:
if (scripts_to_add_.count(script
Mark Dittmer
2014/07/31 18:37:33
Done.
|
| + scripts_to_add_.erase(script); |
| + } else { |
| + scripts_to_remove_.insert(script); |
| + } |
| + if (is_loading()) { |
| + ResetPendingLoad(); |
| + } else { |
| + StartLoad(); |
| + } |
| +} |
| + |
| +void DeclarativeUserScriptMaster::ClearScripts() { |
| + clear_scripts_ = true; |
| + scripts_to_add_.clear(); |
| + scripts_to_remove_.clear(); |
| + if (is_loading()) { |
| + ResetPendingLoad(); |
| + } else { |
| + StartLoad(); |
| + } |
| +} |
| + |
| +void DeclarativeUserScriptMaster::StartLoad() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + DCHECK(!is_loading()); |
| + |
| + 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()); |
| + new_user_scripts.erase(scripts_to_remove_.begin(), scripts_to_remove_.end()); |
|
Mark Dittmer
2014/07/31 18:37:32
This is wrong because std::set::erase cannot take
|
| + 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()); |
| + |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::FILE, |
| + FROM_HERE, |
| + base::Bind(&UserScriptMaster::LoadScriptsOnFileThread, |
| + base::Passed(&user_scripts_), |
| + extensions_info_, |
| + added_extensions_, |
| + make_scoped_refptr( |
| + ExtensionSystem::Get(profile_)->content_verifier()), |
| + base::Bind(&UserScriptMaster::OnScriptsLoaded, |
| + weak_factory_.GetWeakPtr()))); |
| + |
| + // File thread handler has ownership of user_scripts_ until it is given back |
| + // in UserScriptMaster::OnScriptsLoaded(). |
| + user_scripts_.reset(NULL); |
| + |
| + ResetPendingLoadData(); |
| +} |
| + |
| +} // namespace extensions |