OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/debug/trace_event.h" | 9 #include "base/debug/trace_event.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/metrics/field_trial.h" | 12 #include "base/metrics/field_trial.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/pending_task.h" | 14 #include "base/pending_task.h" |
15 #include "base/power_monitor/power_monitor.h" | 15 #include "base/power_monitor/power_monitor.h" |
16 #include "base/process/process_metrics.h" | 16 #include "base/process/process_metrics.h" |
17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
19 #include "base/system_monitor/system_monitor.h" | 19 #include "base/system_monitor/system_monitor.h" |
20 #include "base/threading/thread_restrictions.h" | 20 #include "base/threading/thread_restrictions.h" |
21 #include "base/timer/hi_res_timer_manager.h" | 21 #include "base/timer/hi_res_timer_manager.h" |
| 22 #include "content/browser/browser_startup_configuration.h" |
22 #include "content/browser/browser_thread_impl.h" | 23 #include "content/browser/browser_thread_impl.h" |
23 #include "content/browser/device_orientation/device_motion_service.h" | 24 #include "content/browser/device_orientation/device_motion_service.h" |
24 #include "content/browser/download/save_file_manager.h" | 25 #include "content/browser/download/save_file_manager.h" |
25 #include "content/browser/gamepad/gamepad_service.h" | 26 #include "content/browser/gamepad/gamepad_service.h" |
26 #include "content/browser/gpu/browser_gpu_channel_host_factory.h" | 27 #include "content/browser/gpu/browser_gpu_channel_host_factory.h" |
27 #include "content/browser/gpu/gpu_data_manager_impl.h" | 28 #include "content/browser/gpu/gpu_data_manager_impl.h" |
28 #include "content/browser/gpu/gpu_process_host.h" | 29 #include "content/browser/gpu/gpu_process_host.h" |
29 #include "content/browser/gpu/gpu_process_host_ui_shim.h" | 30 #include "content/browser/gpu/gpu_process_host_ui_shim.h" |
30 #include "content/browser/histogram_synchronizer.h" | 31 #include "content/browser/histogram_synchronizer.h" |
31 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 32 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
32 #include "content/browser/net/browser_online_state_observer.h" | 33 #include "content/browser/net/browser_online_state_observer.h" |
33 #include "content/browser/plugin_service_impl.h" | 34 #include "content/browser/plugin_service_impl.h" |
34 #include "content/browser/renderer_host/media/audio_mirroring_manager.h" | 35 #include "content/browser/renderer_host/media/audio_mirroring_manager.h" |
35 #include "content/browser/renderer_host/media/media_stream_manager.h" | 36 #include "content/browser/renderer_host/media/media_stream_manager.h" |
36 #include "content/browser/speech/speech_recognition_manager_impl.h" | 37 #include "content/browser/speech/speech_recognition_manager_impl.h" |
| 38 #include "content/browser/startup_task_runner.h" |
37 #include "content/browser/tracing/trace_controller_impl.h" | 39 #include "content/browser/tracing/trace_controller_impl.h" |
38 #include "content/browser/webui/content_web_ui_controller_factory.h" | 40 #include "content/browser/webui/content_web_ui_controller_factory.h" |
39 #include "content/browser/webui/url_data_manager.h" | 41 #include "content/browser/webui/url_data_manager.h" |
40 #include "content/public/browser/browser_main_parts.h" | 42 #include "content/public/browser/browser_main_parts.h" |
41 #include "content/public/browser/browser_shutdown.h" | 43 #include "content/public/browser/browser_shutdown.h" |
42 #include "content/public/browser/compositor_util.h" | 44 #include "content/public/browser/compositor_util.h" |
43 #include "content/public/browser/content_browser_client.h" | 45 #include "content/public/browser/content_browser_client.h" |
44 #include "content/public/browser/render_process_host.h" | 46 #include "content/public/browser/render_process_host.h" |
45 #include "content/public/common/content_switches.h" | 47 #include "content/public/common/content_switches.h" |
46 #include "content/public/common/main_function_params.h" | 48 #include "content/public/common/main_function_params.h" |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 // BrowserMainLoop construction / destruction ============================= | 294 // BrowserMainLoop construction / destruction ============================= |
293 | 295 |
294 BrowserMainLoop* BrowserMainLoop::GetInstance() { | 296 BrowserMainLoop* BrowserMainLoop::GetInstance() { |
295 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 297 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
296 return g_current_browser_main_loop; | 298 return g_current_browser_main_loop; |
297 } | 299 } |
298 | 300 |
299 BrowserMainLoop::BrowserMainLoop(const MainFunctionParams& parameters) | 301 BrowserMainLoop::BrowserMainLoop(const MainFunctionParams& parameters) |
300 : parameters_(parameters), | 302 : parameters_(parameters), |
301 parsed_command_line_(parameters.command_line), | 303 parsed_command_line_(parameters.command_line), |
302 result_code_(RESULT_CODE_NORMAL_EXIT) { | 304 result_code_(RESULT_CODE_NORMAL_EXIT), |
| 305 created_threads_(false) { |
303 DCHECK(!g_current_browser_main_loop); | 306 DCHECK(!g_current_browser_main_loop); |
304 g_current_browser_main_loop = this; | 307 g_current_browser_main_loop = this; |
305 } | 308 } |
306 | 309 |
307 BrowserMainLoop::~BrowserMainLoop() { | 310 BrowserMainLoop::~BrowserMainLoop() { |
308 DCHECK_EQ(this, g_current_browser_main_loop); | 311 DCHECK_EQ(this, g_current_browser_main_loop); |
309 #if !defined(OS_IOS) | 312 #if !defined(OS_IOS) |
310 ui::Clipboard::DestroyClipboardForCurrentThread(); | 313 ui::Clipboard::DestroyClipboardForCurrentThread(); |
311 #endif // !defined(OS_IOS) | 314 #endif // !defined(OS_IOS) |
312 g_current_browser_main_loop = NULL; | 315 g_current_browser_main_loop = NULL; |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
476 | 479 |
477 #if defined(TCMALLOC_TRACE_MEMORY_SUPPORTED) | 480 #if defined(TCMALLOC_TRACE_MEMORY_SUPPORTED) |
478 trace_memory_controller_.reset(new base::debug::TraceMemoryController( | 481 trace_memory_controller_.reset(new base::debug::TraceMemoryController( |
479 base::MessageLoop::current()->message_loop_proxy(), | 482 base::MessageLoop::current()->message_loop_proxy(), |
480 ::HeapProfilerWithPseudoStackStart, | 483 ::HeapProfilerWithPseudoStackStart, |
481 ::HeapProfilerStop, | 484 ::HeapProfilerStop, |
482 ::GetHeapProfile)); | 485 ::GetHeapProfile)); |
483 #endif | 486 #endif |
484 } | 487 } |
485 | 488 |
486 void BrowserMainLoop::CreateThreads() { | 489 int BrowserMainLoop::PreCreateThreads() { |
487 TRACE_EVENT0("startup", "BrowserMainLoop::CreateThreads") | |
488 | 490 |
489 if (parts_) { | 491 if (parts_) { |
490 TRACE_EVENT0("startup", | 492 TRACE_EVENT0("startup", |
491 "BrowserMainLoop::CreateThreads:PreCreateThreads"); | 493 "BrowserMainLoop::CreateThreads:PreCreateThreads"); |
492 result_code_ = parts_->PreCreateThreads(); | 494 result_code_ = parts_->PreCreateThreads(); |
493 } | 495 } |
494 | 496 |
495 #if defined(ENABLE_PLUGINS) | 497 #if defined(ENABLE_PLUGINS) |
496 // Prior to any processing happening on the io thread, we create the | 498 // Prior to any processing happening on the io thread, we create the |
497 // plugin service as it is predominantly used from the io thread, | 499 // plugin service as it is predominantly used from the io thread, |
498 // but must be created on the main thread. The service ctor is | 500 // but must be created on the main thread. The service ctor is |
499 // inexpensive and does not invoke the io_thread() accessor. | 501 // inexpensive and does not invoke the io_thread() accessor. |
500 { | 502 { |
501 TRACE_EVENT0("startup", "BrowserMainLoop::CreateThreads:PluginService") | 503 TRACE_EVENT0("startup", "BrowserMainLoop::CreateThreads:PluginService") |
502 PluginService::GetInstance()->Init(); | 504 PluginService::GetInstance()->Init(); |
503 } | 505 } |
504 #endif | 506 #endif |
505 | 507 |
506 #if !defined(OS_IOS) && (!defined(GOOGLE_CHROME_BUILD) || defined(OS_ANDROID)) | 508 #if !defined(OS_IOS) && (!defined(GOOGLE_CHROME_BUILD) || defined(OS_ANDROID)) |
507 // Single-process is an unsupported and not fully tested mode, so | 509 // Single-process is an unsupported and not fully tested mode, so |
508 // don't enable it for official Chrome builds (except on Android). | 510 // don't enable it for official Chrome builds (except on Android). |
509 if (parsed_command_line_.HasSwitch(switches::kSingleProcess)) | 511 if (parsed_command_line_.HasSwitch(switches::kSingleProcess)) |
510 RenderProcessHost::SetRunRendererInProcess(true); | 512 RenderProcessHost::SetRunRendererInProcess(true); |
511 #endif | 513 #endif |
| 514 return result_code_; |
| 515 } |
512 | 516 |
513 if (result_code_ > 0) | 517 void BrowserMainLoop::CreateStartupTasks() { |
514 return; | 518 TRACE_EVENT0("startup", "BrowserMainLoop::CreateStartupTasks") |
| 519 |
| 520 scoped_refptr<StartupTaskRunner> task_runner = |
| 521 new StartupTaskRunner(BrowserMayStartAsynchronously(), |
| 522 BrowserStartupComplete, |
| 523 base::MessageLoop::current()->message_loop_proxy()); |
| 524 |
| 525 StartupTask pre_create_threads = |
| 526 base::Bind(&BrowserMainLoop::PreCreateThreads, base::Unretained(this)); |
| 527 task_runner->AddTask(pre_create_threads); |
| 528 |
| 529 StartupTask create_threads = |
| 530 base::Bind(&BrowserMainLoop::CreateThreads, base::Unretained(this)); |
| 531 task_runner->AddTask(create_threads); |
| 532 |
| 533 StartupTask browser_thread_started = base::Bind( |
| 534 &BrowserMainLoop::BrowserThreadsStarted, base::Unretained(this)); |
| 535 task_runner->AddTask(browser_thread_started); |
| 536 |
| 537 StartupTask pre_main_message_loop_run = base::Bind( |
| 538 &BrowserMainLoop::PreMainMessageLoopRun, base::Unretained(this)); |
| 539 task_runner->AddTask(pre_main_message_loop_run); |
| 540 |
| 541 task_runner->StartRunningTasks(); |
| 542 } |
| 543 |
| 544 int BrowserMainLoop::CreateThreads() { |
| 545 TRACE_EVENT0("startup", "BrowserMainLoop::CreateThreads"); |
515 | 546 |
516 base::Thread::Options default_options; | 547 base::Thread::Options default_options; |
517 base::Thread::Options io_message_loop_options; | 548 base::Thread::Options io_message_loop_options; |
518 io_message_loop_options.message_loop_type = base::MessageLoop::TYPE_IO; | 549 io_message_loop_options.message_loop_type = base::MessageLoop::TYPE_IO; |
519 base::Thread::Options ui_message_loop_options; | 550 base::Thread::Options ui_message_loop_options; |
520 ui_message_loop_options.message_loop_type = base::MessageLoop::TYPE_UI; | 551 ui_message_loop_options.message_loop_type = base::MessageLoop::TYPE_UI; |
521 | 552 |
522 // Start threads in the order they occur in the BrowserThread::ID | 553 // Start threads in the order they occur in the BrowserThread::ID |
523 // enumeration, except for BrowserThread::UI which is the main | 554 // enumeration, except for BrowserThread::UI which is the main |
524 // thread. | 555 // thread. |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
589 if (thread_to_start) { | 620 if (thread_to_start) { |
590 (*thread_to_start).reset(new BrowserProcessSubThread(id)); | 621 (*thread_to_start).reset(new BrowserProcessSubThread(id)); |
591 (*thread_to_start)->StartWithOptions(*options); | 622 (*thread_to_start)->StartWithOptions(*options); |
592 } else { | 623 } else { |
593 NOTREACHED(); | 624 NOTREACHED(); |
594 } | 625 } |
595 | 626 |
596 TRACE_EVENT_END0("startup", "BrowserMainLoop::CreateThreads:start"); | 627 TRACE_EVENT_END0("startup", "BrowserMainLoop::CreateThreads:start"); |
597 | 628 |
598 } | 629 } |
| 630 created_threads_ = true; |
| 631 return result_code_; |
| 632 } |
599 | 633 |
600 #if !defined(OS_IOS) | 634 int BrowserMainLoop::PreMainMessageLoopRun() { |
601 indexed_db_thread_.reset(new base::Thread("IndexedDB")); | |
602 indexed_db_thread_->Start(); | |
603 #endif | |
604 | |
605 BrowserThreadsStarted(); | |
606 | |
607 if (parts_) { | 635 if (parts_) { |
608 TRACE_EVENT0("startup", | 636 TRACE_EVENT0("startup", |
609 "BrowserMainLoop::CreateThreads:PreMainMessageLoopRun"); | 637 "BrowserMainLoop::CreateThreads:PreMainMessageLoopRun"); |
610 parts_->PreMainMessageLoopRun(); | 638 parts_->PreMainMessageLoopRun(); |
611 } | 639 } |
612 | 640 |
613 // If the UI thread blocks, the whole UI is unresponsive. | 641 // If the UI thread blocks, the whole UI is unresponsive. |
614 // Do not allow disk IO from the UI thread. | 642 // Do not allow disk IO from the UI thread. |
615 base::ThreadRestrictions::SetIOAllowed(false); | 643 base::ThreadRestrictions::SetIOAllowed(false); |
616 base::ThreadRestrictions::DisallowWaiting(); | 644 base::ThreadRestrictions::DisallowWaiting(); |
| 645 return result_code_; |
617 } | 646 } |
618 | 647 |
619 void BrowserMainLoop::RunMainMessageLoopParts() { | 648 void BrowserMainLoop::RunMainMessageLoopParts() { |
620 TRACE_EVENT_BEGIN_ETW("BrowserMain:MESSAGE_LOOP", 0, ""); | 649 TRACE_EVENT_BEGIN_ETW("BrowserMain:MESSAGE_LOOP", 0, ""); |
621 | 650 |
622 bool ran_main_loop = false; | 651 bool ran_main_loop = false; |
623 if (parts_) | 652 if (parts_) |
624 ran_main_loop = parts_->MainMessageLoopRun(&result_code_); | 653 ran_main_loop = parts_->MainMessageLoopRun(&result_code_); |
625 | 654 |
626 if (!ran_main_loop) | 655 if (!ran_main_loop) |
627 MainMessageLoopRun(); | 656 MainMessageLoopRun(); |
628 | 657 |
629 TRACE_EVENT_END_ETW("BrowserMain:MESSAGE_LOOP", 0, ""); | 658 TRACE_EVENT_END_ETW("BrowserMain:MESSAGE_LOOP", 0, ""); |
630 } | 659 } |
631 | 660 |
632 void BrowserMainLoop::ShutdownThreadsAndCleanUp() { | 661 void BrowserMainLoop::ShutdownThreadsAndCleanUp() { |
| 662 |
| 663 if (!created_threads_) { |
| 664 // Called early, nothing to do |
| 665 return; |
| 666 } |
633 // Teardown may start in PostMainMessageLoopRun, and during teardown we | 667 // Teardown may start in PostMainMessageLoopRun, and during teardown we |
634 // need to be able to perform IO. | 668 // need to be able to perform IO. |
635 base::ThreadRestrictions::SetIOAllowed(true); | 669 base::ThreadRestrictions::SetIOAllowed(true); |
636 BrowserThread::PostTask( | 670 BrowserThread::PostTask( |
637 BrowserThread::IO, FROM_HERE, | 671 BrowserThread::IO, FROM_HERE, |
638 base::Bind(base::IgnoreResult(&base::ThreadRestrictions::SetIOAllowed), | 672 base::Bind(base::IgnoreResult(&base::ThreadRestrictions::SetIOAllowed), |
639 true)); | 673 true)); |
640 | 674 |
641 if (parts_) | 675 if (parts_) |
642 parts_->PostMainMessageLoopRun(); | 676 parts_->PostMainMessageLoopRun(); |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
758 const char* kThreadName = "CrBrowserMain"; | 792 const char* kThreadName = "CrBrowserMain"; |
759 base::PlatformThread::SetName(kThreadName); | 793 base::PlatformThread::SetName(kThreadName); |
760 if (main_message_loop_) | 794 if (main_message_loop_) |
761 main_message_loop_->set_thread_name(kThreadName); | 795 main_message_loop_->set_thread_name(kThreadName); |
762 | 796 |
763 // Register the main thread by instantiating it, but don't call any methods. | 797 // Register the main thread by instantiating it, but don't call any methods. |
764 main_thread_.reset( | 798 main_thread_.reset( |
765 new BrowserThreadImpl(BrowserThread::UI, base::MessageLoop::current())); | 799 new BrowserThreadImpl(BrowserThread::UI, base::MessageLoop::current())); |
766 } | 800 } |
767 | 801 |
768 void BrowserMainLoop::BrowserThreadsStarted() { | 802 int BrowserMainLoop::BrowserThreadsStarted() { |
769 TRACE_EVENT0("startup", "BrowserMainLoop::BrowserThreadsStarted") | 803 TRACE_EVENT0("startup", "BrowserMainLoop::BrowserThreadsStarted") |
| 804 |
| 805 #if !defined(OS_IOS) |
| 806 indexed_db_thread_.reset(new base::Thread("IndexedDB")); |
| 807 indexed_db_thread_->Start(); |
| 808 #endif |
| 809 |
770 #if defined(OS_ANDROID) | 810 #if defined(OS_ANDROID) |
771 // Up the priority of anything that touches with display tasks | 811 // Up the priority of anything that touches with display tasks |
772 // (this thread is UI thread, and io_thread_ is for IPCs). | 812 // (this thread is UI thread, and io_thread_ is for IPCs). |
773 io_thread_->SetPriority(base::kThreadPriority_Display); | 813 io_thread_->SetPriority(base::kThreadPriority_Display); |
774 base::PlatformThread::SetThreadPriority( | 814 base::PlatformThread::SetThreadPriority( |
775 base::PlatformThread::CurrentHandle(), | 815 base::PlatformThread::CurrentHandle(), |
776 base::kThreadPriority_Display); | 816 base::kThreadPriority_Display); |
777 #endif | 817 #endif |
778 | 818 |
779 #if !defined(OS_IOS) | 819 #if !defined(OS_IOS) |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
837 !parsed_command_line_.HasSwitch(switches::kInProcessGPU)) { | 877 !parsed_command_line_.HasSwitch(switches::kInProcessGPU)) { |
838 TRACE_EVENT_INSTANT0("gpu", "Post task to launch GPU process", | 878 TRACE_EVENT_INSTANT0("gpu", "Post task to launch GPU process", |
839 TRACE_EVENT_SCOPE_THREAD); | 879 TRACE_EVENT_SCOPE_THREAD); |
840 BrowserThread::PostTask( | 880 BrowserThread::PostTask( |
841 BrowserThread::IO, FROM_HERE, base::Bind( | 881 BrowserThread::IO, FROM_HERE, base::Bind( |
842 base::IgnoreResult(&GpuProcessHost::Get), | 882 base::IgnoreResult(&GpuProcessHost::Get), |
843 GpuProcessHost::GPU_PROCESS_KIND_SANDBOXED, | 883 GpuProcessHost::GPU_PROCESS_KIND_SANDBOXED, |
844 CAUSE_FOR_GPU_LAUNCH_BROWSER_STARTUP)); | 884 CAUSE_FOR_GPU_LAUNCH_BROWSER_STARTUP)); |
845 } | 885 } |
846 #endif // !defined(OS_IOS) | 886 #endif // !defined(OS_IOS) |
| 887 return result_code_; |
847 } | 888 } |
848 | 889 |
849 void BrowserMainLoop::InitializeToolkit() { | 890 void BrowserMainLoop::InitializeToolkit() { |
850 TRACE_EVENT0("startup", "BrowserMainLoop::InitializeToolkit") | 891 TRACE_EVENT0("startup", "BrowserMainLoop::InitializeToolkit") |
851 // TODO(evan): this function is rather subtle, due to the variety | 892 // TODO(evan): this function is rather subtle, due to the variety |
852 // of intersecting ifdefs we have. To keep it easy to follow, there | 893 // of intersecting ifdefs we have. To keep it easy to follow, there |
853 // are no #else branches on any #ifs. | 894 // are no #else branches on any #ifs. |
854 // TODO(stevenjb): Move platform specific code into platform specific Parts | 895 // TODO(stevenjb): Move platform specific code into platform specific Parts |
855 // (Need to add InitializeToolkit stage to BrowserParts). | 896 // (Need to add InitializeToolkit stage to BrowserParts). |
856 #if defined(OS_LINUX) || defined(OS_OPENBSD) | 897 #if defined(OS_LINUX) || defined(OS_OPENBSD) |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
901 if (parameters_.ui_task) | 942 if (parameters_.ui_task) |
902 base::MessageLoopForUI::current()->PostTask(FROM_HERE, | 943 base::MessageLoopForUI::current()->PostTask(FROM_HERE, |
903 *parameters_.ui_task); | 944 *parameters_.ui_task); |
904 | 945 |
905 base::RunLoop run_loop; | 946 base::RunLoop run_loop; |
906 run_loop.Run(); | 947 run_loop.Run(); |
907 #endif | 948 #endif |
908 } | 949 } |
909 | 950 |
910 } // namespace content | 951 } // namespace content |
OLD | NEW |