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

Unified Diff: chrome/browser/extensions/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/user_script_master.cc
diff --git a/chrome/browser/extensions/user_script_master.cc b/chrome/browser/extensions/user_script_master.cc
index a3e58465001a36a1748ba245f3d8adb4c3e725f5..340c7305a028d943f3ea1d98d17dd91c55087915 100644
--- a/chrome/browser/extensions/user_script_master.cc
+++ b/chrome/browser/extensions/user_script_master.cc
@@ -15,7 +15,6 @@
#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"
@@ -40,7 +39,7 @@ typedef base::Callback<void(scoped_ptr<UserScriptList>,
LoadScriptsCallback;
void VerifyContent(scoped_refptr<ContentVerifier> verifier,
- const std::string& extension_id,
+ const ExtensionId& extension_id,
const base::FilePath& extension_root,
const base::FilePath& relative_path,
const std::string& content) {
@@ -54,7 +53,7 @@ void VerifyContent(scoped_refptr<ContentVerifier> verifier,
}
}
-bool LoadScriptContent(const std::string& extension_id,
+bool LoadScriptContent(const ExtensionId& extension_id,
UserScript::File* script_file,
const SubstitutionMap* localization_messages,
scoped_refptr<ContentVerifier> verifier) {
@@ -115,7 +114,7 @@ bool LoadScriptContent(const std::string& extension_id,
}
SubstitutionMap* GetLocalizationMessages(const ExtensionsInfo& extensions_info,
- const std::string& extension_id) {
+ const ExtensionId& extension_id) {
ExtensionsInfo::const_iterator iter = extensions_info.find(extension_id);
if (iter == extensions_info.end())
return NULL;
@@ -126,7 +125,7 @@ SubstitutionMap* GetLocalizationMessages(const ExtensionsInfo& extensions_info,
void LoadUserScripts(UserScriptList* user_scripts,
const ExtensionsInfo& extensions_info,
- const std::set<std::string>& new_extensions,
+ const std::set<ExtensionId>& new_extensions,
ContentVerifier* verifier) {
for (size_t i = 0; i < user_scripts->size(); ++i) {
UserScript& script = user_scripts->at(i);
@@ -199,7 +198,7 @@ scoped_ptr<base::SharedMemory> Serialize(const UserScriptList& scripts) {
void LoadScriptsOnFileThread(scoped_ptr<UserScriptList> user_scripts,
const ExtensionsInfo& extensions_info,
- const std::set<std::string>& new_extensions,
+ const std::set<ExtensionId>& new_extensions,
scoped_refptr<ContentVerifier> verifier,
LoadScriptsCallback callback) {
DCHECK(user_scripts.get());
@@ -332,7 +331,7 @@ bool UserScriptMaster::ParseMetadataHeader(
// static
void UserScriptMaster::LoadScriptsForTest(UserScriptList* user_scripts) {
ExtensionsInfo info;
- std::set<std::string> new_extensions;
+ std::set<ExtensionId> new_extensions;
for (UserScriptList::const_iterator iter = user_scripts->begin();
iter != user_scripts->end();
++iter) {
@@ -342,27 +341,37 @@ void UserScriptMaster::LoadScriptsForTest(UserScriptList* user_scripts) {
user_scripts, info, new_extensions, NULL /* no verifier for testing */);
}
-UserScriptMaster::UserScriptMaster(Profile* profile)
- : user_scripts_(new UserScriptList()),
- extensions_service_ready_(false),
- pending_load_(false),
+UserScriptMaster::UserScriptMaster(Profile* profile,
+ ExtensionId owner_extension_id)
+ : pending_load_(false),
profile_(profile),
- extension_registry_observer_(this),
- weak_factory_(this) {
- extension_registry_observer_.Add(ExtensionRegistry::Get(profile_));
- registrar_.Add(this, chrome::NOTIFICATION_EXTENSIONS_READY,
- content::Source<Profile>(profile_));
- registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED,
+ weak_factory_(this),
+ owner_extension_id_(owner_extension_id) {
+ registrar_.Add(this,
+ content::NOTIFICATION_RENDERER_PROCESS_CREATED,
content::NotificationService::AllBrowserContextsAndSources());
}
UserScriptMaster::~UserScriptMaster() {
}
+void UserScriptMaster::Observe(int type,
+ const content::NotificationSource& source,
+ const content::NotificationDetails& details) {
+ DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_CREATED, type);
+ content::RenderProcessHost* process =
+ content::Source<content::RenderProcessHost>(source).ptr();
+ Profile* profile = Profile::FromBrowserContext(process->GetBrowserContext());
+ if (!profile_->IsSameProfile(profile))
+ return;
+ if (ScriptsReady())
+ SendUpdate(process, GetSharedMemory(), GetAllManagedExtensions());
+}
+
void UserScriptMaster::OnScriptsLoaded(
scoped_ptr<UserScriptList> user_scripts,
scoped_ptr<base::SharedMemory> shared_memory) {
- user_scripts_.reset(user_scripts.release());
+ AcquireUserScripts(make_scoped_ptr(user_scripts.release()));
if (pending_load_) {
// While we were loading, there were further changes. Don't bother
// notifying about these scripts and instead just immediately reload.
@@ -389,11 +398,10 @@ void UserScriptMaster::OnScriptsLoaded(
for (content::RenderProcessHost::iterator i(
content::RenderProcessHost::AllHostsIterator());
!i.IsAtEnd(); i.Advance()) {
- SendUpdate(i.GetCurrentValue(),
- shared_memory_.get(),
- changed_extensions_);
+ SendUpdate(
+ i.GetCurrentValue(), shared_memory_.get(), GetChangedExtensions());
}
- changed_extensions_.clear();
+ ResetChangedExtensions();
content::NotificationService::current()->Notify(
chrome::NOTIFICATION_USER_SCRIPTS_UPDATED,
@@ -401,129 +409,25 @@ void UserScriptMaster::OnScriptsLoaded(
content::Details<base::SharedMemory>(shared_memory_.get()));
}
-void UserScriptMaster::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());
- if (is_loading())
- pending_load_ = true;
- else
- StartLoad();
- }
-}
-
-void UserScriptMaster::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());
- if (is_loading())
- pending_load_ = true;
- else
- StartLoad();
-}
-
-void UserScriptMaster::Observe(int type,
- const content::NotificationSource& source,
- const content::NotificationDetails& details) {
- bool should_start_load = false;
- switch (type) {
- case chrome::NOTIFICATION_EXTENSIONS_READY:
- extensions_service_ready_ = true;
- should_start_load = true;
- break;
- 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()) {
- SendUpdate(process,
- GetSharedMemory(),
- std::set<std::string>()); // Include all extensions.
- }
- break;
- }
- default:
- DCHECK(false);
- }
-
- if (should_start_load) {
- if (is_loading())
- pending_load_ = true;
- else
- StartLoad();
- }
-}
-
-void UserScriptMaster::StartLoad() {
- DCHECK_CURRENTLY_ON(BrowserThread::UI);
- DCHECK(!is_loading());
-
- // 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<std::string>::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);
- }
- }
-
- BrowserThread::PostTask(
- BrowserThread::FILE,
+void UserScriptMaster::LoadScripts(scoped_ptr<UserScriptList> scripts,
+ const ExtensionsInfo& extensions_info) {
+ content::BrowserThread::PostTask(
+ content::BrowserThread::FILE,
FROM_HERE,
base::Bind(&LoadScriptsOnFileThread,
- base::Passed(&user_scripts_),
- extensions_info_,
- added_extensions_,
+ base::Passed(&scripts),
+ GetExtensionsInfo(),
Devlin 2014/07/31 20:32:24 Wasn't this passed into the function?
+ GetAddedExtensions(),
make_scoped_refptr(
ExtensionSystem::Get(profile_)->content_verifier()),
base::Bind(&UserScriptMaster::OnScriptsLoaded,
weak_factory_.GetWeakPtr())));
- added_extensions_.clear();
- removed_extensions_.clear();
- user_scripts_.reset(NULL);
}
void UserScriptMaster::SendUpdate(
content::RenderProcessHost* process,
base::SharedMemory* shared_memory,
- const std::set<std::string>& changed_extensions) {
+ const std::set<ExtensionId>& changed_extensions) {
// Don't allow injection of content scripts into <webview>.
if (process->IsIsolatedGuest())
return;
@@ -544,8 +448,8 @@ void UserScriptMaster::SendUpdate(
return; // This can legitimately fail if the renderer asserts at startup.
if (base::SharedMemory::IsHandleValid(handle_for_process)) {
- process->Send(new ExtensionMsg_UpdateUserScripts(handle_for_process,
- changed_extensions));
+ process->Send(new ExtensionMsg_UpdateUserScripts(
+ handle_for_process, owner_extension_id_, changed_extensions));
}
}

Powered by Google App Engine
This is Rietveld 408576698