* Add Web Discovery build flag * Remove obsolete kDontAskEnableWebDiscovery pref This legacy pref was only being registered and immediately cleared during migration. Remove it entirely. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
111 lines
3.9 KiB
C++
111 lines
3.9 KiB
C++
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
#include "brave/browser/extensions/brave_component_loader.h"
|
|
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
#include "base/check.h"
|
|
#include "base/check_op.h"
|
|
#include "base/command_line.h"
|
|
#include "base/feature_list.h"
|
|
#include "base/functional/bind.h"
|
|
#include "base/json/json_reader.h"
|
|
#include "brave/components/brave_extension/grit/brave_extension.h"
|
|
#include "brave/components/constants/brave_switches.h"
|
|
#include "brave/components/constants/pref_names.h"
|
|
#include "brave/components/web_discovery/buildflags/buildflags.h"
|
|
#include "chrome/browser/extensions/extension_service.h"
|
|
#include "chrome/browser/profiles/profile.h"
|
|
#include "components/prefs/pref_service.h"
|
|
#include "extensions/browser/extension_registry.h"
|
|
#include "extensions/browser/extension_system.h"
|
|
#include "extensions/common/constants.h"
|
|
#include "extensions/common/mojom/manifest.mojom.h"
|
|
#include "ui/base/resource/resource_bundle.h"
|
|
|
|
#if BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE)
|
|
#include "brave/components/web_discovery/common/features.h"
|
|
#endif
|
|
|
|
namespace extensions {
|
|
|
|
BraveComponentLoader::BraveComponentLoader(Profile* profile)
|
|
: ComponentLoader(profile),
|
|
profile_(profile),
|
|
profile_prefs_(profile->GetPrefs()) {
|
|
pref_change_registrar_.Init(profile_prefs_);
|
|
#if BUILDFLAG(ENABLE_WEB_DISCOVERY)
|
|
pref_change_registrar_.Add(
|
|
kWebDiscoveryEnabled,
|
|
base::BindRepeating(&BraveComponentLoader::UpdateBraveExtension,
|
|
base::Unretained(this)));
|
|
#endif
|
|
}
|
|
|
|
BraveComponentLoader::~BraveComponentLoader() = default;
|
|
|
|
void BraveComponentLoader::AddDefaultComponentExtensions(
|
|
bool skip_session_components) {
|
|
ComponentLoader::AddDefaultComponentExtensions(skip_session_components);
|
|
UpdateBraveExtension();
|
|
}
|
|
|
|
bool BraveComponentLoader::UseBraveExtensionBackgroundPage() {
|
|
#if BUILDFLAG(ENABLE_WEB_DISCOVERY)
|
|
bool native_enabled = false;
|
|
#if BUILDFLAG(ENABLE_WEB_DISCOVERY_NATIVE)
|
|
native_enabled = base::FeatureList::IsEnabled(
|
|
web_discovery::features::kBraveWebDiscoveryNative);
|
|
#endif
|
|
return !native_enabled && profile_prefs_->GetBoolean(kWebDiscoveryEnabled);
|
|
#else
|
|
return false;
|
|
#endif // BUILDFLAG(ENABLE_WEB_DISCOVERY)
|
|
}
|
|
|
|
void BraveComponentLoader::UpdateBraveExtension() {
|
|
const base::CommandLine& command_line =
|
|
*base::CommandLine::ForCurrentProcess();
|
|
if (command_line.HasSwitch(switches::kDisableBraveExtension)) {
|
|
return;
|
|
}
|
|
|
|
base::FilePath brave_extension_path(FILE_PATH_LITERAL(""));
|
|
brave_extension_path =
|
|
brave_extension_path.Append(FILE_PATH_LITERAL("brave_extension"));
|
|
auto& resource_bundle = ui::ResourceBundle::GetSharedInstance();
|
|
std::optional<base::DictValue> manifest = base::JSONReader::ReadDict(
|
|
resource_bundle.LoadDataResourceString(IDR_BRAVE_EXTENSION),
|
|
base::JSON_PARSE_CHROMIUM_EXTENSIONS);
|
|
CHECK(manifest) << "invalid Brave Extension manifest";
|
|
|
|
// The background page is a conditional. Replace MAYBE_background in the
|
|
// manifest to "background" or remove it.
|
|
auto background_value = manifest->Extract("MAYBE_background");
|
|
if (UseBraveExtensionBackgroundPage() && background_value) {
|
|
manifest->Set("background", std::move(*background_value));
|
|
}
|
|
|
|
extensions::ExtensionRegistry* registry =
|
|
extensions::ExtensionRegistry::Get(profile_);
|
|
const Extension* current_extension =
|
|
registry->GetInstalledExtension(brave_extension_id);
|
|
|
|
if (current_extension) {
|
|
const auto* current_manifest = current_extension->manifest();
|
|
if (current_manifest && *current_manifest->value() == *manifest) {
|
|
return; // Skip reload, nothing is actually changed.
|
|
}
|
|
Remove(brave_extension_id);
|
|
}
|
|
|
|
const auto id = Add(std::move(*manifest), brave_extension_path);
|
|
CHECK_EQ(id, brave_extension_id);
|
|
}
|
|
|
|
} // namespace extensions
|