Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Unified Diff: extensions/browser/extensions_test.cc

Issue 2802433004: ExtensionsTest: Move initialization to SetUp and avoid potential UAF. (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: extensions/browser/extensions_test.cc
diff --git a/extensions/browser/extensions_test.cc b/extensions/browser/extensions_test.cc
index 4935dd98b4691fd07b79b4697d626362ee2daf8c..21415103d40cacda41f0fbeaaaf0a6bb5f601da4 100644
--- a/extensions/browser/extensions_test.cc
+++ b/extensions/browser/extensions_test.cc
@@ -4,6 +4,7 @@
#include "extensions/browser/extensions_test.h"
+#include "base/memory/ptr_util.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service_factory.h"
@@ -31,24 +32,18 @@ std::unique_ptr<content::TestBrowserContext> CreateTestIncognitoContext() {
namespace extensions {
-// This class does work in the constructor instead of SetUp() to give subclasses
-// a valid BrowserContext to use while initializing their members. For example:
-//
-// class MyExtensionsTest : public ExtensionsTest {
-// MyExtensionsTest()
-// : my_object_(browser_context())) {
-// }
-// };
-// TODO(crbug.com/708256): All these instances are setup in the constructor, but
-// destroyed in TearDown(), which may cause problems. Move this initialization
-// to SetUp().
-ExtensionsTest::ExtensionsTest()
- : content_browser_client_(new TestContentBrowserClient),
- content_utility_client_(new TestContentUtilityClient),
- browser_context_(new content::TestBrowserContext),
- incognito_context_(CreateTestIncognitoContext()),
- extensions_browser_client_(
- new TestExtensionsBrowserClient(browser_context_.get())) {
+ExtensionsTest::ExtensionsTest() {}
+
+ExtensionsTest::~ExtensionsTest() {}
+
+void ExtensionsTest::SetUp() {
+ content_browser_client_ = base::MakeUnique<TestContentBrowserClient>();
+ content_utility_client_ = base::MakeUnique<TestContentUtilityClient>();
+ browser_context_ = base::MakeUnique<content::TestBrowserContext>();
+ incognito_context_ = CreateTestIncognitoContext();
+ extensions_browser_client_ =
+ base::MakeUnique<TestExtensionsBrowserClient>(browser_context_.get());
+
content::SetBrowserClientForTesting(content_browser_client_.get());
content::SetUtilityClientForTesting(content_utility_client_.get());
ExtensionsBrowserClient::Set(extensions_browser_client_.get());
@@ -75,15 +70,7 @@ ExtensionsTest::ExtensionsTest()
ExtensionPrefsFactory::GetInstance()->SetInstanceForTesting(
browser_context(), std::move(extension_prefs));
-}
-ExtensionsTest::~ExtensionsTest() {
- ExtensionsBrowserClient::Set(nullptr);
- content::SetBrowserClientForTesting(nullptr);
- content::SetUtilityClientForTesting(nullptr);
-}
-
-void ExtensionsTest::SetUp() {
// Crashing here? Don't use this class in Chrome's unit_tests. See header.
BrowserContextDependencyManager::GetInstance()
->CreateBrowserContextServicesForTest(browser_context_.get());
@@ -96,13 +83,14 @@ void ExtensionsTest::TearDown() {
BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
browser_context_.get());
- // TODO(crbug.com/708256): |extension_browser_client_| is reset here but not
- // unset as the singleton until the destructor. This can lead to use after
- // free errors.
extensions_browser_client_.reset();
browser_context_.reset();
incognito_context_.reset();
pref_service_.reset();
+
+ ExtensionsBrowserClient::Set(nullptr);
+ content::SetBrowserClientForTesting(nullptr);
+ content::SetUtilityClientForTesting(nullptr);
}
} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698