Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / components / keyed_service / core / keyed_service_factory.cc
blob261ff29d556c319e143b8fe9e6646c77cf13a29d
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
3 // found in the LICENSE file.
5 #include "components/keyed_service/core/keyed_service_factory.h"
7 #include "base/logging.h"
8 #include "base/stl_util.h"
9 #include "components/keyed_service/core/dependency_manager.h"
10 #include "components/keyed_service/core/keyed_service.h"
12 KeyedServiceFactory::KeyedServiceFactory(const char* name,
13 DependencyManager* manager)
14 : KeyedServiceBaseFactory(name, manager) {
17 KeyedServiceFactory::~KeyedServiceFactory() {
18 DCHECK(mapping_.empty());
21 void KeyedServiceFactory::SetTestingFactory(
22 base::SupportsUserData* context,
23 TestingFactoryFunction testing_factory) {
24 // Destroying the context may cause us to lose data about whether |context|
25 // has our preferences registered on it (since the context object itself
26 // isn't dead). See if we need to readd it once we've gone through normal
27 // destruction.
28 bool add_context = ArePreferencesSetOn(context);
30 #ifndef NDEBUG
31 // Ensure that |context| is not marked as stale (e.g., due to it aliasing an
32 // instance that was destroyed in an earlier test) in order to avoid accesses
33 // to |context| in |BrowserContextShutdown| from causing
34 // |AssertBrowserContextWasntDestroyed| to raise an error.
35 MarkContextLiveForTesting(context);
36 #endif
38 // We have to go through the shutdown and destroy mechanisms because there
39 // are unit tests that create a service on a context and then change the
40 // testing service mid-test.
41 ContextShutdown(context);
42 ContextDestroyed(context);
44 if (add_context)
45 MarkPreferencesSetOn(context);
47 testing_factories_[context] = testing_factory;
50 KeyedService* KeyedServiceFactory::SetTestingFactoryAndUse(
51 base::SupportsUserData* context,
52 TestingFactoryFunction testing_factory) {
53 DCHECK(testing_factory);
54 SetTestingFactory(context, testing_factory);
55 return GetServiceForContext(context, true);
58 KeyedService* KeyedServiceFactory::GetServiceForContext(
59 base::SupportsUserData* context,
60 bool create) {
61 context = GetContextToUse(context);
62 if (!context)
63 return nullptr;
65 // NOTE: If you modify any of the logic below, make sure to update the
66 // refcounted version in refcounted_context_keyed_service_factory.cc!
67 const auto& it = mapping_.find(context);
68 if (it != mapping_.end())
69 return it->second;
71 // Object not found.
72 if (!create)
73 return nullptr; // And we're forbidden from creating one.
75 // Create new object.
76 // Check to see if we have a per-context testing factory that we should use
77 // instead of default behavior.
78 KeyedService* service = nullptr;
79 const auto& jt = testing_factories_.find(context);
80 if (jt != testing_factories_.end()) {
81 if (jt->second) {
82 if (!IsOffTheRecord(context))
83 RegisterUserPrefsOnContextForTest(context);
84 service = jt->second(context);
86 } else {
87 service = BuildServiceInstanceFor(context);
90 Associate(context, service);
91 return service;
94 void KeyedServiceFactory::Associate(base::SupportsUserData* context,
95 KeyedService* service) {
96 DCHECK(!ContainsKey(mapping_, context));
97 mapping_.insert(std::make_pair(context, service));
100 void KeyedServiceFactory::Disassociate(base::SupportsUserData* context) {
101 const auto& it = mapping_.find(context);
102 if (it != mapping_.end()) {
103 delete it->second;
104 mapping_.erase(it);
108 void KeyedServiceFactory::ContextShutdown(base::SupportsUserData* context) {
109 const auto& it = mapping_.find(context);
110 if (it != mapping_.end() && it->second)
111 it->second->Shutdown();
114 void KeyedServiceFactory::ContextDestroyed(base::SupportsUserData* context) {
115 Disassociate(context);
117 // For unit tests, we also remove the factory function both so we don't
118 // maintain a big map of dead pointers, but also since we may have a second
119 // object that lives at the same address (see other comments about unit tests
120 // in this file).
121 testing_factories_.erase(context);
123 KeyedServiceBaseFactory::ContextDestroyed(context);
126 void KeyedServiceFactory::SetEmptyTestingFactory(
127 base::SupportsUserData* context) {
128 SetTestingFactory(context, nullptr);
131 bool KeyedServiceFactory::HasTestingFactory(base::SupportsUserData* context) {
132 return testing_factories_.find(context) != testing_factories_.end();
135 void KeyedServiceFactory::CreateServiceNow(base::SupportsUserData* context) {
136 GetServiceForContext(context, true);