electron/shell/browser/extensions/electron_extension_loader.cc
electron-roller[bot] 90af7d7fe2
chore: bump chromium to 111.0.5518.0 (main) (#36575)
* chore: bump chromium in DEPS to 110.0.5461.0

* chore: update patches

* 3903024: hid: Add connection count tracking methods for HidDelegate

https://chromium-review.googlesource.com/c/chromium/src/+/3903024

* 4076211: Turn FrameTreeNode::frame_tree into raw_ref.

https://chromium-review.googlesource.com/c/chromium/src/+/4076211

* chore: bump chromium in DEPS to 110.0.5463.0

* chore: bump chromium in DEPS to 110.0.5465.0

* fix patches

* 3835037: Add new slides media session actions to Picture-in-Picture window

https://chromium-review.googlesource.com/c/chromium/src/+/3835037

* chore: update patches

* chore: bump chromium in DEPS to 110.0.5467.0

* chore: update patches

* chore: bump chromium in DEPS to 110.0.5469.0

* chore: bump chromium in DEPS to 110.0.5471.0

* chore: bump chromium in DEPS to 110.0.5473.0

* chore: bump chromium in DEPS to 110.0.5475.0

* chore: update patches

* 4074449: Add gl::FrameData to software path

https://chromium-review.googlesource.com/c/chromium/src/+/4074449

* 4065264: [Extensions] Add a new side panel view type

https://chromium-review.googlesource.com/c/chromium/src/+/4065264

* 4060548: Remove base::Value::GetListDeprecated().

https://chromium-review.googlesource.com/c/chromium/src/+/4060548

* chore: add missing RefCountedMemory include

* 4081108: task posting v3: remove task_runner_util{.h,_unittest.cc}

https://chromium-review.googlesource.com/c/chromium/src/+/4081108

* 4072471: Rename Mixed Download Blocking to Insecure Download Blocking

https://chromium-review.googlesource.com/c/chromium/src/+/4072471

* 4025927: [Code Health] Migrate e/c/manifest.cc to base::Value::Dict interface

https://chromium-review.googlesource.com/c/chromium/src/+/4025927

* chore: fixup patch indices

* chore: bump chromium in DEPS to 110.0.5477.0

* chore: fixup preconnect_manager.patch

* chore: fixup patch indices

* fixup! 4074449: Add gl::FrameData to software path

* 4074449: Add gl::FrameData to software path

This commit also reformatted the two files in this patch. The only
change here is the addition of the |data| arg to |OnSwapBuffers|.

https://chromium-review.googlesource.com/c/chromium/src/+/4074449

* 4081108: task posting v3: remove task_runner_util{.h,_unittest.cc}

https://chromium-review.googlesource.com/c/chromium/src/+/4081108

* 4085814: [Test Automation] Move NativeWindowTracker to ui/views

https://chromium-review.googlesource.com/c/chromium/src/+/4085814

* 4032656: hid: Abstract HidSystemTrayIcon class for profiles' HID connections

https://chromium-review.googlesource.com/c/chromium/src/+/4032656

* chore: bump chromium in DEPS to 110.0.5479.0

* chore: fixup patches & simplify printing patch

To be specific, I replaced some combination of line removals &
commenting-out with `#if 0` blocks since they were already there for
android.

Should be functionally the same, just written differently for better
patch maintainability.

* chore: bump chromium in DEPS to 110.0.5481.0

* chore: update patch indicies

* 4098946: Migrate Extension::Create() argument to base::Value::Dict (part 4 of 4)

https://chromium-review.googlesource.com/c/chromium/src/+/4098946

* chore: bump chromium in DEPS to 111.0.5482.0

* chore: bump chromium in DEPS to 111.0.5484.0

* chore: bump chromium in DEPS to 111.0.5486.0

* chore: update patch indices

* 4112903: Reland "Move gl::FrameData to gfx::FrameData"

https://chromium-review.googlesource.com/c/chromium/src/+/4112903

* 4056216: Option to create a tab target with Target.createTarget in /json/new

https://chromium-review.googlesource.com/c/chromium/src/+/4056216

* chore: bump chromium in DEPS to 111.0.5488.0

* chore: bump chromium in DEPS to 111.0.5490.0

* chore: bump chromium in DEPS to 111.0.5492.0

* chore: bump chromium in DEPS to 111.0.5494.0

* chore: bump chromium in DEPS to 111.0.5496.0

* chore: bump chromium in DEPS to 111.0.5498.0

* chore: bump chromium in DEPS to 111.0.5500.0

* chore: bump chromium in DEPS to 111.0.5502.0

* chore: update patch indices

+ small update to printing.patch due to:
3653941: [printing] Extract settings logic from PrintJobWorker
https://chromium-review.googlesource.com/c/chromium/src/+/3653941

* 4113994: Cleanup: Rename webui_generated_resources_* to webui_resources_*.
https://chromium-review.googlesource.com/c/chromium/src/+/4113994

* 4112537: Remove DictionaryPrefUpdate.
https://chromium-review.googlesource.com/c/chromium/src/+/4112537

* 4072073: Remove //chrome/browser/ash dependency from pdf_extension_util.cc
https://chromium-review.googlesource.com/c/chromium/src/+/4072073

* 4055223: [Remove FileSystemConnector] Remove DownloadItemRerouteInfo
https://chromium-review.googlesource.com/c/chromium/src/+/4055223

* Migrate base::DictionaryValue to base::Value::Dict

This relates to multiple CLs cleaning up this class in this roll,
but the ones that are specifically relevant here:

4116096: [CodeHealth] Remove DictionaryValue::GetInteger
https://chromium-review.googlesource.com/c/chromium/src/+/4116096

4113764: [CodeHealth] Remove deprecated DictionaryValue::SetInteger()
https://chromium-review.googlesource.com/c/chromium/src/+/4113764

* 3653941: [printing] Extract settings logic from PrintJobWorker
https://chromium-review.googlesource.com/c/chromium/src/+/3653941

* chore: bump chromium in DEPS to 111.0.5504.0

* chore: update patches

* (WIP) 4003663: Enable Microtask queues per WindowAgent.

https://chromium-review.googlesource.com/c/chromium/src/+/4003663

* chore: bump chromium in DEPS to 111.0.5506.0

* chore: update patches

* fix: printing patch

It was complaining that this method isn't used on windows

* chore: bump chromium in DEPS to 111.0.5508.0

* chore: bump chromium in DEPS to 111.0.5510.0

* chore: bump chromium in DEPS to 111.0.5512.0

* chore: bump chromium in DEPS to 111.0.5514.0

* chore: bump chromium in DEPS to 111.0.5516.0

* chore: update patches

* chore: bump chromium in DEPS to 111.0.5518.0

* chore: update patches

* 4027428: [rsafor] Move rsaFor requests to a separate permission

https://chromium-review.googlesource.com/c/chromium/src/+/4027428

* Revert "(WIP) 4003663: Enable Microtask queues per WindowAgent."

This reverts commit cc36d226e3f3fe5f4bea6538102d55ce3203190f.

* chore: disable Microtask queues per WindowAgent.

see https://chromium-review.googlesource.com/c/chromium/src/+/4003663

* chore: cleanup after rebase

* fixup: disable Microtask queues per WindowAgent.

* chore: cleanup password from keychain after test

Co-authored-by: electron-roller[bot] <84116207+electron-roller[bot]@users.noreply.github.com>
Co-authored-by: John Kleinschmidt <jkleinsc@electronjs.org>
Co-authored-by: Jeremy Rose <jeremya@chromium.org>
Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
Co-authored-by: Calvin Watford <cwatford@slack-corp.com>
Co-authored-by: clavin <clavin@electronjs.org>
2023-01-05 21:35:34 -05:00

227 lines
8.6 KiB
C++

// 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.
#include "shell/browser/extensions/electron_extension_loader.h"
#include <utility>
#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/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "extensions/browser/extension_file_task_runner.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/pref_names.h"
#include "extensions/common/error_utils.h"
#include "extensions/common/file_util.h"
#include "extensions/common/manifest_constants.h"
namespace extensions {
using LoadErrorBehavior = ExtensionRegistrar::LoadErrorBehavior;
namespace {
std::pair<scoped_refptr<const Extension>, std::string> LoadUnpacked(
const base::FilePath& extension_dir,
int load_flags) {
// 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)) {
std::string err = "Extension directory not found: " +
base::UTF16ToUTF8(extension_dir.LossyDisplayName());
return std::make_pair(nullptr, err);
}
// remove _metadata folder. Otherwise, the following warning will be thrown
// Cannot load extension with file or directory name _metadata.
// Filenames starting with "_" are reserved for use by the system.
// see: https://bugs.chromium.org/p/chromium/issues/detail?id=377278
base::FilePath metadata_dir = extension_dir.Append(kMetadataFolder);
if (base::DirectoryExists(metadata_dir)) {
base::DeletePathRecursively(metadata_dir);
}
std::string load_error;
scoped_refptr<Extension> extension = file_util::LoadExtension(
extension_dir, extensions::mojom::ManifestLocation::kCommandLine,
load_flags, &load_error);
if (!extension.get()) {
std::string err = "Loading extension at " +
base::UTF16ToUTF8(extension_dir.LossyDisplayName()) +
" failed with: " + load_error;
return std::make_pair(nullptr, err);
}
std::string warnings;
// Log warnings.
if (!extension->install_warnings().empty()) {
std::string warning_prefix =
"Warnings loading extension at " +
base::UTF16ToUTF8(extension_dir.LossyDisplayName());
for (const auto& warning : extension->install_warnings()) {
std::string unrecognized_manifest_error = ErrorUtils::FormatErrorMessage(
manifest_errors::kUnrecognizedManifestKey, warning.key);
if (warning.message == unrecognized_manifest_error) {
// filter kUnrecognizedManifestKey error. This error does not have any
// impact e.g: Unrecognized manifest key 'minimum_chrome_version' etc.
LOG(WARNING) << warning_prefix << ": " << warning.message;
} else {
warnings += " " + warning.message + "\n";
}
}
if (warnings != "") {
warnings = warning_prefix + ":\n" + warnings;
}
}
return std::make_pair(extension, warnings);
}
} // namespace
ElectronExtensionLoader::ElectronExtensionLoader(
content::BrowserContext* browser_context)
: browser_context_(browser_context),
extension_registrar_(browser_context, this) {}
ElectronExtensionLoader::~ElectronExtensionLoader() = default;
void ElectronExtensionLoader::LoadExtension(
const base::FilePath& extension_dir,
int load_flags,
base::OnceCallback<void(const Extension*, const std::string&)> cb) {
GetExtensionFileTaskRunner()->PostTaskAndReplyWithResult(
FROM_HERE, base::BindOnce(&LoadUnpacked, extension_dir, load_flags),
base::BindOnce(&ElectronExtensionLoader::FinishExtensionLoad,
weak_factory_.GetWeakPtr(), std::move(cb)));
}
void ElectronExtensionLoader::ReloadExtension(const ExtensionId& extension_id) {
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;
}
void ElectronExtensionLoader::UnloadExtension(
const ExtensionId& extension_id,
extensions::UnloadedExtensionReason reason) {
extension_registrar_.RemoveExtension(extension_id, reason);
}
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;
if (extension) {
extension_registrar_.AddExtension(extension);
// 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()));
}
}
std::move(cb).Run(extension.get(), result.second);
}
void ElectronExtensionLoader::FinishExtensionReload(
const ExtensionId& old_extension_id,
std::pair<scoped_refptr<const Extension>, std::string> result) {
scoped_refptr<const Extension> extension = result.first;
if (extension) {
extension_registrar_.AddExtension(extension);
}
}
void ElectronExtensionLoader::PreAddExtension(const Extension* extension,
const Extension* old_extension) {
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());
}
}
}
void ElectronExtensionLoader::PostActivateExtension(
scoped_refptr<const Extension> extension) {}
void ElectronExtensionLoader::PostDeactivateExtension(
scoped_refptr<const Extension> extension) {}
void ElectronExtensionLoader::LoadExtensionForReload(
const ExtensionId& extension_id,
const base::FilePath& path,
LoadErrorBehavior load_error_behavior) {
CHECK(!path.empty());
// 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;
GetExtensionFileTaskRunner()->PostTaskAndReplyWithResult(
FROM_HERE, base::BindOnce(&LoadUnpacked, path, load_flags),
base::BindOnce(&ElectronExtensionLoader::FinishExtensionReload,
weak_factory_.GetWeakPtr(), extension_id));
did_schedule_reload_ = true;
}
bool ElectronExtensionLoader::CanEnableExtension(const Extension* extension) {
return true;
}
bool ElectronExtensionLoader::CanDisableExtension(const Extension* extension) {
// Extensions cannot be disabled by the user.
return false;
}
bool ElectronExtensionLoader::ShouldBlockExtension(const Extension* extension) {
return false;
}
} // namespace extensions