2019-07-24 23:01:08 +00:00
|
|
|
// Copyright 2018 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_loader.h"
|
2019-07-24 23:01:08 +00:00
|
|
|
|
2020-01-21 17:43:18 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2019-07-24 23:01:08 +00:00
|
|
|
#include "base/auto_reset.h"
|
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/files/file_path.h"
|
|
|
|
#include "base/files/file_util.h"
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "base/sequenced_task_runner.h"
|
2020-12-18 23:11:43 +00:00
|
|
|
#include "base/strings/string_number_conversions.h"
|
2020-02-03 22:01:10 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2019-07-24 23:01:08 +00:00
|
|
|
#include "base/task_runner_util.h"
|
|
|
|
#include "base/threading/thread_restrictions.h"
|
2020-12-18 23:11:43 +00:00
|
|
|
#include "base/time/time.h"
|
2019-07-24 23:01:08 +00:00
|
|
|
#include "extensions/browser/extension_file_task_runner.h"
|
|
|
|
#include "extensions/browser/extension_prefs.h"
|
|
|
|
#include "extensions/browser/extension_registry.h"
|
2020-12-18 23:11:43 +00:00
|
|
|
#include "extensions/browser/pref_names.h"
|
2019-07-24 23:01:08 +00:00
|
|
|
#include "extensions/common/file_util.h"
|
|
|
|
|
|
|
|
namespace extensions {
|
|
|
|
|
|
|
|
using LoadErrorBehavior = ExtensionRegistrar::LoadErrorBehavior;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
std::pair<scoped_refptr<const Extension>, std::string> LoadUnpacked(
|
2021-02-01 22:41:08 +00:00
|
|
|
const base::FilePath& extension_dir,
|
|
|
|
int load_flags) {
|
2019-07-24 23:01:08 +00:00
|
|
|
// app_shell only supports unpacked extensions.
|
|
|
|
// NOTE: If you add packed extension support consider removing the flag
|
|
|
|
// FOLLOW_SYMLINKS_ANYWHERE below. Packed extensions should not have symlinks.
|
|
|
|
if (!base::DirectoryExists(extension_dir)) {
|
2020-02-03 22:01:10 +00:00
|
|
|
std::string err = "Extension directory not found: " +
|
|
|
|
base::UTF16ToUTF8(extension_dir.LossyDisplayName());
|
|
|
|
return std::make_pair(nullptr, err);
|
2019-07-24 23:01:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string load_error;
|
|
|
|
scoped_refptr<Extension> extension = file_util::LoadExtension(
|
|
|
|
extension_dir, Manifest::COMMAND_LINE, load_flags, &load_error);
|
|
|
|
if (!extension.get()) {
|
2020-02-03 22:01:10 +00:00
|
|
|
std::string err = "Loading extension at " +
|
|
|
|
base::UTF16ToUTF8(extension_dir.LossyDisplayName()) +
|
|
|
|
" failed with: " + load_error;
|
|
|
|
return std::make_pair(nullptr, err);
|
2019-07-24 23:01:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
std::string warnings;
|
2019-07-24 23:01:08 +00:00
|
|
|
// Log warnings.
|
|
|
|
if (extension->install_warnings().size()) {
|
2020-02-03 22:01:10 +00:00
|
|
|
warnings += "Warnings loading extension at " +
|
2020-09-10 02:39:45 +00:00
|
|
|
base::UTF16ToUTF8(extension_dir.LossyDisplayName()) + ":\n";
|
2020-02-03 22:01:10 +00:00
|
|
|
for (const auto& warning : extension->install_warnings()) {
|
2020-09-10 02:39:45 +00:00
|
|
|
warnings += " " + warning.message + "\n";
|
2020-02-03 22:01:10 +00:00
|
|
|
}
|
2019-07-24 23:01:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
return std::make_pair(extension, warnings);
|
2019-07-24 23:01:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
ElectronExtensionLoader::ElectronExtensionLoader(
|
2019-07-24 23:01:08 +00:00
|
|
|
content::BrowserContext* browser_context)
|
|
|
|
: browser_context_(browser_context),
|
2021-01-26 18:16:21 +00:00
|
|
|
extension_registrar_(browser_context, this) {}
|
2019-07-24 23:01:08 +00:00
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
ElectronExtensionLoader::~ElectronExtensionLoader() = default;
|
2019-07-24 23:01:08 +00:00
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
void ElectronExtensionLoader::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) {
|
2020-01-21 17:43:18 +00:00
|
|
|
base::PostTaskAndReplyWithResult(
|
|
|
|
GetExtensionFileTaskRunner().get(), FROM_HERE,
|
2021-02-01 22:41:08 +00:00
|
|
|
base::BindOnce(&LoadUnpacked, extension_dir, load_flags),
|
2020-02-03 22:01:10 +00:00
|
|
|
base::BindOnce(&ElectronExtensionLoader::FinishExtensionLoad,
|
|
|
|
weak_factory_.GetWeakPtr(), std::move(cb)));
|
2019-07-24 23:01:08 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
void ElectronExtensionLoader::ReloadExtension(const ExtensionId& extension_id) {
|
2019-07-24 23:01:08 +00:00
|
|
|
const Extension* extension = ExtensionRegistry::Get(browser_context_)
|
|
|
|
->GetInstalledExtension(extension_id);
|
|
|
|
// We shouldn't be trying to reload extensions that haven't been added.
|
|
|
|
DCHECK(extension);
|
|
|
|
|
|
|
|
// This should always start false since it's only set here, or in
|
|
|
|
// LoadExtensionForReload() as a result of the call below.
|
|
|
|
DCHECK_EQ(false, did_schedule_reload_);
|
|
|
|
base::AutoReset<bool> reset_did_schedule_reload(&did_schedule_reload_, false);
|
|
|
|
|
|
|
|
extension_registrar_.ReloadExtension(extension_id, LoadErrorBehavior::kQuiet);
|
|
|
|
if (did_schedule_reload_)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
void ElectronExtensionLoader::UnloadExtension(
|
2020-01-13 22:56:28 +00:00
|
|
|
const ExtensionId& extension_id,
|
|
|
|
extensions::UnloadedExtensionReason reason) {
|
|
|
|
extension_registrar_.RemoveExtension(extension_id, reason);
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
void ElectronExtensionLoader::FinishExtensionLoad(
|
|
|
|
base::OnceCallback<void(const Extension*, const std::string&)> cb,
|
|
|
|
std::pair<scoped_refptr<const Extension>, std::string> result) {
|
|
|
|
scoped_refptr<const Extension> extension = result.first;
|
2020-01-21 17:43:18 +00:00
|
|
|
if (extension) {
|
|
|
|
extension_registrar_.AddExtension(extension);
|
|
|
|
}
|
2020-12-18 23:11:43 +00:00
|
|
|
|
|
|
|
// Write extension install time to ExtensionPrefs. This is required by
|
|
|
|
// WebRequestAPI which calls extensions::ExtensionPrefs::GetInstallTime.
|
|
|
|
//
|
|
|
|
// Implementation for writing the pref was based on
|
|
|
|
// PreferenceAPIBase::SetExtensionControlledPref.
|
|
|
|
{
|
|
|
|
ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(browser_context_);
|
|
|
|
ExtensionPrefs::ScopedDictionaryUpdate update(
|
|
|
|
extension_prefs, extension.get()->id(),
|
|
|
|
extensions::pref_names::kPrefPreferences);
|
|
|
|
auto preference = update.Create();
|
|
|
|
const base::Time install_time = base::Time().Now();
|
|
|
|
preference->SetString("install_time",
|
|
|
|
base::NumberToString(install_time.ToInternalValue()));
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
std::move(cb).Run(extension.get(), result.second);
|
2020-01-21 17:43:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
void ElectronExtensionLoader::FinishExtensionReload(
|
2020-01-13 22:56:28 +00:00
|
|
|
const ExtensionId& old_extension_id,
|
2020-02-03 22:01:10 +00:00
|
|
|
std::pair<scoped_refptr<const Extension>, std::string> result) {
|
|
|
|
scoped_refptr<const Extension> extension = result.first;
|
2019-07-24 23:01:08 +00:00
|
|
|
if (extension) {
|
|
|
|
extension_registrar_.AddExtension(extension);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
void ElectronExtensionLoader::PreAddExtension(const Extension* extension,
|
|
|
|
const Extension* old_extension) {
|
2019-07-24 23:01:08 +00:00
|
|
|
if (old_extension)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// The extension might be disabled if a previous reload attempt failed. In
|
|
|
|
// that case, we want to remove that disable reason.
|
|
|
|
ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(browser_context_);
|
|
|
|
if (extension_prefs->IsExtensionDisabled(extension->id()) &&
|
|
|
|
extension_prefs->HasDisableReason(extension->id(),
|
|
|
|
disable_reason::DISABLE_RELOAD)) {
|
|
|
|
extension_prefs->RemoveDisableReason(extension->id(),
|
|
|
|
disable_reason::DISABLE_RELOAD);
|
|
|
|
// Only re-enable the extension if there are no other disable reasons.
|
|
|
|
if (extension_prefs->GetDisableReasons(extension->id()) ==
|
|
|
|
disable_reason::DISABLE_NONE) {
|
|
|
|
extension_prefs->SetExtensionEnabled(extension->id());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
void ElectronExtensionLoader::PostActivateExtension(
|
2019-07-24 23:01:08 +00:00
|
|
|
scoped_refptr<const Extension> extension) {}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
void ElectronExtensionLoader::PostDeactivateExtension(
|
2019-07-24 23:01:08 +00:00
|
|
|
scoped_refptr<const Extension> extension) {}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
void ElectronExtensionLoader::LoadExtensionForReload(
|
2019-07-24 23:01:08 +00:00
|
|
|
const ExtensionId& extension_id,
|
|
|
|
const base::FilePath& path,
|
|
|
|
LoadErrorBehavior load_error_behavior) {
|
|
|
|
CHECK(!path.empty());
|
|
|
|
|
2021-02-01 22:41:08 +00:00
|
|
|
// TODO(nornagon): we should save whether file access was granted
|
|
|
|
// when loading this extension and retain it here. As is, reloading an
|
|
|
|
// extension will cause the file access permission to be dropped.
|
|
|
|
int load_flags = Extension::FOLLOW_SYMLINKS_ANYWHERE;
|
2019-07-24 23:01:08 +00:00
|
|
|
base::PostTaskAndReplyWithResult(
|
|
|
|
GetExtensionFileTaskRunner().get(), FROM_HERE,
|
2021-02-01 22:41:08 +00:00
|
|
|
base::BindOnce(&LoadUnpacked, path, load_flags),
|
2020-02-03 22:01:10 +00:00
|
|
|
base::BindOnce(&ElectronExtensionLoader::FinishExtensionReload,
|
2019-07-24 23:01:08 +00:00
|
|
|
weak_factory_.GetWeakPtr(), extension_id));
|
|
|
|
did_schedule_reload_ = true;
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
bool ElectronExtensionLoader::CanEnableExtension(const Extension* extension) {
|
2019-07-24 23:01:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
bool ElectronExtensionLoader::CanDisableExtension(const Extension* extension) {
|
2019-07-24 23:01:08 +00:00
|
|
|
// Extensions cannot be disabled by the user.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-03 22:01:10 +00:00
|
|
|
bool ElectronExtensionLoader::ShouldBlockExtension(const Extension* extension) {
|
2019-07-24 23:01:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace extensions
|