browser_context_dependency_manager.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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/memory/singleton.h" 9#include "components/keyed_service/core/dependency_graph.h" 10#include "components/keyed_service/core/keyed_service_export.h" 11 12#ifndef NDEBUG 13#include <set> 14#endif 15 16class BrowserContextKeyedBaseFactory; 17 18namespace content { 19class BrowserContext; 20} 21 22namespace user_prefs { 23class 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. 29class 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 49 // services want to be started when a context is created. If you want your 50 // KeyedService 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 72 // Marks |context| as live (i.e., not stale). This method can be called as a 73 // safeguard against |AssertBrowserContextWasntDestroyed()| checks going off 74 // due to |context| aliasing a BrowserContext instance from a prior test 75 // (i.e., 0xWhatever might be created, be destroyed, and then a new 76 // BrowserContext object might be created at 0xWhatever). 77 void MarkBrowserContextLiveForTesting(content::BrowserContext* context); 78#endif 79 80 static BrowserContextDependencyManager* GetInstance(); 81 82 private: 83 friend class BrowserContextDependencyManagerUnittests; 84 friend struct DefaultSingletonTraits<BrowserContextDependencyManager>; 85 86 // Helper function used by CreateBrowserContextServices[ForTest]. 87 void DoCreateBrowserContextServices(content::BrowserContext* context, 88 bool is_testing_context); 89 90 BrowserContextDependencyManager(); 91 virtual ~BrowserContextDependencyManager(); 92 93#ifndef NDEBUG 94 void DumpBrowserContextDependencies(content::BrowserContext* context); 95#endif 96 97 DependencyGraph dependency_graph_; 98 99#ifndef NDEBUG 100 // A list of context objects that have gone through the Shutdown() 101 // phase. These pointers are most likely invalid, but we keep track of their 102 // locations in memory so we can nicely assert if we're asked to do anything 103 // with them. 104 std::set<content::BrowserContext*> dead_context_pointers_; 105#endif 106}; 107 108#endif // COMPONENTS_KEYED_SERVICE_CONTENT_BROWSER_CONTEXT_DEPENDENCY_MANAGER_H_ 109