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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 | 116 |
117 #if defined(USE_AURA) | 117 #if defined(USE_AURA) |
118 #include "content/public/browser/context_factory.h" | 118 #include "content/public/browser/context_factory.h" |
119 #include "ui/aura/env.h" | 119 #include "ui/aura/env.h" |
120 #endif | 120 #endif |
121 | 121 |
122 #if defined(OS_ANDROID) | 122 #if defined(OS_ANDROID) |
123 #include "base/android/jni_android.h" | 123 #include "base/android/jni_android.h" |
124 #include "components/tracing/common/graphics_memory_dump_provider_android.h" | 124 #include "components/tracing/common/graphics_memory_dump_provider_android.h" |
125 #include "content/browser/android/browser_startup_controller.h" | 125 #include "content/browser/android/browser_startup_controller.h" |
| 126 #include "content/browser/android/launcher_thread.h" |
126 #include "content/browser/android/scoped_surface_request_manager.h" | 127 #include "content/browser/android/scoped_surface_request_manager.h" |
127 #include "content/browser/android/tracing_controller_android.h" | 128 #include "content/browser/android/tracing_controller_android.h" |
128 #include "content/browser/media/android/browser_media_player_manager.h" | 129 #include "content/browser/media/android/browser_media_player_manager.h" |
129 #include "content/browser/screen_orientation/screen_orientation_delegate_android
.h" | 130 #include "content/browser/screen_orientation/screen_orientation_delegate_android
.h" |
130 #include "media/base/android/media_drm_bridge_client.h" | 131 #include "media/base/android/media_drm_bridge_client.h" |
131 #include "ui/android/screen_android.h" | 132 #include "ui/android/screen_android.h" |
132 #include "ui/display/screen.h" | 133 #include "ui/display/screen.h" |
133 #include "ui/gl/gl_surface.h" | 134 #include "ui/gl/gl_surface.h" |
134 #endif | 135 #endif |
135 | 136 |
(...skipping 860 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
996 // | 997 // |
997 // Must be size_t so we can increment it. | 998 // Must be size_t so we can increment it. |
998 for (size_t thread_id = BrowserThread::UI + 1; | 999 for (size_t thread_id = BrowserThread::UI + 1; |
999 thread_id < BrowserThread::ID_COUNT; | 1000 thread_id < BrowserThread::ID_COUNT; |
1000 ++thread_id) { | 1001 ++thread_id) { |
1001 // If this thread ID is backed by a real thread, |thread_to_start| will be | 1002 // If this thread ID is backed by a real thread, |thread_to_start| will be |
1002 // set to the appropriate BrowserProcessSubThread*. And |options| can be | 1003 // set to the appropriate BrowserProcessSubThread*. And |options| can be |
1003 // updated away from its default. | 1004 // updated away from its default. |
1004 std::unique_ptr<BrowserProcessSubThread>* thread_to_start = nullptr; | 1005 std::unique_ptr<BrowserProcessSubThread>* thread_to_start = nullptr; |
1005 base::Thread::Options options; | 1006 base::Thread::Options options; |
| 1007 // If |message_loop| is not nullptr, then this BrowserThread will use this |
| 1008 // message loop instead of creating a new thread. Note that means this |
| 1009 // thread will not be joined on shutdown, and may cause use-after-free if |
| 1010 // anything tries to access objects deleted by AtExitManager, such as |
| 1011 // non-leaky LazyInstance. |
| 1012 base::MessageLoop* message_loop = nullptr; |
| 1013 bool redirect_thread = redirect_nonUInonIO_browser_threads; |
1006 | 1014 |
1007 // Otherwise this thread ID will be backed by a SingleThreadTaskRunner using | 1015 // Otherwise this thread ID will be backed by a SingleThreadTaskRunner using |
1008 // |non_ui_non_io_task_runner_traits| (which can be augmented below). | 1016 // |non_ui_non_io_task_runner_traits| (which can be augmented below). |
1009 // TODO(gab): Existing non-UI/non-IO BrowserThreads allow sync primitives so | 1017 // TODO(gab): Existing non-UI/non-IO BrowserThreads allow sync primitives so |
1010 // the initial redirection will as well but they probably don't need to. | 1018 // the initial redirection will as well but they probably don't need to. |
1011 base::TaskTraits non_ui_non_io_task_runner_traits = | 1019 base::TaskTraits non_ui_non_io_task_runner_traits = |
1012 base::TaskTraits().MayBlock().WithBaseSyncPrimitives(); | 1020 base::TaskTraits().MayBlock().WithBaseSyncPrimitives(); |
1013 | 1021 |
1014 switch (thread_id) { | 1022 switch (thread_id) { |
1015 case BrowserThread::DB: | 1023 case BrowserThread::DB: |
1016 TRACE_EVENT_BEGIN1("startup", | 1024 TRACE_EVENT_BEGIN1("startup", |
1017 "BrowserMainLoop::CreateThreads:start", | 1025 "BrowserMainLoop::CreateThreads:start", |
1018 "Thread", "BrowserThread::DB"); | 1026 "Thread", "BrowserThread::DB"); |
1019 if (redirect_nonUInonIO_browser_threads) { | 1027 if (redirect_thread) { |
1020 non_ui_non_io_task_runner_traits | 1028 non_ui_non_io_task_runner_traits |
1021 .WithPriority(base::TaskPriority::USER_VISIBLE) | 1029 .WithPriority(base::TaskPriority::USER_VISIBLE) |
1022 .WithShutdownBehavior(base::TaskShutdownBehavior::BLOCK_SHUTDOWN); | 1030 .WithShutdownBehavior(base::TaskShutdownBehavior::BLOCK_SHUTDOWN); |
1023 } else { | 1031 } else { |
1024 thread_to_start = &db_thread_; | 1032 thread_to_start = &db_thread_; |
1025 options.timer_slack = base::TIMER_SLACK_MAXIMUM; | 1033 options.timer_slack = base::TIMER_SLACK_MAXIMUM; |
1026 } | 1034 } |
1027 break; | 1035 break; |
1028 case BrowserThread::FILE_USER_BLOCKING: | 1036 case BrowserThread::FILE_USER_BLOCKING: |
1029 TRACE_EVENT_BEGIN1("startup", | 1037 TRACE_EVENT_BEGIN1("startup", |
1030 "BrowserMainLoop::CreateThreads:start", | 1038 "BrowserMainLoop::CreateThreads:start", |
1031 "Thread", "BrowserThread::FILE_USER_BLOCKING"); | 1039 "Thread", "BrowserThread::FILE_USER_BLOCKING"); |
1032 if (redirect_nonUInonIO_browser_threads) { | 1040 if (redirect_thread) { |
1033 non_ui_non_io_task_runner_traits | 1041 non_ui_non_io_task_runner_traits |
1034 .WithPriority(base::TaskPriority::USER_BLOCKING) | 1042 .WithPriority(base::TaskPriority::USER_BLOCKING) |
1035 .WithShutdownBehavior(base::TaskShutdownBehavior::BLOCK_SHUTDOWN); | 1043 .WithShutdownBehavior(base::TaskShutdownBehavior::BLOCK_SHUTDOWN); |
1036 } else { | 1044 } else { |
1037 thread_to_start = &file_user_blocking_thread_; | 1045 thread_to_start = &file_user_blocking_thread_; |
1038 } | 1046 } |
1039 break; | 1047 break; |
1040 case BrowserThread::FILE: | 1048 case BrowserThread::FILE: |
1041 TRACE_EVENT_BEGIN1("startup", | 1049 TRACE_EVENT_BEGIN1("startup", |
1042 "BrowserMainLoop::CreateThreads:start", | 1050 "BrowserMainLoop::CreateThreads:start", |
1043 "Thread", "BrowserThread::FILE"); | 1051 "Thread", "BrowserThread::FILE"); |
1044 | 1052 |
1045 #if defined(OS_WIN) | 1053 #if defined(OS_WIN) |
1046 // On Windows, the FILE thread needs to have a UI message loop which | 1054 // On Windows, the FILE thread needs to have a UI message loop which |
1047 // pumps messages in such a way that Google Update can communicate back | 1055 // pumps messages in such a way that Google Update can communicate back |
1048 // to us. | 1056 // to us. |
1049 // TODO(robliao): Need to support COM in TaskScheduler before | 1057 // TODO(robliao): Need to support COM in TaskScheduler before |
1050 // redirecting the FILE thread on Windows. http://crbug.com/662122 | 1058 // redirecting the FILE thread on Windows. http://crbug.com/662122 |
1051 thread_to_start = &file_thread_; | 1059 thread_to_start = &file_thread_; |
1052 options = ui_message_loop_options; | 1060 options = ui_message_loop_options; |
1053 options.timer_slack = base::TIMER_SLACK_MAXIMUM; | 1061 options.timer_slack = base::TIMER_SLACK_MAXIMUM; |
1054 #else | 1062 #else |
1055 if (redirect_nonUInonIO_browser_threads) { | 1063 if (redirect_thread) { |
1056 non_ui_non_io_task_runner_traits | 1064 non_ui_non_io_task_runner_traits |
1057 .WithPriority(base::TaskPriority::USER_VISIBLE) | 1065 .WithPriority(base::TaskPriority::USER_VISIBLE) |
1058 .WithShutdownBehavior(base::TaskShutdownBehavior::BLOCK_SHUTDOWN); | 1066 .WithShutdownBehavior(base::TaskShutdownBehavior::BLOCK_SHUTDOWN); |
1059 } else { | 1067 } else { |
1060 thread_to_start = &file_thread_; | 1068 thread_to_start = &file_thread_; |
1061 options = io_message_loop_options; | 1069 options = io_message_loop_options; |
1062 options.timer_slack = base::TIMER_SLACK_MAXIMUM; | 1070 options.timer_slack = base::TIMER_SLACK_MAXIMUM; |
1063 } | 1071 } |
1064 #endif | 1072 #endif |
1065 break; | 1073 break; |
1066 case BrowserThread::PROCESS_LAUNCHER: | 1074 case BrowserThread::PROCESS_LAUNCHER: |
1067 TRACE_EVENT_BEGIN1("startup", | 1075 TRACE_EVENT_BEGIN1("startup", |
1068 "BrowserMainLoop::CreateThreads:start", | 1076 "BrowserMainLoop::CreateThreads:start", |
1069 "Thread", "BrowserThread::PROCESS_LAUNCHER"); | 1077 "Thread", "BrowserThread::PROCESS_LAUNCHER"); |
1070 if (redirect_nonUInonIO_browser_threads) { | 1078 #if defined(OS_ANDROID) |
| 1079 // Android specializes Launcher thread so it is accessible in java. |
| 1080 // Note Android never does clean shutdown, so shutdown use-after-free |
| 1081 // concerns are not a problem in practice. |
| 1082 redirect_thread = false; |
| 1083 message_loop = android::LauncherThread::GetMessageLoop(); |
| 1084 #endif |
| 1085 if (redirect_thread) { |
1071 non_ui_non_io_task_runner_traits | 1086 non_ui_non_io_task_runner_traits |
1072 .WithPriority(base::TaskPriority::USER_BLOCKING) | 1087 .WithPriority(base::TaskPriority::USER_BLOCKING) |
1073 .WithShutdownBehavior(base::TaskShutdownBehavior::BLOCK_SHUTDOWN); | 1088 .WithShutdownBehavior(base::TaskShutdownBehavior::BLOCK_SHUTDOWN); |
1074 } else { | 1089 } else { |
1075 thread_to_start = &process_launcher_thread_; | 1090 thread_to_start = &process_launcher_thread_; |
1076 options.timer_slack = base::TIMER_SLACK_MAXIMUM; | 1091 options.timer_slack = base::TIMER_SLACK_MAXIMUM; |
1077 } | 1092 } |
1078 break; | 1093 break; |
1079 case BrowserThread::CACHE: | 1094 case BrowserThread::CACHE: |
1080 TRACE_EVENT_BEGIN1("startup", | 1095 TRACE_EVENT_BEGIN1("startup", |
1081 "BrowserMainLoop::CreateThreads:start", | 1096 "BrowserMainLoop::CreateThreads:start", |
1082 "Thread", "BrowserThread::CACHE"); | 1097 "Thread", "BrowserThread::CACHE"); |
1083 #if defined(OS_WIN) | 1098 #if defined(OS_WIN) |
1084 // TaskScheduler doesn't support async I/O on Windows as CACHE thread is | 1099 // TaskScheduler doesn't support async I/O on Windows as CACHE thread is |
1085 // the only user and this use case is going away in | 1100 // the only user and this use case is going away in |
1086 // https://codereview.chromium.org/2216583003/. | 1101 // https://codereview.chromium.org/2216583003/. |
1087 // TODO(gavinp): Remove this ifdef (and thus enable redirection of the | 1102 // TODO(gavinp): Remove this ifdef (and thus enable redirection of the |
1088 // CACHE thread on Windows) once that CL lands. | 1103 // CACHE thread on Windows) once that CL lands. |
1089 thread_to_start = &cache_thread_; | 1104 thread_to_start = &cache_thread_; |
1090 options = io_message_loop_options; | 1105 options = io_message_loop_options; |
1091 options.timer_slack = base::TIMER_SLACK_MAXIMUM; | 1106 options.timer_slack = base::TIMER_SLACK_MAXIMUM; |
1092 #else // OS_WIN | 1107 #else // OS_WIN |
1093 if (redirect_nonUInonIO_browser_threads) { | 1108 if (redirect_thread) { |
1094 non_ui_non_io_task_runner_traits | 1109 non_ui_non_io_task_runner_traits |
1095 .WithPriority(base::TaskPriority::USER_BLOCKING) | 1110 .WithPriority(base::TaskPriority::USER_BLOCKING) |
1096 .WithShutdownBehavior(base::TaskShutdownBehavior::BLOCK_SHUTDOWN); | 1111 .WithShutdownBehavior(base::TaskShutdownBehavior::BLOCK_SHUTDOWN); |
1097 } else { | 1112 } else { |
1098 thread_to_start = &cache_thread_; | 1113 thread_to_start = &cache_thread_; |
1099 options.timer_slack = base::TIMER_SLACK_MAXIMUM; | 1114 options.timer_slack = base::TIMER_SLACK_MAXIMUM; |
1100 } | 1115 } |
1101 #endif // OS_WIN | 1116 #endif // OS_WIN |
1102 break; | 1117 break; |
1103 case BrowserThread::IO: | 1118 case BrowserThread::IO: |
(...skipping 10 matching lines...) Expand all Loading... |
1114 break; | 1129 break; |
1115 case BrowserThread::UI: // Falls through. | 1130 case BrowserThread::UI: // Falls through. |
1116 case BrowserThread::ID_COUNT: // Falls through. | 1131 case BrowserThread::ID_COUNT: // Falls through. |
1117 NOTREACHED(); | 1132 NOTREACHED(); |
1118 break; | 1133 break; |
1119 } | 1134 } |
1120 | 1135 |
1121 BrowserThread::ID id = static_cast<BrowserThread::ID>(thread_id); | 1136 BrowserThread::ID id = static_cast<BrowserThread::ID>(thread_id); |
1122 | 1137 |
1123 if (thread_to_start) { | 1138 if (thread_to_start) { |
1124 (*thread_to_start).reset(new BrowserProcessSubThread(id)); | 1139 (*thread_to_start) |
| 1140 .reset(message_loop ? new BrowserProcessSubThread(id, message_loop) |
| 1141 : new BrowserProcessSubThread(id)); |
1125 if (!(*thread_to_start)->StartWithOptions(options)) | 1142 if (!(*thread_to_start)->StartWithOptions(options)) |
1126 LOG(FATAL) << "Failed to start the browser thread: id == " << id; | 1143 LOG(FATAL) << "Failed to start the browser thread: id == " << id; |
1127 } else { | 1144 } else { |
1128 scoped_refptr<base::SingleThreadTaskRunner> redirection_task_runner = | 1145 scoped_refptr<base::SingleThreadTaskRunner> redirection_task_runner = |
1129 base::CreateSingleThreadTaskRunnerWithTraits( | 1146 base::CreateSingleThreadTaskRunnerWithTraits( |
1130 non_ui_non_io_task_runner_traits); | 1147 non_ui_non_io_task_runner_traits); |
1131 DCHECK(redirection_task_runner); | 1148 DCHECK(redirection_task_runner); |
1132 BrowserThreadImpl::RedirectThreadIDToTaskRunner( | 1149 BrowserThreadImpl::RedirectThreadIDToTaskRunner( |
1133 id, std::move(redirection_task_runner)); | 1150 id, std::move(redirection_task_runner)); |
1134 } | 1151 } |
(...skipping 620 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1755 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE), | 1772 BrowserThread::GetTaskRunnerForThread(BrowserThread::FILE), |
1756 MediaInternals::GetInstance()); | 1773 MediaInternals::GetInstance()); |
1757 } | 1774 } |
1758 CHECK(audio_manager_); | 1775 CHECK(audio_manager_); |
1759 | 1776 |
1760 audio_system_ = media::AudioSystemImpl::Create(audio_manager_.get()); | 1777 audio_system_ = media::AudioSystemImpl::Create(audio_manager_.get()); |
1761 CHECK(audio_system_); | 1778 CHECK(audio_system_); |
1762 } | 1779 } |
1763 | 1780 |
1764 } // namespace content | 1781 } // namespace content |
OLD | NEW |