| 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/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 680 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 691 | 691 |
| 692 TRACE_EVENT_END_ETW("BrowserMain:MESSAGE_LOOP", 0, ""); | 692 TRACE_EVENT_END_ETW("BrowserMain:MESSAGE_LOOP", 0, ""); |
| 693 } | 693 } |
| 694 | 694 |
| 695 void BrowserMainLoop::ShutdownThreadsAndCleanUp() { | 695 void BrowserMainLoop::ShutdownThreadsAndCleanUp() { |
| 696 | 696 |
| 697 if (!created_threads_) { | 697 if (!created_threads_) { |
| 698 // Called early, nothing to do | 698 // Called early, nothing to do |
| 699 return; | 699 return; |
| 700 } | 700 } |
| 701 TRACE_EVENT0("shutdown", "BrowserMainLoop::ShutdownThreadsAndCleanUp") |
| 702 |
| 701 // Teardown may start in PostMainMessageLoopRun, and during teardown we | 703 // Teardown may start in PostMainMessageLoopRun, and during teardown we |
| 702 // need to be able to perform IO. | 704 // need to be able to perform IO. |
| 703 base::ThreadRestrictions::SetIOAllowed(true); | 705 base::ThreadRestrictions::SetIOAllowed(true); |
| 704 BrowserThread::PostTask( | 706 BrowserThread::PostTask( |
| 705 BrowserThread::IO, FROM_HERE, | 707 BrowserThread::IO, FROM_HERE, |
| 706 base::Bind(base::IgnoreResult(&base::ThreadRestrictions::SetIOAllowed), | 708 base::Bind(base::IgnoreResult(&base::ThreadRestrictions::SetIOAllowed), |
| 707 true)); | 709 true)); |
| 708 | 710 |
| 709 if (parts_) | 711 if (parts_) { |
| 712 TRACE_EVENT0("shutdown", |
| 713 "BrowserMainLoop::Subsystem:PostMainMessageLoopRun"); |
| 710 parts_->PostMainMessageLoopRun(); | 714 parts_->PostMainMessageLoopRun(); |
| 715 } |
| 711 | 716 |
| 712 trace_memory_controller_.reset(); | 717 trace_memory_controller_.reset(); |
| 713 | 718 |
| 714 #if !defined(OS_IOS) | 719 #if !defined(OS_IOS) |
| 715 // Destroying the GpuProcessHostUIShims on the UI thread posts a task to | 720 // Destroying the GpuProcessHostUIShims on the UI thread posts a task to |
| 716 // delete related objects on the GPU thread. This must be done before | 721 // delete related objects on the GPU thread. This must be done before |
| 717 // stopping the GPU thread. The GPU thread will close IPC channels to renderer | 722 // stopping the GPU thread. The GPU thread will close IPC channels to renderer |
| 718 // processes so this has to happen before stopping the IO thread. | 723 // processes so this has to happen before stopping the IO thread. |
| 719 GpuProcessHostUIShim::DestroyAll(); | 724 { |
| 720 | 725 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:GPUProcessHostShim"); |
| 726 GpuProcessHostUIShim::DestroyAll(); |
| 727 } |
| 721 // Cancel pending requests and prevent new requests. | 728 // Cancel pending requests and prevent new requests. |
| 722 if (resource_dispatcher_host_) | 729 if (resource_dispatcher_host_) { |
| 730 TRACE_EVENT0("shutdown", |
| 731 "BrowserMainLoop::Subsystem:ResourceDispatcherHost"); |
| 723 resource_dispatcher_host_.get()->Shutdown(); | 732 resource_dispatcher_host_.get()->Shutdown(); |
| 733 } |
| 724 | 734 |
| 725 #if defined(USE_AURA) | 735 #if defined(USE_AURA) |
| 726 ImageTransportFactory::Terminate(); | 736 { |
| 737 TRACE_EVENT0("shutdown", |
| 738 "BrowserMainLoop::Subsystem:ImageTransportFactory"); |
| 739 ImageTransportFactory::Terminate(); |
| 740 } |
| 727 #endif | 741 #endif |
| 728 | 742 |
| 729 // The device monitors are using |system_monitor_| as dependency, so delete | 743 // The device monitors are using |system_monitor_| as dependency, so delete |
| 730 // them before |system_monitor_| goes away. | 744 // them before |system_monitor_| goes away. |
| 731 // On Mac and windows, the monitor needs to be destroyed on the same thread | 745 // On Mac and windows, the monitor needs to be destroyed on the same thread |
| 732 // as they were created. On Linux, the monitor will be deleted when IO thread | 746 // as they were created. On Linux, the monitor will be deleted when IO thread |
| 733 // goes away. | 747 // goes away. |
| 734 #if defined(OS_WIN) | 748 #if defined(OS_WIN) |
| 735 system_message_window_.reset(); | 749 system_message_window_.reset(); |
| 736 #elif defined(OS_MACOSX) | 750 #elif defined(OS_MACOSX) |
| (...skipping 16 matching lines...) Expand all Loading... |
| 753 // | 767 // |
| 754 // | 768 // |
| 755 // - The IO thread is the only user of the CACHE thread. | 769 // - The IO thread is the only user of the CACHE thread. |
| 756 // | 770 // |
| 757 // - The PROCESS_LAUNCHER thread must be stopped after IO in case | 771 // - The PROCESS_LAUNCHER thread must be stopped after IO in case |
| 758 // the IO thread posted a task to terminate a process on the | 772 // the IO thread posted a task to terminate a process on the |
| 759 // process launcher thread. | 773 // process launcher thread. |
| 760 // | 774 // |
| 761 // - (Not sure why DB stops last.) | 775 // - (Not sure why DB stops last.) |
| 762 switch (thread_id) { | 776 switch (thread_id) { |
| 763 case BrowserThread::DB: | 777 case BrowserThread::DB: { |
| 764 db_thread_.reset(); | 778 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:DBThread"); |
| 779 db_thread_.reset(); |
| 780 } |
| 765 break; | 781 break; |
| 766 case BrowserThread::FILE_USER_BLOCKING: | 782 case BrowserThread::FILE_USER_BLOCKING: { |
| 767 file_user_blocking_thread_.reset(); | 783 TRACE_EVENT0("shutdown", |
| 784 "BrowserMainLoop::Subsystem:FileUserBlockingThread"); |
| 785 file_user_blocking_thread_.reset(); |
| 786 } |
| 768 break; | 787 break; |
| 769 case BrowserThread::FILE: | 788 case BrowserThread::FILE: { |
| 789 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:FileThread"); |
| 770 #if !defined(OS_IOS) | 790 #if !defined(OS_IOS) |
| 771 // Clean up state that lives on or uses the file_thread_ before | 791 // Clean up state that lives on or uses the file_thread_ before |
| 772 // it goes away. | 792 // it goes away. |
| 773 if (resource_dispatcher_host_) | 793 if (resource_dispatcher_host_) |
| 774 resource_dispatcher_host_.get()->save_file_manager()->Shutdown(); | 794 resource_dispatcher_host_.get()->save_file_manager()->Shutdown(); |
| 775 #endif // !defined(OS_IOS) | 795 #endif // !defined(OS_IOS) |
| 776 file_thread_.reset(); | 796 file_thread_.reset(); |
| 797 } |
| 777 break; | 798 break; |
| 778 case BrowserThread::PROCESS_LAUNCHER: | 799 case BrowserThread::PROCESS_LAUNCHER: { |
| 779 process_launcher_thread_.reset(); | 800 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:LauncherThread"); |
| 801 process_launcher_thread_.reset(); |
| 802 } |
| 780 break; | 803 break; |
| 781 case BrowserThread::CACHE: | 804 case BrowserThread::CACHE: { |
| 782 cache_thread_.reset(); | 805 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:CacheThread"); |
| 806 cache_thread_.reset(); |
| 807 } |
| 783 break; | 808 break; |
| 784 case BrowserThread::IO: | 809 case BrowserThread::IO: { |
| 785 io_thread_.reset(); | 810 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:IOThread"); |
| 811 io_thread_.reset(); |
| 812 } |
| 786 break; | 813 break; |
| 787 case BrowserThread::UI: | 814 case BrowserThread::UI: |
| 788 case BrowserThread::ID_COUNT: | 815 case BrowserThread::ID_COUNT: |
| 789 default: | 816 default: |
| 790 NOTREACHED(); | 817 NOTREACHED(); |
| 791 break; | 818 break; |
| 792 } | 819 } |
| 793 } | 820 } |
| 794 | 821 |
| 795 #if !defined(OS_IOS) | 822 #if !defined(OS_IOS) |
| 796 indexed_db_thread_.reset(); | 823 { |
| 824 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:IndexedDBThread"); |
| 825 indexed_db_thread_.reset(); |
| 826 } |
| 797 #endif | 827 #endif |
| 798 | 828 |
| 799 // Close the blocking I/O pool after the other threads. Other threads such | 829 // Close the blocking I/O pool after the other threads. Other threads such |
| 800 // as the I/O thread may need to schedule work like closing files or flushing | 830 // as the I/O thread may need to schedule work like closing files or flushing |
| 801 // data during shutdown, so the blocking pool needs to be available. There | 831 // data during shutdown, so the blocking pool needs to be available. There |
| 802 // may also be slow operations pending that will blcok shutdown, so closing | 832 // may also be slow operations pending that will blcok shutdown, so closing |
| 803 // it here (which will block until required operations are complete) gives | 833 // it here (which will block until required operations are complete) gives |
| 804 // more head start for those operations to finish. | 834 // more head start for those operations to finish. |
| 805 BrowserThreadImpl::ShutdownThreadPool(); | 835 { |
| 836 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:ThreadPool"); |
| 837 BrowserThreadImpl::ShutdownThreadPool(); |
| 838 } |
| 806 | 839 |
| 807 #if !defined(OS_IOS) | 840 #if !defined(OS_IOS) |
| 808 // Must happen after the IO thread is shutdown since this may be accessed from | 841 // Must happen after the IO thread is shutdown since this may be accessed from |
| 809 // it. | 842 // it. |
| 810 BrowserGpuChannelHostFactory::Terminate(); | 843 { |
| 844 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:GPUChannelFactory"); |
| 845 BrowserGpuChannelHostFactory::Terminate(); |
| 846 } |
| 811 | 847 |
| 812 // Must happen after the I/O thread is shutdown since this class lives on the | 848 // Must happen after the I/O thread is shutdown since this class lives on the |
| 813 // I/O thread and isn't threadsafe. | 849 // I/O thread and isn't threadsafe. |
| 814 GamepadService::GetInstance()->Terminate(); | 850 { |
| 815 DeviceInertialSensorService::GetInstance()->Shutdown(); | 851 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:GamepadService"); |
| 816 | 852 GamepadService::GetInstance()->Terminate(); |
| 817 URLDataManager::DeleteDataSources(); | 853 } |
| 854 { |
| 855 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:SensorService"); |
| 856 DeviceInertialSensorService::GetInstance()->Shutdown(); |
| 857 } |
| 858 { |
| 859 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:DeleteDataSources"); |
| 860 URLDataManager::DeleteDataSources(); |
| 861 } |
| 818 #endif // !defined(OS_IOS) | 862 #endif // !defined(OS_IOS) |
| 819 | 863 |
| 820 if (parts_) | 864 if (parts_) { |
| 865 TRACE_EVENT0("shutdown", "BrowserMainLoop::Subsystem:PostDestroyThreads"); |
| 821 parts_->PostDestroyThreads(); | 866 parts_->PostDestroyThreads(); |
| 867 } |
| 822 } | 868 } |
| 823 | 869 |
| 824 void BrowserMainLoop::InitializeMainThread() { | 870 void BrowserMainLoop::InitializeMainThread() { |
| 825 TRACE_EVENT0("startup", "BrowserMainLoop::InitializeMainThread") | 871 TRACE_EVENT0("startup", "BrowserMainLoop::InitializeMainThread") |
| 826 const char* kThreadName = "CrBrowserMain"; | 872 const char* kThreadName = "CrBrowserMain"; |
| 827 base::PlatformThread::SetName(kThreadName); | 873 base::PlatformThread::SetName(kThreadName); |
| 828 if (main_message_loop_) | 874 if (main_message_loop_) |
| 829 main_message_loop_->set_thread_name(kThreadName); | 875 main_message_loop_->set_thread_name(kThreadName); |
| 830 | 876 |
| 831 // Register the main thread by instantiating it, but don't call any methods. | 877 // Register the main thread by instantiating it, but don't call any methods. |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 989 if (parameters_.ui_task) | 1035 if (parameters_.ui_task) |
| 990 base::MessageLoopForUI::current()->PostTask(FROM_HERE, | 1036 base::MessageLoopForUI::current()->PostTask(FROM_HERE, |
| 991 *parameters_.ui_task); | 1037 *parameters_.ui_task); |
| 992 | 1038 |
| 993 base::RunLoop run_loop; | 1039 base::RunLoop run_loop; |
| 994 run_loop.Run(); | 1040 run_loop.Run(); |
| 995 #endif | 1041 #endif |
| 996 } | 1042 } |
| 997 | 1043 |
| 998 } // namespace content | 1044 } // namespace content |
| OLD | NEW |