Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / keyed_service / content / browser_context_dependency_manager.h
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.
4
5 #ifndef COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_DEPENDENCY_MANAGER_H_
6 #define COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_DEPENDENCY_MANAGER_H_
7
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"
12
13 class BrowserContextKeyedBaseFactory;
14 template <typename T>
15 struct DefaultSingletonTraits;
16
17 namespace content {
18 class BrowserContext;
19 }
20
21 namespace user_prefs {
22 class PrefRegistrySyncable;
23 }
24
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 {
30  public:
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
34   // tests.
35   void RegisterProfilePrefsForServices(
36       const content::BrowserContext* context,
37       user_prefs::PrefRegistrySyncable* registry);
38
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);
46
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);
52
53   // Called by each BrowserContext to alert us that we should destroy services
54   // associated with it.
55   void DestroyBrowserContextServices(content::BrowserContext* context);
56
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);
64
65 #ifndef NDEBUG
66   // Debugging assertion called as part of GetServiceForBrowserContext in debug
67   // mode. This will NOTREACHED() whenever the user is trying to access a stale
68   // BrowserContext*.
69   void AssertBrowserContextWasntDestroyed(content::BrowserContext* context);
70
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);
77 #endif  // NDEBUG
78
79   static BrowserContextDependencyManager* GetInstance();
80
81  private:
82   friend class BrowserContextDependencyManagerUnittests;
83   friend struct DefaultSingletonTraits<BrowserContextDependencyManager>;
84
85   // Helper function used by CreateBrowserContextServices[ForTest].
86   void DoCreateBrowserContextServices(content::BrowserContext* context,
87                                       bool is_testing_context);
88
89   BrowserContextDependencyManager();
90   ~BrowserContextDependencyManager() override;
91
92 #ifndef NDEBUG
93   // DependencyManager:
94   void DumpContextDependencies(
95       const base::SupportsUserData* context) const final;
96 #endif  // NDEBUG
97
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_;
102 };
103
104 #endif  // COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_DEPENDENCY_MANAGER_H_