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/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); | |
|
Miranda Callahan
2012/03/15 16:55:35
see my comment in PKSF -- SetTestingFactory(GetPro
| |
| 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) |
| 30 } | 61 return NULL; |
| 31 | 62 |
| 32 bool RefcountedProfileKeyedServiceFactory::GetAssociation( | 63 // NOTE: If you modify any of the logic below, make sure to update the |
| 33 Profile* profile, | 64 // non-refcounted version in profile_keyed_service_factory.cc! |
| 34 ProfileKeyedBase** out) const { | 65 scoped_refptr<RefcountedProfileKeyedService> service; |
| 35 bool found = false; | |
| 36 RefCountedStorage::const_iterator it = mapping_.find(profile); | 66 RefCountedStorage::const_iterator it = mapping_.find(profile); |
| 37 if (it != mapping_.end()) { | 67 if (it != mapping_.end()) { |
| 38 found = true; | 68 return it->second; |
| 69 } else if (create) { | |
| 70 // not found but creation allowed | |
|
Miranda Callahan
2012/03/15 16:55:35
nit as before: sentencing
| |
| 39 | 71 |
| 40 if (out) | 72 // Check to see if we have a per-Profile factory |
|
Miranda Callahan
2012/03/15 16:55:35
nit as before: append a period.
| |
| 41 *out = it->second.get(); | 73 std::map<Profile*, FactoryFunction>::iterator jt = factories_.find(profile); |
| 74 if (jt != factories_.end()) { | |
| 75 if (jt->second) { | |
| 76 if (!profile->IsOffTheRecord()) | |
| 77 RegisterUserPrefsOnProfile(profile); | |
| 78 service = jt->second(profile); | |
| 79 } else { | |
| 80 service = NULL; | |
| 81 } | |
| 82 } else { | |
| 83 service = BuildServiceInstanceFor(profile); | |
| 84 } | |
| 85 } else { | |
| 86 // not found, creation forbidden | |
|
Miranda Callahan
2012/03/15 16:55:35
nit: I sentence thee
| |
| 87 return NULL; | |
| 42 } | 88 } |
| 43 return found; | 89 |
| 90 DCHECK(mapping_.find(profile) == mapping_.end()); | |
| 91 mapping_.insert(std::make_pair(profile, service)); | |
|
Miranda Callahan
2012/03/15 16:55:35
These two lines are encapsulated in the "Associate
Elliot Glaysher
2012/03/15 18:02:41
I only did that in PKSF because only in the non-re
| |
| 92 return service; | |
| 44 } | 93 } |
| 45 | 94 |
| 46 void RefcountedProfileKeyedServiceFactory::ProfileShutdown(Profile* profile) { | 95 void RefcountedProfileKeyedServiceFactory::ProfileShutdown(Profile* profile) { |
| 47 RefCountedStorage::iterator it = mapping_.find(profile); | 96 RefCountedStorage::iterator it = mapping_.find(profile); |
| 48 if (it != mapping_.end() && it->second) | 97 if (it != mapping_.end() && it->second) |
| 49 it->second->ShutdownOnUIThread(); | 98 it->second->ShutdownOnUIThread(); |
| 50 } | 99 } |
| 51 | 100 |
| 52 void RefcountedProfileKeyedServiceFactory::ProfileDestroyed(Profile* profile) { | 101 void RefcountedProfileKeyedServiceFactory::ProfileDestroyed(Profile* profile) { |
| 53 RefCountedStorage::iterator it = mapping_.find(profile); | 102 RefCountedStorage::iterator it = mapping_.find(profile); |
| 54 if (it != mapping_.end()) { | 103 if (it != mapping_.end()) { |
| 55 // We "merely" drop our reference to the service. Hopefully this will cause | 104 // We "merely" drop our reference to the service. Hopefully this will cause |
| 56 // the service to be destroyed. If not, oh well. | 105 // the service to be destroyed. If not, oh well. |
| 57 mapping_.erase(it); | 106 mapping_.erase(it); |
| 58 } | 107 } |
| 59 | 108 |
| 109 // 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 | |
| 111 // object that lives at the same address (see other comments about unit tests | |
| 112 // in this file). | |
| 113 factories_.erase(profile); | |
| 114 | |
| 60 ProfileKeyedBaseFactory::ProfileDestroyed(profile); | 115 ProfileKeyedBaseFactory::ProfileDestroyed(profile); |
| 61 } | 116 } |
| 117 | |
| 118 void RefcountedProfileKeyedServiceFactory::SetEmptyTestingFactory( | |
| 119 Profile* profile) { | |
| 120 SetTestingFactory(profile, NULL); | |
| 121 } | |
| 122 | |
| 123 void RefcountedProfileKeyedServiceFactory::CreateServiceNow(Profile* profile) { | |
| 124 GetServiceForProfile(profile, true); | |
| 125 } | |
| OLD | NEW |