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
;
15 struct DefaultSingletonTraits
;
21 namespace user_prefs
{
22 class PrefRegistrySyncable
;
25 // A singleton that listens for context destruction notifications and
26 // rebroadcasts them to each BrowserContextKeyedBaseFactory in a safe order
27 // based on the stated dependencies by each service.
28 class KEYED_SERVICE_EXPORT BrowserContextDependencyManager
29 : public DependencyManager
{
31 // Registers profile-specific preferences for all services via |registry|.
32 // |context| should be the BrowserContext containing |registry| and is used as
33 // a key to prevent multiple registrations on the same BrowserContext in
35 void RegisterProfilePrefsForServices(
36 const content::BrowserContext
* context
,
37 user_prefs::PrefRegistrySyncable
* registry
);
39 // Called by each BrowserContext to alert us of its creation. Several
40 // services want to be started when a context is created. If you want your
41 // KeyedService to be started with the BrowserContext, override
42 // BrowserContextKeyedBaseFactory::ServiceIsCreatedWithBrowserContext() to
43 // return true. This method also registers any service-related preferences
44 // for non-incognito profiles.
45 void CreateBrowserContextServices(content::BrowserContext
* context
);
47 // Similar to CreateBrowserContextServices(), except this is used for creating
48 // test BrowserContexts - these contexts will not create services for any
49 // BrowserContextKeyedBaseFactories that return true from
50 // ServiceIsNULLWhileTesting().
51 void CreateBrowserContextServicesForTest(content::BrowserContext
* context
);
53 // Called by each BrowserContext to alert us that we should destroy services
54 // associated with it.
55 void DestroyBrowserContextServices(content::BrowserContext
* context
);
57 // Registers a |callback| that will be called just before executing
58 // CreateBrowserContextServices() or CreateBrowserContextServicesForTest().
59 // This can be useful in browser tests which wish to substitute test or mock
60 // builders for the keyed services.
61 scoped_ptr
<base::CallbackList
<void(content::BrowserContext
*)>::Subscription
>
62 RegisterWillCreateBrowserContextServicesCallbackForTesting(
63 const base::Callback
<void(content::BrowserContext
*)>& callback
);
66 // Debugging assertion called as part of GetServiceForBrowserContext in debug
67 // mode. This will NOTREACHED() whenever the user is trying to access a stale
69 void AssertBrowserContextWasntDestroyed(content::BrowserContext
* context
);
71 // Marks |context| as live (i.e., not stale). This method can be called as a
72 // safeguard against |AssertBrowserContextWasntDestroyed()| checks going off
73 // due to |context| aliasing a BrowserContext instance from a prior test
74 // (i.e., 0xWhatever might be created, be destroyed, and then a new
75 // BrowserContext object might be created at 0xWhatever).
76 void MarkBrowserContextLiveForTesting(content::BrowserContext
* context
);
79 static BrowserContextDependencyManager
* GetInstance();
82 friend class BrowserContextDependencyManagerUnittests
;
83 friend struct DefaultSingletonTraits
<BrowserContextDependencyManager
>;
85 // Helper function used by CreateBrowserContextServices[ForTest].
86 void DoCreateBrowserContextServices(content::BrowserContext
* context
,
87 bool is_testing_context
);
89 BrowserContextDependencyManager();
90 ~BrowserContextDependencyManager() override
;
94 void DumpContextDependencies(
95 const base::SupportsUserData
* context
) const final
;
98 // A list of callbacks to call just before executing
99 // CreateBrowserContextServices() or CreateBrowserContextServicesForTest().
100 base::CallbackList
<void(content::BrowserContext
*)>
101 will_create_browser_context_services_callbacks_
;
104 #endif // COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_DEPENDENCY_MANAGER_H_