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/dependency_manager.h"
8 #include "base/logging.h"
9 #include "base/supports_user_data.h"
10 #include "components/keyed_service/core/keyed_service_base_factory.h"
13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h"
17 DependencyManager::DependencyManager() {
20 DependencyManager::~DependencyManager() {
23 void DependencyManager::AddComponent(KeyedServiceBaseFactory
* component
) {
24 dependency_graph_
.AddNode(component
);
27 void DependencyManager::RemoveComponent(KeyedServiceBaseFactory
* component
) {
28 dependency_graph_
.RemoveNode(component
);
31 void DependencyManager::AddEdge(KeyedServiceBaseFactory
* depended
,
32 KeyedServiceBaseFactory
* dependee
) {
33 dependency_graph_
.AddEdge(depended
, dependee
);
36 void DependencyManager::RegisterPrefsForServices(
37 const base::SupportsUserData
* context
,
38 user_prefs::PrefRegistrySyncable
* pref_registry
) {
39 std::vector
<DependencyNode
*> construction_order
;
40 if (!dependency_graph_
.GetConstructionOrder(&construction_order
)) {
44 for (const auto& dependency_node
: construction_order
) {
45 KeyedServiceBaseFactory
* factory
=
46 static_cast<KeyedServiceBaseFactory
*>(dependency_node
);
47 factory
->RegisterPrefsIfNecessaryForContext(context
, pref_registry
);
51 void DependencyManager::CreateContextServices(base::SupportsUserData
* context
,
52 bool is_testing_context
) {
54 MarkContextLiveForTesting(context
);
57 std::vector
<DependencyNode
*> construction_order
;
58 if (!dependency_graph_
.GetConstructionOrder(&construction_order
)) {
63 DumpContextDependencies(context
);
66 for (const auto& dependency_node
: construction_order
) {
67 KeyedServiceBaseFactory
* factory
=
68 static_cast<KeyedServiceBaseFactory
*>(dependency_node
);
69 if (is_testing_context
&& factory
->ServiceIsNULLWhileTesting() &&
70 !factory
->HasTestingFactory(context
)) {
71 factory
->SetEmptyTestingFactory(context
);
72 } else if (factory
->ServiceIsCreatedWithContext()) {
73 factory
->CreateServiceNow(context
);
78 void DependencyManager::DestroyContextServices(
79 base::SupportsUserData
* context
) {
80 std::vector
<DependencyNode
*> destruction_order
;
81 if (!dependency_graph_
.GetDestructionOrder(&destruction_order
)) {
86 DumpContextDependencies(context
);
89 for (const auto& dependency_node
: destruction_order
) {
90 KeyedServiceBaseFactory
* factory
=
91 static_cast<KeyedServiceBaseFactory
*>(dependency_node
);
92 factory
->ContextShutdown(context
);
96 // The context is now dead to the rest of the program.
97 dead_context_pointers_
.insert(context
);
100 for (const auto& dependency_node
: destruction_order
) {
101 KeyedServiceBaseFactory
* factory
=
102 static_cast<KeyedServiceBaseFactory
*>(dependency_node
);
103 factory
->ContextDestroyed(context
);
108 void DependencyManager::AssertContextWasntDestroyed(
109 const base::SupportsUserData
* context
) {
110 if (dead_context_pointers_
.find(context
) != dead_context_pointers_
.end()) {
111 NOTREACHED() << "Attempted to access a context that was ShutDown(). "
112 << "This is most likely a heap smasher in progress. After "
113 << "KeyedService::Shutdown() completes, your service MUST "
114 << "NOT refer to depended services again.";
118 void DependencyManager::MarkContextLiveForTesting(
119 const base::SupportsUserData
* context
) {
120 dead_context_pointers_
.erase(context
);
125 std::string
KeyedServiceBaseFactoryGetNodeName(DependencyNode
* node
) {
126 return static_cast<KeyedServiceBaseFactory
*>(node
)->name();
131 void DependencyManager::DumpDependenciesAsGraphviz(
132 const std::string
& top_level_name
,
133 const base::FilePath
& dot_file
) const {
134 DCHECK(!dot_file
.empty());
135 std::string contents
= dependency_graph_
.DumpAsGraphviz(
136 top_level_name
, base::Bind(&KeyedServiceBaseFactoryGetNodeName
));
137 base::WriteFile(dot_file
, contents
.c_str(), contents
.size());