2019-07-24 23:01:08 +00:00
|
|
|
// 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.
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
#include "shell/browser/extensions/electron_extension_system.h"
|
2019-07-24 23:01:08 +00:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2020-01-21 17:43:18 +00:00
|
|
|
#include <utility>
|
2019-07-24 23:01:08 +00:00
|
|
|
|
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/files/file_path.h"
|
|
|
|
#include "base/files/file_util.h"
|
2020-02-13 00:39:12 +00:00
|
|
|
#include "base/json/json_string_value_serializer.h"
|
|
|
|
#include "base/path_service.h"
|
2019-07-24 23:01:08 +00:00
|
|
|
#include "base/task/post_task.h"
|
2020-02-13 00:39:12 +00:00
|
|
|
#include "chrome/common/chrome_paths.h"
|
2019-07-24 23:01:08 +00:00
|
|
|
#include "content/public/browser/browser_context.h"
|
|
|
|
#include "content/public/browser/browser_task_traits.h"
|
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
#include "content/public/browser/notification_details.h"
|
|
|
|
#include "content/public/browser/notification_service.h"
|
|
|
|
#include "content/public/browser/notification_source.h"
|
2020-04-29 06:16:10 +00:00
|
|
|
#include "electron/buildflags/buildflags.h"
|
2019-07-24 23:01:08 +00:00
|
|
|
#include "extensions/browser/api/app_runtime/app_runtime_api.h"
|
|
|
|
#include "extensions/browser/extension_registry.h"
|
|
|
|
#include "extensions/browser/info_map.h"
|
2020-09-02 00:59:56 +00:00
|
|
|
#include "extensions/browser/management_policy.h"
|
2019-07-24 23:01:08 +00:00
|
|
|
#include "extensions/browser/notification_types.h"
|
|
|
|
#include "extensions/browser/null_app_sorting.h"
|
|
|
|
#include "extensions/browser/quota_service.h"
|
|
|
|
#include "extensions/browser/runtime_data.h"
|
|
|
|
#include "extensions/browser/service_worker_manager.h"
|
2021-03-31 02:17:09 +00:00
|
|
|
#include "extensions/browser/user_script_manager.h"
|
2019-07-24 23:01:08 +00:00
|
|
|
#include "extensions/browser/value_store/value_store_factory_impl.h"
|
|
|
|
#include "extensions/common/constants.h"
|
|
|
|
#include "extensions/common/file_util.h"
|
2020-02-03 22:01:10 +00:00
|
|
|
#include "shell/browser/extensions/electron_extension_loader.h"
|
2019-07-24 23:01:08 +00:00
|
|
|
|
2020-04-29 06:16:10 +00:00
|
|
|
#if BUILDFLAG(ENABLE_PDF_VIEWER)
|
2020-09-21 02:08:15 +00:00
|
|
|
#include "chrome/browser/pdf/pdf_extension_util.h" // nogncheck
|
2020-04-29 06:16:10 +00:00
|
|
|
#endif
|
|
|
|
|
2019-07-24 23:01:08 +00:00
|
|
|
using content::BrowserContext;
|
|
|
|
using content::BrowserThread;
|
|
|
|
|
|
|
|
namespace extensions {
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
ElectronExtensionSystem::ElectronExtensionSystem(
|
|
|
|
BrowserContext* browser_context)
|
2019-07-24 23:01:08 +00:00
|
|
|
: browser_context_(browser_context),
|
2021-01-26 18:16:21 +00:00
|
|
|
store_factory_(new ValueStoreFactoryImpl(browser_context->GetPath())) {}
|
2019-07-24 23:01:08 +00:00
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
ElectronExtensionSystem::~ElectronExtensionSystem() = default;
|
2019-07-24 23:01:08 +00:00
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
void ElectronExtensionSystem::LoadExtension(
|
2020-01-21 17:43:18 +00:00
|
|
|
const base::FilePath& extension_dir,
|
2021-02-01 22:41:08 +00:00
|
|
|
int load_flags,
|
2020-02-03 22:01:10 +00:00
|
|
|
base::OnceCallback<void(const Extension*, const std::string&)> cb) {
|
2021-02-01 22:41:08 +00:00
|
|
|
extension_loader_->LoadExtension(extension_dir, load_flags, std::move(cb));
|
2019-07-24 23:01:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
void ElectronExtensionSystem::FinishInitialization() {
|
2019-07-24 23:01:08 +00:00
|
|
|
// Inform the rest of the extensions system to start.
|
|
|
|
ready_.Signal();
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
void ElectronExtensionSystem::ReloadExtension(const ExtensionId& extension_id) {
|
2019-07-24 23:01:08 +00:00
|
|
|
extension_loader_->ReloadExtension(extension_id);
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
void ElectronExtensionSystem::RemoveExtension(const ExtensionId& extension_id) {
|
2020-01-13 22:56:28 +00:00
|
|
|
extension_loader_->UnloadExtension(
|
|
|
|
extension_id, extensions::UnloadedExtensionReason::UNINSTALL);
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
void ElectronExtensionSystem::Shutdown() {
|
2019-07-24 23:01:08 +00:00
|
|
|
extension_loader_.reset();
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
void ElectronExtensionSystem::InitForRegularProfile(bool extensions_enabled) {
|
2019-07-24 23:01:08 +00:00
|
|
|
service_worker_manager_ =
|
|
|
|
std::make_unique<ServiceWorkerManager>(browser_context_);
|
|
|
|
runtime_data_ =
|
|
|
|
std::make_unique<RuntimeData>(ExtensionRegistry::Get(browser_context_));
|
|
|
|
quota_service_ = std::make_unique<QuotaService>();
|
2021-03-31 02:17:09 +00:00
|
|
|
user_script_manager_ = std::make_unique<UserScriptManager>(browser_context_);
|
2019-07-24 23:01:08 +00:00
|
|
|
app_sorting_ = std::make_unique<NullAppSorting>();
|
2020-02-03 22:01:10 +00:00
|
|
|
extension_loader_ =
|
|
|
|
std::make_unique<ElectronExtensionLoader>(browser_context_);
|
2020-02-13 00:39:12 +00:00
|
|
|
|
|
|
|
if (!browser_context_->IsOffTheRecord())
|
|
|
|
LoadComponentExtensions();
|
2020-09-02 00:59:56 +00:00
|
|
|
|
|
|
|
management_policy_.reset(new ManagementPolicy);
|
2020-02-13 00:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<base::DictionaryValue> ParseManifest(
|
|
|
|
base::StringPiece manifest_contents) {
|
|
|
|
JSONStringValueDeserializer deserializer(manifest_contents);
|
|
|
|
std::unique_ptr<base::Value> manifest = deserializer.Deserialize(NULL, NULL);
|
|
|
|
|
|
|
|
if (!manifest.get() || !manifest->is_dict()) {
|
|
|
|
LOG(ERROR) << "Failed to parse extension manifest.";
|
|
|
|
return std::unique_ptr<base::DictionaryValue>();
|
|
|
|
}
|
|
|
|
return base::DictionaryValue::From(std::move(manifest));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ElectronExtensionSystem::LoadComponentExtensions() {
|
2020-04-29 06:16:10 +00:00
|
|
|
#if BUILDFLAG(ENABLE_PDF_VIEWER)
|
2020-02-13 00:39:12 +00:00
|
|
|
std::string utf8_error;
|
|
|
|
std::string pdf_manifest_string = pdf_extension_util::GetManifest();
|
|
|
|
std::unique_ptr<base::DictionaryValue> pdf_manifest =
|
|
|
|
ParseManifest(pdf_manifest_string);
|
2021-03-30 21:53:40 +00:00
|
|
|
if (pdf_manifest) {
|
|
|
|
base::FilePath root_directory;
|
|
|
|
CHECK(base::PathService::Get(chrome::DIR_RESOURCES, &root_directory));
|
|
|
|
root_directory = root_directory.Append(FILE_PATH_LITERAL("pdf"));
|
|
|
|
scoped_refptr<const Extension> pdf_extension =
|
|
|
|
extensions::Extension::Create(
|
|
|
|
root_directory, extensions::Manifest::COMPONENT, *pdf_manifest,
|
|
|
|
extensions::Extension::REQUIRE_KEY, &utf8_error);
|
|
|
|
extension_loader_->registrar()->AddExtension(pdf_extension);
|
|
|
|
}
|
2020-04-29 06:16:10 +00:00
|
|
|
#endif
|
2019-07-24 23:01:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
ExtensionService* ElectronExtensionSystem::extension_service() {
|
2019-07-24 23:01:08 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
RuntimeData* ElectronExtensionSystem::runtime_data() {
|
2019-07-24 23:01:08 +00:00
|
|
|
return runtime_data_.get();
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
ManagementPolicy* ElectronExtensionSystem::management_policy() {
|
2020-09-02 00:59:56 +00:00
|
|
|
return management_policy_.get();
|
2019-07-24 23:01:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
ServiceWorkerManager* ElectronExtensionSystem::service_worker_manager() {
|
2019-07-24 23:01:08 +00:00
|
|
|
return service_worker_manager_.get();
|
|
|
|
}
|
|
|
|
|
2021-03-31 02:17:09 +00:00
|
|
|
UserScriptManager* ElectronExtensionSystem::user_script_manager() {
|
|
|
|
return new UserScriptManager(browser_context_);
|
2019-07-24 23:01:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
StateStore* ElectronExtensionSystem::state_store() {
|
2019-07-24 23:01:08 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
StateStore* ElectronExtensionSystem::rules_store() {
|
2019-07-24 23:01:08 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
scoped_refptr<ValueStoreFactory> ElectronExtensionSystem::store_factory() {
|
2019-07-24 23:01:08 +00:00
|
|
|
return store_factory_;
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
InfoMap* ElectronExtensionSystem::info_map() {
|
2019-07-24 23:01:08 +00:00
|
|
|
if (!info_map_.get())
|
|
|
|
info_map_ = new InfoMap;
|
|
|
|
return info_map_.get();
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
QuotaService* ElectronExtensionSystem::quota_service() {
|
2019-07-24 23:01:08 +00:00
|
|
|
return quota_service_.get();
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
AppSorting* ElectronExtensionSystem::app_sorting() {
|
2019-07-24 23:01:08 +00:00
|
|
|
return app_sorting_.get();
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
void ElectronExtensionSystem::RegisterExtensionWithRequestContexts(
|
2019-07-24 23:01:08 +00:00
|
|
|
const Extension* extension,
|
2020-01-29 12:01:37 +00:00
|
|
|
base::OnceClosure callback) {
|
2021-01-25 01:27:40 +00:00
|
|
|
base::PostTaskAndReply(FROM_HERE, {BrowserThread::IO},
|
|
|
|
base::BindOnce(&InfoMap::AddExtension, info_map(),
|
|
|
|
base::RetainedRef(extension),
|
|
|
|
base::Time::Now(), false, false),
|
|
|
|
std::move(callback));
|
2019-07-24 23:01:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
void ElectronExtensionSystem::UnregisterExtensionWithRequestContexts(
|
2019-07-24 23:01:08 +00:00
|
|
|
const std::string& extension_id,
|
|
|
|
const UnloadedExtensionReason reason) {}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
const base::OneShotEvent& ElectronExtensionSystem::ready() const {
|
2019-07-24 23:01:08 +00:00
|
|
|
return ready_;
|
|
|
|
}
|
|
|
|
|
2020-06-22 17:35:10 +00:00
|
|
|
bool ElectronExtensionSystem::is_ready() const {
|
|
|
|
return ready_.is_signaled();
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
ContentVerifier* ElectronExtensionSystem::content_verifier() {
|
2019-07-24 23:01:08 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
std::unique_ptr<ExtensionSet> ElectronExtensionSystem::GetDependentExtensions(
|
2019-07-24 23:01:08 +00:00
|
|
|
const Extension* extension) {
|
|
|
|
return std::make_unique<ExtensionSet>();
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
void ElectronExtensionSystem::InstallUpdate(
|
2019-07-24 23:01:08 +00:00
|
|
|
const std::string& extension_id,
|
|
|
|
const std::string& public_key,
|
|
|
|
const base::FilePath& temp_dir,
|
|
|
|
bool install_immediately,
|
|
|
|
InstallUpdateCallback install_update_callback) {
|
|
|
|
NOTREACHED();
|
2020-08-12 18:33:58 +00:00
|
|
|
base::DeletePathRecursively(temp_dir);
|
2019-07-24 23:01:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
bool ElectronExtensionSystem::FinishDelayedInstallationIfReady(
|
2019-07-24 23:01:08 +00:00
|
|
|
const std::string& extension_id,
|
|
|
|
bool install_immediately) {
|
|
|
|
NOTREACHED();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-13 23:39:26 +00:00
|
|
|
void ElectronExtensionSystem::PerformActionBasedOnOmahaAttributes(
|
|
|
|
const std::string& extension_id,
|
|
|
|
const base::Value& attributes) {
|
|
|
|
NOTREACHED();
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
void ElectronExtensionSystem::OnExtensionRegisteredWithRequestContexts(
|
2019-07-24 23:01:08 +00:00
|
|
|
scoped_refptr<Extension> extension) {
|
|
|
|
ExtensionRegistry* registry = ExtensionRegistry::Get(browser_context_);
|
|
|
|
registry->AddReady(extension);
|
|
|
|
registry->TriggerOnReady(extension.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace extensions
|