OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/profiles/profile_dependency_manager.h" | 5 #include "chrome/browser/profiles/profile_dependency_manager.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <deque> | 8 #include <deque> |
9 #include <iterator> | 9 #include <iterator> |
10 | 10 |
11 #include "chrome/browser/background/background_contents_service_factory.h" | |
11 #include "chrome/browser/profiles/profile_keyed_service.h" | 12 #include "chrome/browser/profiles/profile_keyed_service.h" |
12 #include "chrome/browser/profiles/profile_keyed_service_factory.h" | 13 #include "chrome/browser/profiles/profile_keyed_service_factory.h" |
14 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
15 #include "chrome/browser/sessions/session_service_factory.h" | |
16 #include "chrome/browser/sessions/tab_restore_service_factory.h" | |
13 #include "content/common/notification_service.h" | 17 #include "content/common/notification_service.h" |
14 | 18 |
15 class Profile; | 19 class Profile; |
16 | 20 |
21 namespace { | |
22 | |
23 bool g_initialized = false; | |
24 | |
25 // TODO(erg): This needs to be something else. I don't think putting every | |
26 // FooServiceFactory here will scale or is desireable long term. | |
Elliot Glaysher
2011/06/30 23:55:03
Does anyone have any ideas on something cleaner he
Miranda Callahan
2011/07/01 15:20:24
Perhaps I'm not seeing the problem correctly, but
Elliot Glaysher
2011/07/01 17:29:45
And what about their dependencies? And once we add
Miranda Callahan
2011/07/01 19:00:21
Ok, "what about their dependencies" is a good argu
Elliot Glaysher
2011/07/06 17:26:18
I am not sure what you're proposing.
| |
27 void AssertFactoriesBuilt() { | |
28 if (!g_initialized) { | |
29 BackgroundContentsServiceFactory::GetInstance(); | |
30 SessionServiceFactory::GetInstance(); | |
31 TabRestoreServiceFactory::GetInstance(); | |
32 TemplateURLServiceFactory::GetInstance(); | |
33 | |
34 g_initialized = true; | |
35 } | |
36 } | |
37 | |
38 } // namespace | |
39 | |
17 void ProfileDependencyManager::AddComponent( | 40 void ProfileDependencyManager::AddComponent( |
18 ProfileKeyedServiceFactory* component) { | 41 ProfileKeyedServiceFactory* component) { |
19 all_components_.push_back(component); | 42 all_components_.push_back(component); |
20 destruction_order_.clear(); | 43 destruction_order_.clear(); |
21 } | 44 } |
22 | 45 |
23 void ProfileDependencyManager::RemoveComponent( | 46 void ProfileDependencyManager::RemoveComponent( |
24 ProfileKeyedServiceFactory* component) { | 47 ProfileKeyedServiceFactory* component) { |
25 all_components_.erase(std::remove(all_components_.begin(), | 48 all_components_.erase(std::remove(all_components_.begin(), |
26 all_components_.end(), | 49 all_components_.end(), |
(...skipping 12 matching lines...) Expand all Loading... | |
39 | 62 |
40 destruction_order_.clear(); | 63 destruction_order_.clear(); |
41 } | 64 } |
42 | 65 |
43 void ProfileDependencyManager::AddEdge(ProfileKeyedServiceFactory* depended, | 66 void ProfileDependencyManager::AddEdge(ProfileKeyedServiceFactory* depended, |
44 ProfileKeyedServiceFactory* dependee) { | 67 ProfileKeyedServiceFactory* dependee) { |
45 edges_.insert(std::make_pair(depended, dependee)); | 68 edges_.insert(std::make_pair(depended, dependee)); |
46 destruction_order_.clear(); | 69 destruction_order_.clear(); |
47 } | 70 } |
48 | 71 |
72 void ProfileDependencyManager::CreateProfileServices(Profile* profile, | |
73 bool is_testing_profile) { | |
74 #ifndef NDEBUG | |
75 // Unmark |profile| as dead. This exists because of unit tests, which will | |
76 // often have similar stack structures. 0xWhatever might be created, go out | |
77 // of scope, and then a new Profile object might be created at 0xWhatever. | |
78 dead_profile_pointers_.erase(profile); | |
79 #endif | |
80 | |
81 AssertFactoriesBuilt(); | |
82 | |
83 // Iterate in reverse destruction order for creation. | |
Miranda Callahan
2011/07/01 15:20:24
You'll probably have to call BuildDestructionOrder
Elliot Glaysher
2011/07/01 17:29:45
Done.
| |
84 for (std::vector<ProfileKeyedServiceFactory*>::reverse_iterator rit = | |
85 destruction_order_.rbegin(); rit != destruction_order_.rend(); | |
86 ++rit) { | |
87 if (is_testing_profile && (*rit)->ServiceIsNULLWhileTesting()) { | |
88 (*rit)->SetTestingFactory(profile, NULL); | |
89 } else if ((*rit)->ServiceIsCreatedWithProfile()) { | |
90 // Create the service. | |
91 (*rit)->GetServiceForProfile(profile, true); | |
92 } | |
93 } | |
94 } | |
95 | |
49 void ProfileDependencyManager::DestroyProfileServices(Profile* profile) { | 96 void ProfileDependencyManager::DestroyProfileServices(Profile* profile) { |
50 if (destruction_order_.empty()) | 97 if (destruction_order_.empty()) |
51 BuildDestructionOrder(); | 98 BuildDestructionOrder(); |
52 | 99 |
53 for (std::vector<ProfileKeyedServiceFactory*>::const_iterator it = | 100 for (std::vector<ProfileKeyedServiceFactory*>::const_iterator it = |
54 destruction_order_.begin(); it != destruction_order_.end(); ++it) { | 101 destruction_order_.begin(); it != destruction_order_.end(); ++it) { |
55 (*it)->ProfileShutdown(profile); | 102 (*it)->ProfileShutdown(profile); |
56 } | 103 } |
57 | 104 |
58 #ifndef NDEBUG | 105 #ifndef NDEBUG |
59 // The profile is now dead to the rest of the program. | 106 // The profile is now dead to the rest of the program. |
60 dead_profile_pointers_.insert(profile); | 107 dead_profile_pointers_.insert(profile); |
61 #endif | 108 #endif |
62 | 109 |
63 for (std::vector<ProfileKeyedServiceFactory*>::const_iterator it = | 110 for (std::vector<ProfileKeyedServiceFactory*>::const_iterator it = |
64 destruction_order_.begin(); it != destruction_order_.end(); ++it) { | 111 destruction_order_.begin(); it != destruction_order_.end(); ++it) { |
65 (*it)->ProfileDestroyed(profile); | 112 (*it)->ProfileDestroyed(profile); |
66 } | 113 } |
67 } | 114 } |
68 | 115 |
69 #ifndef NDEBUG | 116 #ifndef NDEBUG |
70 void ProfileDependencyManager::ProfileNowExists(Profile* profile) { | |
71 dead_profile_pointers_.erase(profile); | |
72 } | |
73 | |
74 void ProfileDependencyManager::AssertProfileWasntDestroyed(Profile* profile) { | 117 void ProfileDependencyManager::AssertProfileWasntDestroyed(Profile* profile) { |
75 if (dead_profile_pointers_.find(profile) != dead_profile_pointers_.end()) { | 118 if (dead_profile_pointers_.find(profile) != dead_profile_pointers_.end()) { |
76 NOTREACHED() << "Attempted to access a Profile that was ShutDown(). This " | 119 NOTREACHED() << "Attempted to access a Profile that was ShutDown(). This " |
77 << "is most likely a heap smasher in progress. After " | 120 << "is most likely a heap smasher in progress. After " |
78 << "ProfileKeyedService::Shutdown() completes, your service " | 121 << "ProfileKeyedService::Shutdown() completes, your service " |
79 << "MUST NOT refer to depended Profile services again."; | 122 << "MUST NOT refer to depended Profile services again."; |
80 } | 123 } |
81 } | 124 } |
82 #endif | 125 #endif |
83 | 126 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
134 } | 177 } |
135 } | 178 } |
136 | 179 |
137 if (edges.size()) { | 180 if (edges.size()) { |
138 NOTREACHED() << "Dependency graph has a cycle. We are doomed."; | 181 NOTREACHED() << "Dependency graph has a cycle. We are doomed."; |
139 } | 182 } |
140 | 183 |
141 std::reverse(output.begin(), output.end()); | 184 std::reverse(output.begin(), output.end()); |
142 destruction_order_ = output; | 185 destruction_order_ = output; |
143 } | 186 } |
OLD | NEW |