OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/keyed_service/core/keyed_service_factory.h" | 5 #include "components/keyed_service/core/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 "base/trace_event/trace_event.h" | 9 #include "base/trace_event/trace_event.h" |
10 #include "components/keyed_service/core/dependency_manager.h" | 10 #include "components/keyed_service/core/dependency_manager.h" |
11 #include "components/keyed_service/core/keyed_service.h" | 11 #include "components/keyed_service/core/keyed_service.h" |
12 | 12 |
13 KeyedServiceFactory::KeyedServiceFactory(const char* name, | 13 KeyedServiceFactory::KeyedServiceFactory(const char* name, |
14 DependencyManager* manager) | 14 DependencyManager* manager) |
15 : KeyedServiceBaseFactory(name, manager) { | 15 : KeyedServiceBaseFactory(name, manager) { |
16 } | 16 } |
17 | 17 |
18 KeyedServiceFactory::~KeyedServiceFactory() { | 18 KeyedServiceFactory::~KeyedServiceFactory() { |
19 DCHECK(mapping_.empty()); | 19 DCHECK(mapping_.empty()); |
20 } | 20 } |
21 | 21 |
22 void KeyedServiceFactory::SetTestingFactory( | 22 void KeyedServiceFactory::SetTestingFactory( |
23 base::SupportsUserData* context, | 23 base::SupportsUserData* context, |
24 TestingFactoryFunction testing_factory) { | 24 TestingFactoryFunction testing_factory) { |
25 context = GetUnderlyingContext(context); | |
Elliot Glaysher
2015/04/21 17:54:32
This seems like it could be very error prone. Is t
sdefresne
2015/04/22 08:53:15
Yes, all calls to GetUnderlyingContext/GetOriginal
Elliot Glaysher
2015/04/22 17:34:07
Thanks, that sounds like a good plan.
| |
25 // Destroying the context may cause us to lose data about whether |context| | 26 // Destroying the context may cause us to lose data about whether |context| |
26 // has our preferences registered on it (since the context object itself | 27 // has our preferences registered on it (since the context object itself |
27 // isn't dead). See if we need to readd it once we've gone through normal | 28 // isn't dead). See if we need to readd it once we've gone through normal |
28 // destruction. | 29 // destruction. |
29 bool add_context = ArePreferencesSetOn(context); | 30 bool add_context = ArePreferencesSetOn(context); |
30 | 31 |
31 #ifndef NDEBUG | 32 #ifndef NDEBUG |
32 // Ensure that |context| is not marked as stale (e.g., due to it aliasing an | 33 // Ensure that |context| is not marked as stale (e.g., due to it aliasing an |
33 // instance that was destroyed in an earlier test) in order to avoid accesses | 34 // instance that was destroyed in an earlier test) in order to avoid accesses |
34 // to |context| in |BrowserContextShutdown| from causing | 35 // to |context| in |BrowserContextShutdown| from causing |
(...skipping 10 matching lines...) Expand all Loading... | |
45 if (add_context) | 46 if (add_context) |
46 MarkPreferencesSetOn(context); | 47 MarkPreferencesSetOn(context); |
47 | 48 |
48 testing_factories_[context] = testing_factory; | 49 testing_factories_[context] = testing_factory; |
49 } | 50 } |
50 | 51 |
51 KeyedService* KeyedServiceFactory::SetTestingFactoryAndUse( | 52 KeyedService* KeyedServiceFactory::SetTestingFactoryAndUse( |
52 base::SupportsUserData* context, | 53 base::SupportsUserData* context, |
53 TestingFactoryFunction testing_factory) { | 54 TestingFactoryFunction testing_factory) { |
54 DCHECK(testing_factory); | 55 DCHECK(testing_factory); |
56 context = GetUnderlyingContext(context); | |
55 SetTestingFactory(context, testing_factory); | 57 SetTestingFactory(context, testing_factory); |
56 return GetServiceForContext(context, true); | 58 return GetServiceForContext(context, true); |
57 } | 59 } |
58 | 60 |
59 KeyedService* KeyedServiceFactory::GetServiceForContext( | 61 KeyedService* KeyedServiceFactory::GetServiceForContext( |
60 base::SupportsUserData* context, | 62 base::SupportsUserData* context, |
61 bool create) { | 63 bool create) { |
62 TRACE_EVENT0("browser,startup", "KeyedServiceFactory::GetServiceForContext"); | 64 TRACE_EVENT0("browser,startup", "KeyedServiceFactory::GetServiceForContext"); |
65 context = GetUnderlyingContext(context); | |
63 context = GetContextToUse(context); | 66 context = GetContextToUse(context); |
64 if (!context) | 67 if (!context) |
65 return nullptr; | 68 return nullptr; |
66 | 69 |
67 // NOTE: If you modify any of the logic below, make sure to update the | 70 // NOTE: If you modify any of the logic below, make sure to update the |
68 // refcounted version in refcounted_context_keyed_service_factory.cc! | 71 // refcounted version in refcounted_context_keyed_service_factory.cc! |
69 const auto& it = mapping_.find(context); | 72 const auto& it = mapping_.find(context); |
70 if (it != mapping_.end()) | 73 if (it != mapping_.end()) |
71 return it->second; | 74 return it->second; |
72 | 75 |
(...skipping 15 matching lines...) Expand all Loading... | |
88 } else { | 91 } else { |
89 service = BuildServiceInstanceFor(context); | 92 service = BuildServiceInstanceFor(context); |
90 } | 93 } |
91 | 94 |
92 Associate(context, service); | 95 Associate(context, service); |
93 return service; | 96 return service; |
94 } | 97 } |
95 | 98 |
96 void KeyedServiceFactory::Associate(base::SupportsUserData* context, | 99 void KeyedServiceFactory::Associate(base::SupportsUserData* context, |
97 KeyedService* service) { | 100 KeyedService* service) { |
101 context = GetUnderlyingContext(context); | |
98 DCHECK(!ContainsKey(mapping_, context)); | 102 DCHECK(!ContainsKey(mapping_, context)); |
99 mapping_.insert(std::make_pair(context, service)); | 103 mapping_.insert(std::make_pair(context, service)); |
100 } | 104 } |
101 | 105 |
102 void KeyedServiceFactory::Disassociate(base::SupportsUserData* context) { | 106 void KeyedServiceFactory::Disassociate(base::SupportsUserData* context) { |
107 context = GetUnderlyingContext(context); | |
103 const auto& it = mapping_.find(context); | 108 const auto& it = mapping_.find(context); |
104 if (it != mapping_.end()) { | 109 if (it != mapping_.end()) { |
105 delete it->second; | 110 delete it->second; |
106 mapping_.erase(it); | 111 mapping_.erase(it); |
107 } | 112 } |
108 } | 113 } |
109 | 114 |
110 void KeyedServiceFactory::ContextShutdown(base::SupportsUserData* context) { | 115 void KeyedServiceFactory::ContextShutdown(base::SupportsUserData* context) { |
116 context = GetUnderlyingContext(context); | |
111 const auto& it = mapping_.find(context); | 117 const auto& it = mapping_.find(context); |
112 if (it != mapping_.end() && it->second) | 118 if (it != mapping_.end() && it->second) |
113 it->second->Shutdown(); | 119 it->second->Shutdown(); |
114 } | 120 } |
115 | 121 |
116 void KeyedServiceFactory::ContextDestroyed(base::SupportsUserData* context) { | 122 void KeyedServiceFactory::ContextDestroyed(base::SupportsUserData* context) { |
123 context = GetUnderlyingContext(context); | |
117 Disassociate(context); | 124 Disassociate(context); |
118 | 125 |
119 // For unit tests, we also remove the factory function both so we don't | 126 // For unit tests, we also remove the factory function both so we don't |
120 // maintain a big map of dead pointers, but also since we may have a second | 127 // maintain a big map of dead pointers, but also since we may have a second |
121 // object that lives at the same address (see other comments about unit tests | 128 // object that lives at the same address (see other comments about unit tests |
122 // in this file). | 129 // in this file). |
123 testing_factories_.erase(context); | 130 testing_factories_.erase(context); |
124 | 131 |
125 KeyedServiceBaseFactory::ContextDestroyed(context); | 132 KeyedServiceBaseFactory::ContextDestroyed(context); |
126 } | 133 } |
127 | 134 |
128 void KeyedServiceFactory::SetEmptyTestingFactory( | 135 void KeyedServiceFactory::SetEmptyTestingFactory( |
129 base::SupportsUserData* context) { | 136 base::SupportsUserData* context) { |
137 context = GetUnderlyingContext(context); | |
130 SetTestingFactory(context, nullptr); | 138 SetTestingFactory(context, nullptr); |
131 } | 139 } |
132 | 140 |
133 bool KeyedServiceFactory::HasTestingFactory(base::SupportsUserData* context) { | 141 bool KeyedServiceFactory::HasTestingFactory(base::SupportsUserData* context) { |
142 context = GetUnderlyingContext(context); | |
134 return testing_factories_.find(context) != testing_factories_.end(); | 143 return testing_factories_.find(context) != testing_factories_.end(); |
135 } | 144 } |
136 | 145 |
137 void KeyedServiceFactory::CreateServiceNow(base::SupportsUserData* context) { | 146 void KeyedServiceFactory::CreateServiceNow(base::SupportsUserData* context) { |
147 context = GetUnderlyingContext(context); | |
138 GetServiceForContext(context, true); | 148 GetServiceForContext(context, true); |
139 } | 149 } |
OLD | NEW |