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. Testing configuration is also 38 // done at this time. (If you want your BrowserContextKeyedService to be 39 // started with the BrowserContext, override BrowserContextKeyedBaseFactory:: 40 // ServiceIsCreatedWithBrowserContext() to return true.) 41 void CreateBrowserContextServices(content::BrowserContext* context, 42 bool is_testing_context); 43 44 // Called by each BrowserContext to alert us that we should destroy services 45 // associated with it. 46 void DestroyBrowserContextServices(content::BrowserContext* context); 47 48 #ifndef NDEBUG 49 // Debugging assertion called as part of GetServiceForBrowserContext in debug 50 // mode. This will NOTREACHED() whenever the user is trying to access a stale 51 // BrowserContext*. 52 void AssertBrowserContextWasntDestroyed(content::BrowserContext* context); 53 #endif 54 55 static BrowserContextDependencyManager* GetInstance(); 56 57 private: 58 friend class BrowserContextDependencyManagerUnittests; 59 friend struct DefaultSingletonTraits<BrowserContextDependencyManager>; 60 61 BrowserContextDependencyManager(); 62 virtual ~BrowserContextDependencyManager(); 63 64 #ifndef NDEBUG 65 void DumpBrowserContextDependencies(content::BrowserContext* context); 66 #endif 67 68 DependencyGraph dependency_graph_; 69 70 #ifndef NDEBUG 71 // A list of context objects that have gone through the Shutdown() 72 // phase. These pointers are most likely invalid, but we keep track of their 73 // locations in memory so we can nicely assert if we're asked to do anything 74 // with them. 75 std::set<content::BrowserContext*> dead_context_pointers_; 76 #endif 77 }; 78 79 #endif // COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_DEPENDENCY_MANAGER_H_ 80