- add sources.
[platform/framework/web/crosswalk.git] / src / components / browser_context_keyed_service / browser_context_dependency_manager.h
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.
4
5 #ifndef COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_DEPENDENCY_MANAGER_H_
6 #define COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_DEPENDENCY_MANAGER_H_
7
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"
11
12 #ifndef NDEBUG
13 #include <set>
14 #endif
15
16 class BrowserContextKeyedBaseFactory;
17
18 namespace content {
19 class BrowserContext;
20 }
21
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 {
26  public:
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);
31
32   // Adds a dependency between two factories.
33   void AddEdge(BrowserContextKeyedBaseFactory* depended,
34                BrowserContextKeyedBaseFactory* dependee);
35
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);
43
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);
53
54   // Called by each BrowserContext to alert us that we should destroy services
55   // associated with it.
56   void DestroyBrowserContextServices(content::BrowserContext* context);
57
58 #ifndef NDEBUG
59   // Debugging assertion called as part of GetServiceForBrowserContext in debug
60   // mode. This will NOTREACHED() whenever the user is trying to access a stale
61   // BrowserContext*.
62   void AssertBrowserContextWasntDestroyed(content::BrowserContext* context);
63 #endif
64
65   static BrowserContextDependencyManager* GetInstance();
66
67  private:
68   friend class BrowserContextDependencyManagerUnittests;
69   friend struct DefaultSingletonTraits<BrowserContextDependencyManager>;
70
71   // Helper function used by CreateBrowserContextServices[ForTest].
72   void DoCreateBrowserContextServices(content::BrowserContext* context,
73                                       bool is_testing_context,
74                                       bool force_register_prefs);
75
76   BrowserContextDependencyManager();
77   virtual ~BrowserContextDependencyManager();
78
79 #ifndef NDEBUG
80   void DumpBrowserContextDependencies(content::BrowserContext* context);
81 #endif
82
83   DependencyGraph dependency_graph_;
84
85 #ifndef NDEBUG
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
89   // with them.
90   std::set<content::BrowserContext*> dead_context_pointers_;
91 #endif
92 };
93
94 #endif  // COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_DEPENDENCY_MANAGER_H_