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

Unified Diff: components/keyed_service/core/keyed_service_factory.cc

Issue 654753010: Refactor (Refcounted)BrowserContextKeyedServiceFactory (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments Created 6 years, 2 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: components/keyed_service/core/keyed_service_factory.cc
diff --git a/components/keyed_service/content/browser_context_keyed_service_factory.cc b/components/keyed_service/core/keyed_service_factory.cc
similarity index 50%
copy from components/keyed_service/content/browser_context_keyed_service_factory.cc
copy to components/keyed_service/core/keyed_service_factory.cc
index a33ffedbe48a4bc330dfd5d28c16aa7af364288e..261ff29d556c319e143b8fe9e6646c77cf13a29d 100644
--- a/components/keyed_service/content/browser_context_keyed_service_factory.cc
+++ b/components/keyed_service/core/keyed_service_factory.cc
@@ -2,18 +2,24 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
-
-#include <map>
+#include "components/keyed_service/core/keyed_service_factory.h"
#include "base/logging.h"
#include "base/stl_util.h"
-#include "components/keyed_service/content/browser_context_dependency_manager.h"
+#include "components/keyed_service/core/dependency_manager.h"
#include "components/keyed_service/core/keyed_service.h"
-#include "content/public/browser/browser_context.h"
-void BrowserContextKeyedServiceFactory::SetTestingFactory(
- content::BrowserContext* context,
+KeyedServiceFactory::KeyedServiceFactory(const char* name,
+ DependencyManager* manager)
+ : KeyedServiceBaseFactory(name, manager) {
+}
+
+KeyedServiceFactory::~KeyedServiceFactory() {
+ DCHECK(mapping_.empty());
+}
+
+void KeyedServiceFactory::SetTestingFactory(
+ base::SupportsUserData* context,
TestingFactoryFunction testing_factory) {
// Destroying the context may cause us to lose data about whether |context|
// has our preferences registered on it (since the context object itself
@@ -32,8 +38,8 @@ void BrowserContextKeyedServiceFactory::SetTestingFactory(
// We have to go through the shutdown and destroy mechanisms because there
// are unit tests that create a service on a context and then change the
// testing service mid-test.
- BrowserContextShutdown(context);
- BrowserContextDestroyed(context);
+ ContextShutdown(context);
+ ContextDestroyed(context);
if (add_context)
MarkPreferencesSetOn(context);
@@ -41,50 +47,40 @@ void BrowserContextKeyedServiceFactory::SetTestingFactory(
testing_factories_[context] = testing_factory;
}
-KeyedService* BrowserContextKeyedServiceFactory::SetTestingFactoryAndUse(
- content::BrowserContext* context,
+KeyedService* KeyedServiceFactory::SetTestingFactoryAndUse(
+ base::SupportsUserData* context,
TestingFactoryFunction testing_factory) {
DCHECK(testing_factory);
SetTestingFactory(context, testing_factory);
- return GetServiceForBrowserContext(context, true);
-}
-
-BrowserContextKeyedServiceFactory::BrowserContextKeyedServiceFactory(
- const char* name,
- BrowserContextDependencyManager* manager)
- : BrowserContextKeyedBaseFactory(name, manager) {}
-
-BrowserContextKeyedServiceFactory::~BrowserContextKeyedServiceFactory() {
- DCHECK(mapping_.empty());
+ return GetServiceForContext(context, true);
}
-KeyedService* BrowserContextKeyedServiceFactory::GetServiceForBrowserContext(
- content::BrowserContext* context,
+KeyedService* KeyedServiceFactory::GetServiceForContext(
+ base::SupportsUserData* context,
bool create) {
- context = GetBrowserContextToUse(context);
+ context = GetContextToUse(context);
if (!context)
- return NULL;
+ return nullptr;
// NOTE: If you modify any of the logic below, make sure to update the
// refcounted version in refcounted_context_keyed_service_factory.cc!
- BrowserContextKeyedServices::const_iterator it = mapping_.find(context);
+ const auto& it = mapping_.find(context);
if (it != mapping_.end())
return it->second;
// Object not found.
if (!create)
- return NULL; // And we're forbidden from creating one.
+ return nullptr; // And we're forbidden from creating one.
// Create new object.
- // Check to see if we have a per-BrowserContext testing factory that we should
- // use instead of default behavior.
- KeyedService* service = NULL;
- BrowserContextOverriddenTestingFunctions::const_iterator jt =
- testing_factories_.find(context);
+ // Check to see if we have a per-context testing factory that we should use
+ // instead of default behavior.
+ KeyedService* service = nullptr;
+ const auto& jt = testing_factories_.find(context);
if (jt != testing_factories_.end()) {
if (jt->second) {
- if (!context->IsOffTheRecord())
- RegisterUserPrefsOnBrowserContextForTest(context);
+ if (!IsOffTheRecord(context))
+ RegisterUserPrefsOnContextForTest(context);
service = jt->second(context);
}
} else {
@@ -95,31 +91,27 @@ KeyedService* BrowserContextKeyedServiceFactory::GetServiceForBrowserContext(
return service;
}
-void BrowserContextKeyedServiceFactory::Associate(
- content::BrowserContext* context,
- KeyedService* service) {
+void KeyedServiceFactory::Associate(base::SupportsUserData* context,
+ KeyedService* service) {
DCHECK(!ContainsKey(mapping_, context));
mapping_.insert(std::make_pair(context, service));
}
-void BrowserContextKeyedServiceFactory::Disassociate(
- content::BrowserContext* context) {
- BrowserContextKeyedServices::iterator it = mapping_.find(context);
+void KeyedServiceFactory::Disassociate(base::SupportsUserData* context) {
+ const auto& it = mapping_.find(context);
if (it != mapping_.end()) {
delete it->second;
mapping_.erase(it);
}
}
-void BrowserContextKeyedServiceFactory::BrowserContextShutdown(
- content::BrowserContext* context) {
- BrowserContextKeyedServices::iterator it = mapping_.find(context);
+void KeyedServiceFactory::ContextShutdown(base::SupportsUserData* context) {
+ const auto& it = mapping_.find(context);
if (it != mapping_.end() && it->second)
it->second->Shutdown();
}
-void BrowserContextKeyedServiceFactory::BrowserContextDestroyed(
- content::BrowserContext* context) {
+void KeyedServiceFactory::ContextDestroyed(base::SupportsUserData* context) {
Disassociate(context);
// For unit tests, we also remove the factory function both so we don't
@@ -128,20 +120,18 @@ void BrowserContextKeyedServiceFactory::BrowserContextDestroyed(
// in this file).
testing_factories_.erase(context);
- BrowserContextKeyedBaseFactory::BrowserContextDestroyed(context);
+ KeyedServiceBaseFactory::ContextDestroyed(context);
}
-void BrowserContextKeyedServiceFactory::SetEmptyTestingFactory(
- content::BrowserContext* context) {
- SetTestingFactory(context, NULL);
+void KeyedServiceFactory::SetEmptyTestingFactory(
+ base::SupportsUserData* context) {
+ SetTestingFactory(context, nullptr);
}
-bool BrowserContextKeyedServiceFactory::HasTestingFactory(
- content::BrowserContext* context) {
+bool KeyedServiceFactory::HasTestingFactory(base::SupportsUserData* context) {
return testing_factories_.find(context) != testing_factories_.end();
}
-void BrowserContextKeyedServiceFactory::CreateServiceNow(
- content::BrowserContext* context) {
- GetServiceForBrowserContext(context, true);
+void KeyedServiceFactory::CreateServiceNow(base::SupportsUserData* context) {
+ GetServiceForContext(context, true);
}

Powered by Google App Engine
This is Rietveld 408576698