OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/browser_child_process_host.h" | 5 #include "chrome/browser/browser_child_process_host.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/lazy_instance.h" |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
11 #include "base/path_service.h" | 12 #include "base/path_service.h" |
12 #include "base/process_util.h" | 13 #include "base/process_util.h" |
13 #include "base/singleton.h" | |
14 #include "base/stl_util-inl.h" | 14 #include "base/stl_util-inl.h" |
15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
16 #include "chrome/app/breakpad_mac.h" | 16 #include "chrome/app/breakpad_mac.h" |
17 #include "chrome/browser/browser_thread.h" | 17 #include "chrome/browser/browser_thread.h" |
18 #include "chrome/browser/prefs/pref_service.h" | 18 #include "chrome/browser/prefs/pref_service.h" |
19 #include "chrome/common/child_process_logging.h" | 19 #include "chrome/common/child_process_logging.h" |
20 #include "chrome/common/chrome_constants.h" | 20 #include "chrome/common/chrome_constants.h" |
21 #include "chrome/common/chrome_paths_internal.h" | 21 #include "chrome/common/chrome_paths_internal.h" |
22 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
23 #include "chrome/common/notification_service.h" | 23 #include "chrome/common/notification_service.h" |
24 #include "chrome/common/plugin_messages.h" | 24 #include "chrome/common/plugin_messages.h" |
25 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
26 #include "chrome/common/process_watcher.h" | 26 #include "chrome/common/process_watcher.h" |
27 #include "chrome/common/result_codes.h" | 27 #include "chrome/common/result_codes.h" |
28 #include "chrome/installer/util/google_update_settings.h" | 28 #include "chrome/installer/util/google_update_settings.h" |
29 | 29 |
30 #if defined(OS_LINUX) | 30 #if defined(OS_LINUX) |
31 #include "base/linux_util.h" | 31 #include "base/linux_util.h" |
32 #endif // OS_LINUX | 32 #endif // OS_LINUX |
33 | 33 |
34 namespace { | 34 namespace { |
35 | 35 |
36 typedef std::list<BrowserChildProcessHost*> ChildProcessList; | 36 typedef std::list<BrowserChildProcessHost*> ChildProcessList; |
| 37 static base::LazyInstance<ChildProcessList> g_child_process_list( |
| 38 base::LINKER_INITIALIZED); |
37 | 39 |
38 // The NotificationTask is used to notify about plugin process connection/ | 40 // The NotificationTask is used to notify about plugin process connection/ |
39 // disconnection. It is needed because the notifications in the | 41 // disconnection. It is needed because the notifications in the |
40 // NotificationService must happen in the main thread. | 42 // NotificationService must happen in the main thread. |
41 class ChildNotificationTask : public Task { | 43 class ChildNotificationTask : public Task { |
42 public: | 44 public: |
43 ChildNotificationTask( | 45 ChildNotificationTask( |
44 NotificationType notification_type, ChildProcessInfo* info) | 46 NotificationType notification_type, ChildProcessInfo* info) |
45 : notification_type_(notification_type), info_(*info) { } | 47 : notification_type_(notification_type), info_(*info) { } |
46 | 48 |
47 virtual void Run() { | 49 virtual void Run() { |
48 NotificationService::current()-> | 50 NotificationService::current()-> |
49 Notify(notification_type_, NotificationService::AllSources(), | 51 Notify(notification_type_, NotificationService::AllSources(), |
50 Details<ChildProcessInfo>(&info_)); | 52 Details<ChildProcessInfo>(&info_)); |
51 } | 53 } |
52 | 54 |
53 private: | 55 private: |
54 NotificationType notification_type_; | 56 NotificationType notification_type_; |
55 ChildProcessInfo info_; | 57 ChildProcessInfo info_; |
56 }; | 58 }; |
57 | 59 |
58 } // namespace | 60 } // namespace |
59 | 61 |
60 | 62 |
61 BrowserChildProcessHost::BrowserChildProcessHost( | 63 BrowserChildProcessHost::BrowserChildProcessHost( |
62 ProcessType type, ResourceDispatcherHost* resource_dispatcher_host) | 64 ProcessType type, ResourceDispatcherHost* resource_dispatcher_host) |
63 : Receiver(type, -1), | 65 : Receiver(type, -1), |
64 ALLOW_THIS_IN_INITIALIZER_LIST(client_(this)), | 66 ALLOW_THIS_IN_INITIALIZER_LIST(client_(this)), |
65 resource_dispatcher_host_(resource_dispatcher_host) { | 67 resource_dispatcher_host_(resource_dispatcher_host) { |
66 Singleton<ChildProcessList>::get()->push_back(this); | 68 g_child_process_list.Get().push_back(this); |
67 } | 69 } |
68 | 70 |
69 | 71 |
70 BrowserChildProcessHost::~BrowserChildProcessHost() { | 72 BrowserChildProcessHost::~BrowserChildProcessHost() { |
71 Singleton<ChildProcessList>::get()->remove(this); | 73 g_child_process_list.Get().remove(this); |
72 | 74 |
73 if (resource_dispatcher_host_) | 75 if (resource_dispatcher_host_) |
74 resource_dispatcher_host_->CancelRequestsForProcess(id()); | 76 resource_dispatcher_host_->CancelRequestsForProcess(id()); |
75 } | 77 } |
76 | 78 |
77 // static | 79 // static |
78 void BrowserChildProcessHost::SetCrashReporterCommandLine( | 80 void BrowserChildProcessHost::SetCrashReporterCommandLine( |
79 CommandLine* command_line) { | 81 CommandLine* command_line) { |
80 #if defined(USE_LINUX_BREAKPAD) | 82 #if defined(USE_LINUX_BREAKPAD) |
81 if (IsCrashReporterEnabled()) { | 83 if (IsCrashReporterEnabled()) { |
82 command_line->AppendSwitchASCII(switches::kEnableCrashReporter, | 84 command_line->AppendSwitchASCII(switches::kEnableCrashReporter, |
83 child_process_logging::GetClientId() + "," + base::GetLinuxDistro()); | 85 child_process_logging::GetClientId() + "," + base::GetLinuxDistro()); |
84 } | 86 } |
85 #elif defined(OS_MACOSX) | 87 #elif defined(OS_MACOSX) |
86 if (IsCrashReporterEnabled()) { | 88 if (IsCrashReporterEnabled()) { |
87 command_line->AppendSwitchASCII(switches::kEnableCrashReporter, | 89 command_line->AppendSwitchASCII(switches::kEnableCrashReporter, |
88 child_process_logging::GetClientId()); | 90 child_process_logging::GetClientId()); |
89 } | 91 } |
90 #endif // OS_MACOSX | 92 #endif // OS_MACOSX |
91 } | 93 } |
92 | 94 |
93 // static | 95 // static |
94 void BrowserChildProcessHost::TerminateAll() { | 96 void BrowserChildProcessHost::TerminateAll() { |
95 // Make a copy since the ChildProcessHost dtor mutates the original list. | 97 // Make a copy since the ChildProcessHost dtor mutates the original list. |
96 ChildProcessList copy = *(Singleton<ChildProcessList>::get()); | 98 ChildProcessList copy = g_child_process_list.Get(); |
97 STLDeleteElements(©); | 99 STLDeleteElements(©); |
98 } | 100 } |
99 | 101 |
100 void BrowserChildProcessHost::Launch( | 102 void BrowserChildProcessHost::Launch( |
101 #if defined(OS_WIN) | 103 #if defined(OS_WIN) |
102 const FilePath& exposed_dir, | 104 const FilePath& exposed_dir, |
103 #elif defined(OS_POSIX) | 105 #elif defined(OS_POSIX) |
104 bool use_zygote, | 106 bool use_zygote, |
105 const base::environment_vector& environ, | 107 const base::environment_vector& environ, |
106 #endif | 108 #endif |
(...skipping 16 matching lines...) Expand all Loading... |
123 DCHECK(child_process_->GetHandle()) | 125 DCHECK(child_process_->GetHandle()) |
124 << "Requesting a child process handle before launch has completed OK."; | 126 << "Requesting a child process handle before launch has completed OK."; |
125 return child_process_->GetHandle(); | 127 return child_process_->GetHandle(); |
126 } | 128 } |
127 | 129 |
128 bool BrowserChildProcessHost::Send(IPC::Message* msg) { | 130 bool BrowserChildProcessHost::Send(IPC::Message* msg) { |
129 return SendOnChannel(msg); | 131 return SendOnChannel(msg); |
130 } | 132 } |
131 | 133 |
132 void BrowserChildProcessHost::ForceShutdown() { | 134 void BrowserChildProcessHost::ForceShutdown() { |
133 Singleton<ChildProcessList>::get()->remove(this); | 135 g_child_process_list.Get().remove(this); |
134 ChildProcessHost::ForceShutdown(); | 136 ChildProcessHost::ForceShutdown(); |
135 } | 137 } |
136 | 138 |
137 void BrowserChildProcessHost::Notify(NotificationType type) { | 139 void BrowserChildProcessHost::Notify(NotificationType type) { |
138 BrowserThread::PostTask( | 140 BrowserThread::PostTask( |
139 BrowserThread::UI, FROM_HERE, new ChildNotificationTask(type, this)); | 141 BrowserThread::UI, FROM_HERE, new ChildNotificationTask(type, this)); |
140 } | 142 } |
141 | 143 |
142 bool BrowserChildProcessHost::DidChildCrash() { | 144 bool BrowserChildProcessHost::DidChildCrash() { |
143 return child_process_->DidProcessCrash(); | 145 return child_process_->DidProcessCrash(); |
(...skipping 18 matching lines...) Expand all Loading... |
162 const IPC::Message& msg) { | 164 const IPC::Message& msg) { |
163 bool msg_is_ok = true; | 165 bool msg_is_ok = true; |
164 bool handled = false; | 166 bool handled = false; |
165 if (resource_dispatcher_host_) { | 167 if (resource_dispatcher_host_) { |
166 handled = resource_dispatcher_host_->OnMessageReceived( | 168 handled = resource_dispatcher_host_->OnMessageReceived( |
167 msg, this, &msg_is_ok); | 169 msg, this, &msg_is_ok); |
168 } | 170 } |
169 if (!handled && (msg.type() == PluginProcessHostMsg_ShutdownRequest::ID)) { | 171 if (!handled && (msg.type() == PluginProcessHostMsg_ShutdownRequest::ID)) { |
170 // Must remove the process from the list now, in case it gets used for a | 172 // Must remove the process from the list now, in case it gets used for a |
171 // new instance before our watcher tells us that the process terminated. | 173 // new instance before our watcher tells us that the process terminated. |
172 Singleton<ChildProcessList>::get()->remove(this); | 174 g_child_process_list.Get().remove(this); |
173 } | 175 } |
174 if (!msg_is_ok) | 176 if (!msg_is_ok) |
175 base::KillProcess(handle(), ResultCodes::KILLED_BAD_MESSAGE, false); | 177 base::KillProcess(handle(), ResultCodes::KILLED_BAD_MESSAGE, false); |
176 return handled; | 178 return handled; |
177 } | 179 } |
178 | 180 |
179 BrowserChildProcessHost::ClientHook::ClientHook(BrowserChildProcessHost* host) | 181 BrowserChildProcessHost::ClientHook::ClientHook(BrowserChildProcessHost* host) |
180 : host_(host) { | 182 : host_(host) { |
181 } | 183 } |
182 | 184 |
183 void BrowserChildProcessHost::ClientHook::OnProcessLaunched() { | 185 void BrowserChildProcessHost::ClientHook::OnProcessLaunched() { |
184 if (!host_->child_process_->GetHandle()) { | 186 if (!host_->child_process_->GetHandle()) { |
185 host_->OnChildDied(); | 187 host_->OnChildDied(); |
186 return; | 188 return; |
187 } | 189 } |
188 host_->set_handle(host_->child_process_->GetHandle()); | 190 host_->set_handle(host_->child_process_->GetHandle()); |
189 host_->OnProcessLaunched(); | 191 host_->OnProcessLaunched(); |
190 } | 192 } |
191 | 193 |
192 BrowserChildProcessHost::Iterator::Iterator() | 194 BrowserChildProcessHost::Iterator::Iterator() |
193 : all_(true), type_(UNKNOWN_PROCESS) { | 195 : all_(true), type_(UNKNOWN_PROCESS) { |
194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)) << | 196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)) << |
195 "ChildProcessInfo::Iterator must be used on the IO thread."; | 197 "ChildProcessInfo::Iterator must be used on the IO thread."; |
196 iterator_ = Singleton<ChildProcessList>::get()->begin(); | 198 iterator_ = g_child_process_list.Get().begin(); |
197 } | 199 } |
198 | 200 |
199 BrowserChildProcessHost::Iterator::Iterator(ProcessType type) | 201 BrowserChildProcessHost::Iterator::Iterator(ProcessType type) |
200 : all_(false), type_(type) { | 202 : all_(false), type_(type) { |
201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)) << | 203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)) << |
202 "ChildProcessInfo::Iterator must be used on the IO thread."; | 204 "ChildProcessInfo::Iterator must be used on the IO thread."; |
203 iterator_ = Singleton<ChildProcessList>::get()->begin(); | 205 iterator_ = g_child_process_list.Get().begin(); |
204 if (!Done() && (*iterator_)->type() != type_) | 206 if (!Done() && (*iterator_)->type() != type_) |
205 ++(*this); | 207 ++(*this); |
206 } | 208 } |
207 | 209 |
208 BrowserChildProcessHost* BrowserChildProcessHost::Iterator::operator++() { | 210 BrowserChildProcessHost* BrowserChildProcessHost::Iterator::operator++() { |
209 do { | 211 do { |
210 ++iterator_; | 212 ++iterator_; |
211 if (Done()) | 213 if (Done()) |
212 break; | 214 break; |
213 | 215 |
214 if (!all_ && (*iterator_)->type() != type_) | 216 if (!all_ && (*iterator_)->type() != type_) |
215 continue; | 217 continue; |
216 | 218 |
217 return *iterator_; | 219 return *iterator_; |
218 } while (true); | 220 } while (true); |
219 | 221 |
220 return NULL; | 222 return NULL; |
221 } | 223 } |
222 | 224 |
223 bool BrowserChildProcessHost::Iterator::Done() { | 225 bool BrowserChildProcessHost::Iterator::Done() { |
224 return iterator_ == Singleton<ChildProcessList>::get()->end(); | 226 return iterator_ == g_child_process_list.Get().end(); |
225 } | 227 } |
OLD | NEW |