browser_context_dependency_manager.h revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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 "base/memory/singleton.h" 11#include "components/keyed_service/core/dependency_graph.h" 12#include "components/keyed_service/core/keyed_service_export.h" 13 14#ifndef NDEBUG 15#include <set> 16#endif 17 18class BrowserContextKeyedBaseFactory; 19 20namespace content { 21class BrowserContext; 22} 23 24namespace user_prefs { 25class PrefRegistrySyncable; 26} 27 28// A singleton that listens for context destruction notifications and 29// rebroadcasts them to each BrowserContextKeyedBaseFactory in a safe order 30// based on the stated dependencies by each service. 31class KEYED_SERVICE_EXPORT BrowserContextDependencyManager { 32 public: 33 // Adds/Removes a component from our list of live components. Removing will 34 // also remove live dependency links. 35 void AddComponent(BrowserContextKeyedBaseFactory* component); 36 void RemoveComponent(BrowserContextKeyedBaseFactory* component); 37 38 // Adds a dependency between two factories. 39 void AddEdge(BrowserContextKeyedBaseFactory* depended, 40 BrowserContextKeyedBaseFactory* dependee); 41 42 // Registers profile-specific preferences for all services via |registry|. 43 // |context| should be the BrowserContext containing |registry| and is used as 44 // a key to prevent multiple registrations on the same BrowserContext in 45 // tests. 46 void RegisterProfilePrefsForServices( 47 const content::BrowserContext* context, 48 user_prefs::PrefRegistrySyncable* registry); 49 50 // Called by each BrowserContext to alert us of its creation. Several 51 // services want to be started when a context is created. If you want your 52 // KeyedService to be started with the BrowserContext, override 53 // BrowserContextKeyedBaseFactory::ServiceIsCreatedWithBrowserContext() to 54 // return true. This method also registers any service-related preferences 55 // for non-incognito profiles. 56 void CreateBrowserContextServices(content::BrowserContext* context); 57 58 // Similar to CreateBrowserContextServices(), except this is used for creating 59 // test BrowserContexts - these contexts will not create services for any 60 // BrowserContextKeyedBaseFactories that return true from 61 // ServiceIsNULLWhileTesting(). 62 void CreateBrowserContextServicesForTest(content::BrowserContext* context); 63 64 // Called by each BrowserContext to alert us that we should destroy services 65 // associated with it. 66 void DestroyBrowserContextServices(content::BrowserContext* context); 67 68 // Registers a |callback| that will be called just before executing 69 // CreateBrowserContextServices() or CreateBrowserContextServicesForTest(). 70 // This can be useful in browser tests which wish to substitute test or mock 71 // builders for the keyed services. 72 scoped_ptr<base::CallbackList<void(content::BrowserContext*)>::Subscription> 73 RegisterWillCreateBrowserContextServicesCallbackForTesting( 74 const base::Callback<void(content::BrowserContext*)>& callback); 75 76#ifndef NDEBUG 77 // Debugging assertion called as part of GetServiceForBrowserContext in debug 78 // mode. This will NOTREACHED() whenever the user is trying to access a stale 79 // BrowserContext*. 80 void AssertBrowserContextWasntDestroyed(content::BrowserContext* context); 81 82 // Marks |context| as live (i.e., not stale). This method can be called as a 83 // safeguard against |AssertBrowserContextWasntDestroyed()| checks going off 84 // due to |context| aliasing a BrowserContext instance from a prior test 85 // (i.e., 0xWhatever might be created, be destroyed, and then a new 86 // BrowserContext object might be created at 0xWhatever). 87 void MarkBrowserContextLiveForTesting(content::BrowserContext* context); 88#endif 89 90 static BrowserContextDependencyManager* GetInstance(); 91 92 private: 93 friend class BrowserContextDependencyManagerUnittests; 94 friend struct DefaultSingletonTraits<BrowserContextDependencyManager>; 95 96 // Helper function used by CreateBrowserContextServices[ForTest]. 97 void DoCreateBrowserContextServices(content::BrowserContext* context, 98 bool is_testing_context); 99 100 BrowserContextDependencyManager(); 101 virtual ~BrowserContextDependencyManager(); 102 103#ifndef NDEBUG 104 void DumpBrowserContextDependencies(content::BrowserContext* context); 105#endif 106 107 DependencyGraph dependency_graph_; 108 109 // A list of callbacks to call just before executing 110 // CreateBrowserContextServices() or CreateBrowserContextServicesForTest(). 111 base::CallbackList<void(content::BrowserContext*)> 112 will_create_browser_context_services_callbacks_; 113 114#ifndef NDEBUG 115 // A list of context objects that have gone through the Shutdown() 116 // phase. These pointers are most likely invalid, but we keep track of their 117 // locations in memory so we can nicely assert if we're asked to do anything 118 // with them. 119 std::set<content::BrowserContext*> dead_context_pointers_; 120#endif 121}; 122 123#endif // COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_DEPENDENCY_MANAGER_H_ 124