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

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

Issue 822453002: Introduce HostID and de-couple Extensions from "script injection System" [browser side] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: gn Created 6 years 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/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..8127631994168620b31f9f70dac6ec5a862fb15f
--- /dev/null
+++ b/chrome/browser/extensions/extension_user_script_loader.cc
@@ -0,0 +1,209 @@
+// 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/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"
+#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;
+using extensions::ExtensionsBrowserClient;
+
+namespace extensions {
+
+namespace {
+
+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));
+ 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),
+ extension_system_ready_(false),
+ extension_registry_observer_(this),
+ weak_factory_(this) {
+ extension_registry_observer_.Add(ExtensionRegistry::Get(profile));
+ if (listen_for_extension_system_loaded) {
+ ExtensionSystem::Get(profile)->ready().Post(
+ FROM_HERE,
+ base::Bind(&ExtensionUserScriptLoader::OnExtensionSystemReady,
+ weak_factory_.GetWeakPtr()));
+ } else {
+ extension_system_ready_ = true;
+ }
+}
+
+ExtensionUserScriptLoader::~ExtensionUserScriptLoader() {
+}
+
+bool ExtensionUserScriptLoader::ready() {
+ return extension_system_ready_;
+}
+
+ContentVerifier* ExtensionUserScriptLoader::GetContentVerifier() {
+ return ExtensionSystem::Get(profile_)->content_verifier();
+}
+
+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();
+ 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) {
+ 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;
+ 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) {
+ 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) {
+ std::set<ConsumerID> ids_to_delete;
+
+ for (ConsumersInfo::iterator it = consumers_info_.begin();
+ it != consumers_info_.end(); ++it) {
+ ConsumerID id = it->first;
+ if (id.host_id.compare(extension->id()) == 0)
+ ids_to_delete.insert(id);
+ }
+
+ for (std::set<ConsumerID>::iterator it = ids_to_delete.begin();
+ it != ids_to_delete.end(); ++it)
+ consumers_info_.erase(*it);
+}
+
+void ExtensionUserScriptLoader::OnExtensionSystemReady() {
+ extension_system_ready_ = true;
+ AttemptLoad();
+}
+
+void ExtensionUserScriptLoader::UpdateConsumersInfo() {
+ ExtensionRegistry* registry = ExtensionRegistry::Get(profile_);
+ for (std::set<ConsumerID>::const_iterator it = changed_consumers_.begin();
+ it != changed_consumers_.end(); ++it) {
+ if (consumers_info_.find(*it) == consumers_info_.end()) {
+ const Extension* extension = registry->GetExtensionById(
+ it->host_id, ExtensionRegistry::EVERYTHING);
+ // |changed_consumers_| may include consumers that have been removed,
+ // which leads to the above lookup failing. In this case, just continue.
+ if (!extension)
+ continue;
+ consumers_info_[*it] = ExtensionSet::ExtensionPathAndDefaultLocale(
+ extension->path(), LocaleInfo::GetDefaultLocale(extension));
+ }
+ }
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698