| 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 "chrome/browser/profiles/profile.h" |
| 8 #include "chrome/browser/profiles/profile_keyed_service.h" | 9 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 9 #include "chrome/browser/profiles/refcounted_profile_keyed_service.h" | 10 #include "chrome/browser/profiles/refcounted_profile_keyed_service.h" |
| 10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 11 | 12 |
| 12 using content::BrowserThread; | 13 using content::BrowserThread; |
| 13 | 14 |
| 15 void RefcountedProfileKeyedServiceFactory::SetTestingFactory( |
| 16 Profile* profile, |
| 17 FactoryFunction factory) { |
| 18 // Destroying the profile may cause us to lose data about whether |profile| |
| 19 // has our preferences registered on it (since the profile object itself |
| 20 // isn't dead). See if we need to readd it once we've gone through normal |
| 21 // destruction. |
| 22 bool add_profile = ArePreferencesSetOn(profile); |
| 23 |
| 24 // We have to go through the shutdown and destroy mechanisms because there |
| 25 // are unit tests that create a service on a profile and then change the |
| 26 // testing service mid-test. |
| 27 ProfileShutdown(profile); |
| 28 ProfileDestroyed(profile); |
| 29 |
| 30 if (add_profile) |
| 31 MarkPreferencesSetOn(profile); |
| 32 |
| 33 factories_[profile] = factory; |
| 34 } |
| 35 |
| 36 scoped_refptr<RefcountedProfileKeyedService> |
| 37 RefcountedProfileKeyedServiceFactory::SetTestingFactoryAndUse( |
| 38 Profile* profile, |
| 39 FactoryFunction factory) { |
| 40 DCHECK(factory); |
| 41 SetTestingFactory(profile, factory); |
| 42 return GetServiceForProfile(profile, true); |
| 43 } |
| 44 |
| 14 RefcountedProfileKeyedServiceFactory::RefcountedProfileKeyedServiceFactory( | 45 RefcountedProfileKeyedServiceFactory::RefcountedProfileKeyedServiceFactory( |
| 15 const char* name, | 46 const char* name, |
| 16 ProfileDependencyManager* manager) | 47 ProfileDependencyManager* manager) |
| 17 : ProfileKeyedBaseFactory(name, manager) { | 48 : ProfileKeyedBaseFactory(name, manager) { |
| 18 } | 49 } |
| 19 | 50 |
| 20 RefcountedProfileKeyedServiceFactory::~RefcountedProfileKeyedServiceFactory() { | 51 RefcountedProfileKeyedServiceFactory::~RefcountedProfileKeyedServiceFactory() { |
| 21 DCHECK(mapping_.empty()); | 52 DCHECK(mapping_.empty()); |
| 22 } | 53 } |
| 23 | 54 |
| 24 void RefcountedProfileKeyedServiceFactory::Associate(Profile* profile, | 55 scoped_refptr<RefcountedProfileKeyedService> |
| 25 ProfileKeyedBase* base) { | 56 RefcountedProfileKeyedServiceFactory::GetServiceForProfile( |
| 26 DCHECK(mapping_.find(profile) == mapping_.end()); | 57 Profile* profile, |
| 27 mapping_.insert(std::make_pair( | 58 bool create) { |
| 28 profile, make_scoped_refptr( | 59 profile = GetProfileToUse(profile); |
| 29 static_cast<RefcountedProfileKeyedService*>(base)))); | 60 if (!profile) |
| 61 return NULL; |
| 62 |
| 63 // NOTE: If you modify any of the logic below, make sure to update the |
| 64 // non-refcounted version in profile_keyed_service_factory.cc! |
| 65 scoped_refptr<RefcountedProfileKeyedService> service; |
| 66 RefCountedStorage::const_iterator it = mapping_.find(profile); |
| 67 if (it != mapping_.end()) { |
| 68 return it->second; |
| 69 } else if (create) { |
| 70 // Object not found, and we must create it. |
| 71 // |
| 72 // Check to see if we have a per-Profile testing factory that we should use |
| 73 // instead of default behavior. |
| 74 std::map<Profile*, FactoryFunction>::iterator jt = factories_.find(profile); |
| 75 if (jt != factories_.end()) { |
| 76 if (jt->second) { |
| 77 if (!profile->IsOffTheRecord()) |
| 78 RegisterUserPrefsOnProfile(profile); |
| 79 service = jt->second(profile); |
| 80 } else { |
| 81 service = NULL; |
| 82 } |
| 83 } else { |
| 84 service = BuildServiceInstanceFor(profile); |
| 85 } |
| 86 } else { |
| 87 // Object not found, and we're forbidden from creating one. |
| 88 return NULL; |
| 89 } |
| 90 |
| 91 Associate(profile, service); |
| 92 return service; |
| 30 } | 93 } |
| 31 | 94 |
| 32 bool RefcountedProfileKeyedServiceFactory::GetAssociation( | 95 void RefcountedProfileKeyedServiceFactory::Associate( |
| 33 Profile* profile, | 96 Profile* profile, |
| 34 ProfileKeyedBase** out) const { | 97 const scoped_refptr<RefcountedProfileKeyedService>& service) { |
| 35 bool found = false; | 98 DCHECK(mapping_.find(profile) == mapping_.end()); |
| 36 RefCountedStorage::const_iterator it = mapping_.find(profile); | 99 mapping_.insert(std::make_pair(profile, service)); |
| 37 if (it != mapping_.end()) { | |
| 38 found = true; | |
| 39 | |
| 40 if (out) | |
| 41 *out = it->second.get(); | |
| 42 } | |
| 43 return found; | |
| 44 } | 100 } |
| 45 | 101 |
| 46 void RefcountedProfileKeyedServiceFactory::ProfileShutdown(Profile* profile) { | 102 void RefcountedProfileKeyedServiceFactory::ProfileShutdown(Profile* profile) { |
| 47 RefCountedStorage::iterator it = mapping_.find(profile); | 103 RefCountedStorage::iterator it = mapping_.find(profile); |
| 48 if (it != mapping_.end() && it->second) | 104 if (it != mapping_.end() && it->second) |
| 49 it->second->ShutdownOnUIThread(); | 105 it->second->ShutdownOnUIThread(); |
| 50 } | 106 } |
| 51 | 107 |
| 52 void RefcountedProfileKeyedServiceFactory::ProfileDestroyed(Profile* profile) { | 108 void RefcountedProfileKeyedServiceFactory::ProfileDestroyed(Profile* profile) { |
| 53 RefCountedStorage::iterator it = mapping_.find(profile); | 109 RefCountedStorage::iterator it = mapping_.find(profile); |
| 54 if (it != mapping_.end()) { | 110 if (it != mapping_.end()) { |
| 55 // We "merely" drop our reference to the service. Hopefully this will cause | 111 // We "merely" drop our reference to the service. Hopefully this will cause |
| 56 // the service to be destroyed. If not, oh well. | 112 // the service to be destroyed. If not, oh well. |
| 57 mapping_.erase(it); | 113 mapping_.erase(it); |
| 58 } | 114 } |
| 59 | 115 |
| 116 // For unit tests, we also remove the factory function both so we don't |
| 117 // maintain a big map of dead pointers, but also since we may have a second |
| 118 // object that lives at the same address (see other comments about unit tests |
| 119 // in this file). |
| 120 factories_.erase(profile); |
| 121 |
| 60 ProfileKeyedBaseFactory::ProfileDestroyed(profile); | 122 ProfileKeyedBaseFactory::ProfileDestroyed(profile); |
| 61 } | 123 } |
| 124 |
| 125 void RefcountedProfileKeyedServiceFactory::SetEmptyTestingFactory( |
| 126 Profile* profile) { |
| 127 SetTestingFactory(profile, NULL); |
| 128 } |
| 129 |
| 130 void RefcountedProfileKeyedServiceFactory::CreateServiceNow(Profile* profile) { |
| 131 GetServiceForProfile(profile, true); |
| 132 } |
| OLD | NEW |