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/content/browser_context_keyed_service_factory
.h" | 5 #include "components/keyed_service/core/keyed_service_factory.h" |
6 | |
7 #include <map> | |
8 | 6 |
9 #include "base/logging.h" | 7 #include "base/logging.h" |
10 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
11 #include "components/keyed_service/content/browser_context_dependency_manager.h" | 9 #include "components/keyed_service/core/dependency_manager.h" |
12 #include "components/keyed_service/core/keyed_service.h" | 10 #include "components/keyed_service/core/keyed_service.h" |
13 #include "content/public/browser/browser_context.h" | |
14 | 11 |
15 void BrowserContextKeyedServiceFactory::SetTestingFactory( | 12 KeyedServiceFactory::KeyedServiceFactory(const char* name, |
16 content::BrowserContext* context, | 13 DependencyManager* manager) |
| 14 : KeyedServiceBaseFactory(name, manager) { |
| 15 } |
| 16 |
| 17 KeyedServiceFactory::~KeyedServiceFactory() { |
| 18 DCHECK(mapping_.empty()); |
| 19 } |
| 20 |
| 21 void KeyedServiceFactory::SetTestingFactory( |
| 22 base::SupportsUserData* context, |
17 TestingFactoryFunction testing_factory) { | 23 TestingFactoryFunction testing_factory) { |
18 // Destroying the context may cause us to lose data about whether |context| | 24 // Destroying the context may cause us to lose data about whether |context| |
19 // has our preferences registered on it (since the context object itself | 25 // has our preferences registered on it (since the context object itself |
20 // isn't dead). See if we need to readd it once we've gone through normal | 26 // isn't dead). See if we need to readd it once we've gone through normal |
21 // destruction. | 27 // destruction. |
22 bool add_context = ArePreferencesSetOn(context); | 28 bool add_context = ArePreferencesSetOn(context); |
23 | 29 |
24 #ifndef NDEBUG | 30 #ifndef NDEBUG |
25 // Ensure that |context| is not marked as stale (e.g., due to it aliasing an | 31 // Ensure that |context| is not marked as stale (e.g., due to it aliasing an |
26 // instance that was destroyed in an earlier test) in order to avoid accesses | 32 // instance that was destroyed in an earlier test) in order to avoid accesses |
27 // to |context| in |BrowserContextShutdown| from causing | 33 // to |context| in |BrowserContextShutdown| from causing |
28 // |AssertBrowserContextWasntDestroyed| to raise an error. | 34 // |AssertBrowserContextWasntDestroyed| to raise an error. |
29 MarkContextLiveForTesting(context); | 35 MarkContextLiveForTesting(context); |
30 #endif | 36 #endif |
31 | 37 |
32 // We have to go through the shutdown and destroy mechanisms because there | 38 // We have to go through the shutdown and destroy mechanisms because there |
33 // are unit tests that create a service on a context and then change the | 39 // are unit tests that create a service on a context and then change the |
34 // testing service mid-test. | 40 // testing service mid-test. |
35 BrowserContextShutdown(context); | 41 ContextShutdown(context); |
36 BrowserContextDestroyed(context); | 42 ContextDestroyed(context); |
37 | 43 |
38 if (add_context) | 44 if (add_context) |
39 MarkPreferencesSetOn(context); | 45 MarkPreferencesSetOn(context); |
40 | 46 |
41 testing_factories_[context] = testing_factory; | 47 testing_factories_[context] = testing_factory; |
42 } | 48 } |
43 | 49 |
44 KeyedService* BrowserContextKeyedServiceFactory::SetTestingFactoryAndUse( | 50 KeyedService* KeyedServiceFactory::SetTestingFactoryAndUse( |
45 content::BrowserContext* context, | 51 base::SupportsUserData* context, |
46 TestingFactoryFunction testing_factory) { | 52 TestingFactoryFunction testing_factory) { |
47 DCHECK(testing_factory); | 53 DCHECK(testing_factory); |
48 SetTestingFactory(context, testing_factory); | 54 SetTestingFactory(context, testing_factory); |
49 return GetServiceForBrowserContext(context, true); | 55 return GetServiceForContext(context, true); |
50 } | 56 } |
51 | 57 |
52 BrowserContextKeyedServiceFactory::BrowserContextKeyedServiceFactory( | 58 KeyedService* KeyedServiceFactory::GetServiceForContext( |
53 const char* name, | 59 base::SupportsUserData* context, |
54 BrowserContextDependencyManager* manager) | |
55 : BrowserContextKeyedBaseFactory(name, manager) {} | |
56 | |
57 BrowserContextKeyedServiceFactory::~BrowserContextKeyedServiceFactory() { | |
58 DCHECK(mapping_.empty()); | |
59 } | |
60 | |
61 KeyedService* BrowserContextKeyedServiceFactory::GetServiceForBrowserContext( | |
62 content::BrowserContext* context, | |
63 bool create) { | 60 bool create) { |
64 context = GetBrowserContextToUse(context); | 61 context = GetContextToUse(context); |
65 if (!context) | 62 if (!context) |
66 return NULL; | 63 return nullptr; |
67 | 64 |
68 // NOTE: If you modify any of the logic below, make sure to update the | 65 // NOTE: If you modify any of the logic below, make sure to update the |
69 // refcounted version in refcounted_context_keyed_service_factory.cc! | 66 // refcounted version in refcounted_context_keyed_service_factory.cc! |
70 BrowserContextKeyedServices::const_iterator it = mapping_.find(context); | 67 const auto& it = mapping_.find(context); |
71 if (it != mapping_.end()) | 68 if (it != mapping_.end()) |
72 return it->second; | 69 return it->second; |
73 | 70 |
74 // Object not found. | 71 // Object not found. |
75 if (!create) | 72 if (!create) |
76 return NULL; // And we're forbidden from creating one. | 73 return nullptr; // And we're forbidden from creating one. |
77 | 74 |
78 // Create new object. | 75 // Create new object. |
79 // Check to see if we have a per-BrowserContext testing factory that we should | 76 // Check to see if we have a per-context testing factory that we should use |
80 // use instead of default behavior. | 77 // instead of default behavior. |
81 KeyedService* service = NULL; | 78 KeyedService* service = nullptr; |
82 BrowserContextOverriddenTestingFunctions::const_iterator jt = | 79 const auto& jt = testing_factories_.find(context); |
83 testing_factories_.find(context); | |
84 if (jt != testing_factories_.end()) { | 80 if (jt != testing_factories_.end()) { |
85 if (jt->second) { | 81 if (jt->second) { |
86 if (!context->IsOffTheRecord()) | 82 if (!IsOffTheRecord(context)) |
87 RegisterUserPrefsOnBrowserContextForTest(context); | 83 RegisterUserPrefsOnContextForTest(context); |
88 service = jt->second(context); | 84 service = jt->second(context); |
89 } | 85 } |
90 } else { | 86 } else { |
91 service = BuildServiceInstanceFor(context); | 87 service = BuildServiceInstanceFor(context); |
92 } | 88 } |
93 | 89 |
94 Associate(context, service); | 90 Associate(context, service); |
95 return service; | 91 return service; |
96 } | 92 } |
97 | 93 |
98 void BrowserContextKeyedServiceFactory::Associate( | 94 void KeyedServiceFactory::Associate(base::SupportsUserData* context, |
99 content::BrowserContext* context, | 95 KeyedService* service) { |
100 KeyedService* service) { | |
101 DCHECK(!ContainsKey(mapping_, context)); | 96 DCHECK(!ContainsKey(mapping_, context)); |
102 mapping_.insert(std::make_pair(context, service)); | 97 mapping_.insert(std::make_pair(context, service)); |
103 } | 98 } |
104 | 99 |
105 void BrowserContextKeyedServiceFactory::Disassociate( | 100 void KeyedServiceFactory::Disassociate(base::SupportsUserData* context) { |
106 content::BrowserContext* context) { | 101 const auto& it = mapping_.find(context); |
107 BrowserContextKeyedServices::iterator it = mapping_.find(context); | |
108 if (it != mapping_.end()) { | 102 if (it != mapping_.end()) { |
109 delete it->second; | 103 delete it->second; |
110 mapping_.erase(it); | 104 mapping_.erase(it); |
111 } | 105 } |
112 } | 106 } |
113 | 107 |
114 void BrowserContextKeyedServiceFactory::BrowserContextShutdown( | 108 void KeyedServiceFactory::ContextShutdown(base::SupportsUserData* context) { |
115 content::BrowserContext* context) { | 109 const auto& it = mapping_.find(context); |
116 BrowserContextKeyedServices::iterator it = mapping_.find(context); | |
117 if (it != mapping_.end() && it->second) | 110 if (it != mapping_.end() && it->second) |
118 it->second->Shutdown(); | 111 it->second->Shutdown(); |
119 } | 112 } |
120 | 113 |
121 void BrowserContextKeyedServiceFactory::BrowserContextDestroyed( | 114 void KeyedServiceFactory::ContextDestroyed(base::SupportsUserData* context) { |
122 content::BrowserContext* context) { | |
123 Disassociate(context); | 115 Disassociate(context); |
124 | 116 |
125 // For unit tests, we also remove the factory function both so we don't | 117 // For unit tests, we also remove the factory function both so we don't |
126 // maintain a big map of dead pointers, but also since we may have a second | 118 // maintain a big map of dead pointers, but also since we may have a second |
127 // object that lives at the same address (see other comments about unit tests | 119 // object that lives at the same address (see other comments about unit tests |
128 // in this file). | 120 // in this file). |
129 testing_factories_.erase(context); | 121 testing_factories_.erase(context); |
130 | 122 |
131 BrowserContextKeyedBaseFactory::BrowserContextDestroyed(context); | 123 KeyedServiceBaseFactory::ContextDestroyed(context); |
132 } | 124 } |
133 | 125 |
134 void BrowserContextKeyedServiceFactory::SetEmptyTestingFactory( | 126 void KeyedServiceFactory::SetEmptyTestingFactory( |
135 content::BrowserContext* context) { | 127 base::SupportsUserData* context) { |
136 SetTestingFactory(context, NULL); | 128 SetTestingFactory(context, nullptr); |
137 } | 129 } |
138 | 130 |
139 bool BrowserContextKeyedServiceFactory::HasTestingFactory( | 131 bool KeyedServiceFactory::HasTestingFactory(base::SupportsUserData* context) { |
140 content::BrowserContext* context) { | |
141 return testing_factories_.find(context) != testing_factories_.end(); | 132 return testing_factories_.find(context) != testing_factories_.end(); |
142 } | 133 } |
143 | 134 |
144 void BrowserContextKeyedServiceFactory::CreateServiceNow( | 135 void KeyedServiceFactory::CreateServiceNow(base::SupportsUserData* context) { |
145 content::BrowserContext* context) { | 136 GetServiceForContext(context, true); |
146 GetServiceForBrowserContext(context, true); | |
147 } | 137 } |
OLD | NEW |