| 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/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/profiles/profile_dependency_manager.h" | 12 #include "chrome/browser/profiles/profile_dependency_manager.h" |
| 13 #include "chrome/browser/profiles/profile_keyed_service.h" | 13 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 14 | 14 |
| 15 void ProfileKeyedServiceFactory::SetTestingFactory(Profile* profile, | 15 void ProfileKeyedServiceFactory::SetTestingFactory( |
| 16 FactoryFunction factory) { | 16 content::BrowserContext* profile, FactoryFunction factory) { |
| 17 // Destroying the profile may cause us to lose data about whether |profile| | 17 // Destroying the profile may cause us to lose data about whether |profile| |
| 18 // has our preferences registered on it (since the profile object itself | 18 // has our preferences registered on it (since the profile 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_profile = ArePreferencesSetOn(profile); |
| 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 profile and then change the |
| 25 // testing service mid-test. | 25 // testing service mid-test. |
| 26 ProfileShutdown(profile); | 26 ProfileShutdown(profile); |
| 27 ProfileDestroyed(profile); | 27 ProfileDestroyed(profile); |
| 28 | 28 |
| 29 if (add_profile) | 29 if (add_profile) |
| 30 MarkPreferencesSetOn(profile); | 30 MarkPreferencesSetOn(profile); |
| 31 | 31 |
| 32 factories_[profile] = factory; | 32 factories_[profile] = factory; |
| 33 } | 33 } |
| 34 | 34 |
| 35 ProfileKeyedService* ProfileKeyedServiceFactory::SetTestingFactoryAndUse( | 35 ProfileKeyedService* ProfileKeyedServiceFactory::SetTestingFactoryAndUse( |
| 36 Profile* profile, | 36 content::BrowserContext* profile, |
| 37 FactoryFunction factory) { | 37 FactoryFunction factory) { |
| 38 DCHECK(factory); | 38 DCHECK(factory); |
| 39 SetTestingFactory(profile, factory); | 39 SetTestingFactory(profile, factory); |
| 40 return GetServiceForProfile(profile, true); | 40 return GetServiceForProfile(profile, true); |
| 41 } | 41 } |
| 42 | 42 |
| 43 ProfileKeyedServiceFactory::ProfileKeyedServiceFactory( | 43 ProfileKeyedServiceFactory::ProfileKeyedServiceFactory( |
| 44 const char* name, ProfileDependencyManager* manager) | 44 const char* name, ProfileDependencyManager* manager) |
| 45 : ProfileKeyedBaseFactory(name, manager) { | 45 : ProfileKeyedBaseFactory(name, manager) { |
| 46 } | 46 } |
| 47 | 47 |
| 48 ProfileKeyedServiceFactory::~ProfileKeyedServiceFactory() { | 48 ProfileKeyedServiceFactory::~ProfileKeyedServiceFactory() { |
| 49 DCHECK(mapping_.empty()); | 49 DCHECK(mapping_.empty()); |
| 50 } | 50 } |
| 51 | 51 |
| 52 ProfileKeyedService* ProfileKeyedServiceFactory::GetServiceForProfile( | 52 ProfileKeyedService* ProfileKeyedServiceFactory::GetServiceForProfile( |
| 53 Profile* profile, | 53 content::BrowserContext* profile, |
| 54 bool create) { | 54 bool create) { |
| 55 profile = GetProfileToUse(profile); | 55 profile = GetProfileToUse(profile); |
| 56 if (!profile) | 56 if (!profile) |
| 57 return NULL; | 57 return NULL; |
| 58 | 58 |
| 59 // NOTE: If you modify any of the logic below, make sure to update the | 59 // NOTE: If you modify any of the logic below, make sure to update the |
| 60 // refcounted version in refcounted_profile_keyed_service_factory.cc! | 60 // refcounted version in refcounted_profile_keyed_service_factory.cc! |
| 61 ProfileKeyedServices::const_iterator it = mapping_.find(profile); | 61 ProfileKeyedServices::const_iterator it = mapping_.find(profile); |
| 62 if (it != mapping_.end()) | 62 if (it != mapping_.end()) |
| 63 return it->second; | 63 return it->second; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 78 service = jt->second(profile); | 78 service = jt->second(profile); |
| 79 } | 79 } |
| 80 } else { | 80 } else { |
| 81 service = BuildServiceInstanceFor(profile); | 81 service = BuildServiceInstanceFor(profile); |
| 82 } | 82 } |
| 83 | 83 |
| 84 Associate(profile, service); | 84 Associate(profile, service); |
| 85 return service; | 85 return service; |
| 86 } | 86 } |
| 87 | 87 |
| 88 void ProfileKeyedServiceFactory::Associate(Profile* profile, | 88 void ProfileKeyedServiceFactory::Associate(content::BrowserContext* profile, |
| 89 ProfileKeyedService* service) { | 89 ProfileKeyedService* service) { |
| 90 DCHECK(!ContainsKey(mapping_, profile)); | 90 DCHECK(!ContainsKey(mapping_, profile)); |
| 91 mapping_.insert(std::make_pair(profile, service)); | 91 mapping_.insert(std::make_pair(profile, service)); |
| 92 } | 92 } |
| 93 | 93 |
| 94 void ProfileKeyedServiceFactory::ProfileShutdown(Profile* profile) { | 94 void ProfileKeyedServiceFactory::ProfileShutdown( |
| 95 content::BrowserContext* profile) { |
| 95 ProfileKeyedServices::iterator it = mapping_.find(profile); | 96 ProfileKeyedServices::iterator it = mapping_.find(profile); |
| 96 if (it != mapping_.end() && it->second) | 97 if (it != mapping_.end() && it->second) |
| 97 it->second->Shutdown(); | 98 it->second->Shutdown(); |
| 98 } | 99 } |
| 99 | 100 |
| 100 void ProfileKeyedServiceFactory::ProfileDestroyed(Profile* profile) { | 101 void ProfileKeyedServiceFactory::ProfileDestroyed( |
| 102 content::BrowserContext* profile) { |
| 101 ProfileKeyedServices::iterator it = mapping_.find(profile); | 103 ProfileKeyedServices::iterator it = mapping_.find(profile); |
| 102 if (it != mapping_.end()) { | 104 if (it != mapping_.end()) { |
| 103 delete it->second; | 105 delete it->second; |
| 104 mapping_.erase(it); | 106 mapping_.erase(it); |
| 105 } | 107 } |
| 106 | 108 |
| 107 // For unit tests, we also remove the factory function both so we don't | 109 // For unit tests, we also remove the factory function both so we don't |
| 108 // maintain a big map of dead pointers, but also since we may have a second | 110 // maintain a big map of dead pointers, but also since we may have a second |
| 109 // object that lives at the same address (see other comments about unit tests | 111 // object that lives at the same address (see other comments about unit tests |
| 110 // in this file). | 112 // in this file). |
| 111 factories_.erase(profile); | 113 factories_.erase(profile); |
| 112 | 114 |
| 113 ProfileKeyedBaseFactory::ProfileDestroyed(profile); | 115 ProfileKeyedBaseFactory::ProfileDestroyed(profile); |
| 114 } | 116 } |
| 115 | 117 |
| 116 void ProfileKeyedServiceFactory::SetEmptyTestingFactory( | 118 void ProfileKeyedServiceFactory::SetEmptyTestingFactory( |
| 117 Profile* profile) { | 119 content::BrowserContext* profile) { |
| 118 SetTestingFactory(profile, NULL); | 120 SetTestingFactory(profile, NULL); |
| 119 } | 121 } |
| 120 | 122 |
| 121 void ProfileKeyedServiceFactory::CreateServiceNow(Profile* profile) { | 123 void ProfileKeyedServiceFactory::CreateServiceNow( |
| 124 content::BrowserContext* profile) { |
| 122 GetServiceForProfile(profile, true); | 125 GetServiceForProfile(profile, true); |
| 123 } | 126 } |
| OLD | NEW |