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 namespace user_prefs { 23 class PrefRegistrySyncable; 24 } 25 26 // A singleton that listens for context destruction notifications and 27 // rebroadcasts them to each BrowserContextKeyedBaseFactory in a safe order 28 // based on the stated dependencies by each service. 29 class BROWSER_CONTEXT_KEYED_SERVICE_EXPORT BrowserContextDependencyManager { 30 public: 31 // Adds/Removes a component from our list of live components. Removing will 32 // also remove live dependency links. 33 void AddComponent(BrowserContextKeyedBaseFactory* component); 34 void RemoveComponent(BrowserContextKeyedBaseFactory* component); 35 36 // Adds a dependency between two factories. 37 void AddEdge(BrowserContextKeyedBaseFactory* depended, 38 BrowserContextKeyedBaseFactory* dependee); 39 40 // Registers profile-specific preferences for all services via |registry|. 41 // |context| should be the BrowserContext containing |registry| and is used as 42 // a key to prevent multiple registrations on the same BrowserContext in 43 // tests. 44 void RegisterProfilePrefsForServices( 45 const content::BrowserContext* context, 46 user_prefs::PrefRegistrySyncable* registry); 47 48 // Called by each BrowserContext to alert us of its creation. Several services 49 // want to be started when a context is created. If you want your 50 // BrowserContextKeyedService to be started with the BrowserContext, override 51 // BrowserContextKeyedBaseFactory::ServiceIsCreatedWithBrowserContext() to 52 // return true. This method also registers any service-related preferences 53 // for non-incognito profiles. 54 void CreateBrowserContextServices(content::BrowserContext* context); 55 56 // Similar to CreateBrowserContextServices(), except this is used for creating 57 // test BrowserContexts - these contexts will not create services for any 58 // BrowserContextKeyedBaseFactories that return true from 59 // ServiceIsNULLWhileTesting(). 60 void CreateBrowserContextServicesForTest(content::BrowserContext* context); 61 62 // Called by each BrowserContext to alert us that we should destroy services 63 // associated with it. 64 void DestroyBrowserContextServices(content::BrowserContext* context); 65 66 #ifndef NDEBUG 67 // Debugging assertion called as part of GetServiceForBrowserContext in debug 68 // mode. This will NOTREACHED() whenever the user is trying to access a stale 69 // BrowserContext*. 70 void AssertBrowserContextWasntDestroyed(content::BrowserContext* context); 71 #endif 72 73 static BrowserContextDependencyManager* GetInstance(); 74 75 private: 76 friend class BrowserContextDependencyManagerUnittests; 77 friend struct DefaultSingletonTraits<BrowserContextDependencyManager>; 78 79 // Helper function used by CreateBrowserContextServices[ForTest]. 80 void DoCreateBrowserContextServices(content::BrowserContext* context, 81 bool is_testing_context); 82 83 BrowserContextDependencyManager(); 84 virtual ~BrowserContextDependencyManager(); 85 86 #ifndef NDEBUG 87 void DumpBrowserContextDependencies(content::BrowserContext* context); 88 #endif 89 90 DependencyGraph dependency_graph_; 91 92 #ifndef NDEBUG 93 // A list of context objects that have gone through the Shutdown() 94 // phase. These pointers are most likely invalid, but we keep track of their 95 // locations in memory so we can nicely assert if we're asked to do anything 96 // with them. 97 std::set<content::BrowserContext*> dead_context_pointers_; 98 #endif 99 }; 100 101 #endif // COMPONENTS_BROWSER_CONTEXT_KEYED_SERVICE_BROWSER_CONTEXT_DEPENDENCY_MANAGER_H_ 102