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_CONTENT_BROWSER_CONTEXT_DEPENDENCY_MANAGER_H_
6 #define COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_DEPENDENCY_MANAGER_H_
8 #include "base/callback_forward.h"
9 #include "base/callback_list.h"
10 #include "base/memory/singleton.h"
11 #include "components/keyed_service/core/dependency_graph.h"
12 #include "components/keyed_service/core/keyed_service_export.h"
18 class BrowserContextKeyedBaseFactory
;
24 namespace user_prefs
{
25 class PrefRegistrySyncable
;
28 // A singleton that listens for context destruction notifications and
29 // rebroadcasts them to each BrowserContextKeyedBaseFactory in a safe order
30 // based on the stated dependencies by each service.
31 class KEYED_SERVICE_EXPORT BrowserContextDependencyManager
{
33 // Adds/Removes a component from our list of live components. Removing will
34 // also remove live dependency links.
35 void AddComponent(BrowserContextKeyedBaseFactory
* component
);
36 void RemoveComponent(BrowserContextKeyedBaseFactory
* component
);
38 // Adds a dependency between two factories.
39 void AddEdge(BrowserContextKeyedBaseFactory
* depended
,
40 BrowserContextKeyedBaseFactory
* dependee
);
42 // Registers profile-specific preferences for all services via |registry|.
43 // |context| should be the BrowserContext containing |registry| and is used as
44 // a key to prevent multiple registrations on the same BrowserContext in
46 void RegisterProfilePrefsForServices(
47 const content::BrowserContext
* context
,
48 user_prefs::PrefRegistrySyncable
* registry
);
50 // Called by each BrowserContext to alert us of its creation. Several
51 // services want to be started when a context is created. If you want your
52 // KeyedService to be started with the BrowserContext, override
53 // BrowserContextKeyedBaseFactory::ServiceIsCreatedWithBrowserContext() to
54 // return true. This method also registers any service-related preferences
55 // for non-incognito profiles.
56 void CreateBrowserContextServices(content::BrowserContext
* context
);
58 // Similar to CreateBrowserContextServices(), except this is used for creating
59 // test BrowserContexts - these contexts will not create services for any
60 // BrowserContextKeyedBaseFactories that return true from
61 // ServiceIsNULLWhileTesting().
62 void CreateBrowserContextServicesForTest(content::BrowserContext
* context
);
64 // Called by each BrowserContext to alert us that we should destroy services
65 // associated with it.
66 void DestroyBrowserContextServices(content::BrowserContext
* context
);
68 // Registers a |callback| that will be called just before executing
69 // CreateBrowserContextServices() or CreateBrowserContextServicesForTest().
70 // This can be useful in browser tests which wish to substitute test or mock
71 // builders for the keyed services.
72 scoped_ptr
<base::CallbackList
<void(content::BrowserContext
*)>::Subscription
>
73 RegisterWillCreateBrowserContextServicesCallbackForTesting(
74 const base::Callback
<void(content::BrowserContext
*)>& callback
);
77 // Debugging assertion called as part of GetServiceForBrowserContext in debug
78 // mode. This will NOTREACHED() whenever the user is trying to access a stale
80 void AssertBrowserContextWasntDestroyed(content::BrowserContext
* context
);
82 // Marks |context| as live (i.e., not stale). This method can be called as a
83 // safeguard against |AssertBrowserContextWasntDestroyed()| checks going off
84 // due to |context| aliasing a BrowserContext instance from a prior test
85 // (i.e., 0xWhatever might be created, be destroyed, and then a new
86 // BrowserContext object might be created at 0xWhatever).
87 void MarkBrowserContextLiveForTesting(content::BrowserContext
* context
);
90 static BrowserContextDependencyManager
* GetInstance();
93 friend class BrowserContextDependencyManagerUnittests
;
94 friend struct DefaultSingletonTraits
<BrowserContextDependencyManager
>;
96 // Helper function used by CreateBrowserContextServices[ForTest].
97 void DoCreateBrowserContextServices(content::BrowserContext
* context
,
98 bool is_testing_context
);
100 BrowserContextDependencyManager();
101 virtual ~BrowserContextDependencyManager();
104 void DumpBrowserContextDependencies(content::BrowserContext
* context
);
107 DependencyGraph dependency_graph_
;
109 // A list of callbacks to call just before executing
110 // CreateBrowserContextServices() or CreateBrowserContextServicesForTest().
111 base::CallbackList
<void(content::BrowserContext
*)>
112 will_create_browser_context_services_callbacks_
;
115 // A list of context objects that have gone through the Shutdown()
116 // phase. These pointers are most likely invalid, but we keep track of their
117 // locations in memory so we can nicely assert if we're asked to do anything
119 std::set
<content::BrowserContext
*> dead_context_pointers_
;
123 #endif // COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_DEPENDENCY_MANAGER_H_