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 // TODO(phajdan.jr): Figure out the next steps here. |
58 if (!profile) | 58 // profile = GetProfileToUse(profile); |
59 return NULL; | 59 // if (!profile) |
| 60 // return NULL; |
60 | 61 |
61 // NOTE: If you modify any of the logic below, make sure to update the | 62 // NOTE: If you modify any of the logic below, make sure to update the |
62 // non-refcounted version in profile_keyed_service_factory.cc! | 63 // non-refcounted version in profile_keyed_service_factory.cc! |
63 RefCountedStorage::const_iterator it = mapping_.find(profile); | 64 RefCountedStorage::const_iterator it = mapping_.find(profile); |
64 if (it != mapping_.end()) | 65 if (it != mapping_.end()) |
65 return it->second; | 66 return it->second; |
66 | 67 |
67 // Object not found. | 68 // Object not found. |
68 if (!create) | 69 if (!create) |
69 return NULL; // And we're forbidden from creating one. | 70 return NULL; // And we're forbidden from creating one. |
(...skipping 11 matching lines...) Expand all Loading... |
81 } | 82 } |
82 } else { | 83 } else { |
83 service = BuildServiceInstanceFor(profile); | 84 service = BuildServiceInstanceFor(profile); |
84 } | 85 } |
85 | 86 |
86 Associate(profile, service); | 87 Associate(profile, service); |
87 return service; | 88 return service; |
88 } | 89 } |
89 | 90 |
90 void RefcountedProfileKeyedServiceFactory::Associate( | 91 void RefcountedProfileKeyedServiceFactory::Associate( |
91 Profile* profile, | 92 content::BrowserContext* profile, |
92 const scoped_refptr<RefcountedProfileKeyedService>& service) { | 93 const scoped_refptr<RefcountedProfileKeyedService>& service) { |
93 DCHECK(!ContainsKey(mapping_, profile)); | 94 DCHECK(!ContainsKey(mapping_, profile)); |
94 mapping_.insert(std::make_pair(profile, service)); | 95 mapping_.insert(std::make_pair(profile, service)); |
95 } | 96 } |
96 | 97 |
97 void RefcountedProfileKeyedServiceFactory::ProfileShutdown(Profile* profile) { | 98 void RefcountedProfileKeyedServiceFactory::ProfileShutdown(content::BrowserConte
xt* 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(content::BrowserCont
ext* profile) { |
104 // We "merely" drop our reference to the service. Hopefully this will cause | 105 // We "merely" drop our reference to the service. Hopefully this will cause |
105 // the service to be destroyed. If not, oh well. | 106 // the service to be destroyed. If not, oh well. |
106 mapping_.erase(profile); | 107 mapping_.erase(profile); |
107 | 108 |
108 // 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 |
109 // 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 |
110 // 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 |
111 // in this file). | 112 // in this file). |
112 factories_.erase(profile); | 113 factories_.erase(profile); |
113 | 114 |
114 ProfileKeyedBaseFactory::ProfileDestroyed(profile); | 115 ProfileKeyedBaseFactory::ProfileDestroyed(profile); |
115 } | 116 } |
116 | 117 |
117 void RefcountedProfileKeyedServiceFactory::SetEmptyTestingFactory( | 118 void RefcountedProfileKeyedServiceFactory::SetEmptyTestingFactory( |
118 Profile* profile) { | 119 content::BrowserContext* profile) { |
119 SetTestingFactory(profile, NULL); | 120 SetTestingFactory(profile, NULL); |
120 } | 121 } |
121 | 122 |
122 void RefcountedProfileKeyedServiceFactory::CreateServiceNow(Profile* profile) { | 123 void RefcountedProfileKeyedServiceFactory::CreateServiceNow(content::BrowserCont
ext* profile) { |
123 GetServiceForProfile(profile, true); | 124 GetServiceForProfile(profile, true); |
124 } | 125 } |
OLD | NEW |