| Index: extensions/browser/extension_user_script_loader.cc
|
| diff --git a/extensions/browser/extension_user_script_loader.cc b/extensions/browser/extension_user_script_loader.cc
|
| index dafcdd51a1b12d708c7f4d58edc1d11aa5746a95..09cc16bd924f7b9f413fc604c8e843b9e731a3e9 100644
|
| --- a/extensions/browser/extension_user_script_loader.cc
|
| +++ b/extensions/browser/extension_user_script_loader.cc
|
| @@ -33,6 +33,10 @@ namespace extensions {
|
|
|
| namespace {
|
|
|
| +using LoadScriptsCallback =
|
| + base::Callback<void(scoped_ptr<UserScriptList>,
|
| + scoped_ptr<base::SharedMemory>)>;
|
| +
|
| // Verifies file contents as they are read.
|
| void VerifyContent(const scoped_refptr<ContentVerifier>& verifier,
|
| const std::string& extension_id,
|
| @@ -50,7 +54,7 @@ void VerifyContent(const scoped_refptr<ContentVerifier>& verifier,
|
| }
|
|
|
| // Loads user scripts from the extension who owns these scripts.
|
| -bool ExtensionLoadScriptContent(
|
| +bool LoadScriptContent(
|
| const HostID& host_id,
|
| UserScript::File* script_file,
|
| const UserScriptLoader::SubstitutionMap* localization_messages,
|
| @@ -108,15 +112,62 @@ bool ExtensionLoadScriptContent(
|
| return true;
|
| }
|
|
|
| +UserScriptLoader::SubstitutionMap* GetLocalizationMessages(
|
| + const UserScriptLoader::HostsInfo& hosts_info,
|
| + const HostID& host_id) {
|
| + UserScriptLoader::HostsInfo::const_iterator iter = hosts_info.find(host_id);
|
| + if (iter == hosts_info.end())
|
| + return nullptr;
|
| + return file_util::LoadMessageBundleSubstitutionMap(
|
| + iter->second.first, host_id.id(), iter->second.second);
|
| +}
|
| +
|
| +void LoadUserScripts(UserScriptList* user_scripts,
|
| + const UserScriptLoader::HostsInfo& hosts_info,
|
| + const std::set<int>& added_script_ids,
|
| + const scoped_refptr<ContentVerifier>& verifier) {
|
| + for (UserScriptList::iterator script = user_scripts->begin();
|
| + script != user_scripts->end(); ++script) {
|
| + if (added_script_ids.count(script->id()) == 0)
|
| + continue;
|
| + scoped_ptr<UserScriptLoader::SubstitutionMap> localization_messages(
|
| + GetLocalizationMessages(hosts_info, script->host_id()));
|
| + for (size_t k = 0; k < script->js_scripts().size(); ++k) {
|
| + UserScript::File& script_file = script->js_scripts()[k];
|
| + if (script_file.GetContent().empty())
|
| + LoadScriptContent(script->host_id(), &script_file, NULL, verifier);
|
| + }
|
| + for (size_t k = 0; k < script->css_scripts().size(); ++k) {
|
| + UserScript::File& script_file = script->css_scripts()[k];
|
| + if (script_file.GetContent().empty())
|
| + LoadScriptContent(script->host_id(), &script_file,
|
| + localization_messages.get(), verifier);
|
| + }
|
| + }
|
| +}
|
| +
|
| +void LoadScriptsOnFileThread(scoped_ptr<UserScriptList> user_scripts,
|
| + const UserScriptLoader::HostsInfo& hosts_info,
|
| + const std::set<int>& added_script_ids,
|
| + const scoped_refptr<ContentVerifier>& verifier,
|
| + LoadScriptsCallback callback) {
|
| + DCHECK(user_scripts.get());
|
| + LoadUserScripts(user_scripts.get(), hosts_info, added_script_ids, verifier);
|
| + scoped_ptr<base::SharedMemory> memory =
|
| + UserScriptLoader::Serialize(*user_scripts);
|
| + content::BrowserThread::PostTask(
|
| + content::BrowserThread::UI, FROM_HERE,
|
| + base::Bind(callback, base::Passed(&user_scripts), base::Passed(&memory)));
|
| +}
|
| +
|
| } // namespace
|
|
|
| ExtensionUserScriptLoader::ExtensionUserScriptLoader(
|
| BrowserContext* browser_context,
|
| const HostID& host_id,
|
| bool listen_for_extension_system_loaded)
|
| - : UserScriptLoader(
|
| - browser_context,
|
| - host_id,
|
| + : UserScriptLoader(browser_context, host_id),
|
| + content_verifier_(
|
| ExtensionSystem::Get(browser_context)->content_verifier()),
|
| extension_registry_observer_(this),
|
| weak_factory_(this) {
|
| @@ -135,6 +186,33 @@ ExtensionUserScriptLoader::ExtensionUserScriptLoader(
|
| ExtensionUserScriptLoader::~ExtensionUserScriptLoader() {
|
| }
|
|
|
| +void ExtensionUserScriptLoader::LoadScriptsForTest(
|
| + UserScriptList* user_scripts) {
|
| + HostsInfo info;
|
| + std::set<int> added_script_ids;
|
| + for (UserScriptList::iterator it = user_scripts->begin();
|
| + it != user_scripts->end(); ++it) {
|
| + added_script_ids.insert(it->id());
|
| + }
|
| + LoadUserScripts(user_scripts, info, added_script_ids,
|
| + NULL /* no verifier for testing */);
|
| +}
|
| +
|
| +void ExtensionUserScriptLoader::LoadScripts(
|
| + const std::set<HostID>& changed_hosts,
|
| + const std::set<int>& added_script_ids) {
|
| + UpdateHostsInfo(changed_hosts);
|
| +
|
| + content::BrowserThread::PostTask(
|
| + content::BrowserThread::FILE, FROM_HERE,
|
| + base::Bind(&LoadScriptsOnFileThread, base::Passed(&user_scripts_),
|
| + hosts_info_, added_script_ids, content_verifier_,
|
| + base::Bind(&ExtensionUserScriptLoader::OnScriptsLoaded,
|
| + weak_factory_.GetWeakPtr())));
|
| + ClearScripts(false);
|
| + user_scripts_.reset(NULL);
|
| +}
|
| +
|
| void ExtensionUserScriptLoader::UpdateHostsInfo(
|
| const std::set<HostID>& changed_hosts) {
|
| ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context());
|
| @@ -145,22 +223,18 @@ void ExtensionUserScriptLoader::UpdateHostsInfo(
|
| // which leads to the above lookup failing. In this case, just continue.
|
| if (!extension)
|
| continue;
|
| - AddHostInfo(host_id, ExtensionSet::ExtensionPathAndDefaultLocale(
|
| - extension->path(),
|
| - LocaleInfo::GetDefaultLocale(extension)));
|
| + if (hosts_info_.find(host_id) != hosts_info_.end())
|
| + return;
|
| + hosts_info_[host_id] = ExtensionSet::ExtensionPathAndDefaultLocale(
|
| + extension->path(), LocaleInfo::GetDefaultLocale(extension));
|
| }
|
| }
|
|
|
| -UserScriptLoader::LoadUserScriptsContentFunction
|
| -ExtensionUserScriptLoader::GetLoadUserScriptsFunction() {
|
| - return base::Bind(&ExtensionLoadScriptContent);
|
| -}
|
| -
|
| void ExtensionUserScriptLoader::OnExtensionUnloaded(
|
| content::BrowserContext* browser_context,
|
| const Extension* extension,
|
| UnloadedExtensionInfo::Reason reason) {
|
| - RemoveHostInfo(HostID(HostID::EXTENSIONS, extension->id()));
|
| + hosts_info_.erase(HostID(HostID::EXTENSIONS, extension->id()));
|
| }
|
|
|
| void ExtensionUserScriptLoader::OnExtensionSystemReady() {
|
|
|