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(content::BrowserContext* prof
ile, |
16 FactoryFunction factory) { | 16 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 // TODO(phajdan.jr): Migrate this. |
56 if (!profile) | 56 //profile = GetProfileToUse(profile); |
57 return NULL; | 57 //if (!profile) |
| 58 // return NULL; |
58 | 59 |
59 // NOTE: If you modify any of the logic below, make sure to update the | 60 // NOTE: If you modify any of the logic below, make sure to update the |
60 // refcounted version in refcounted_profile_keyed_service_factory.cc! | 61 // refcounted version in refcounted_profile_keyed_service_factory.cc! |
61 ProfileKeyedServices::const_iterator it = mapping_.find(profile); | 62 ProfileKeyedServices::const_iterator it = mapping_.find(profile); |
62 if (it != mapping_.end()) | 63 if (it != mapping_.end()) |
63 return it->second; | 64 return it->second; |
64 | 65 |
65 // Object not found. | 66 // Object not found. |
66 if (!create) | 67 if (!create) |
67 return NULL; // And we're forbidden from creating one. | 68 return NULL; // And we're forbidden from creating one. |
(...skipping 10 matching lines...) Expand all Loading... |
78 service = jt->second(profile); | 79 service = jt->second(profile); |
79 } | 80 } |
80 } else { | 81 } else { |
81 service = BuildServiceInstanceFor(profile); | 82 service = BuildServiceInstanceFor(profile); |
82 } | 83 } |
83 | 84 |
84 Associate(profile, service); | 85 Associate(profile, service); |
85 return service; | 86 return service; |
86 } | 87 } |
87 | 88 |
88 void ProfileKeyedServiceFactory::Associate(Profile* profile, | 89 void ProfileKeyedServiceFactory::Associate(content::BrowserContext* profile, |
89 ProfileKeyedService* service) { | 90 ProfileKeyedService* service) { |
90 DCHECK(!ContainsKey(mapping_, profile)); | 91 DCHECK(!ContainsKey(mapping_, profile)); |
91 mapping_.insert(std::make_pair(profile, service)); | 92 mapping_.insert(std::make_pair(profile, service)); |
92 } | 93 } |
93 | 94 |
94 void ProfileKeyedServiceFactory::ProfileShutdown(Profile* profile) { | 95 void ProfileKeyedServiceFactory::ProfileShutdown(content::BrowserContext* profil
e) { |
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(content::BrowserContext* profi
le) { |
101 ProfileKeyedServices::iterator it = mapping_.find(profile); | 102 ProfileKeyedServices::iterator it = mapping_.find(profile); |
102 if (it != mapping_.end()) { | 103 if (it != mapping_.end()) { |
103 delete it->second; | 104 delete it->second; |
104 mapping_.erase(it); | 105 mapping_.erase(it); |
105 } | 106 } |
106 | 107 |
107 // For unit tests, we also remove the factory function both so we don't | 108 // 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 | 109 // 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 | 110 // object that lives at the same address (see other comments about unit tests |
110 // in this file). | 111 // in this file). |
111 factories_.erase(profile); | 112 factories_.erase(profile); |
112 | 113 |
113 ProfileKeyedBaseFactory::ProfileDestroyed(profile); | 114 ProfileKeyedBaseFactory::ProfileDestroyed(profile); |
114 } | 115 } |
115 | 116 |
116 void ProfileKeyedServiceFactory::SetEmptyTestingFactory( | 117 void ProfileKeyedServiceFactory::SetEmptyTestingFactory( |
117 Profile* profile) { | 118 content::BrowserContext* profile) { |
118 SetTestingFactory(profile, NULL); | 119 SetTestingFactory(profile, NULL); |
119 } | 120 } |
120 | 121 |
121 void ProfileKeyedServiceFactory::CreateServiceNow(Profile* profile) { | 122 void ProfileKeyedServiceFactory::CreateServiceNow(content::BrowserContext* profi
le) { |
122 GetServiceForProfile(profile, true); | 123 GetServiceForProfile(profile, true); |
123 } | 124 } |
OLD | NEW |