| 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/refcounted_profile_keyed_service_factory.h" | 5 #include "chrome/browser/profiles/refcounted_profile_keyed_service_factory.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/profiles/profile_keyed_service.h" | 10 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 11 #include "chrome/browser/profiles/refcounted_profile_keyed_service.h" | 11 #include "chrome/browser/profiles/refcounted_profile_keyed_service.h" |
| 12 | 12 |
| 13 void RefcountedProfileKeyedServiceFactory::SetTestingFactory( | 13 void RefcountedProfileKeyedServiceFactory::SetTestingFactory( |
| 14 Profile* profile, | 14 content::BrowserContext* profile, |
| 15 FactoryFunction factory) { | 15 FactoryFunction factory) { |
| 16 // Destroying the profile may cause us to lose data about whether |profile| | 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 | 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 | 18 // isn't dead). See if we need to readd it once we've gone through normal |
| 19 // destruction. | 19 // destruction. |
| 20 bool add_profile = ArePreferencesSetOn(profile); | 20 bool add_profile = ArePreferencesSetOn(profile); |
| 21 | 21 |
| 22 // We have to go through the shutdown and destroy mechanisms because there | 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 | 23 // are unit tests that create a service on a profile and then change the |
| 24 // testing service mid-test. | 24 // testing service mid-test. |
| 25 ProfileShutdown(profile); | 25 ProfileShutdown(profile); |
| 26 ProfileDestroyed(profile); | 26 ProfileDestroyed(profile); |
| 27 | 27 |
| 28 if (add_profile) | 28 if (add_profile) |
| 29 MarkPreferencesSetOn(profile); | 29 MarkPreferencesSetOn(profile); |
| 30 | 30 |
| 31 factories_[profile] = factory; | 31 factories_[profile] = factory; |
| 32 } | 32 } |
| 33 | 33 |
| 34 scoped_refptr<RefcountedProfileKeyedService> | 34 scoped_refptr<RefcountedProfileKeyedService> |
| 35 RefcountedProfileKeyedServiceFactory::SetTestingFactoryAndUse( | 35 RefcountedProfileKeyedServiceFactory::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 RefcountedProfileKeyedServiceFactory::RefcountedProfileKeyedServiceFactory( | 43 RefcountedProfileKeyedServiceFactory::RefcountedProfileKeyedServiceFactory( |
| 44 const char* name, | 44 const char* name, |
| 45 ProfileDependencyManager* manager) | 45 ProfileDependencyManager* manager) |
| 46 : ProfileKeyedBaseFactory(name, manager) { | 46 : ProfileKeyedBaseFactory(name, manager) { |
| 47 } | 47 } |
| 48 | 48 |
| 49 RefcountedProfileKeyedServiceFactory::~RefcountedProfileKeyedServiceFactory() { | 49 RefcountedProfileKeyedServiceFactory::~RefcountedProfileKeyedServiceFactory() { |
| 50 DCHECK(mapping_.empty()); | 50 DCHECK(mapping_.empty()); |
| 51 } | 51 } |
| 52 | 52 |
| 53 scoped_refptr<RefcountedProfileKeyedService> | 53 scoped_refptr<RefcountedProfileKeyedService> |
| 54 RefcountedProfileKeyedServiceFactory::GetServiceForProfile( | 54 RefcountedProfileKeyedServiceFactory::GetServiceForProfile( |
| 55 Profile* profile, | 55 content::BrowserContext* profile, |
| 56 bool create) { | 56 bool create) { |
| 57 profile = GetProfileToUse(profile); | 57 profile = GetProfileToUse(profile); |
| 58 if (!profile) | 58 if (!profile) |
| 59 return NULL; | 59 return NULL; |
| 60 | 60 |
| 61 // 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 |
| 62 // non-refcounted version in profile_keyed_service_factory.cc! | 62 // non-refcounted version in profile_keyed_service_factory.cc! |
| 63 RefCountedStorage::const_iterator it = mapping_.find(profile); | 63 RefCountedStorage::const_iterator it = mapping_.find(profile); |
| 64 if (it != mapping_.end()) | 64 if (it != mapping_.end()) |
| 65 return it->second; | 65 return it->second; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 81 } | 81 } |
| 82 } else { | 82 } else { |
| 83 service = BuildServiceInstanceFor(profile); | 83 service = BuildServiceInstanceFor(profile); |
| 84 } | 84 } |
| 85 | 85 |
| 86 Associate(profile, service); | 86 Associate(profile, service); |
| 87 return service; | 87 return service; |
| 88 } | 88 } |
| 89 | 89 |
| 90 void RefcountedProfileKeyedServiceFactory::Associate( | 90 void RefcountedProfileKeyedServiceFactory::Associate( |
| 91 Profile* profile, | 91 content::BrowserContext* profile, |
| 92 const scoped_refptr<RefcountedProfileKeyedService>& service) { | 92 const scoped_refptr<RefcountedProfileKeyedService>& service) { |
| 93 DCHECK(!ContainsKey(mapping_, profile)); | 93 DCHECK(!ContainsKey(mapping_, profile)); |
| 94 mapping_.insert(std::make_pair(profile, service)); | 94 mapping_.insert(std::make_pair(profile, service)); |
| 95 } | 95 } |
| 96 | 96 |
| 97 void RefcountedProfileKeyedServiceFactory::ProfileShutdown(Profile* profile) { | 97 void RefcountedProfileKeyedServiceFactory::ProfileShutdown( |
| 98 content::BrowserContext* profile) { |
| 98 RefCountedStorage::iterator it = mapping_.find(profile); | 99 RefCountedStorage::iterator it = mapping_.find(profile); |
| 99 if (it != mapping_.end() && it->second) | 100 if (it != mapping_.end() && it->second) |
| 100 it->second->ShutdownOnUIThread(); | 101 it->second->ShutdownOnUIThread(); |
| 101 } | 102 } |
| 102 | 103 |
| 103 void RefcountedProfileKeyedServiceFactory::ProfileDestroyed(Profile* profile) { | 104 void RefcountedProfileKeyedServiceFactory::ProfileDestroyed( |
| 105 content::BrowserContext* profile) { |
| 104 // We "merely" drop our reference to the service. Hopefully this will cause | 106 // We "merely" drop our reference to the service. Hopefully this will cause |
| 105 // the service to be destroyed. If not, oh well. | 107 // the service to be destroyed. If not, oh well. |
| 106 mapping_.erase(profile); | 108 mapping_.erase(profile); |
| 107 | 109 |
| 108 // For unit tests, we also remove the factory function both so we don't | 110 // For unit tests, we also remove the factory function both so we don't |
| 109 // maintain a big map of dead pointers, but also since we may have a second | 111 // maintain a big map of dead pointers, but also since we may have a second |
| 110 // object that lives at the same address (see other comments about unit tests | 112 // object that lives at the same address (see other comments about unit tests |
| 111 // in this file). | 113 // in this file). |
| 112 factories_.erase(profile); | 114 factories_.erase(profile); |
| 113 | 115 |
| 114 ProfileKeyedBaseFactory::ProfileDestroyed(profile); | 116 ProfileKeyedBaseFactory::ProfileDestroyed(profile); |
| 115 } | 117 } |
| 116 | 118 |
| 117 void RefcountedProfileKeyedServiceFactory::SetEmptyTestingFactory( | 119 void RefcountedProfileKeyedServiceFactory::SetEmptyTestingFactory( |
| 118 Profile* profile) { | 120 content::BrowserContext* profile) { |
| 119 SetTestingFactory(profile, NULL); | 121 SetTestingFactory(profile, NULL); |
| 120 } | 122 } |
| 121 | 123 |
| 122 void RefcountedProfileKeyedServiceFactory::CreateServiceNow(Profile* profile) { | 124 void RefcountedProfileKeyedServiceFactory::CreateServiceNow( |
| 125 content::BrowserContext* profile) { |
| 123 GetServiceForProfile(profile, true); | 126 GetServiceForProfile(profile, true); |
| 124 } | 127 } |
| OLD | NEW |