This source file includes following definitions.
- SetTestingFactory
- SetTestingFactoryAndUse
- GetServiceForBrowserContext
- Associate
- Disassociate
- BrowserContextShutdown
- BrowserContextDestroyed
- SetEmptyTestingFactory
- CreateServiceNow
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
#include <map>
#include "base/logging.h"
#include "base/stl_util.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/browser_context.h"
void BrowserContextKeyedServiceFactory::SetTestingFactory(
content::BrowserContext* context,
TestingFactoryFunction testing_factory) {
bool add_context = ArePreferencesSetOn(context);
#ifndef NDEBUG
dependency_manager_->MarkBrowserContextLiveForTesting(context);
#endif
BrowserContextShutdown(context);
BrowserContextDestroyed(context);
if (add_context)
MarkPreferencesSetOn(context);
testing_factories_[context] = testing_factory;
}
KeyedService* BrowserContextKeyedServiceFactory::SetTestingFactoryAndUse(
content::BrowserContext* 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());
}
KeyedService* BrowserContextKeyedServiceFactory::GetServiceForBrowserContext(
content::BrowserContext* context,
bool create) {
context = GetBrowserContextToUse(context);
if (!context)
return NULL;
BrowserContextKeyedServices::const_iterator it = mapping_.find(context);
if (it != mapping_.end())
return it->second;
if (!create)
return NULL;
KeyedService* service = NULL;
BrowserContextOverriddenTestingFunctions::const_iterator jt =
testing_factories_.find(context);
if (jt != testing_factories_.end()) {
if (jt->second) {
if (!context->IsOffTheRecord())
RegisterUserPrefsOnBrowserContextForTest(context);
service = jt->second(context);
}
} else {
service = BuildServiceInstanceFor(context);
}
Associate(context, service);
return service;
}
void BrowserContextKeyedServiceFactory::Associate(
content::BrowserContext* 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);
if (it != mapping_.end()) {
delete it->second;
mapping_.erase(it);
}
}
void BrowserContextKeyedServiceFactory::BrowserContextShutdown(
content::BrowserContext* context) {
BrowserContextKeyedServices::iterator it = mapping_.find(context);
if (it != mapping_.end() && it->second)
it->second->Shutdown();
}
void BrowserContextKeyedServiceFactory::BrowserContextDestroyed(
content::BrowserContext* context) {
Disassociate(context);
testing_factories_.erase(context);
BrowserContextKeyedBaseFactory::BrowserContextDestroyed(context);
}
void BrowserContextKeyedServiceFactory::SetEmptyTestingFactory(
content::BrowserContext* context) {
SetTestingFactory(context, NULL);
}
void BrowserContextKeyedServiceFactory::CreateServiceNow(
content::BrowserContext* context) {
GetServiceForBrowserContext(context, true);
}