1 // Copyright (c) 2011 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 CHROME_BROWSER_PROFILES_PROFILE_DEPENDENCY_MANAGER_H_ 6 #define CHROME_BROWSER_PROFILES_PROFILE_DEPENDENCY_MANAGER_H_ 7 8 #include <map> 9 #include <vector> 10 11 #include "base/memory/singleton.h" 12 13 class Profile; 14 class ProfileKeyedServiceFactory; 15 16 // A singleton that listens for profile destruction notifications and 17 // rebroadcasts them to each ProfileKeyedServiceFactory in a safe order based 18 // on the stated dependencies by each service. 19 class ProfileDependencyManager { 20 public: 21 // Adds/Removes a component from our list of live components. Removing will 22 // also remove live dependency links. 23 void AddComponent(ProfileKeyedServiceFactory* component); 24 void RemoveComponent(ProfileKeyedServiceFactory* component); 25 26 // Adds a dependency between two factories. 27 void AddEdge(ProfileKeyedServiceFactory* depended, 28 ProfileKeyedServiceFactory* dependee); 29 30 // Called by each Profile to alert us that we should destroy services 31 // associated with it. 32 // 33 // Why not use the existing PROFILE_DESTROYED notification? 34 // 35 // - Because we need to do everything here after the application has handled 36 // being notified about PROFILE_DESTROYED. 37 // - Because this class is a singleton and Singletons can't rely on 38 // NotificationService in unit tests because NotificationService is 39 // replaced in many tests. 40 void DestroyProfileServices(Profile* profile); 41 42 static ProfileDependencyManager* GetInstance(); 43 44 private: 45 friend class ProfileDependencyManagerUnittests; 46 friend struct DefaultSingletonTraits<ProfileDependencyManager>; 47 48 typedef std::multimap<ProfileKeyedServiceFactory*, 49 ProfileKeyedServiceFactory*> EdgeMap; 50 51 ProfileDependencyManager(); 52 virtual ~ProfileDependencyManager(); 53 54 // Using the dependency graph defined in |edges_|, fills |destruction_order_| 55 // so that Observe() can notify each ProfileKeyedServiceFactory in order. 56 void BuildDestructionOrder(); 57 58 std::vector<ProfileKeyedServiceFactory*> all_components_; 59 60 EdgeMap edges_; 61 62 std::vector<ProfileKeyedServiceFactory*> destruction_order_; 63 }; 64 65 #endif // CHROME_BROWSER_PROFILES_PROFILE_DEPENDENCY_MANAGER_H_ 66