
This reverts commit88f92a1d90
. Reason for revert: Fix the build break Bug: 405411622 Original change's description: > Revert "[extensions] Remove ExtensionService::Enable/DisableExtension" > > This reverts commitd6d5be9789
. > > Reason for revert: Build break: https://ci.chromium.org/ui/p/chromium/builders/ci/android-desktop-x64-compile-rel/27664/overview > > ../../chrome/browser/extensions/chrome_content_verifier_delegate.cc:271:9: error: redefinition of 'registrar' > 271 | auto* registrar = ExtensionRegistrar::Get(context_); > > Bug: 405411622 > Original change's description: > > [extensions] Remove ExtensionService::Enable/DisableExtension > > > > Use ExtensionRegistrar version instead. > > > > Bug: 405411622 > > Change-Id: Ibf36f4a81740f0b2fd4feea703fb05e0555f2609 > > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6496507 > > Reviewed-by: David Bertoni <dbertoni@chromium.org> > > Commit-Queue: Achuith Bhandarkar <achuith@chromium.org> > > Cr-Commit-Position: refs/heads/main@{#1453052} > > Bug: 405411622 > No-Presubmit: true > No-Tree-Checks: true > No-Try: true > Change-Id: I9aff00a9135f96e25e214ff3fabeb07f8c88f859 > Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6497261 > Auto-Submit: S Ganesh <ganesh@chromium.org> > Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com> > Commit-Queue: Patti Lor <patricialor@chromium.org> > Owners-Override: Patti Lor <patricialor@chromium.org> > Cr-Commit-Position: refs/heads/main@{#1453056} Bug: 405411622 Change-Id: I2f02df5cdef57fc2093c71836e00d2be9efcefbb Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6496521 Reviewed-by: S Ganesh <ganesh@chromium.org> Owners-Override: Achuith Bhandarkar <achuith@chromium.org> Commit-Queue: Achuith Bhandarkar <achuith@chromium.org> Cr-Commit-Position: refs/heads/main@{#1453400}
135 lines
4.6 KiB
C++
135 lines
4.6 KiB
C++
// Copyright 2016 The Chromium Authors
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#include <memory>
|
|
|
|
#include "base/dcheck_is_on.h"
|
|
#include "base/functional/bind.h"
|
|
#include "base/memory/scoped_refptr.h"
|
|
#include "build/build_config.h"
|
|
#include "build/chromeos_buildflags.h"
|
|
#include "chrome/browser/extensions/extension_service_test_base.h"
|
|
#include "chrome/test/base/testing_browser_process.h"
|
|
#include "extensions/browser/extension_function.h"
|
|
#include "extensions/browser/extension_registrar.h"
|
|
#include "extensions/browser/extension_registry.h"
|
|
#include "extensions/common/extension_builder.h"
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
namespace extensions {
|
|
|
|
namespace {
|
|
|
|
void SuccessCallback(bool* did_respond,
|
|
ExtensionFunction::ResponseType type,
|
|
base::Value::List results,
|
|
const std::string& error,
|
|
mojom::ExtraResponseDataPtr) {
|
|
EXPECT_EQ(ExtensionFunction::ResponseType::kSucceeded, type);
|
|
*did_respond = true;
|
|
}
|
|
|
|
void FailCallback(bool* did_respond,
|
|
ExtensionFunction::ResponseType type,
|
|
base::Value::List results,
|
|
const std::string& error,
|
|
mojom::ExtraResponseDataPtr) {
|
|
EXPECT_EQ(ExtensionFunction::ResponseType::kFailed, type);
|
|
*did_respond = true;
|
|
}
|
|
|
|
class ValidationFunction : public ExtensionFunction {
|
|
public:
|
|
explicit ValidationFunction(bool should_succeed)
|
|
: should_succeed_(should_succeed), did_respond_(false) {
|
|
set_response_callback(base::BindOnce(
|
|
(should_succeed ? &SuccessCallback : &FailCallback), &did_respond_));
|
|
}
|
|
|
|
ResponseAction Run() override {
|
|
EXPECT_TRUE(should_succeed_);
|
|
return RespondNow(NoArguments());
|
|
}
|
|
|
|
bool did_respond() { return did_respond_; }
|
|
|
|
private:
|
|
~ValidationFunction() override = default;
|
|
bool should_succeed_;
|
|
bool did_respond_;
|
|
};
|
|
} // namespace
|
|
|
|
using ChromeExtensionFunctionUnitTest = ExtensionServiceTestBase;
|
|
|
|
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_CHROMEOS)
|
|
#define MAYBE_SimpleFunctionTest DISABLED_SimpleFunctionTest
|
|
#else
|
|
#define MAYBE_SimpleFunctionTest SimpleFunctionTest
|
|
#endif
|
|
TEST_F(ChromeExtensionFunctionUnitTest, MAYBE_SimpleFunctionTest) {
|
|
scoped_refptr<ValidationFunction> function(new ValidationFunction(true));
|
|
function->RunWithValidation().Execute();
|
|
EXPECT_TRUE(function->did_respond());
|
|
}
|
|
|
|
TEST_F(ChromeExtensionFunctionUnitTest, BrowserShutdownValidationFunctionTest) {
|
|
TestingBrowserProcess::GetGlobal()->SetShuttingDown(true);
|
|
scoped_refptr<ValidationFunction> function(new ValidationFunction(false));
|
|
function->RunWithValidation().Execute();
|
|
TestingBrowserProcess::GetGlobal()->SetShuttingDown(false);
|
|
EXPECT_TRUE(function->did_respond());
|
|
}
|
|
|
|
// Verifies that destroying the ExtensionFunction without responding is ok if
|
|
// the extension has been unloaded.
|
|
TEST_F(ChromeExtensionFunctionUnitTest, DestructionWithoutResponseOnUnload) {
|
|
InitializeEmptyExtensionService();
|
|
scoped_refptr<const Extension> extension = ExtensionBuilder("foo").Build();
|
|
registrar()->AddExtension(extension);
|
|
ASSERT_TRUE(registry()->enabled_extensions().Contains(extension->id()));
|
|
|
|
auto function = base::MakeRefCounted<ValidationFunction>(false);
|
|
function->set_extension(extension);
|
|
function->SetBrowserContextForTesting(browser_context());
|
|
|
|
registrar()->DisableExtension(extension->id(),
|
|
{disable_reason::DISABLE_USER_ACTION});
|
|
ASSERT_TRUE(registry()->disabled_extensions().Contains(extension->id()));
|
|
|
|
// Destroying the extension function without responding if the extension has
|
|
// been unloaded should not cause a crash.
|
|
function.reset();
|
|
}
|
|
|
|
#if DCHECK_IS_ON()
|
|
using ChromeExtensionFunctionDeathTest = ChromeExtensionFunctionUnitTest;
|
|
|
|
// Verify that destroying the extension function without responding causes a
|
|
// DCHECK failure.
|
|
#if BUILDFLAG(IS_WIN)
|
|
#define MAYBE_DestructionWithoutResponse DISABLED_DestructionWithoutResponse
|
|
#else
|
|
#define MAYBE_DestructionWithoutResponse DestructionWithoutResponse
|
|
#endif
|
|
TEST_F(ChromeExtensionFunctionDeathTest, MAYBE_DestructionWithoutResponse) {
|
|
ASSERT_DEATH(
|
|
{
|
|
InitializeEmptyExtensionService();
|
|
scoped_refptr<const Extension> extension =
|
|
ExtensionBuilder("foo").Build();
|
|
registrar()->AddExtension(extension);
|
|
|
|
ASSERT_TRUE(registry()->enabled_extensions().Contains(extension->id()));
|
|
|
|
auto function = base::MakeRefCounted<ValidationFunction>(false);
|
|
function->set_extension(extension);
|
|
function.reset();
|
|
},
|
|
"");
|
|
}
|
|
#endif // DCHECK_IS_ON()
|
|
|
|
} // namespace extensions
|