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 #ifndef COMPONENTS_KEYED_SERVICE_CORE_DEPENDENCY_MANAGER_H_
6 #define COMPONENTS_KEYED_SERVICE_CORE_DEPENDENCY_MANAGER_H_
10 #include "components/keyed_service/core/dependency_graph.h"
11 #include "components/keyed_service/core/keyed_service_export.h"
17 class KeyedServiceBaseFactory
;
21 class SupportsUserData
;
24 namespace user_prefs
{
25 class PrefRegistrySyncable
;
28 // DependencyManager manages dependency between KeyedServiceBaseFactory
29 // broadcasting the context creation and destruction to each factory in
30 // a safe order based on the stated dependencies.
31 class KEYED_SERVICE_EXPORT DependencyManager
{
34 virtual ~DependencyManager();
36 // Adds/Removes a component from our list of live components. Removing will
37 // also remove live dependency links.
38 void AddComponent(KeyedServiceBaseFactory
* component
);
39 void RemoveComponent(KeyedServiceBaseFactory
* component
);
41 // Adds a dependency between two factories.
42 void AddEdge(KeyedServiceBaseFactory
* depended
,
43 KeyedServiceBaseFactory
* dependee
);
45 // Registers preferences for all services via |registry| associated with
46 // |context| (the association is managed by the embedder). The |context|
47 // is used as a key to prevent multiple registration during tests.
48 void RegisterPrefsForServices(const base::SupportsUserData
* context
,
49 user_prefs::PrefRegistrySyncable
* registry
);
51 // Called upon creation of |context| to create services that want to be
52 // started at the creation of a context and register service-related
55 // To have a KeyedService started when a context is created the method
56 // KeyedServiceBaseFactory::ServiceIsCreatedWithContext() must be overridden
59 // If |is_testing_context| then the service will not be started unless the
60 // method KeyedServiceBaseFactory::ServiceIsNULLWhileTesting() return false.
61 void CreateContextServices(base::SupportsUserData
* context
,
62 bool is_testing_context
);
64 // Called upon destruction of |context| to destroy all services associated
66 void DestroyContextServices(base::SupportsUserData
* context
);
69 // Debugging assertion called as part of GetServiceForContext() in debug
70 // mode. This will NOTREACHED() whenever the |context| is considered stale.
71 void AssertContextWasntDestroyed(const base::SupportsUserData
* context
);
73 // Marks |context| as live (i.e., not stale). This method can be called as a
74 // safeguard against |AssertContextWasntDestroyed()| checks going off due to
75 // |context| aliasing am instance from a prior test (i.e., 0xWhatever might
76 // be created, be destroyed, and then a new object might be created at
78 void MarkContextLiveForTesting(const base::SupportsUserData
* context
);
80 // Dumps service dependency graph as a Graphviz dot file |dot_file| with a
81 // title |top_level_name|. Helper for |DumpContextDependencies|.
82 void DumpDependenciesAsGraphviz(const std::string
& top_level_name
,
83 const base::FilePath
& dot_file
) const;
87 friend class KeyedServiceBaseFactory
;
90 // Hook for subclass to dump the dependency graph of service for |context|.
91 virtual void DumpContextDependencies(
92 const base::SupportsUserData
* context
) const = 0;
95 DependencyGraph dependency_graph_
;
98 // A list of context objects that have gone through the Shutdown() phase.
99 // These pointers are most likely invalid, but we keep track of their
100 // locations in memory so we can nicely assert if we're asked to do anything
102 std::set
<const base::SupportsUserData
*> dead_context_pointers_
;
106 #endif // COMPONENTS_KEYED_SERVICE_CORE_DEPENDENCY_MANAGER_H_