Chromium Code Reviews| 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 "chrome/browser/profiles/profile_keyed_service_factory.h" | 5 #include "chrome/browser/profiles/profile_keyed_service_factory.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/memory/singleton.h" | 9 #include "base/memory/singleton.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/profiles/profile_dependency_manager.h" | 11 #include "chrome/browser/profiles/profile_dependency_manager.h" |
| 12 #include "chrome/browser/profiles/profile_keyed_service.h" | 12 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 13 | 13 |
| 14 void ProfileKeyedServiceFactory::SetTestingFactory(Profile* profile, | |
| 15 FactoryFunction factory) { | |
| 16 // Destroying the profile may cause us to lose data about whether |profile| | |
| 17 // has our preferences registered on it (since the profile object itself | |
| 18 // isn't dead). See if we need to readd it once we've gone through normal | |
| 19 // destruction. | |
| 20 bool add_profile = ArePreferencesSetOn(profile); | |
| 21 | |
| 22 // We have to go through the shutdown and destroy mechanisms because there | |
| 23 // are unit tests that create a service on a profile and then change the | |
| 24 // testing service mid-test. | |
| 25 ProfileShutdown(profile); | |
| 26 ProfileDestroyed(profile); | |
| 27 | |
| 28 if (add_profile) | |
| 29 MarkPreferencesSetOn(profile); | |
| 30 | |
| 31 factories_[profile] = factory; | |
| 32 } | |
| 33 | |
| 34 ProfileKeyedService* ProfileKeyedServiceFactory::SetTestingFactoryAndUse( | |
| 35 Profile* profile, | |
| 36 FactoryFunction factory) { | |
| 37 DCHECK(factory); | |
| 38 SetTestingFactory(profile, factory); | |
|
Miranda Callahan
2012/03/15 15:31:07
Do we actually need to call SetTestingFactory(GetP
Elliot Glaysher
2012/03/15 18:02:41
IIRC, that would be incorrect. There are a few tes
| |
| 39 return GetServiceForProfile(profile, true); | |
| 40 } | |
| 41 | |
| 14 ProfileKeyedServiceFactory::ProfileKeyedServiceFactory( | 42 ProfileKeyedServiceFactory::ProfileKeyedServiceFactory( |
| 15 const char* name, ProfileDependencyManager* manager) | 43 const char* name, ProfileDependencyManager* manager) |
| 16 : ProfileKeyedBaseFactory(name, manager) { | 44 : ProfileKeyedBaseFactory(name, manager) { |
| 17 } | 45 } |
| 18 | 46 |
| 19 ProfileKeyedServiceFactory::~ProfileKeyedServiceFactory() { | 47 ProfileKeyedServiceFactory::~ProfileKeyedServiceFactory() { |
| 20 DCHECK(mapping_.empty()); | 48 DCHECK(mapping_.empty()); |
| 21 } | 49 } |
| 22 | 50 |
| 23 ProfileKeyedService* ProfileKeyedServiceFactory::GetServiceForProfile( | 51 ProfileKeyedService* ProfileKeyedServiceFactory::GetServiceForProfile( |
| 24 Profile* profile, | 52 Profile* profile, |
| 25 bool create) { | 53 bool create) { |
| 26 return static_cast<ProfileKeyedService*>(GetBaseForProfile(profile, create)); | 54 profile = GetProfileToUse(profile); |
| 55 if (!profile) | |
| 56 return NULL; | |
| 57 | |
| 58 // NOTE: If you modify any of the logic below, make sure to update the | |
| 59 // refcounted version in refcounted_profile_keyed_service_factory.cc! | |
| 60 ProfileKeyedService* service = NULL; | |
| 61 std::map<Profile*, ProfileKeyedService*>::const_iterator it = | |
| 62 mapping_.find(profile); | |
| 63 if (it != mapping_.end()) { | |
| 64 return it->second; | |
| 65 } else if (create) { | |
| 66 // not found but creation allowed | |
| 67 | |
|
Miranda Callahan
2012/03/15 15:31:07
nit: remove blank line, and sentence-ify the "not
| |
| 68 // Check to see if we have a per-Profile factory | |
|
Miranda Callahan
2012/03/15 15:31:07
nit: s/factory/factory:/
| |
| 69 std::map<Profile*, FactoryFunction>::iterator jt = factories_.find(profile); | |
| 70 if (jt != factories_.end()) { | |
| 71 if (jt->second) { | |
| 72 if (!profile->IsOffTheRecord()) | |
| 73 RegisterUserPrefsOnProfile(profile); | |
| 74 service = jt->second(profile); | |
| 75 } else { | |
| 76 service = NULL; | |
| 77 } | |
| 78 } else { | |
| 79 service = BuildServiceInstanceFor(profile); | |
| 80 } | |
| 81 } else { | |
| 82 // not found, creation forbidden | |
|
Miranda Callahan
2012/03/15 15:31:07
nit: sentence-ify comment above.
| |
| 83 return NULL; | |
| 84 } | |
| 85 | |
| 86 Associate(profile, service); | |
| 87 return service; | |
| 27 } | 88 } |
| 28 | 89 |
| 29 void ProfileKeyedServiceFactory::Associate(Profile* profile, | 90 void ProfileKeyedServiceFactory::Associate(Profile* profile, |
| 30 ProfileKeyedBase* service) { | 91 ProfileKeyedService* service) { |
| 31 DCHECK(mapping_.find(profile) == mapping_.end()); | 92 DCHECK(mapping_.find(profile) == mapping_.end()); |
| 32 mapping_.insert(std::make_pair( | 93 mapping_.insert(std::make_pair(profile, service)); |
| 33 profile, static_cast<ProfileKeyedService*>(service))); | |
| 34 } | |
| 35 | |
| 36 bool ProfileKeyedServiceFactory::GetAssociation( | |
| 37 Profile* profile, | |
| 38 ProfileKeyedBase** out) const { | |
| 39 bool found = false; | |
| 40 std::map<Profile*, ProfileKeyedService*>::const_iterator it = | |
| 41 mapping_.find(profile); | |
| 42 if (it != mapping_.end()) { | |
| 43 found = true; | |
| 44 if (out) | |
| 45 *out = it->second; | |
| 46 } | |
| 47 return found; | |
| 48 } | 94 } |
| 49 | 95 |
| 50 void ProfileKeyedServiceFactory::ProfileShutdown(Profile* profile) { | 96 void ProfileKeyedServiceFactory::ProfileShutdown(Profile* profile) { |
| 51 std::map<Profile*, ProfileKeyedService*>::iterator it = | 97 std::map<Profile*, ProfileKeyedService*>::iterator it = |
| 52 mapping_.find(profile); | 98 mapping_.find(profile); |
| 53 if (it != mapping_.end() && it->second) | 99 if (it != mapping_.end() && it->second) |
| 54 it->second->Shutdown(); | 100 it->second->Shutdown(); |
| 55 } | 101 } |
| 56 | 102 |
| 57 void ProfileKeyedServiceFactory::ProfileDestroyed(Profile* profile) { | 103 void ProfileKeyedServiceFactory::ProfileDestroyed(Profile* profile) { |
| 58 std::map<Profile*, ProfileKeyedService*>::iterator it = | 104 std::map<Profile*, ProfileKeyedService*>::iterator it = |
| 59 mapping_.find(profile); | 105 mapping_.find(profile); |
| 60 if (it != mapping_.end()) { | 106 if (it != mapping_.end()) { |
| 61 delete it->second; | 107 delete it->second; |
| 62 mapping_.erase(it); | 108 mapping_.erase(it); |
| 63 } | 109 } |
| 64 | 110 |
| 111 // For unit tests, we also remove the factory function both so we don't | |
| 112 // maintain a big map of dead pointers, but also since we may have a second | |
| 113 // object that lives at the same address (see other comments about unit tests | |
| 114 // in this file). | |
| 115 factories_.erase(profile); | |
| 116 | |
| 65 ProfileKeyedBaseFactory::ProfileDestroyed(profile); | 117 ProfileKeyedBaseFactory::ProfileDestroyed(profile); |
| 66 } | 118 } |
| 119 | |
| 120 void ProfileKeyedServiceFactory::SetEmptyTestingFactory( | |
| 121 Profile* profile) { | |
| 122 SetTestingFactory(profile, NULL); | |
| 123 } | |
| 124 | |
| 125 void ProfileKeyedServiceFactory::CreateServiceNow(Profile* profile) { | |
| 126 GetServiceForProfile(profile, true); | |
| 127 } | |
| OLD | NEW |