| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/browser_context_keyed_service/browser_context_keyed_service
_factory.h" | 5 #include "components/browser_context_keyed_service/browser_context_keyed_service
_factory.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "components/browser_context_keyed_service/browser_context_dependency_ma
nager.h" | 11 #include "components/browser_context_keyed_service/browser_context_dependency_ma
nager.h" |
| 12 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" | 12 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" |
| 13 #include "content/public/browser/browser_context.h" | 13 #include "content/public/browser/browser_context.h" |
| 14 | 14 |
| 15 void ProfileKeyedServiceFactory::SetTestingFactory( | 15 void BrowserContextKeyedServiceFactory::SetTestingFactory( |
| 16 content::BrowserContext* profile, FactoryFunction factory) { | 16 content::BrowserContext* context, FactoryFunction factory) { |
| 17 // Destroying the profile may cause us to lose data about whether |profile| | 17 // Destroying the context may cause us to lose data about whether |context| |
| 18 // has our preferences registered on it (since the profile object itself | 18 // has our preferences registered on it (since the context object itself |
| 19 // isn't dead). See if we need to readd it once we've gone through normal | 19 // isn't dead). See if we need to readd it once we've gone through normal |
| 20 // destruction. | 20 // destruction. |
| 21 bool add_profile = ArePreferencesSetOn(profile); | 21 bool add_context = ArePreferencesSetOn(context); |
| 22 | 22 |
| 23 // We have to go through the shutdown and destroy mechanisms because there | 23 // We have to go through the shutdown and destroy mechanisms because there |
| 24 // are unit tests that create a service on a profile and then change the | 24 // are unit tests that create a service on a context and then change the |
| 25 // testing service mid-test. | 25 // testing service mid-test. |
| 26 ProfileShutdown(profile); | 26 BrowserContextShutdown(context); |
| 27 ProfileDestroyed(profile); | 27 BrowserContextDestroyed(context); |
| 28 | 28 |
| 29 if (add_profile) | 29 if (add_context) |
| 30 MarkPreferencesSetOn(profile); | 30 MarkPreferencesSetOn(context); |
| 31 | 31 |
| 32 factories_[profile] = factory; | 32 factories_[context] = factory; |
| 33 } | 33 } |
| 34 | 34 |
| 35 ProfileKeyedService* ProfileKeyedServiceFactory::SetTestingFactoryAndUse( | 35 BrowserContextKeyedService* |
| 36 content::BrowserContext* profile, | 36 BrowserContextKeyedServiceFactory::SetTestingFactoryAndUse( |
| 37 content::BrowserContext* context, |
| 37 FactoryFunction factory) { | 38 FactoryFunction factory) { |
| 38 DCHECK(factory); | 39 DCHECK(factory); |
| 39 SetTestingFactory(profile, factory); | 40 SetTestingFactory(context, factory); |
| 40 return GetServiceForProfile(profile, true); | 41 return GetServiceForBrowserContext(context, true); |
| 41 } | 42 } |
| 42 | 43 |
| 43 ProfileKeyedServiceFactory::ProfileKeyedServiceFactory( | 44 BrowserContextKeyedServiceFactory::BrowserContextKeyedServiceFactory( |
| 44 const char* name, ProfileDependencyManager* manager) | 45 const char* name, BrowserContextDependencyManager* manager) |
| 45 : ProfileKeyedBaseFactory(name, manager) { | 46 : BrowserContextKeyedBaseFactory(name, manager) { |
| 46 } | 47 } |
| 47 | 48 |
| 48 ProfileKeyedServiceFactory::~ProfileKeyedServiceFactory() { | 49 BrowserContextKeyedServiceFactory::~BrowserContextKeyedServiceFactory() { |
| 49 DCHECK(mapping_.empty()); | 50 DCHECK(mapping_.empty()); |
| 50 } | 51 } |
| 51 | 52 |
| 52 ProfileKeyedService* ProfileKeyedServiceFactory::GetServiceForProfile( | 53 BrowserContextKeyedService* |
| 53 content::BrowserContext* profile, | 54 BrowserContextKeyedServiceFactory::GetServiceForBrowserContext( |
| 55 content::BrowserContext* context, |
| 54 bool create) { | 56 bool create) { |
| 55 profile = GetBrowserContextToUse(profile); | 57 context = GetBrowserContextToUse(context); |
| 56 if (!profile) | 58 if (!context) |
| 57 return NULL; | 59 return NULL; |
| 58 | 60 |
| 59 // NOTE: If you modify any of the logic below, make sure to update the | 61 // NOTE: If you modify any of the logic below, make sure to update the |
| 60 // refcounted version in refcounted_profile_keyed_service_factory.cc! | 62 // refcounted version in refcounted_context_keyed_service_factory.cc! |
| 61 ProfileKeyedServices::const_iterator it = mapping_.find(profile); | 63 BrowserContextKeyedServices::const_iterator it = mapping_.find(context); |
| 62 if (it != mapping_.end()) | 64 if (it != mapping_.end()) |
| 63 return it->second; | 65 return it->second; |
| 64 | 66 |
| 65 // Object not found. | 67 // Object not found. |
| 66 if (!create) | 68 if (!create) |
| 67 return NULL; // And we're forbidden from creating one. | 69 return NULL; // And we're forbidden from creating one. |
| 68 | 70 |
| 69 // Create new object. | 71 // Create new object. |
| 70 // Check to see if we have a per-Profile testing factory that we should use | 72 // Check to see if we have a per-BrowserContext testing factory that we should |
| 71 // instead of default behavior. | 73 // use instead of default behavior. |
| 72 ProfileKeyedService* service = NULL; | 74 BrowserContextKeyedService* service = NULL; |
| 73 ProfileOverriddenFunctions::const_iterator jt = factories_.find(profile); | 75 BrowserContextOverriddenFunctions::const_iterator jt = |
| 76 factories_.find(context); |
| 74 if (jt != factories_.end()) { | 77 if (jt != factories_.end()) { |
| 75 if (jt->second) { | 78 if (jt->second) { |
| 76 if (!profile->IsOffTheRecord()) | 79 if (!context->IsOffTheRecord()) |
| 77 RegisterUserPrefsOnProfile(profile); | 80 RegisterUserPrefsOnBrowserContext(context); |
| 78 service = jt->second(profile); | 81 service = jt->second(context); |
| 79 } | 82 } |
| 80 } else { | 83 } else { |
| 81 service = BuildServiceInstanceFor(profile); | 84 service = BuildServiceInstanceFor(context); |
| 82 } | 85 } |
| 83 | 86 |
| 84 Associate(profile, service); | 87 Associate(context, service); |
| 85 return service; | 88 return service; |
| 86 } | 89 } |
| 87 | 90 |
| 88 void ProfileKeyedServiceFactory::Associate(content::BrowserContext* profile, | 91 void BrowserContextKeyedServiceFactory::Associate( |
| 89 ProfileKeyedService* service) { | 92 content::BrowserContext* context, |
| 90 DCHECK(!ContainsKey(mapping_, profile)); | 93 BrowserContextKeyedService* service) { |
| 91 mapping_.insert(std::make_pair(profile, service)); | 94 DCHECK(!ContainsKey(mapping_, context)); |
| 95 mapping_.insert(std::make_pair(context, service)); |
| 92 } | 96 } |
| 93 | 97 |
| 94 void ProfileKeyedServiceFactory::ProfileShutdown( | 98 void BrowserContextKeyedServiceFactory::BrowserContextShutdown( |
| 95 content::BrowserContext* profile) { | 99 content::BrowserContext* context) { |
| 96 ProfileKeyedServices::iterator it = mapping_.find(profile); | 100 BrowserContextKeyedServices::iterator it = mapping_.find(context); |
| 97 if (it != mapping_.end() && it->second) | 101 if (it != mapping_.end() && it->second) |
| 98 it->second->Shutdown(); | 102 it->second->Shutdown(); |
| 99 } | 103 } |
| 100 | 104 |
| 101 void ProfileKeyedServiceFactory::ProfileDestroyed( | 105 void BrowserContextKeyedServiceFactory::BrowserContextDestroyed( |
| 102 content::BrowserContext* profile) { | 106 content::BrowserContext* context) { |
| 103 ProfileKeyedServices::iterator it = mapping_.find(profile); | 107 BrowserContextKeyedServices::iterator it = mapping_.find(context); |
| 104 if (it != mapping_.end()) { | 108 if (it != mapping_.end()) { |
| 105 delete it->second; | 109 delete it->second; |
| 106 mapping_.erase(it); | 110 mapping_.erase(it); |
| 107 } | 111 } |
| 108 | 112 |
| 109 // For unit tests, we also remove the factory function both so we don't | 113 // For unit tests, we also remove the factory function both so we don't |
| 110 // maintain a big map of dead pointers, but also since we may have a second | 114 // maintain a big map of dead pointers, but also since we may have a second |
| 111 // object that lives at the same address (see other comments about unit tests | 115 // object that lives at the same address (see other comments about unit tests |
| 112 // in this file). | 116 // in this file). |
| 113 factories_.erase(profile); | 117 factories_.erase(context); |
| 114 | 118 |
| 115 ProfileKeyedBaseFactory::ProfileDestroyed(profile); | 119 BrowserContextKeyedBaseFactory::BrowserContextDestroyed(context); |
| 116 } | 120 } |
| 117 | 121 |
| 118 void ProfileKeyedServiceFactory::SetEmptyTestingFactory( | 122 void BrowserContextKeyedServiceFactory::SetEmptyTestingFactory( |
| 119 content::BrowserContext* profile) { | 123 content::BrowserContext* context) { |
| 120 SetTestingFactory(profile, NULL); | 124 SetTestingFactory(context, NULL); |
| 121 } | 125 } |
| 122 | 126 |
| 123 void ProfileKeyedServiceFactory::CreateServiceNow( | 127 void BrowserContextKeyedServiceFactory::CreateServiceNow( |
| 124 content::BrowserContext* profile) { | 128 content::BrowserContext* context) { |
| 125 GetServiceForProfile(profile, true); | 129 GetServiceForBrowserContext(context, true); |
| 126 } | 130 } |
| OLD | NEW |