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.
5 #ifndef CHROME_BROWSER_PROFILES_PROFILE_DEPENDENCY_MANAGER_H_
6 #define CHROME_BROWSER_PROFILES_PROFILE_DEPENDENCY_MANAGER_H_
11 #include "base/memory/singleton.h"
19 class ProfileKeyedBaseFactory
;
21 // A singleton that listens for profile destruction notifications and
22 // rebroadcasts them to each ProfileKeyedBaseFactory in a safe order based
23 // on the stated dependencies by each service.
24 class ProfileDependencyManager
{
26 // Adds/Removes a component from our list of live components. Removing will
27 // also remove live dependency links.
28 void AddComponent(ProfileKeyedBaseFactory
* component
);
29 void RemoveComponent(ProfileKeyedBaseFactory
* component
);
31 // Adds a dependency between two factories.
32 void AddEdge(ProfileKeyedBaseFactory
* depended
,
33 ProfileKeyedBaseFactory
* dependee
);
35 // Called by each Profile to alert us of its creation. Several services want
36 // to be started when a profile is created. Testing configuration is also
37 // done at this time. (If you want your ProfileKeyedService to be started
38 // with the Profile, override ProfileKeyedBaseFactory::
39 // ServiceIsCreatedWithProfile() to return true.)
40 void CreateProfileServices(Profile
* profile
, bool is_testing_profile
);
42 // Called by each Profile to alert us that we should destroy services
43 // associated with it.
45 // Why not use the existing PROFILE_DESTROYED notification?
47 // - Because we need to do everything here after the application has handled
48 // being notified about PROFILE_DESTROYED.
49 // - Because this class is a singleton and Singletons can't rely on
50 // NotificationService in unit tests because NotificationService is
51 // replaced in many tests.
52 void DestroyProfileServices(Profile
* profile
);
55 // Debugging assertion called as part of GetServiceForProfile in debug
56 // mode. This will NOTREACHED() whenever the user is trying to access a stale
58 void AssertProfileWasntDestroyed(Profile
* profile
);
61 static ProfileDependencyManager
* GetInstance();
64 friend class ProfileDependencyManagerUnittests
;
65 friend struct DefaultSingletonTraits
<ProfileDependencyManager
>;
67 typedef std::multimap
<ProfileKeyedBaseFactory
*,
68 ProfileKeyedBaseFactory
*> EdgeMap
;
70 ProfileDependencyManager();
71 virtual ~ProfileDependencyManager();
73 // Ensures that all the factories have been created before building the
75 void AssertFactoriesBuilt();
77 // Using the dependency graph defined in |edges_|, fills |destruction_order_|
78 // so that Observe() can notify each ProfileKeyedBaseFactory in order.
79 void BuildDestructionOrder(Profile
* profile
);
82 // Creates a dot file with our dependency information.
83 std::string
DumpGraphvizDependency();
86 std::vector
<ProfileKeyedBaseFactory
*> all_components_
;
90 std::vector
<ProfileKeyedBaseFactory
*> destruction_order_
;
92 // Whether AssertFactoriesBuilt has been done.
93 bool built_factories_
;
96 // A list of profile objects that have gone through the Shutdown()
97 // phase. These pointers are most likely invalid, but we keep track of their
98 // locations in memory so we can nicely assert if we're asked to do anything
100 std::set
<Profile
*> dead_profile_pointers_
;
104 #endif // CHROME_BROWSER_PROFILES_PROFILE_DEPENDENCY_MANAGER_H_