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 "components/keyed_service/core/dependency_manager.h"
11 #include "components/keyed_service/core/keyed_service_export.h"
13 class BrowserContextKeyedBaseFactory
;
17 struct DefaultSingletonTraits
;
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
32 : public DependencyManager
{
34 // Registers profile-specific preferences for all services via |registry|.
35 // |context| should be the BrowserContext containing |registry| and is used as
36 // a key to prevent multiple registrations on the same BrowserContext in
38 void RegisterProfilePrefsForServices(
39 content::BrowserContext
* context
,
40 user_prefs::PrefRegistrySyncable
* registry
);
42 // Called by each BrowserContext to alert us of its creation. Several
43 // services want to be started when a context is created. If you want your
44 // KeyedService to be started with the BrowserContext, override
45 // BrowserContextKeyedBaseFactory::ServiceIsCreatedWithBrowserContext() to
46 // return true. This method also registers any service-related preferences
47 // for non-incognito profiles.
48 void CreateBrowserContextServices(content::BrowserContext
* context
);
50 // Similar to CreateBrowserContextServices(), except this is used for creating
51 // test BrowserContexts - these contexts will not create services for any
52 // BrowserContextKeyedBaseFactories that return true from
53 // ServiceIsNULLWhileTesting().
54 void CreateBrowserContextServicesForTest(content::BrowserContext
* context
);
56 // Called by each BrowserContext to alert us that we should destroy services
57 // associated with it.
58 void DestroyBrowserContextServices(content::BrowserContext
* context
);
60 // Registers a |callback| that will be called just before executing
61 // CreateBrowserContextServices() or CreateBrowserContextServicesForTest().
62 // This can be useful in browser tests which wish to substitute test or mock
63 // builders for the keyed services.
64 scoped_ptr
<base::CallbackList
<void(content::BrowserContext
*)>::Subscription
>
65 RegisterWillCreateBrowserContextServicesCallbackForTesting(
66 const base::Callback
<void(content::BrowserContext
*)>& callback
);
69 // Debugging assertion called as part of GetServiceForBrowserContext in debug
70 // mode. This will NOTREACHED() whenever the user is trying to access a stale
72 void AssertBrowserContextWasntDestroyed(content::BrowserContext
* context
);
74 // Marks |context| as live (i.e., not stale). This method can be called as a
75 // safeguard against |AssertBrowserContextWasntDestroyed()| checks going off
76 // due to |context| aliasing a BrowserContext instance from a prior test
77 // (i.e., 0xWhatever might be created, be destroyed, and then a new
78 // BrowserContext object might be created at 0xWhatever).
79 void MarkBrowserContextLiveForTesting(content::BrowserContext
* context
);
82 static BrowserContextDependencyManager
* GetInstance();
85 friend class BrowserContextDependencyManagerUnittests
;
86 friend struct base::DefaultSingletonTraits
<BrowserContextDependencyManager
>;
88 // Helper function used by CreateBrowserContextServices[ForTest].
89 void DoCreateBrowserContextServices(content::BrowserContext
* context
,
90 bool is_testing_context
);
92 BrowserContextDependencyManager();
93 ~BrowserContextDependencyManager() override
;
97 void DumpContextDependencies(base::SupportsUserData
* context
) const final
;
100 // A list of callbacks to call just before executing
101 // CreateBrowserContextServices() or CreateBrowserContextServicesForTest().
102 base::CallbackList
<void(content::BrowserContext
*)>
103 will_create_browser_context_services_callbacks_
;
106 #endif // COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_DEPENDENCY_MANAGER_H_