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 "content/browser/browser_main_loop.h" | 5 #include "content/browser/browser_main_loop.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 BrowserMainLoop::BrowserMainLoop(const content::MainFunctionParams& parameters) | 151 BrowserMainLoop::BrowserMainLoop(const content::MainFunctionParams& parameters) |
152 : parameters_(parameters), | 152 : parameters_(parameters), |
153 parsed_command_line_(parameters.command_line), | 153 parsed_command_line_(parameters.command_line), |
154 result_code_(content::RESULT_CODE_NORMAL_EXIT) { | 154 result_code_(content::RESULT_CODE_NORMAL_EXIT) { |
155 #if defined(OS_WIN) | 155 #if defined(OS_WIN) |
156 OleInitialize(NULL); | 156 OleInitialize(NULL); |
157 #endif | 157 #endif |
158 } | 158 } |
159 | 159 |
160 BrowserMainLoop::~BrowserMainLoop() { | 160 BrowserMainLoop::~BrowserMainLoop() { |
161 // Destroy added parts in reverse order. | 161 // Release and destroy |parts_| before destroying other members. |
162 for (int i = static_cast<int>(parts_list_.size())-1; i >= 0; --i) | 162 parts_.reset(); |
163 delete parts_list_[i]; | |
164 parts_list_.clear(); | |
165 | 163 |
166 #if defined(OS_WIN) | 164 #if defined(OS_WIN) |
167 OleUninitialize(); | 165 OleUninitialize(); |
168 #endif | 166 #endif |
169 } | 167 } |
170 | 168 |
171 void BrowserMainLoop::Init() { | 169 void BrowserMainLoop::Init() { |
172 GetContentClient()->browser()->CreateBrowserMainParts( | 170 parts_.reset( |
173 parameters_, &parts_list_); | 171 GetContentClient()->browser()->CreateBrowserMainParts(parameters_)); |
174 } | 172 } |
175 | 173 |
176 // BrowserMainLoop stages ================================================== | 174 // BrowserMainLoop stages ================================================== |
177 | 175 |
178 void BrowserMainLoop::EarlyInitialization() { | 176 void BrowserMainLoop::EarlyInitialization() { |
179 for (size_t i = 0; i < parts_list_.size(); ++i) | 177 if (parts_.get()) |
180 parts_list_[i]->PreEarlyInitialization(); | 178 parts_->PreEarlyInitialization(); |
181 | 179 |
182 #if defined(OS_WIN) | 180 #if defined(OS_WIN) |
183 net::EnsureWinsockInit(); | 181 net::EnsureWinsockInit(); |
184 #endif | 182 #endif |
185 | 183 |
186 #if !defined(USE_OPENSSL) | 184 #if !defined(USE_OPENSSL) |
187 // Use NSS for SSL by default. | 185 // Use NSS for SSL by default. |
188 // The default client socket factory uses NSS for SSL by default on | 186 // The default client socket factory uses NSS for SSL by default on |
189 // Windows and Mac. | 187 // Windows and Mac. |
190 bool init_nspr = false; | 188 bool init_nspr = false; |
(...skipping 25 matching lines...) Expand all Loading... |
216 if (parsed_command_line_.HasSwitch( | 214 if (parsed_command_line_.HasSwitch( |
217 switches::kEnableDNSCertProvenanceChecking)) { | 215 switches::kEnableDNSCertProvenanceChecking)) { |
218 net::SSLConfigService::EnableDNSCertProvenanceChecking(); | 216 net::SSLConfigService::EnableDNSCertProvenanceChecking(); |
219 } | 217 } |
220 | 218 |
221 // TODO(abarth): Should this move to InitializeNetworkOptions? This doesn't | 219 // TODO(abarth): Should this move to InitializeNetworkOptions? This doesn't |
222 // seem dependent on SSL initialization(). | 220 // seem dependent on SSL initialization(). |
223 if (parsed_command_line_.HasSwitch(switches::kEnableTcpFastOpen)) | 221 if (parsed_command_line_.HasSwitch(switches::kEnableTcpFastOpen)) |
224 net::set_tcp_fastopen_enabled(true); | 222 net::set_tcp_fastopen_enabled(true); |
225 | 223 |
226 for (size_t i = 0; i < parts_list_.size(); ++i) | 224 if (parts_.get()) |
227 parts_list_[i]->PostEarlyInitialization(); | 225 parts_->PostEarlyInitialization(); |
228 } | 226 } |
229 | 227 |
230 void BrowserMainLoop::MainMessageLoopStart() { | 228 void BrowserMainLoop::MainMessageLoopStart() { |
231 for (size_t i = 0; i < parts_list_.size(); ++i) | 229 if (parts_.get()) |
232 parts_list_[i]->PreMainMessageLoopStart(); | 230 parts_->PreMainMessageLoopStart(); |
233 | 231 |
234 #if defined(OS_WIN) | 232 #if defined(OS_WIN) |
235 // If we're running tests (ui_task is non-null), then the ResourceBundle | 233 // If we're running tests (ui_task is non-null), then the ResourceBundle |
236 // has already been initialized. | 234 // has already been initialized. |
237 if (!parameters_.ui_task) { | 235 if (!parameters_.ui_task) { |
238 // Override the configured locale with the user's preferred UI language. | 236 // Override the configured locale with the user's preferred UI language. |
239 l10n_util::OverrideLocaleWithUILanguageList(); | 237 l10n_util::OverrideLocaleWithUILanguageList(); |
240 } | 238 } |
241 #endif | 239 #endif |
242 | 240 |
(...skipping 10 matching lines...) Expand all Loading... |
253 | 251 |
254 system_monitor_.reset(new base::SystemMonitor); | 252 system_monitor_.reset(new base::SystemMonitor); |
255 hi_res_timer_manager_.reset(new HighResolutionTimerManager); | 253 hi_res_timer_manager_.reset(new HighResolutionTimerManager); |
256 | 254 |
257 network_change_notifier_.reset(net::NetworkChangeNotifier::Create()); | 255 network_change_notifier_.reset(net::NetworkChangeNotifier::Create()); |
258 | 256 |
259 #if defined(OS_WIN) | 257 #if defined(OS_WIN) |
260 system_message_window_.reset(new SystemMessageWindowWin); | 258 system_message_window_.reset(new SystemMessageWindowWin); |
261 #endif | 259 #endif |
262 | 260 |
263 for (size_t i = 0; i < parts_list_.size(); ++i) | 261 if (parts_.get()) |
264 parts_list_[i]->PostMainMessageLoopStart(); | 262 parts_->PostMainMessageLoopStart(); |
265 } | 263 } |
266 | 264 |
267 void BrowserMainLoop::RunMainMessageLoopParts( | 265 void BrowserMainLoop::RunMainMessageLoopParts( |
268 bool* completed_main_message_loop) { | 266 bool* completed_main_message_loop) { |
269 for (size_t i = 0; i < parts_list_.size(); ++i) | 267 if (parts_.get()) |
270 parts_list_[i]->PreMainMessageLoopRun(); | 268 parts_->PreMainMessageLoopRun(); |
271 | 269 |
272 TRACE_EVENT_BEGIN_ETW("BrowserMain:MESSAGE_LOOP", 0, ""); | 270 TRACE_EVENT_BEGIN_ETW("BrowserMain:MESSAGE_LOOP", 0, ""); |
273 // If the UI thread blocks, the whole UI is unresponsive. | 271 // If the UI thread blocks, the whole UI is unresponsive. |
274 // Do not allow disk IO from the UI thread. | 272 // Do not allow disk IO from the UI thread. |
275 base::ThreadRestrictions::SetIOAllowed(false); | 273 base::ThreadRestrictions::SetIOAllowed(false); |
276 | 274 |
277 // Iterate through each of the parts. If any of them ran the main | |
278 // message loop then they should return |true|. Otherwise | |
279 // BrowserMainLoop::MainMessageLoopRun loop will be run. | |
280 bool ran_main_loop = false; | 275 bool ran_main_loop = false; |
281 for (size_t i = 0; i < parts_list_.size(); ++i) { | 276 if (parts_.get()) |
282 int result_code = result_code_; | 277 ran_main_loop = parts_->MainMessageLoopRun(&result_code_); |
283 if (parts_list_[i]->MainMessageLoopRun(&result_code)) { | 278 |
284 ran_main_loop = true; | |
285 result_code_ = result_code; | |
286 } | |
287 } | |
288 if (!ran_main_loop) | 279 if (!ran_main_loop) |
289 MainMessageLoopRun(); | 280 MainMessageLoopRun(); |
290 | 281 |
291 TRACE_EVENT_END_ETW("BrowserMain:MESSAGE_LOOP", 0, ""); | 282 TRACE_EVENT_END_ETW("BrowserMain:MESSAGE_LOOP", 0, ""); |
292 | 283 |
293 if (completed_main_message_loop) | 284 if (completed_main_message_loop) |
294 *completed_main_message_loop = true; | 285 *completed_main_message_loop = true; |
295 | 286 |
296 for (size_t i = 0; i < parts_list_.size(); ++i) | 287 if (parts_.get()) |
297 parts_list_[i]->PostMainMessageLoopRun(); | 288 parts_->PostMainMessageLoopRun(); |
298 } | 289 } |
299 | 290 |
300 void BrowserMainLoop::InitializeMainThread() { | 291 void BrowserMainLoop::InitializeMainThread() { |
301 const char* kThreadName = "CrBrowserMain"; | 292 const char* kThreadName = "CrBrowserMain"; |
302 base::PlatformThread::SetName(kThreadName); | 293 base::PlatformThread::SetName(kThreadName); |
303 main_message_loop_->set_thread_name(kThreadName); | 294 main_message_loop_->set_thread_name(kThreadName); |
304 | 295 |
305 // Register the main thread by instantiating it, but don't call any methods. | 296 // Register the main thread by instantiating it, but don't call any methods. |
306 main_thread_.reset(new BrowserThreadImpl(BrowserThread::UI, | 297 main_thread_.reset(new BrowserThreadImpl(BrowserThread::UI, |
307 MessageLoop::current())); | 298 MessageLoop::current())); |
(...skipping 28 matching lines...) Expand all Loading... |
336 | 327 |
337 #if defined(OS_WIN) | 328 #if defined(OS_WIN) |
338 // Init common control sex. | 329 // Init common control sex. |
339 INITCOMMONCONTROLSEX config; | 330 INITCOMMONCONTROLSEX config; |
340 config.dwSize = sizeof(config); | 331 config.dwSize = sizeof(config); |
341 config.dwICC = ICC_WIN95_CLASSES; | 332 config.dwICC = ICC_WIN95_CLASSES; |
342 if (!InitCommonControlsEx(&config)) | 333 if (!InitCommonControlsEx(&config)) |
343 LOG_GETLASTERROR(FATAL); | 334 LOG_GETLASTERROR(FATAL); |
344 #endif | 335 #endif |
345 | 336 |
346 for (size_t i = 0; i < parts_list_.size(); ++i) | 337 if (parts_.get()) |
347 parts_list_[i]->ToolkitInitialized(); | 338 parts_->ToolkitInitialized(); |
348 } | 339 } |
349 | 340 |
350 void BrowserMainLoop::MainMessageLoopRun() { | 341 void BrowserMainLoop::MainMessageLoopRun() { |
351 if (parameters_.ui_task) | 342 if (parameters_.ui_task) |
352 MessageLoopForUI::current()->PostTask(FROM_HERE, parameters_.ui_task); | 343 MessageLoopForUI::current()->PostTask(FROM_HERE, parameters_.ui_task); |
353 | 344 |
354 #if defined(OS_MACOSX) | 345 #if defined(OS_MACOSX) |
355 MessageLoopForUI::current()->Run(); | 346 MessageLoopForUI::current()->Run(); |
356 #else | 347 #else |
357 MessageLoopForUI::current()->RunWithDispatcher(NULL); | 348 MessageLoopForUI::current()->RunWithDispatcher(NULL); |
358 #endif | 349 #endif |
359 } | 350 } |
360 | 351 |
361 } // namespace content | 352 } // namespace content |
OLD | NEW |