Chromium Code Reviews| Index: chrome/browser/extensions/extension_user_script_loader.cc |
| diff --git a/chrome/browser/extensions/extension_user_script_loader.cc b/chrome/browser/extensions/extension_user_script_loader.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..406c56e4787919e04773a59f900c038656e01605 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/extension_user_script_loader.cc |
| @@ -0,0 +1,190 @@ |
| +// Copyright 2015 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/extension_user_script_loader.h" |
| + |
| +#include <set> |
| +#include <string> |
| + |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/files/file_path.h" |
| +#include "base/files/file_util.h" |
| +#include "base/version.h" |
| +#include "chrome/browser/chrome_notification_types.h" |
|
Devlin
2015/01/20 17:51:05
nit: prune includes.
Xi Han
2015/01/21 21:30:16
Why I cannot find these unnecessary includes:(
|
| +#include "chrome/browser/profiles/profile.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/notification_service.h" |
| +#include "content/public/browser/render_process_host.h" |
| +#include "extensions/browser/component_extension_resource_manager.h" |
| +#include "extensions/browser/content_verifier.h" |
| +#include "extensions/browser/extension_registry.h" |
| +#include "extensions/browser/extension_system.h" |
| +#include "extensions/browser/extensions_browser_client.h" |
| +#include "extensions/common/extension_messages.h" |
| +#include "extensions/common/file_util.h" |
| +#include "extensions/common/manifest_handlers/default_locale_handler.h" |
| +#include "extensions/common/message_bundle.h" |
| +#include "extensions/common/one_shot_event.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| + |
| +using content::BrowserThread; |
|
Devlin
2015/01/20 17:51:05
nit: Even though you have this line, you specify "
Xi Han
2015/01/21 21:30:16
Removed.
|
| + |
| +namespace extensions { |
| + |
| +namespace { |
| + |
| +// Verifies file contents as they are read. |
| +void VerifyContent(scoped_refptr<ContentVerifier> verifier, |
| + const ConsumerID& consumer_id, |
| + const base::FilePath& extension_root, |
| + const base::FilePath& relative_path, |
| + const std::string& content) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + scoped_refptr<ContentVerifyJob> job(verifier->CreateJobFor( |
| + consumer_id.host_id(), extension_root, relative_path)); |
|
Devlin
2015/01/20 17:51:05
nit: since this is in the ExtensionUserScriptLoade
Xi Han
2015/01/21 21:30:16
Done.
|
| + if (job.get()) { |
| + job->Start(); |
| + job->BytesRead(content.size(), content.data()); |
| + job->DoneReading(); |
| + } |
| +} |
| + |
| +} // namespace |
| + |
| +ExtensionUserScriptLoader::ExtensionUserScriptLoader( |
| + Profile* profile, |
| + const ConsumerID& consumer_id, |
| + bool listen_for_extension_system_loaded) |
| + : UserScriptLoader(profile, |
| + consumer_id, |
| + ExtensionSystem::Get(profile)->content_verifier()), |
| + extension_registry_observer_(this), |
| + weak_factory_(this) { |
| + extension_registry_observer_.Add(ExtensionRegistry::Get(profile)); |
| + if (listen_for_extension_system_loaded) { |
| + SetReady(false); |
|
Devlin
2015/01/20 17:51:05
UserScriptLoader is, by default, set to false. Yo
Xi Han
2015/01/21 21:30:16
Done.
|
| + ExtensionSystem::Get(profile)->ready().Post( |
| + FROM_HERE, |
| + base::Bind(&ExtensionUserScriptLoader::OnExtensionSystemReady, |
| + weak_factory_.GetWeakPtr())); |
| + } else { |
| + SetReady(true); |
| + } |
| +} |
| + |
| +ExtensionUserScriptLoader::~ExtensionUserScriptLoader() { |
| +} |
| + |
| +void ExtensionUserScriptLoader::UpdateConsumersInfo( |
| + const ConsumerIDSet& changed_consumers) { |
| + ExtensionRegistry* registry = ExtensionRegistry::Get(profile()); |
| + for (const ConsumerID& consumer_id : changed_consumers) { |
| + const Extension* extension = registry->GetExtensionById( |
| + consumer_id.host_id(), ExtensionRegistry::ENABLED); |
| + // |changed_consumers_| may include consumers that have been removed, |
| + // which leads to the above lookup failing. In this case, just continue. |
| + if (!extension) |
| + continue; |
| + AddConsumerInfo(consumer_id, ExtensionSet::ExtensionPathAndDefaultLocale( |
| + extension->path(), |
| + LocaleInfo::GetDefaultLocale(extension))); |
| + } |
| +} |
| + |
| +void ExtensionUserScriptLoader::SendUpdate( |
| + content::RenderProcessHost* process, |
| + base::SharedMemoryHandle handle_for_process, |
| + const std::set<ConsumerID>& changed_consumers) { |
| + // TODO(hanxi): update the IPC message to send a set of ConsumerID to render. |
| + // Also, remove this function when the refactor is done on render side. |
| + std::set<std::string> changed_extensions; |
| + for (std::set<ConsumerID>::iterator it = changed_consumers.begin(); |
|
Devlin
2015/01/20 17:51:05
nit: shorter:
for (const ConsumerID& id : changed_
Xi Han
2015/01/21 21:30:16
Done.
|
| + it != changed_consumers.end(); ++it) { |
| + changed_extensions.insert(it->host_id()); |
| + } |
| + |
| + if (base::SharedMemory::IsHandleValid(handle_for_process)) { |
| + process->Send(new ExtensionMsg_UpdateUserScripts( |
| + handle_for_process, consumer_id().host_id(), changed_extensions)); |
| + } |
| +} |
| + |
| +UserScriptLoader::LoadUserScriptsFunctionCallback |
| +ExtensionUserScriptLoader::GetLoadUserScriptsFunction() { |
| + return base::Bind(&ExtensionUserScriptLoader::LoadScriptContent); |
| +} |
| + |
| +// static |
| +bool ExtensionUserScriptLoader::LoadScriptContent( |
| + const ConsumerID& consumer_id, |
| + UserScript::File* script_file, |
| + const SubstitutionMap* localization_messages, |
| + scoped_refptr<ContentVerifier> verifier) { |
| + DCHECK(script_file); |
| + std::string content; |
| + const base::FilePath& path = ExtensionResource::GetFilePath( |
| + script_file->extension_root(), script_file->relative_path(), |
| + ExtensionResource::SYMLINKS_MUST_RESOLVE_WITHIN_ROOT); |
| + if (path.empty()) { |
| + int resource_id = 0; |
| + if (ExtensionsBrowserClient::Get() |
| + ->GetComponentExtensionResourceManager() |
| + ->IsComponentExtensionResource(script_file->extension_root(), |
| + script_file->relative_path(), |
| + &resource_id)) { |
| + const ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| + content = rb.GetRawDataResource(resource_id).as_string(); |
| + } else { |
| + LOG(WARNING) << "Failed to get file path to " |
| + << script_file->relative_path().value() << " from " |
| + << script_file->extension_root().value(); |
| + return false; |
| + } |
| + } else { |
| + if (!base::ReadFileToString(path, &content)) { |
| + LOG(WARNING) << "Failed to load user script file: " << path.value(); |
| + return false; |
| + } |
| + if (verifier.get()) { |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::IO, FROM_HERE, |
| + base::Bind(&VerifyContent, verifier, consumer_id, |
| + script_file->extension_root(), |
| + script_file->relative_path(), content)); |
| + } |
| + } |
| + |
| + // Localize the content. |
| + if (localization_messages) { |
| + std::string error; |
| + MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages, |
| + &content, &error); |
| + if (!error.empty()) |
| + LOG(WARNING) << "Failed to replace messages in script: " << error; |
| + } |
| + |
| + // Remove BOM from the content. |
| + std::string::size_type index = content.find(base::kUtf8ByteOrderMark); |
| + if (index == 0) { |
|
Devlin
2015/01/20 17:51:05
nit: no brackets.
Xi Han
2015/01/21 21:30:16
Done.
|
| + script_file->set_content(content.substr(strlen(base::kUtf8ByteOrderMark))); |
| + } else { |
| + script_file->set_content(content); |
| + } |
| + |
| + return true; |
| +} |
| + |
| +void ExtensionUserScriptLoader::OnExtensionUnloaded( |
| + content::BrowserContext* browser_context, |
| + const Extension* extension, |
| + UnloadedExtensionInfo::Reason reason) { |
| + RemoveConsumerInfo(extension->id()); |
| +} |
| + |
| +void ExtensionUserScriptLoader::OnExtensionSystemReady() { |
| + SetReady(true); |
| +} |
| + |
| +} // namespace extensions |