1 // Copyright (c) 2012 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_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_DEPENDENCY_MANAGER_H_
6 #define COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_DEPENDENCY_MANAGER_H_
8 #include "base/memory/singleton.h"
9 #include "components/browser_context_keyed_service/browser_context_keyed_service_export.h"
10 #include "components/browser_context_keyed_service/dependency_graph.h"
16 class BrowserContextKeyedBaseFactory
;
22 // A singleton that listens for context destruction notifications and
23 // rebroadcasts them to each BrowserContextKeyedBaseFactory in a safe order
24 // based on the stated dependencies by each service.
25 class BROWSER_CONTEXT_KEYED_SERVICE_EXPORT BrowserContextDependencyManager
{
27 // Adds/Removes a component from our list of live components. Removing will
28 // also remove live dependency links.
29 void AddComponent(BrowserContextKeyedBaseFactory
* component
);
30 void RemoveComponent(BrowserContextKeyedBaseFactory
* component
);
32 // Adds a dependency between two factories.
33 void AddEdge(BrowserContextKeyedBaseFactory
* depended
,
34 BrowserContextKeyedBaseFactory
* dependee
);
36 // Called by each BrowserContext to alert us of its creation. Several services
37 // want to be started when a context is created. If you want your
38 // BrowserContextKeyedService to be started with the BrowserContext, override
39 // BrowserContextKeyedBaseFactory::ServiceIsCreatedWithBrowserContext() to
40 // return true. This method also registers any service-related preferences
41 // for non-incognito profiles.
42 void CreateBrowserContextServices(content::BrowserContext
* context
);
44 // Similar to CreateBrowserContextServices(), except this is used for creating
45 // test BrowserContexts - these contexts will not create services for any
46 // BrowserContextKeyedBaseFactories that return true from
47 // ServiceIsNULLWhileTesting(). Callers can pass |force_register_prefs| as
48 // true to have preferences registered even for incognito profiles - this
49 // allows tests to specify a standalone incognito profile without an
50 // associated normal profile.
51 void CreateBrowserContextServicesForTest(content::BrowserContext
* context
,
52 bool force_register_prefs
);
54 // Called by each BrowserContext to alert us that we should destroy services
55 // associated with it.
56 void DestroyBrowserContextServices(content::BrowserContext
* context
);
59 // Debugging assertion called as part of GetServiceForBrowserContext in debug
60 // mode. This will NOTREACHED() whenever the user is trying to access a stale
62 void AssertBrowserContextWasntDestroyed(content::BrowserContext
* context
);
65 static BrowserContextDependencyManager
* GetInstance();
68 friend class BrowserContextDependencyManagerUnittests
;
69 friend struct DefaultSingletonTraits
<BrowserContextDependencyManager
>;
71 // Helper function used by CreateBrowserContextServices[ForTest].
72 void DoCreateBrowserContextServices(content::BrowserContext
* context
,
73 bool is_testing_context
,
74 bool force_register_prefs
);
76 BrowserContextDependencyManager();
77 virtual ~BrowserContextDependencyManager();
80 void DumpBrowserContextDependencies(content::BrowserContext
* context
);
83 DependencyGraph dependency_graph_
;
86 // A list of context objects that have gone through the Shutdown()
87 // phase. These pointers are most likely invalid, but we keep track of their
88 // locations in memory so we can nicely assert if we're asked to do anything
90 std::set
<content::BrowserContext
*> dead_context_pointers_
;
94 #endif // COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_DEPENDENCY_MANAGER_H_