This source file includes following definitions.
- SetTestingFactory
- SetTestingFactoryAndUse
- RefcountedBrowserContextKeyedServiceFactory
- RefcountedBrowserContextKeyedServiceFactory
- GetServiceForBrowserContext
- Associate
- BrowserContextShutdown
- BrowserContextDestroyed
- SetEmptyTestingFactory
- CreateServiceNow
#include "components/keyed_service/content/refcounted_browser_context_keyed_service_factory.h"
#include "base/logging.h"
#include "base/stl_util.h"
#include "components/keyed_service/content/refcounted_browser_context_keyed_service.h"
#include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/browser_context.h"
void RefcountedBrowserContextKeyedServiceFactory::SetTestingFactory(
content::BrowserContext* context,
TestingFactoryFunction testing_factory) {
bool add_context = ArePreferencesSetOn(context);
BrowserContextShutdown(context);
BrowserContextDestroyed(context);
if (add_context)
MarkPreferencesSetOn(context);
testing_factories_[context] = testing_factory;
}
scoped_refptr<RefcountedBrowserContextKeyedService>
RefcountedBrowserContextKeyedServiceFactory::SetTestingFactoryAndUse(
content::BrowserContext* context,
TestingFactoryFunction testing_factory) {
DCHECK(testing_factory);
SetTestingFactory(context, testing_factory);
return GetServiceForBrowserContext(context, true);
}
RefcountedBrowserContextKeyedServiceFactory::
RefcountedBrowserContextKeyedServiceFactory(
const char* name,
BrowserContextDependencyManager* manager)
: BrowserContextKeyedBaseFactory(name, manager) {}
RefcountedBrowserContextKeyedServiceFactory::
~RefcountedBrowserContextKeyedServiceFactory() {
DCHECK(mapping_.empty());
}
scoped_refptr<RefcountedBrowserContextKeyedService>
RefcountedBrowserContextKeyedServiceFactory::GetServiceForBrowserContext(
content::BrowserContext* context,
bool create) {
context = GetBrowserContextToUse(context);
if (!context)
return NULL;
RefCountedStorage::const_iterator it = mapping_.find(context);
if (it != mapping_.end())
return it->second;
if (!create)
return NULL;
scoped_refptr<RefcountedBrowserContextKeyedService> service;
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 RefcountedBrowserContextKeyedServiceFactory::Associate(
content::BrowserContext* context,
const scoped_refptr<RefcountedBrowserContextKeyedService>& service) {
DCHECK(!ContainsKey(mapping_, context));
mapping_.insert(std::make_pair(context, service));
}
void RefcountedBrowserContextKeyedServiceFactory::BrowserContextShutdown(
content::BrowserContext* context) {
RefCountedStorage::iterator it = mapping_.find(context);
if (it != mapping_.end() && it->second.get())
it->second->ShutdownOnUIThread();
}
void RefcountedBrowserContextKeyedServiceFactory::BrowserContextDestroyed(
content::BrowserContext* context) {
mapping_.erase(context);
testing_factories_.erase(context);
BrowserContextKeyedBaseFactory::BrowserContextDestroyed(context);
}
void RefcountedBrowserContextKeyedServiceFactory::SetEmptyTestingFactory(
content::BrowserContext* context) {
SetTestingFactory(context, NULL);
}
void RefcountedBrowserContextKeyedServiceFactory::CreateServiceNow(
content::BrowserContext* context) {
GetServiceForBrowserContext(context, true);
}