Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5426)

Unified Diff: chrome/browser/extensions/shared_user_script_master.cc

Issue 420543002: Declarative content scripts: Browser-side: per-extension shared memory regions (lazily loaded) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactor relationship between UserScriptMaster and its subclasses Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/shared_user_script_master.cc
diff --git a/chrome/browser/extensions/shared_user_script_master.cc b/chrome/browser/extensions/shared_user_script_master.cc
new file mode 100644
index 0000000000000000000000000000000000000000..7560ff3910b75065a50ef570359a5bbbbc8f409e
--- /dev/null
+++ b/chrome/browser/extensions/shared_user_script_master.cc
@@ -0,0 +1,140 @@
+// 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/shared_user_script_master.h"
+
+#include "base/bind.h"
+#include "base/memory/shared_memory.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 "chrome/common/extensions/manifest_handlers/content_scripts_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"
+#include "extensions/common/one_shot_event.h"
+
+namespace extensions {
+
+SharedUserScriptMaster::SharedUserScriptMaster(Profile* profile)
+ : UserScriptMaster(profile, "" /* owner_extension_id */),
+ user_scripts_(new UserScriptList()),
+ weak_factory_(this),
+ extension_registry_observer_(this) {
+ extension_registry_observer_.Add(ExtensionRegistry::Get(this->profile()));
+ ExtensionSystem::Get(this->profile())->ready().Post(
+ FROM_HERE,
+ base::Bind(&SharedUserScriptMaster::OnExtensionSystemReady,
+ weak_factory_.GetWeakPtr()));
+}
+
+SharedUserScriptMaster::~SharedUserScriptMaster() {
+}
+
+void SharedUserScriptMaster::OnExtensionLoaded(
+ content::BrowserContext* browser_context,
+ const Extension* extension) {
+ added_extensions_.insert(extension->id());
+ removed_extensions_.erase(extension->id());
+ extensions_info_[extension->id()] =
+ ExtensionSet::ExtensionPathAndDefaultLocale(
+ extension->path(), LocaleInfo::GetDefaultLocale(extension));
+ if (extensions_service_ready_)
+ changed_extensions_.insert(extension->id());
+}
+
+void SharedUserScriptMaster::OnExtensionUnloaded(
+ content::BrowserContext* browser_context,
+ const Extension* extension,
+ UnloadedExtensionInfo::Reason reason) {
+ removed_extensions_.insert(extension->id());
+ added_extensions_.erase(extension->id());
+ // Remove any content scripts.
+ extensions_info_.erase(extension->id());
+ changed_extensions_.insert(extension->id());
+ AttemptLoad();
+}
+
+void SharedUserScriptMaster::StartLoad() {
+ // Remove any user scripts belonging to any extension that was updated or
+ // removed.
+ for (UserScriptList::iterator iter = user_scripts_->begin();
+ iter != user_scripts_->end();) {
+ if (removed_extensions_.count(iter->extension_id()) > 0 ||
+ added_extensions_.count(iter->extension_id()) > 0) {
+ iter = user_scripts_->erase(iter);
+ } else {
+ ++iter;
+ }
+ }
+
+ // Add any content scripts for extensions that were recently loaded.
+ const ExtensionSet& enabled_extensions =
+ ExtensionRegistry::Get(profile())->enabled_extensions();
+ for (std::set<ExtensionId>::const_iterator iter = added_extensions_.begin();
+ iter != added_extensions_.end();
+ ++iter) {
+ const Extension* extension = enabled_extensions.GetByID(*iter);
+ if (!extension)
+ continue;
+ bool incognito_enabled =
+ util::IsIncognitoEnabled(extension->id(), profile());
+ const UserScriptList& scripts =
+ ContentScriptsInfo::GetContentScripts(extension);
+ for (UserScriptList::const_iterator script = scripts.begin();
+ script != scripts.end();
+ ++script) {
+ user_scripts_->push_back(*script);
+ user_scripts_->back().set_incognito_enabled(incognito_enabled);
+ }
+ }
+
+ LoadScripts(user_scripts_.Pass(), extensions_info_);
+
+ added_extensions_.clear();
+ removed_extensions_.clear();
+ user_scripts_.reset(NULL);
+}
+
+void SharedUserScriptMaster::AcquireUserScripts(
+ scoped_ptr<UserScriptList> scripts) {
+ user_scripts_.reset(scripts.release());
+}
+
+ExtensionsInfo SharedUserScriptMaster::GetExtensionsInfo() {
+ return extensions_info_;
+}
+
+std::set<ExtensionId> SharedUserScriptMaster::GetAddedExtensions() {
+ return added_extensions_;
+}
+
+std::set<ExtensionId> SharedUserScriptMaster::GetAllManagedExtensions() {
+ // Empty set = all extensions.
+ return std::set<ExtensionId>();
+}
+
+std::set<ExtensionId> SharedUserScriptMaster::GetChangedExtensions() {
+ return changed_extensions_;
+}
+
+void SharedUserScriptMaster::ResetChangedExtensions() {
+ changed_extensions_.clear();
+}
+
+void SharedUserScriptMaster::OnExtensionSystemReady() {
+ extensions_service_ready_ = true;
+ AttemptLoad();
+}
+
+void SharedUserScriptMaster::AttemptLoad() {
+ if (is_loading())
+ SignalPendingLoad();
+ else
+ StartLoad();
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698