1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/test_extension_system.h"
#include <memory>
#include <utility>
#include "base/command_line.h"
#include "base/memory/ptr_util.h"
#include "base/memory/scoped_refptr.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/extensions/blocklist.h"
#include "chrome/browser/extensions/chrome_extension_registrar_delegate.h"
#include "chrome/browser/extensions/crx_installer.h"
#include "chrome/browser/extensions/cws_info_service.h"
#include "chrome/browser/extensions/cws_info_service_factory.h"
#include "chrome/browser/extensions/extension_error_controller.h"
#include "chrome/browser/extensions/extension_management.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/external_provider_manager.h"
#include "chrome/browser/extensions/shared_module_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_switches.h"
#include "components/prefs/pref_service.h"
#include "components/services/unzip/content/unzip_service.h"
#include "components/services/unzip/in_process_unzipper.h"
#include "components/value_store/test_value_store_factory.h"
#include "components/value_store/testing_value_store.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/browser/management_policy.h"
#include "extensions/browser/quota_service.h"
#include "extensions/browser/state_store.h"
#include "extensions/browser/user_script_manager.h"
#include "services/data_decoder/data_decoder_service.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "components/user_manager/fake_user_manager.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/user_manager/user_manager.h"
#endif
#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "chrome/browser/extensions/chrome_app_sorting.h"
#else
#include "extensions/browser/null_app_sorting.h"
#endif
using content::BrowserThread;
namespace extensions {
namespace {
// A fake CWSInfoService for tests that utilize the test extension system and
// service infrastructure but do not depend on the actual functionality of the
// service.
class FakeCWSInfoService : public CWSInfoService {
public:
explicit FakeCWSInfoService(Profile* profile) {}
explicit FakeCWSInfoService(const CWSInfoService&) = delete;
FakeCWSInfoService& operator=(const CWSInfoService&) = delete;
~FakeCWSInfoService() override = default;
// CWSInfoServiceInterface:
std::optional<bool> IsLiveInCWS(const Extension& extension) const override;
std::optional<CWSInfo> GetCWSInfo(const Extension& extension) const override;
void CheckAndMaybeFetchInfo() override {}
void AddObserver(Observer* observer) override {}
void RemoveObserver(Observer* observer) override {}
// KeyedService:
// Ensure that the keyed service shutdown is a no-op.
void Shutdown() override {}
};
std::optional<bool> FakeCWSInfoService::IsLiveInCWS(
const Extension& extension) const {
return true;
}
std::optional<CWSInfoServiceInterface::CWSInfo> FakeCWSInfoService::GetCWSInfo(
const Extension& extension) const {
return CWSInfoServiceInterface::CWSInfo();
}
std::unique_ptr<KeyedService> BuildFakeCWSService(
content::BrowserContext* context) {
return std::make_unique<FakeCWSInfoService>(
Profile::FromBrowserContext(context));
}
} // namespace
TestExtensionSystem::InitParams::InitParams() = default;
TestExtensionSystem::InitParams::InitParams(
base::CommandLine* command_line,
base::FilePath install_directory,
base::FilePath unpacked_install_directory,
bool autoupdate_enabled,
bool enable_extensions)
: command_line(command_line),
install_directory(install_directory),
unpacked_install_directory(unpacked_install_directory),
autoupdate_enabled(autoupdate_enabled),
enable_extensions(enable_extensions) {}
TestExtensionSystem::InitParams::~InitParams() = default;
TestExtensionSystem::TestExtensionSystem(Profile* profile)
: profile_(profile),
store_factory_(
base::MakeRefCounted<value_store::TestValueStoreFactory>()),
state_store_(std::make_unique<StateStore>(profile_,
store_factory_,
StateStore::BackendType::RULES,
false)),
management_policy_(std::make_unique<ManagementPolicy>()),
#if BUILDFLAG(ENABLE_EXTENSIONS)
app_sorting_(std::make_unique<ChromeAppSorting>(profile_)),
#else
app_sorting_(std::make_unique<NullAppSorting>()),
#endif
quota_service_(std::make_unique<QuotaService>()) {
management_policy_->RegisterProviders(
ExtensionManagementFactory::GetForBrowserContext(profile_)
->GetProviders());
}
TestExtensionSystem::~TestExtensionSystem() = default;
void TestExtensionSystem::Shutdown() {
if (extension_service_) {
extension_service_->Shutdown();
}
in_process_data_decoder_.reset();
}
void TestExtensionSystem::Init() {
Init(InitParams());
}
void TestExtensionSystem::Init(const InitParams& params) {
base::CommandLine* command_line =
params.command_line ? params.command_line.get()
: base::CommandLine::ForCurrentProcess();
base::FilePath install_directory = params.install_directory.value_or(
profile_->GetPath().AppendASCII(kInstallDirectoryName));
base::FilePath unpacked_install_directory =
params.unpacked_install_directory.value_or(
profile_->GetPath().AppendASCII(kUnpackedInstallDirectoryName));
CreateExtensionService(command_line, install_directory,
unpacked_install_directory, params.autoupdate_enabled,
params.enable_extensions);
}
ExtensionService* TestExtensionSystem::CreateExtensionService(
const base::CommandLine* command_line,
const base::FilePath& install_directory,
bool autoupdate_enabled,
bool extensions_enabled) {
return CreateExtensionService(command_line, install_directory,
base::FilePath(), autoupdate_enabled,
extensions_enabled);
}
ExtensionService* TestExtensionSystem::CreateExtensionService(
const base::CommandLine* command_line,
const base::FilePath& install_directory,
const base::FilePath& unpacked_install_directory,
bool autoupdate_enabled,
bool extensions_enabled) {
if (CWSInfoService::Get(profile_) == nullptr) {
Profile* profile = profile_;
#if BUILDFLAG(IS_CHROMEOS)
// TODO(crbug.com/40891982): Refactor this convenience upstream to test
// callers. Possibly just BuiltInAppTest.BuildGuestMode.
if (profile_->IsGuestSession()) {
profile = profile_->GetOriginalProfile();
}
#endif // BUILDFLAG(IS_CHROMEOS)
// Associate a dummy CWSInfoService with this profile if necessary.
CWSInfoServiceFactory::GetInstance()->SetTestingFactory(
profile, base::BindRepeating(&BuildFakeCWSService));
}
extension_service_ = std::make_unique<ExtensionService>(
profile_, command_line, install_directory, unpacked_install_directory,
ExtensionPrefs::Get(profile_), Blocklist::Get(profile_),
ExtensionErrorController::Get(profile_), autoupdate_enabled,
extensions_enabled, &ready_);
unzip::SetUnzipperLaunchOverrideForTesting(
base::BindRepeating(&unzip::LaunchInProcessUnzipper));
in_process_data_decoder_ =
std::make_unique<data_decoder::test::InProcessDataDecoder>();
ExternalProviderManager::Get(profile_)->ClearProvidersForTesting();
return extension_service_.get();
}
void TestExtensionSystem::CreateUserScriptManager() {
user_script_manager_ = std::make_unique<UserScriptManager>(profile_);
}
ExtensionService* TestExtensionSystem::extension_service() {
return extension_service_.get();
}
ManagementPolicy* TestExtensionSystem::management_policy() {
return management_policy_.get();
}
ServiceWorkerManager* TestExtensionSystem::service_worker_manager() {
return nullptr;
}
UserScriptManager* TestExtensionSystem::user_script_manager() {
return user_script_manager_.get();
}
StateStore* TestExtensionSystem::state_store() {
return state_store_.get();
}
StateStore* TestExtensionSystem::rules_store() {
return state_store_.get();
}
StateStore* TestExtensionSystem::dynamic_user_scripts_store() {
return state_store_.get();
}
scoped_refptr<value_store::ValueStoreFactory>
TestExtensionSystem::store_factory() {
return store_factory_;
}
QuotaService* TestExtensionSystem::quota_service() {
return quota_service_.get();
}
AppSorting* TestExtensionSystem::app_sorting() {
return app_sorting_.get();
}
const base::OneShotEvent& TestExtensionSystem::ready() const {
return ready_;
}
bool TestExtensionSystem::is_ready() const {
return ready_.is_signaled();
}
ContentVerifier* TestExtensionSystem::content_verifier() {
return content_verifier_.get();
}
std::unique_ptr<ExtensionSet> TestExtensionSystem::GetDependentExtensions(
const Extension* extension) {
return SharedModuleService::Get(profile_)->GetDependentExtensions(extension);
}
void TestExtensionSystem::InstallUpdate(
const std::string& extension_id,
const std::string& public_key,
const base::FilePath& temp_dir,
bool install_immediately,
InstallUpdateCallback install_update_callback) {
NOTREACHED();
}
void TestExtensionSystem::PerformActionBasedOnOmahaAttributes(
const std::string& extension_id,
const base::Value::Dict& attributes) {}
value_store::TestingValueStore* TestExtensionSystem::value_store() {
// These tests use TestingValueStore in a way that ensures it only ever mints
// instances of TestingValueStore.
return static_cast<value_store::TestingValueStore*>(
store_factory_->LastCreatedStore());
}
// static
std::unique_ptr<KeyedService> TestExtensionSystem::Build(
content::BrowserContext* profile) {
return base::WrapUnique(
new TestExtensionSystem(static_cast<Profile*>(profile)));
}
void TestExtensionSystem::RecreateAppSorting() {
#if BUILDFLAG(ENABLE_EXTENSIONS)
app_sorting_ = std::make_unique<ChromeAppSorting>(profile_);
#else
app_sorting_ = std::make_unique<NullAppSorting>();
#endif
}
} // namespace extensions
|