| Index: components/browser_context_keyed_service/browser_context_keyed_service_factory.cc
|
| diff --git a/components/browser_context_keyed_service/browser_context_keyed_service_factory.cc b/components/browser_context_keyed_service/browser_context_keyed_service_factory.cc
|
| index 619318ef584b60c8f3e1bd148ef3c5b29bf0085d..8f96a0b4959768e43c857e7356c14b15997367fb 100644
|
| --- a/components/browser_context_keyed_service/browser_context_keyed_service_factory.cc
|
| +++ b/components/browser_context_keyed_service/browser_context_keyed_service_factory.cc
|
| @@ -14,6 +14,14 @@
|
|
|
| void BrowserContextKeyedServiceFactory::SetTestingFactory(
|
| content::BrowserContext* context, FactoryFunction factory) {
|
| + // Should not set a testing factory if we already have a global factory.
|
| + DCHECK(!have_global_factory_);
|
| + CleanupExistingServices(context);
|
| + factories_[context] = factory;
|
| +}
|
| +
|
| +void BrowserContextKeyedServiceFactory::CleanupExistingServices(
|
| + content::BrowserContext* context) {
|
| // Destroying the context may cause us to lose data about whether |context|
|
| // has our preferences registered on it (since the context object itself
|
| // isn't dead). See if we need to readd it once we've gone through normal
|
| @@ -28,8 +36,28 @@ void BrowserContextKeyedServiceFactory::SetTestingFactory(
|
|
|
| if (add_context)
|
| MarkPreferencesSetOn(context);
|
| +}
|
|
|
| - factories_[context] = factory;
|
| +void BrowserContextKeyedServiceFactory::SetGlobalTestingFactory(
|
| + FactoryFunction factory) {
|
| + // Should not call SetGlobalTestingFactory() if factories already exist or
|
| + // if any services have been created already for any profile.
|
| + DCHECK(factories_.empty());
|
| +
|
| + // Walk any existing contexts and clean up any services that have already
|
| + // been created.
|
| + while (!mapping_.empty()) {
|
| + BrowserContextKeyedServices::iterator it = mapping_.begin();
|
| + // Cleanup any existing services, and remove the service from |mapping_|.
|
| + CleanupExistingServices(it->first);
|
| + }
|
| +
|
| + global_factory_ = factory;
|
| + have_global_factory_ = true;
|
| +}
|
| +
|
| +void BrowserContextKeyedServiceFactory::ResetGlobalTestingFactory() {
|
| + have_global_factory_ = false;
|
| }
|
|
|
| BrowserContextKeyedService*
|
| @@ -43,7 +71,8 @@ BrowserContextKeyedServiceFactory::SetTestingFactoryAndUse(
|
|
|
| BrowserContextKeyedServiceFactory::BrowserContextKeyedServiceFactory(
|
| const char* name, BrowserContextDependencyManager* manager)
|
| - : BrowserContextKeyedBaseFactory(name, manager) {
|
| + : BrowserContextKeyedBaseFactory(name, manager),
|
| + have_global_factory_(false) {
|
| }
|
|
|
| BrowserContextKeyedServiceFactory::~BrowserContextKeyedServiceFactory() {
|
| @@ -72,13 +101,21 @@ BrowserContextKeyedServiceFactory::GetServiceForBrowserContext(
|
| // Check to see if we have a per-BrowserContext testing factory that we should
|
| // use instead of default behavior.
|
| BrowserContextKeyedService* service = NULL;
|
| - BrowserContextOverriddenFunctions::const_iterator jt =
|
| - factories_.find(context);
|
| - if (jt != factories_.end()) {
|
| - if (jt->second) {
|
| + bool have_factory = have_global_factory_;
|
| + FactoryFunction factory = global_factory_;
|
| + if (!have_factory) {
|
| + BrowserContextOverriddenFunctions::const_iterator jt =
|
| + factories_.find(context);
|
| + if (jt != factories_.end()) {
|
| + have_factory = true;
|
| + factory = jt->second;
|
| + }
|
| + }
|
| + if (have_factory) {
|
| + if (factory) {
|
| if (!context->IsOffTheRecord())
|
| RegisterUserPrefsOnBrowserContext(context);
|
| - service = jt->second(context);
|
| + service = factory(context);
|
| }
|
| } else {
|
| service = BuildServiceInstanceFor(context);
|
|
|