[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/public/app/content_main_runner.h" |
| 6 | |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 7 | #include <stddef.h> |
[email protected] | 3422af1 | 2012-08-08 01:27:03 | [diff] [blame] | 8 | #include <stdlib.h> |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 9 | #include <string.h> |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 10 | |
| 11 | #include <memory> |
pmonette | 18d3ed3 | 2015-10-16 21:06:06 | [diff] [blame] | 12 | #include <string> |
dcheng | f63a125 | 2015-12-26 20:43:13 | [diff] [blame] | 13 | #include <utility> |
leon.han | 79db18e | 2017-01-27 05:26:26 | [diff] [blame] | 14 | #include <vector> |
pmonette | 18d3ed3 | 2015-10-16 21:06:06 | [diff] [blame] | 15 | |
primiano | be9d6fc | 2016-01-12 22:16:11 | [diff] [blame] | 16 | #include "base/allocator/allocator_check.h" |
[email protected] | 237a1485 | 2012-04-28 02:56:38 | [diff] [blame] | 17 | #include "base/allocator/allocator_extension.h" |
erikchen | bf11b55a | 2017-02-10 01:34:48 | [diff] [blame] | 18 | #include "base/allocator/features.h" |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 19 | #include "base/at_exit.h" |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 20 | #include "base/base_switches.h" |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 21 | #include "base/command_line.h" |
| 22 | #include "base/debug/debugger.h" |
jam | 79dc59a | 2015-08-17 03:38:16 | [diff] [blame] | 23 | #include "base/debug/stack_trace.h" |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 24 | #include "base/feature_list.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 25 | #include "base/files/file_path.h" |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 26 | #include "base/i18n/icu_util.h" |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 27 | #include "base/lazy_instance.h" |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 28 | #include "base/logging.h" |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 29 | #include "base/macros.h" |
[email protected] | 54ad01a | 2014-05-11 01:17:47 | [diff] [blame] | 30 | #include "base/memory/scoped_vector.h" |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 31 | #include "base/metrics/field_trial.h" |
bcwhite | d723c25 | 2016-03-16 17:25:41 | [diff] [blame] | 32 | #include "base/metrics/histogram_base.h" |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 33 | #include "base/metrics/statistics_recorder.h" |
[email protected] | 433df47 | 2012-03-07 20:33:39 | [diff] [blame] | 34 | #include "base/path_service.h" |
[email protected] | 54724e2 | 2013-07-25 13:02:15 | [diff] [blame] | 35 | #include "base/process/launch.h" |
| 36 | #include "base/process/memory.h" |
lawrencewu | 2c93082 | 2016-10-13 13:54:25 | [diff] [blame] | 37 | #include "base/process/process.h" |
[email protected] | 54724e2 | 2013-07-25 13:02:15 | [diff] [blame] | 38 | #include "base/process/process_handle.h" |
yiyaoliu | 9e6a5ab3 | 2015-03-18 18:04:37 | [diff] [blame] | 39 | #include "base/profiler/scoped_tracker.h" |
[email protected] | e37f55e | 2013-06-11 04:29:00 | [diff] [blame] | 40 | #include "base/strings/string_number_conversions.h" |
| 41 | #include "base/strings/string_util.h" |
| 42 | #include "base/strings/stringprintf.h" |
primiano | 50b7444c | 2015-01-28 04:17:00 | [diff] [blame] | 43 | #include "base/trace_event/trace_event.h" |
avi | 66a0772 | 2015-12-25 23:38:12 | [diff] [blame] | 44 | #include "build/build_config.h" |
ssid | 69e8419 | 2017-02-16 14:32:47 | [diff] [blame] | 45 | #include "components/tracing/common/trace_startup.h" |
jam | e1f453c | 2016-03-21 15:51:34 | [diff] [blame] | 46 | #include "content/app/mojo/mojo_init.h" |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 47 | #include "content/common/set_process_title.h" |
[email protected] | 1de7a17 | 2012-05-28 18:36:43 | [diff] [blame] | 48 | #include "content/common/url_schemes.h" |
[email protected] | 8a820c8 | 2014-03-09 18:06:58 | [diff] [blame] | 49 | #include "content/public/app/content_main.h" |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 50 | #include "content/public/app/content_main_delegate.h" |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 51 | #include "content/public/common/content_client.h" |
| 52 | #include "content/public/common/content_constants.h" |
jcivelli | dad0cef | 2017-02-16 18:38:59 | [diff] [blame] | 53 | #include "content/public/common/content_descriptor_keys.h" |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 54 | #include "content/public/common/content_paths.h" |
| 55 | #include "content/public/common/content_switches.h" |
| 56 | #include "content/public/common/main_function_params.h" |
| 57 | #include "content/public/common/sandbox_init.h" |
[email protected] | 4e9b3d5 | 2014-04-08 23:13:45 | [diff] [blame] | 58 | #include "ipc/ipc_descriptors.h" |
[email protected] | 433df47 | 2012-03-07 20:33:39 | [diff] [blame] | 59 | #include "media/base/media.h" |
brettw | 4b46108 | 2016-11-19 18:55:16 | [diff] [blame] | 60 | #include "ppapi/features/features.h" |
jcivelli | dad0cef | 2017-02-16 18:38:59 | [diff] [blame] | 61 | #include "services/service_manager/public/cpp/shared_file_util.h" |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 62 | #include "ui/base/ui_base_paths.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 63 | #include "ui/base/ui_base_switches.h" |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 64 | |
scottmg | 8f970b1 | 2017-01-20 19:37:56 | [diff] [blame] | 65 | #if defined(V8_USE_EXTERNAL_STARTUP_DATA) && \ |
| 66 | !defined(CHROME_MULTIPLE_DLL_BROWSER) |
oth | 05c26fde | 2015-04-05 14:30:57 | [diff] [blame] | 67 | #include "gin/v8_initializer.h" |
baixo | 3a3c88a | 2014-10-28 11:52:21 | [diff] [blame] | 68 | #endif |
| 69 | |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 70 | #if defined(OS_WIN) |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 71 | #include <malloc.h> |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 72 | #include <cstring> |
[email protected] | 6c72e09 | 2014-06-04 11:10:51 | [diff] [blame] | 73 | |
georgesak | 28ad5e6 | 2015-04-16 09:25:19 | [diff] [blame] | 74 | #include "base/trace_event/trace_event_etw_export_win.h" |
pmonette | 18d3ed3 | 2015-10-16 21:06:06 | [diff] [blame] | 75 | #include "base/win/process_startup_helper.h" |
wfh | 48c487e6 | 2016-07-27 22:48:47 | [diff] [blame] | 76 | #include "sandbox/win/src/sandbox_types.h" |
[email protected] | 75f6dea0 | 2014-06-11 12:13:23 | [diff] [blame] | 77 | #include "ui/base/win/atl_module.h" |
robliao | 18e220e8 | 2016-04-19 16:47:12 | [diff] [blame] | 78 | #include "ui/display/win/dpi.h" |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 79 | #elif defined(OS_MACOSX) |
erikchen | bf11b55a | 2017-02-10 01:34:48 | [diff] [blame] | 80 | #include "base/allocator/allocator_shim.h" |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 81 | #include "base/mac/scoped_nsautorelease_pool.h" |
[email protected] | 9dd9015 | 2013-08-02 22:09:13 | [diff] [blame] | 82 | #include "base/power_monitor/power_monitor_device_source.h" |
ccameron | c685e64c | 2015-05-12 07:05:17 | [diff] [blame] | 83 | #include "content/app/mac/mac_init.h" |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 84 | #include "content/browser/mach_broker_mac.h" |
| 85 | #include "content/common/sandbox_init_mac.h" |
| 86 | #endif // OS_WIN |
| 87 | |
| 88 | #if defined(OS_POSIX) |
| 89 | #include <signal.h> |
| 90 | |
jcivelli | dad0cef | 2017-02-16 18:38:59 | [diff] [blame] | 91 | #include "base/file_descriptor_store.h" |
[email protected] | 613eef6 | 2012-11-09 23:46:54 | [diff] [blame] | 92 | #include "base/posix/global_descriptors.h" |
[email protected] | b3aabd34 | 2012-06-04 19:33:56 | [diff] [blame] | 93 | #include "content/public/common/content_descriptors.h" |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 94 | |
| 95 | #if !defined(OS_MACOSX) |
| 96 | #include "content/public/common/zygote_fork_delegate_linux.h" |
| 97 | #endif |
[email protected] | 16caf2c2 | 2013-04-13 01:26:07 | [diff] [blame] | 98 | #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 99 | #include "content/zygote/zygote_main.h" |
| 100 | #endif |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 101 | |
| 102 | #endif // OS_POSIX |
| 103 | |
davidben | ee55ed44 | 2015-10-02 17:06:11 | [diff] [blame] | 104 | #if defined(USE_NSS_CERTS) |
| 105 | #include "crypto/nss_util.h" |
| 106 | #endif |
| 107 | |
jam | ce6dd211 | 2017-02-15 19:31:42 | [diff] [blame] | 108 | #if !defined(CHROME_MULTIPLE_DLL_BROWSER) |
| 109 | #include "content/public/gpu/content_gpu_client.h" |
| 110 | #include "content/public/renderer/content_renderer_client.h" |
| 111 | #include "content/public/utility/content_utility_client.h" |
| 112 | #endif |
| 113 | |
| 114 | #if !defined(CHROME_MULTIPLE_DLL_CHILD) |
| 115 | #include "content/browser/browser_main.h" |
| 116 | #include "content/public/browser/content_browser_client.h" |
| 117 | #endif |
| 118 | |
sadrul | e0d5a0d | 2017-01-30 19:09:02 | [diff] [blame] | 119 | #if !defined(CHROME_MULTIPLE_DLL_BROWSER) && !defined(CHROME_MULTIPLE_DLL_CHILD) |
tapted | 4091f2f | 2017-02-07 00:47:19 | [diff] [blame] | 120 | #include "content/browser/gpu/gpu_main_thread_factory.h" |
jam | ce6dd211 | 2017-02-15 19:31:42 | [diff] [blame] | 121 | #include "content/browser/renderer_host/render_process_host_impl.h" |
| 122 | #include "content/browser/utility_process_host_impl.h" |
| 123 | #include "content/gpu/in_process_gpu_thread.h" |
| 124 | #include "content/renderer/in_process_renderer_thread.h" |
| 125 | #include "content/utility/in_process_utility_thread.h" |
sadrul | e0d5a0d | 2017-01-30 19:09:02 | [diff] [blame] | 126 | #endif |
| 127 | |
xhwang | 785a834 | 2017-01-26 06:46:39 | [diff] [blame] | 128 | #if BUILDFLAG(ENABLE_PEPPER_CDMS) |
| 129 | #include "content/common/media/cdm_host_files.h" |
| 130 | #endif |
| 131 | |
[email protected] | a31fe0f | 2012-05-08 19:28:51 | [diff] [blame] | 132 | namespace content { |
[email protected] | eb39819 | 2012-10-22 20:16:19 | [diff] [blame] | 133 | extern int GpuMain(const content::MainFunctionParams&); |
brettw | 4b46108 | 2016-11-19 18:55:16 | [diff] [blame] | 134 | #if BUILDFLAG(ENABLE_PLUGINS) |
[email protected] | c997bc4 | 2014-04-11 18:25:58 | [diff] [blame] | 135 | #if !defined(OS_LINUX) |
[email protected] | 91355a8 | 2012-10-21 19:52:47 | [diff] [blame] | 136 | extern int PluginMain(const content::MainFunctionParams&); |
[email protected] | c997bc4 | 2014-04-11 18:25:58 | [diff] [blame] | 137 | #endif |
[email protected] | 91355a8 | 2012-10-21 19:52:47 | [diff] [blame] | 138 | extern int PpapiPluginMain(const MainFunctionParams&); |
| 139 | extern int PpapiBrokerMain(const MainFunctionParams&); |
[email protected] | 0106cde | 2013-02-27 02:16:38 | [diff] [blame] | 140 | #endif |
[email protected] | 91355a8 | 2012-10-21 19:52:47 | [diff] [blame] | 141 | extern int RendererMain(const content::MainFunctionParams&); |
| 142 | extern int UtilityMain(const MainFunctionParams&); |
[email protected] | e9ff79c | 2012-10-19 21:31:26 | [diff] [blame] | 143 | } // namespace content |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 144 | |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 145 | namespace content { |
| 146 | |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 147 | namespace { |
| 148 | |
jcivelli | 15ffa46 | 2017-02-09 18:01:30 | [diff] [blame] | 149 | #if defined(V8_USE_EXTERNAL_STARTUP_DATA) && defined(OS_ANDROID) |
| 150 | #if defined __LP64__ |
jcivelli | dad0cef | 2017-02-16 18:38:59 | [diff] [blame] | 151 | #define kV8SnapshotDataDescriptor kV8Snapshot64DataDescriptor |
jcivelli | 15ffa46 | 2017-02-09 18:01:30 | [diff] [blame] | 152 | #else |
jcivelli | dad0cef | 2017-02-16 18:38:59 | [diff] [blame] | 153 | #define kV8SnapshotDataDescriptor kV8Snapshot32DataDescriptor |
jcivelli | 15ffa46 | 2017-02-09 18:01:30 | [diff] [blame] | 154 | #endif |
| 155 | #endif |
| 156 | |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 157 | // This sets up two singletons responsible for managing field trials. The |
| 158 | // |field_trial_list| singleton lives on the stack and must outlive the Run() |
| 159 | // method of the process. |
| 160 | void InitializeFieldTrialAndFeatureList( |
| 161 | std::unique_ptr<base::FieldTrialList>* field_trial_list) { |
| 162 | const base::CommandLine& command_line = |
| 163 | *base::CommandLine::ForCurrentProcess(); |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 164 | |
| 165 | // Initialize statistical testing infrastructure. We set the entropy |
| 166 | // provider to nullptr to disallow non-browser processes from creating |
| 167 | // their own one-time randomized trials; they should be created in the |
| 168 | // browser process. |
| 169 | field_trial_list->reset(new base::FieldTrialList(nullptr)); |
| 170 | |
| 171 | // Ensure any field trials in browser are reflected into the child |
| 172 | // process. |
lawrencewu | c4fe880 | 2016-11-30 16:25:02 | [diff] [blame] | 173 | #if defined(OS_POSIX) |
| 174 | // On POSIX systems that use the zygote, we get the trials from a shared |
| 175 | // memory segment backed by an fd instead of the command line. |
lawrencewu | 2c93082 | 2016-10-13 13:54:25 | [diff] [blame] | 176 | base::FieldTrialList::CreateTrialsFromCommandLine( |
lawrencewu | c4fe880 | 2016-11-30 16:25:02 | [diff] [blame] | 177 | command_line, switches::kFieldTrialHandle, kFieldTrialDescriptor); |
| 178 | #else |
| 179 | base::FieldTrialList::CreateTrialsFromCommandLine( |
| 180 | command_line, switches::kFieldTrialHandle, -1); |
| 181 | #endif |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 182 | |
| 183 | std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList); |
lawrencewu | 5e03cd3 | 2016-12-05 16:23:28 | [diff] [blame] | 184 | base::FieldTrialList::CreateFeaturesFromCommandLine( |
| 185 | command_line, switches::kEnableFeatures, switches::kDisableFeatures, |
| 186 | feature_list.get()); |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 187 | base::FeatureList::SetInstance(std::move(feature_list)); |
| 188 | } |
| 189 | |
jcivelli | 15ffa46 | 2017-02-09 18:01:30 | [diff] [blame] | 190 | void InitializeV8IfNeeded( |
| 191 | const base::CommandLine& command_line, |
| 192 | const std::string& process_type) { |
| 193 | if (process_type == switches::kGpuProcess) |
| 194 | return; |
| 195 | |
| 196 | #if defined(V8_USE_EXTERNAL_STARTUP_DATA) |
| 197 | #if defined(OS_POSIX) && !defined(OS_MACOSX) |
jcivelli | dad0cef | 2017-02-16 18:38:59 | [diff] [blame] | 198 | base::FileDescriptorStore& file_descriptor_store = |
| 199 | base::FileDescriptorStore::GetInstance(); |
| 200 | base::MemoryMappedFile::Region region; |
| 201 | base::ScopedFD v8_snapshot_fd = |
| 202 | file_descriptor_store.MaybeTakeFD(kV8SnapshotDataDescriptor, ®ion); |
| 203 | if (v8_snapshot_fd.is_valid()) { |
| 204 | gin::V8Initializer::LoadV8SnapshotFromFD(v8_snapshot_fd.get(), |
| 205 | region.offset, region.size); |
jcivelli | 15ffa46 | 2017-02-09 18:01:30 | [diff] [blame] | 206 | } else { |
| 207 | gin::V8Initializer::LoadV8Snapshot(); |
| 208 | } |
jcivelli | dad0cef | 2017-02-16 18:38:59 | [diff] [blame] | 209 | base::ScopedFD v8_natives_fd = |
| 210 | file_descriptor_store.MaybeTakeFD(kV8NativesDataDescriptor, ®ion); |
| 211 | if (v8_natives_fd.is_valid()) { |
| 212 | gin::V8Initializer::LoadV8NativesFromFD(v8_natives_fd.get(), |
| 213 | region.offset, region.size); |
jcivelli | 15ffa46 | 2017-02-09 18:01:30 | [diff] [blame] | 214 | } else { |
| 215 | gin::V8Initializer::LoadV8Natives(); |
| 216 | } |
| 217 | #else |
| 218 | #if !defined(CHROME_MULTIPLE_DLL_BROWSER) |
| 219 | gin::V8Initializer::LoadV8Snapshot(); |
| 220 | gin::V8Initializer::LoadV8Natives(); |
| 221 | #endif // !CHROME_MULTIPLE_DLL_BROWSER |
| 222 | #endif // OS_POSIX && !OS_MACOSX |
| 223 | #endif // V8_USE_EXTERNAL_STARTUP_DATA |
| 224 | } |
| 225 | |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 226 | } // namespace |
| 227 | |
yoz | 774dcda2d | 2014-08-27 03:32:58 | [diff] [blame] | 228 | #if !defined(CHROME_MULTIPLE_DLL_CHILD) |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 229 | base::LazyInstance<ContentBrowserClient> |
| 230 | g_empty_content_browser_client = LAZY_INSTANCE_INITIALIZER; |
yoz | 774dcda2d | 2014-08-27 03:32:58 | [diff] [blame] | 231 | #endif // !CHROME_MULTIPLE_DLL_CHILD |
| 232 | |
jam | e1f453c | 2016-03-21 15:51:34 | [diff] [blame] | 233 | #if !defined(CHROME_MULTIPLE_DLL_BROWSER) |
kcwu | 08377ad | 2016-02-18 19:12:42 | [diff] [blame] | 234 | base::LazyInstance<ContentGpuClient> |
| 235 | g_empty_content_gpu_client = LAZY_INSTANCE_INITIALIZER; |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 236 | base::LazyInstance<ContentRendererClient> |
| 237 | g_empty_content_renderer_client = LAZY_INSTANCE_INITIALIZER; |
| 238 | base::LazyInstance<ContentUtilityClient> |
| 239 | g_empty_content_utility_client = LAZY_INSTANCE_INITIALIZER; |
jam | e1f453c | 2016-03-21 15:51:34 | [diff] [blame] | 240 | #endif // !CHROME_MULTIPLE_DLL_BROWSER |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 241 | |
jam | e1f453c | 2016-03-21 15:51:34 | [diff] [blame] | 242 | #if defined(OS_POSIX) |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 243 | |
| 244 | // Setup signal-handling state: resanitize most signals, ignore SIGPIPE. |
| 245 | void SetupSignalHandlers() { |
| 246 | // Sanitise our signal handling state. Signals that were ignored by our |
| 247 | // parent will also be ignored by us. We also inherit our parent's sigmask. |
| 248 | sigset_t empty_signal_set; |
pmonette | 18d3ed3 | 2015-10-16 21:06:06 | [diff] [blame] | 249 | CHECK_EQ(0, sigemptyset(&empty_signal_set)); |
| 250 | CHECK_EQ(0, sigprocmask(SIG_SETMASK, &empty_signal_set, NULL)); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 251 | |
| 252 | struct sigaction sigact; |
| 253 | memset(&sigact, 0, sizeof(sigact)); |
| 254 | sigact.sa_handler = SIG_DFL; |
| 255 | static const int signals_to_reset[] = |
| 256 | {SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGABRT, SIGFPE, SIGSEGV, |
| 257 | SIGALRM, SIGTERM, SIGCHLD, SIGBUS, SIGTRAP}; // SIGPIPE is set below. |
| 258 | for (unsigned i = 0; i < arraysize(signals_to_reset); i++) { |
pmonette | 18d3ed3 | 2015-10-16 21:06:06 | [diff] [blame] | 259 | CHECK_EQ(0, sigaction(signals_to_reset[i], &sigact, NULL)); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | // Always ignore SIGPIPE. We check the return value of write(). |
pmonette | 18d3ed3 | 2015-10-16 21:06:06 | [diff] [blame] | 263 | CHECK_NE(SIG_ERR, signal(SIGPIPE, SIG_IGN)); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 264 | } |
| 265 | |
jcivelli | dad0cef | 2017-02-16 18:38:59 | [diff] [blame] | 266 | void PopulateFDsFromCommandLine() { |
| 267 | const std::string& shared_file_param = |
| 268 | base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 269 | switches::kSharedFiles); |
| 270 | if (shared_file_param.empty()) |
| 271 | return; |
| 272 | |
| 273 | base::Optional<std::map<int, std::string>> shared_file_descriptors = |
| 274 | service_manager::ParseSharedFileSwitchValue(shared_file_param); |
| 275 | if (!shared_file_descriptors) |
| 276 | return; |
| 277 | |
| 278 | for (const auto& descriptor : *shared_file_descriptors) { |
| 279 | base::MemoryMappedFile::Region region; |
| 280 | const std::string& key = descriptor.second; |
| 281 | base::ScopedFD fd = base::GlobalDescriptors::GetInstance()->TakeFD( |
| 282 | descriptor.first, ®ion); |
| 283 | base::FileDescriptorStore::GetInstance().Set(key, std::move(fd), region); |
| 284 | } |
| 285 | } |
| 286 | |
jam | e1f453c | 2016-03-21 15:51:34 | [diff] [blame] | 287 | #endif // OS_POSIX |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 288 | |
sadrul | f6fb2a7 | 2017-02-08 17:59:28 | [diff] [blame] | 289 | void CommonSubprocessInit() { |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 290 | #if defined(OS_WIN) |
| 291 | // HACK: Let Windows know that we have started. This is needed to suppress |
| 292 | // the IDC_APPSTARTING cursor from being displayed for a prolonged period |
| 293 | // while a subprocess is starting. |
| 294 | PostThreadMessage(GetCurrentThreadId(), WM_NULL, 0, 0); |
| 295 | MSG msg; |
| 296 | PeekMessage(&msg, NULL, 0, 0, PM_REMOVE); |
| 297 | #endif |
| 298 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 299 | // Various things break when you're using a locale where the decimal |
| 300 | // separator isn't a period. See e.g. bugs 22782 and 39964. For |
| 301 | // all processes except the browser process (where we call system |
| 302 | // APIs that may rely on the correct locale for formatting numbers |
| 303 | // when presenting them to the user), reset the locale for numeric |
| 304 | // formatting. |
| 305 | // Note that this is not correct for plugin processes -- they can |
| 306 | // surface UI -- but it's likely they get this wrong too so why not. |
| 307 | setlocale(LC_NUMERIC, "C"); |
| 308 | #endif |
jam | 79dc59a | 2015-08-17 03:38:16 | [diff] [blame] | 309 | |
sadrul | f6fb2a7 | 2017-02-08 17:59:28 | [diff] [blame] | 310 | #if !defined(OFFICIAL_BUILD) && defined(OS_WIN) |
jam | 79dc59a | 2015-08-17 03:38:16 | [diff] [blame] | 311 | base::RouteStdioToConsole(false); |
| 312 | LoadLibraryA("dbghelp.dll"); |
| 313 | #endif |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 314 | } |
| 315 | |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 316 | class ContentClientInitializer { |
| 317 | public: |
| 318 | static void Set(const std::string& process_type, |
| 319 | ContentMainDelegate* delegate) { |
| 320 | ContentClient* content_client = GetContentClient(); |
yoz | 774dcda2d | 2014-08-27 03:32:58 | [diff] [blame] | 321 | #if !defined(CHROME_MULTIPLE_DLL_CHILD) |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 322 | if (process_type.empty()) { |
| 323 | if (delegate) |
| 324 | content_client->browser_ = delegate->CreateContentBrowserClient(); |
| 325 | if (!content_client->browser_) |
| 326 | content_client->browser_ = &g_empty_content_browser_client.Get(); |
| 327 | } |
yoz | 774dcda2d | 2014-08-27 03:32:58 | [diff] [blame] | 328 | #endif // !CHROME_MULTIPLE_DLL_CHILD |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 329 | |
jam | e1f453c | 2016-03-21 15:51:34 | [diff] [blame] | 330 | #if !defined(CHROME_MULTIPLE_DLL_BROWSER) |
boliu | 919267f | 2016-05-17 17:12:14 | [diff] [blame] | 331 | base::CommandLine* cmd = base::CommandLine::ForCurrentProcess(); |
kcwu | 08377ad | 2016-02-18 19:12:42 | [diff] [blame] | 332 | if (process_type == switches::kGpuProcess || |
boliu | 919267f | 2016-05-17 17:12:14 | [diff] [blame] | 333 | cmd->HasSwitch(switches::kSingleProcess) || |
| 334 | (process_type.empty() && cmd->HasSwitch(switches::kInProcessGPU))) { |
kcwu | 08377ad | 2016-02-18 19:12:42 | [diff] [blame] | 335 | if (delegate) |
| 336 | content_client->gpu_ = delegate->CreateContentGpuClient(); |
| 337 | if (!content_client->gpu_) |
| 338 | content_client->gpu_ = &g_empty_content_gpu_client.Get(); |
| 339 | } |
| 340 | |
piman | 72dcbed9 | 2016-04-05 04:27:21 | [diff] [blame] | 341 | if (process_type == switches::kRendererProcess || |
boliu | 919267f | 2016-05-17 17:12:14 | [diff] [blame] | 342 | cmd->HasSwitch(switches::kSingleProcess)) { |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 343 | if (delegate) |
| 344 | content_client->renderer_ = delegate->CreateContentRendererClient(); |
| 345 | if (!content_client->renderer_) |
| 346 | content_client->renderer_ = &g_empty_content_renderer_client.Get(); |
[email protected] | 54a83d3 | 2013-07-11 17:53:09 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | if (process_type == switches::kUtilityProcess || |
boliu | 919267f | 2016-05-17 17:12:14 | [diff] [blame] | 350 | cmd->HasSwitch(switches::kSingleProcess)) { |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 351 | if (delegate) |
| 352 | content_client->utility_ = delegate->CreateContentUtilityClient(); |
[email protected] | 56c248b | 2013-05-08 17:51:02 | [diff] [blame] | 353 | // TODO(scottmg): http://crbug.com/237249 Should be in _child. |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 354 | if (!content_client->utility_) |
| 355 | content_client->utility_ = &g_empty_content_utility_client.Get(); |
| 356 | } |
jam | e1f453c | 2016-03-21 15:51:34 | [diff] [blame] | 357 | #endif // !CHROME_MULTIPLE_DLL_BROWSER |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 358 | } |
| 359 | }; |
| 360 | |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 361 | // We dispatch to a process-type-specific FooMain() based on a command-line |
| 362 | // flag. This struct is used to build a table of (flag, main function) pairs. |
| 363 | struct MainFunction { |
| 364 | const char* name; |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 365 | int (*function)(const MainFunctionParams&); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 366 | }; |
| 367 | |
[email protected] | e082556 | 2012-05-10 01:50:47 | [diff] [blame] | 368 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 369 | // On platforms that use the zygote, we have a special subset of |
| 370 | // subprocesses that are launched via the zygote. This function |
| 371 | // fills in some process-launching bits around ZygoteMain(). |
| 372 | // Returns the exit code of the subprocess. |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 373 | int RunZygote(const MainFunctionParams& main_function_params, |
| 374 | ContentMainDelegate* delegate) { |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 375 | static const MainFunction kMainFunctions[] = { |
| 376 | { switches::kRendererProcess, RendererMain }, |
brettw | 4b46108 | 2016-11-19 18:55:16 | [diff] [blame] | 377 | #if BUILDFLAG(ENABLE_PLUGINS) |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 378 | { switches::kPpapiPluginProcess, PpapiPluginMain }, |
[email protected] | 0106cde | 2013-02-27 02:16:38 | [diff] [blame] | 379 | #endif |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 380 | { switches::kUtilityProcess, UtilityMain }, |
| 381 | }; |
| 382 | |
leon.han | 79db18e | 2017-01-27 05:26:26 | [diff] [blame] | 383 | std::vector<std::unique_ptr<ZygoteForkDelegate>> zygote_fork_delegates; |
[email protected] | 433df47 | 2012-03-07 20:33:39 | [diff] [blame] | 384 | if (delegate) { |
[email protected] | 54ad01a | 2014-05-11 01:17:47 | [diff] [blame] | 385 | delegate->ZygoteStarting(&zygote_fork_delegates); |
chcunningham | fd11b3c | 2015-06-09 02:09:42 | [diff] [blame] | 386 | media::InitializeMediaLibrary(); |
[email protected] | 433df47 | 2012-03-07 20:33:39 | [diff] [blame] | 387 | } |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 388 | |
| 389 | // This function call can return multiple times, once per fork(). |
dcheng | f63a125 | 2015-12-26 20:43:13 | [diff] [blame] | 390 | if (!ZygoteMain(main_function_params, std::move(zygote_fork_delegates))) |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 391 | return 1; |
| 392 | |
| 393 | if (delegate) delegate->ZygoteForked(); |
| 394 | |
| 395 | // Zygote::HandleForkRequest may have reallocated the command |
| 396 | // line so update it here with the new version. |
[email protected] | 47927870 | 2014-08-11 20:32:09 | [diff] [blame] | 397 | const base::CommandLine& command_line = |
| 398 | *base::CommandLine::ForCurrentProcess(); |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 399 | std::string process_type = |
| 400 | command_line.GetSwitchValueASCII(switches::kProcessType); |
| 401 | ContentClientInitializer::Set(process_type, delegate); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 402 | |
xhwang | 785a834 | 2017-01-26 06:46:39 | [diff] [blame] | 403 | #if BUILDFLAG(ENABLE_PEPPER_CDMS) |
| 404 | if (process_type != switches::kPpapiPluginProcess) { |
| 405 | DVLOG(1) << "Closing CDM files for non-ppapi process."; |
| 406 | CdmHostFiles::TakeGlobalInstance().reset(); |
| 407 | } else { |
| 408 | DVLOG(1) << "Not closing CDM files for ppapi process."; |
| 409 | } |
| 410 | #endif |
| 411 | |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 412 | MainFunctionParams main_params(command_line); |
[email protected] | 692a954 | 2014-06-25 23:04:47 | [diff] [blame] | 413 | main_params.zygote_child = true; |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 414 | |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 415 | std::unique_ptr<base::FieldTrialList> field_trial_list; |
| 416 | InitializeFieldTrialAndFeatureList(&field_trial_list); |
| 417 | |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 418 | for (size_t i = 0; i < arraysize(kMainFunctions); ++i) { |
| 419 | if (process_type == kMainFunctions[i].name) |
| 420 | return kMainFunctions[i].function(main_params); |
| 421 | } |
| 422 | |
| 423 | if (delegate) |
| 424 | return delegate->RunProcess(process_type, main_params); |
| 425 | |
| 426 | NOTREACHED() << "Unknown zygote process type: " << process_type; |
| 427 | return 1; |
| 428 | } |
[email protected] | 956a692e | 2012-07-11 03:25:47 | [diff] [blame] | 429 | #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 430 | |
[email protected] | 5281947 | 2013-11-24 22:49:55 | [diff] [blame] | 431 | static void RegisterMainThreadFactories() { |
yoz | 774dcda2d | 2014-08-27 03:32:58 | [diff] [blame] | 432 | #if !defined(CHROME_MULTIPLE_DLL_BROWSER) && !defined(CHROME_MULTIPLE_DLL_CHILD) |
[email protected] | 683f427 | 2014-04-17 20:42:18 | [diff] [blame] | 433 | UtilityProcessHostImpl::RegisterUtilityMainThreadFactory( |
[email protected] | 5281947 | 2013-11-24 22:49:55 | [diff] [blame] | 434 | CreateInProcessUtilityThread); |
[email protected] | 683f427 | 2014-04-17 20:42:18 | [diff] [blame] | 435 | RenderProcessHostImpl::RegisterRendererMainThreadFactory( |
[email protected] | 5281947 | 2013-11-24 22:49:55 | [diff] [blame] | 436 | CreateInProcessRendererThread); |
tapted | 4091f2f | 2017-02-07 00:47:19 | [diff] [blame] | 437 | content::RegisterGpuMainThreadFactory(CreateInProcessGpuThread); |
[email protected] | 5281947 | 2013-11-24 22:49:55 | [diff] [blame] | 438 | #else |
[email protected] | 47927870 | 2014-08-11 20:32:09 | [diff] [blame] | 439 | base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); |
[email protected] | 5281947 | 2013-11-24 22:49:55 | [diff] [blame] | 440 | if (command_line.HasSwitch(switches::kSingleProcess)) { |
| 441 | LOG(FATAL) << |
| 442 | "--single-process is not supported in chrome multiple dll browser."; |
| 443 | } |
| 444 | if (command_line.HasSwitch(switches::kInProcessGPU)) { |
| 445 | LOG(FATAL) << |
| 446 | "--in-process-gpu is not supported in chrome multiple dll browser."; |
| 447 | } |
yoz | 774dcda2d | 2014-08-27 03:32:58 | [diff] [blame] | 448 | #endif // !CHROME_MULTIPLE_DLL_BROWSER && !CHROME_MULTIPLE_DLL_CHILD |
[email protected] | 5281947 | 2013-11-24 22:49:55 | [diff] [blame] | 449 | } |
| 450 | |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 451 | // Run the FooMain() for a given process type. |
| 452 | // If |process_type| is empty, runs BrowserMain(). |
| 453 | // Returns the exit code for this process. |
| 454 | int RunNamedProcessTypeMain( |
| 455 | const std::string& process_type, |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 456 | const MainFunctionParams& main_function_params, |
| 457 | ContentMainDelegate* delegate) { |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 458 | static const MainFunction kMainFunctions[] = { |
[email protected] | c955a8d | 2013-08-06 02:51:15 | [diff] [blame] | 459 | #if !defined(CHROME_MULTIPLE_DLL_CHILD) |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 460 | { "", BrowserMain }, |
[email protected] | 1ff6429 | 2013-07-18 20:37:15 | [diff] [blame] | 461 | #endif |
[email protected] | c955a8d | 2013-08-06 02:51:15 | [diff] [blame] | 462 | #if !defined(CHROME_MULTIPLE_DLL_BROWSER) |
brettw | 4b46108 | 2016-11-19 18:55:16 | [diff] [blame] | 463 | #if BUILDFLAG(ENABLE_PLUGINS) |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 464 | { switches::kPpapiPluginProcess, PpapiPluginMain }, |
| 465 | { switches::kPpapiBrokerProcess, PpapiBrokerMain }, |
[email protected] | 1ff6429 | 2013-07-18 20:37:15 | [diff] [blame] | 466 | #endif // ENABLE_PLUGINS |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 467 | { switches::kUtilityProcess, UtilityMain }, |
[email protected] | 56c248b | 2013-05-08 17:51:02 | [diff] [blame] | 468 | { switches::kRendererProcess, RendererMain }, |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 469 | { switches::kGpuProcess, GpuMain }, |
[email protected] | c955a8d | 2013-08-06 02:51:15 | [diff] [blame] | 470 | #endif // !CHROME_MULTIPLE_DLL_BROWSER |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 471 | }; |
| 472 | |
[email protected] | 5281947 | 2013-11-24 22:49:55 | [diff] [blame] | 473 | RegisterMainThreadFactories(); |
[email protected] | d7a2d89 | 2013-08-16 07:45:36 | [diff] [blame] | 474 | |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 475 | for (size_t i = 0; i < arraysize(kMainFunctions); ++i) { |
| 476 | if (process_type == kMainFunctions[i].name) { |
| 477 | if (delegate) { |
| 478 | int exit_code = delegate->RunProcess(process_type, |
| 479 | main_function_params); |
[email protected] | e5df69d4 | 2012-06-08 00:44:14 | [diff] [blame] | 480 | #if defined(OS_ANDROID) |
| 481 | // In Android's browser process, the negative exit code doesn't mean the |
| 482 | // default behavior should be used as the UI message loop is managed by |
| 483 | // the Java and the browser process's default behavior is always |
| 484 | // overridden. |
| 485 | if (process_type.empty()) |
| 486 | return exit_code; |
| 487 | #endif |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 488 | if (exit_code >= 0) |
| 489 | return exit_code; |
| 490 | } |
| 491 | return kMainFunctions[i].function(main_function_params); |
| 492 | } |
| 493 | } |
| 494 | |
[email protected] | e082556 | 2012-05-10 01:50:47 | [diff] [blame] | 495 | #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 496 | // Zygote startup is special -- see RunZygote comments above |
| 497 | // for why we don't use ZygoteMain directly. |
| 498 | if (process_type == switches::kZygoteProcess) |
| 499 | return RunZygote(main_function_params, delegate); |
| 500 | #endif |
| 501 | |
| 502 | // If it's a process we don't know about, the embedder should know. |
| 503 | if (delegate) |
| 504 | return delegate->RunProcess(process_type, main_function_params); |
| 505 | |
| 506 | NOTREACHED() << "Unknown process type: " << process_type; |
| 507 | return 1; |
| 508 | } |
| 509 | |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 510 | class ContentMainRunnerImpl : public ContentMainRunner { |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 511 | public: |
| 512 | ContentMainRunnerImpl() |
| 513 | : is_initialized_(false), |
[email protected] | dab25541 | 2012-02-14 18:38:09 | [diff] [blame] | 514 | is_shutdown_(false), |
[email protected] | 91550a6 | 2012-06-07 17:26:30 | [diff] [blame] | 515 | completed_basic_startup_(false), |
[email protected] | a88f636 | 2014-03-18 04:25:35 | [diff] [blame] | 516 | delegate_(NULL), |
| 517 | ui_task_(NULL) { |
[email protected] | f80131d | 2012-07-25 22:20:25 | [diff] [blame] | 518 | #if defined(OS_WIN) |
| 519 | memset(&sandbox_info_, 0, sizeof(sandbox_info_)); |
| 520 | #endif |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 521 | } |
| 522 | |
dcheng | e933b3e | 2014-10-21 11:44:09 | [diff] [blame] | 523 | ~ContentMainRunnerImpl() override { |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 524 | if (is_initialized_ && !is_shutdown_) |
| 525 | Shutdown(); |
| 526 | } |
| 527 | |
dcheng | e933b3e | 2014-10-21 11:44:09 | [diff] [blame] | 528 | int Initialize(const ContentMainParams& params) override { |
[email protected] | a88f636 | 2014-03-18 04:25:35 | [diff] [blame] | 529 | ui_task_ = params.ui_task; |
| 530 | |
sky | 89636c3 | 2016-12-13 03:24:33 | [diff] [blame] | 531 | #if defined(USE_AURA) |
| 532 | env_mode_ = params.env_mode; |
| 533 | #endif |
| 534 | |
erikchen | bf11b55a | 2017-02-10 01:34:48 | [diff] [blame] | 535 | #if defined(OS_MACOSX) && BUILDFLAG(USE_EXPERIMENTAL_ALLOCATOR_SHIM) |
| 536 | base::allocator::InitializeAllocatorShim(); |
| 537 | #endif |
cpu | 3e11a84 | 2015-01-27 18:13:45 | [diff] [blame] | 538 | base::EnableTerminationOnOutOfMemory(); |
[email protected] | 5d90df5 | 2014-03-27 04:16:00 | [diff] [blame] | 539 | #if defined(OS_WIN) |
pmonette | 18d3ed3 | 2015-10-16 21:06:06 | [diff] [blame] | 540 | base::win::RegisterInvalidParamHandler(); |
[email protected] | 75f6dea0 | 2014-06-11 12:13:23 | [diff] [blame] | 541 | ui::win::CreateATLModuleIfNeeded(); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 542 | |
[email protected] | 8a820c8 | 2014-03-09 18:06:58 | [diff] [blame] | 543 | sandbox_info_ = *params.sandbox_info; |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 544 | #else // !OS_WIN |
[email protected] | e082556 | 2012-05-10 01:50:47 | [diff] [blame] | 545 | |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 546 | #if defined(OS_ANDROID) |
| 547 | // See note at the initialization of ExitManager, below; basically, |
| 548 | // only Android builds have the ctor/dtor handlers set up to use |
| 549 | // TRACE_EVENT right away. |
alexandermont | cd6ea329 | 2016-08-12 17:24:07 | [diff] [blame] | 550 | TRACE_EVENT0("startup,benchmark,rail", "ContentMainRunnerImpl::Initialize"); |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 551 | #endif // OS_ANDROID |
| 552 | |
rmcilroy | 3fb0727 | 2015-02-24 13:33:04 | [diff] [blame] | 553 | base::GlobalDescriptors* g_fds = base::GlobalDescriptors::GetInstance(); |
nedenwang | 0ca26a7 | 2016-08-23 07:34:05 | [diff] [blame] | 554 | ALLOW_UNUSED_LOCAL(g_fds); |
rmcilroy | 3fb0727 | 2015-02-24 13:33:04 | [diff] [blame] | 555 | |
[email protected] | e082556 | 2012-05-10 01:50:47 | [diff] [blame] | 556 | // On Android, |
| 557 | // - setlocale() is not supported. |
| 558 | // - We do not override the signal handlers so that we can get |
| 559 | // stack trace when crashing. |
| 560 | // - The ipc_fd is passed through the Java service. |
| 561 | // Thus, these are all disabled. |
jam | e1f453c | 2016-03-21 15:51:34 | [diff] [blame] | 562 | #if !defined(OS_ANDROID) |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 563 | // Set C library locale to make sure CommandLine can parse argument values |
| 564 | // in correct encoding. |
| 565 | setlocale(LC_ALL, ""); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 566 | |
| 567 | SetupSignalHandlers(); |
rockot | e8b8da4 | 2016-03-02 06:20:23 | [diff] [blame] | 568 | g_fds->Set(kMojoIPCChannel, |
| 569 | kMojoIPCChannel + base::GlobalDescriptors::kBaseDescriptor); |
lawrencewu | c4fe880 | 2016-11-30 16:25:02 | [diff] [blame] | 570 | |
| 571 | g_fds->Set( |
| 572 | kFieldTrialDescriptor, |
| 573 | kFieldTrialDescriptor + base::GlobalDescriptors::kBaseDescriptor); |
jam | e1f453c | 2016-03-21 15:51:34 | [diff] [blame] | 574 | #endif // !OS_ANDROID |
[email protected] | e082556 | 2012-05-10 01:50:47 | [diff] [blame] | 575 | |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 576 | #if defined(OS_LINUX) || defined(OS_OPENBSD) |
| 577 | g_fds->Set(kCrashDumpSignal, |
| 578 | kCrashDumpSignal + base::GlobalDescriptors::kBaseDescriptor); |
rmcilroy | 3fb0727 | 2015-02-24 13:33:04 | [diff] [blame] | 579 | #endif // OS_LINUX || OS_OPENBSD |
| 580 | |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 581 | |
| 582 | #endif // !OS_WIN |
| 583 | |
| 584 | is_initialized_ = true; |
[email protected] | 8a820c8 | 2014-03-09 18:06:58 | [diff] [blame] | 585 | delegate_ = params.delegate; |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 586 | |
[email protected] | d582fbc2 | 2012-09-11 10:59:37 | [diff] [blame] | 587 | // The exit manager is in charge of calling the dtors of singleton objects. |
[email protected] | 122984a | 2012-09-13 09:57:11 | [diff] [blame] | 588 | // On Android, AtExitManager is set up when library is loaded. |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 589 | // A consequence of this is that you can't use the ctor/dtor-based |
| 590 | // TRACE_EVENT methods on Linux or iOS builds till after we set this up. |
jam | e1f453c | 2016-03-21 15:51:34 | [diff] [blame] | 591 | #if !defined(OS_ANDROID) |
[email protected] | a88f636 | 2014-03-18 04:25:35 | [diff] [blame] | 592 | if (!ui_task_) { |
| 593 | // When running browser tests, don't create a second AtExitManager as that |
| 594 | // interfers with shutdown when objects created before ContentMain is |
| 595 | // called are destructed when it returns. |
| 596 | exit_manager_.reset(new base::AtExitManager); |
| 597 | } |
jam | e1f453c | 2016-03-21 15:51:34 | [diff] [blame] | 598 | #endif // !OS_ANDROID |
[email protected] | d582fbc2 | 2012-09-11 10:59:37 | [diff] [blame] | 599 | |
jam | e1f453c | 2016-03-21 15:51:34 | [diff] [blame] | 600 | #if defined(OS_MACOSX) |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 601 | // We need this pool for all the objects created before we get to the |
| 602 | // event loop, but we don't want to leave them hanging around until the |
| 603 | // app quits. Each "main" needs to flush this pool right before it goes into |
| 604 | // its main event loop to get rid of the cruft. |
| 605 | autorelease_pool_.reset(new base::mac::ScopedNSAutoreleasePool()); |
ccameron | c685e64c | 2015-05-12 07:05:17 | [diff] [blame] | 606 | InitializeMac(); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 607 | #endif |
| 608 | |
jcivelli | dad0cef | 2017-02-16 18:38:59 | [diff] [blame] | 609 | // On Android, the command line is initialized and the FDs set when the |
| 610 | // library is loaded and we have already started our TRACE_EVENT0. |
[email protected] | 9fc1dd2 | 2012-06-13 23:54:43 | [diff] [blame] | 611 | #if !defined(OS_ANDROID) |
[email protected] | 8a820c8 | 2014-03-09 18:06:58 | [diff] [blame] | 612 | // argc/argv are ignored on Windows and Android; see command_line.h for |
| 613 | // details. |
| 614 | int argc = 0; |
| 615 | const char** argv = NULL; |
| 616 | |
| 617 | #if !defined(OS_WIN) |
| 618 | argc = params.argc; |
| 619 | argv = params.argv; |
| 620 | #endif |
| 621 | |
[email protected] | 47927870 | 2014-08-11 20:32:09 | [diff] [blame] | 622 | base::CommandLine::Init(argc, argv); |
[email protected] | 8a820c8 | 2014-03-09 18:06:58 | [diff] [blame] | 623 | |
jcivelli | dad0cef | 2017-02-16 18:38:59 | [diff] [blame] | 624 | #if defined(OS_POSIX) |
| 625 | PopulateFDsFromCommandLine(); |
| 626 | #endif |
| 627 | |
erikwright | 23af4fa | 2015-03-11 19:43:26 | [diff] [blame] | 628 | base::EnableTerminationOnHeapCorruption(); |
[email protected] | 83882d4 | 2014-08-01 19:23:09 | [diff] [blame] | 629 | |
yiyaoliu | 9e6a5ab3 | 2015-03-18 18:04:37 | [diff] [blame] | 630 | // TODO(yiyaoliu, vadimt): Remove this once crbug.com/453640 is fixed. |
| 631 | // Enable profiler recording right after command line is initialized so that |
| 632 | // browser startup can be instrumented. |
| 633 | if (delegate_ && delegate_->ShouldEnableProfilerRecording()) |
| 634 | tracked_objects::ScopedTracker::Enable(); |
| 635 | |
[email protected] | 8a820c8 | 2014-03-09 18:06:58 | [diff] [blame] | 636 | SetProcessTitleFromCommandLine(argv); |
pmonette | 18d3ed3 | 2015-10-16 21:06:06 | [diff] [blame] | 637 | #endif // !OS_ANDROID |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 638 | |
[email protected] | be1f5166 | 2014-07-29 10:43:08 | [diff] [blame] | 639 | int exit_code = 0; |
[email protected] | 8a820c8 | 2014-03-09 18:06:58 | [diff] [blame] | 640 | if (delegate_ && delegate_->BasicStartupComplete(&exit_code)) |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 641 | return exit_code; |
| 642 | |
[email protected] | dab25541 | 2012-02-14 18:38:09 | [diff] [blame] | 643 | completed_basic_startup_ = true; |
| 644 | |
[email protected] | 47927870 | 2014-08-11 20:32:09 | [diff] [blame] | 645 | const base::CommandLine& command_line = |
| 646 | *base::CommandLine::ForCurrentProcess(); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 647 | std::string process_type = |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 648 | command_line.GetSwitchValueASCII(switches::kProcessType); |
| 649 | |
[email protected] | 5d90df5 | 2014-03-27 04:16:00 | [diff] [blame] | 650 | // Initialize mojo here so that services can be registered. |
| 651 | InitializeMojo(); |
[email protected] | 5d90df5 | 2014-03-27 04:16:00 | [diff] [blame] | 652 | |
ananta | b1f9313 | 2014-09-26 02:20:35 | [diff] [blame] | 653 | #if defined(OS_WIN) |
ananta | b1f9313 | 2014-09-26 02:20:35 | [diff] [blame] | 654 | if (command_line.HasSwitch(switches::kDeviceScaleFactor)) { |
| 655 | std::string scale_factor_string = command_line.GetSwitchValueASCII( |
| 656 | switches::kDeviceScaleFactor); |
| 657 | double scale_factor = 0; |
pkasting | 91475b0 | 2015-12-17 08:35:24 | [diff] [blame] | 658 | if (base::StringToDouble(scale_factor_string, &scale_factor)) |
robliao | 18e220e8 | 2016-04-19 16:47:12 | [diff] [blame] | 659 | display::win::SetDefaultDeviceScaleFactor(scale_factor); |
ananta | b1f9313 | 2014-09-26 02:20:35 | [diff] [blame] | 660 | } |
ananta | b1f9313 | 2014-09-26 02:20:35 | [diff] [blame] | 661 | #endif |
| 662 | |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 663 | if (!GetContentClient()) |
| 664 | SetContentClient(&empty_content_client_); |
| 665 | ContentClientInitializer::Set(process_type, delegate_); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 666 | |
[email protected] | c7c1e4d | 2012-08-22 04:06:06 | [diff] [blame] | 667 | #if defined(OS_WIN) |
| 668 | // Route stdio to parent console (if any) or create one. |
| 669 | if (command_line.HasSwitch(switches::kEnableLogging)) |
jam | 79dc59a | 2015-08-17 03:38:16 | [diff] [blame] | 670 | base::RouteStdioToConsole(true); |
[email protected] | c7c1e4d | 2012-08-22 04:06:06 | [diff] [blame] | 671 | #endif |
| 672 | |
zhenw | 79cf6180 | 2015-11-20 19:12:48 | [diff] [blame] | 673 | #if !defined(OS_ANDROID) |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 674 | // Enable startup tracing asap to avoid early TRACE_EVENT calls being |
zhenw | 79cf6180 | 2015-11-20 19:12:48 | [diff] [blame] | 675 | // ignored. For Android, startup tracing is enabled in an even earlier place |
| 676 | // content/app/android/library_loader_hooks.cc. |
ssid | 69e8419 | 2017-02-16 14:32:47 | [diff] [blame] | 677 | // Zygote process does not have file thread and renderer process on Win10 |
| 678 | // cannot access the file system. |
| 679 | // TODO(ssid): Check if other processes can enable startup tracing here. |
| 680 | bool can_access_file_system = (process_type != switches::kZygoteProcess && |
| 681 | process_type != switches::kRendererProcess); |
| 682 | tracing::EnableStartupTracingIfNeeded(can_access_file_system); |
zhenw | 79cf6180 | 2015-11-20 19:12:48 | [diff] [blame] | 683 | #endif // !OS_ANDROID |
zhenw | 062a9452 | 2015-06-22 20:55:28 | [diff] [blame] | 684 | |
georgesak | 28ad5e6 | 2015-04-16 09:25:19 | [diff] [blame] | 685 | #if defined(OS_WIN) |
| 686 | // Enable exporting of events to ETW if requested on the command line. |
| 687 | if (command_line.HasSwitch(switches::kTraceExportEventsToETW)) |
| 688 | base::trace_event::TraceEventETWExport::EnableETWExport(); |
| 689 | #endif // OS_WIN |
| 690 | |
[email protected] | aa328c7 | 2013-05-03 10:55:43 | [diff] [blame] | 691 | #if !defined(OS_ANDROID) |
| 692 | // Android tracing started at the beginning of the method. |
| 693 | // Other OSes have to wait till we get here in order for all the memory |
| 694 | // management setup to be completed. |
alexandermont | cd6ea329 | 2016-08-12 17:24:07 | [diff] [blame] | 695 | TRACE_EVENT0("startup,benchmark,rail", "ContentMainRunnerImpl::Initialize"); |
pmonette | 18d3ed3 | 2015-10-16 21:06:06 | [diff] [blame] | 696 | #endif // !OS_ANDROID |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 697 | |
jam | e1f453c | 2016-03-21 15:51:34 | [diff] [blame] | 698 | #if defined(OS_MACOSX) |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 699 | // We need to allocate the IO Ports before the Sandbox is initialized or |
[email protected] | 6c5905b7 | 2013-04-03 19:06:51 | [diff] [blame] | 700 | // the first instance of PowerMonitor is created. |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 701 | // It's important not to allocate the ports for processes which don't |
[email protected] | 6c5905b7 | 2013-04-03 19:06:51 | [diff] [blame] | 702 | // register with the power monitor - see crbug.com/88867. |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 703 | if (process_type.empty() || |
[email protected] | 8a820c8 | 2014-03-09 18:06:58 | [diff] [blame] | 704 | (delegate_ && |
| 705 | delegate_->ProcessRegistersWithSystemProcess(process_type))) { |
[email protected] | 9dd9015 | 2013-08-02 22:09:13 | [diff] [blame] | 706 | base::PowerMonitorDeviceSource::AllocateSystemIOPorts(); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | if (!process_type.empty() && |
[email protected] | 8a820c8 | 2014-03-09 18:06:58 | [diff] [blame] | 710 | (!delegate_ || delegate_->ShouldSendMachPort(process_type))) { |
[email protected] | 3c2119d | 2013-04-11 14:27:28 | [diff] [blame] | 711 | MachBroker::ChildSendTaskPortToParent(); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 712 | } |
| 713 | #elif defined(OS_WIN) |
pmonette | 18d3ed3 | 2015-10-16 21:06:06 | [diff] [blame] | 714 | base::win::SetupCRT(command_line); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 715 | #endif |
| 716 | |
primiano | be9d6fc | 2016-01-12 22:16:11 | [diff] [blame] | 717 | // If we are on a platform where the default allocator is overridden (shim |
| 718 | // layer on windows, tcmalloc on Linux Desktop) smoke-tests that the |
| 719 | // overriding logic is working correctly. If not causes a hard crash, as its |
| 720 | // unexpected absence has security implications. |
| 721 | CHECK(base::allocator::IsAllocatorInitialized()); |
| 722 | |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 723 | #if defined(OS_POSIX) |
| 724 | if (!process_type.empty()) { |
| 725 | // When you hit Ctrl-C in a terminal running the browser |
| 726 | // process, a SIGINT is delivered to the entire process group. |
| 727 | // When debugging the browser process via gdb, gdb catches the |
| 728 | // SIGINT for the browser process (and dumps you back to the gdb |
| 729 | // console) but doesn't for the child processes, killing them. |
| 730 | // The fix is to have child processes ignore SIGINT; they'll die |
| 731 | // on their own when the browser process goes away. |
| 732 | // |
| 733 | // Note that we *can't* rely on BeingDebugged to catch this case because |
| 734 | // we are the child process, which is not being debugged. |
| 735 | // TODO(evanm): move this to some shared subprocess-init function. |
| 736 | if (!base::debug::BeingDebugged()) |
| 737 | signal(SIGINT, SIG_IGN); |
| 738 | } |
| 739 | #endif |
| 740 | |
davidben | 71f35ff | 2015-04-17 20:54:48 | [diff] [blame] | 741 | #if defined(USE_NSS_CERTS) |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 742 | crypto::EarlySetupForNSSInit(); |
| 743 | #endif |
| 744 | |
| 745 | ui::RegisterPathProvider(); |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 746 | RegisterPathProvider(); |
| 747 | RegisterContentSchemes(true); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 748 | |
nedenwang | 0ca26a7 | 2016-08-23 07:34:05 | [diff] [blame] | 749 | #if defined(OS_ANDROID) && (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) |
rmcilroy | 3fb0727 | 2015-02-24 13:33:04 | [diff] [blame] | 750 | int icudata_fd = g_fds->MaybeGet(kAndroidICUDataDescriptor); |
mkosiba | 3c766cc | 2015-01-09 13:10:22 | [diff] [blame] | 751 | if (icudata_fd != -1) { |
rmcilroy | 3fb0727 | 2015-02-24 13:33:04 | [diff] [blame] | 752 | auto icudata_region = g_fds->GetRegion(kAndroidICUDataDescriptor); |
mkosiba | 3c766cc | 2015-01-09 13:10:22 | [diff] [blame] | 753 | CHECK(base::i18n::InitializeICUWithFileDescriptor(icudata_fd, |
| 754 | icudata_region)); |
| 755 | } else { |
[email protected] | 09560f7f | 2014-06-10 18:40:28 | [diff] [blame] | 756 | CHECK(base::i18n::InitializeICU()); |
mkosiba | 3c766cc | 2015-01-09 13:10:22 | [diff] [blame] | 757 | } |
rmcilroy | 3fb0727 | 2015-02-24 13:33:04 | [diff] [blame] | 758 | #else |
| 759 | CHECK(base::i18n::InitializeICU()); |
nedenwang | 0ca26a7 | 2016-08-23 07:34:05 | [diff] [blame] | 760 | #endif // OS_ANDROID && (ICU_UTIL_DATA_IMPL == ICU_UTIL_DATA_FILE) |
baixo | 3a3c88a | 2014-10-28 11:52:21 | [diff] [blame] | 761 | |
oth | 575f7fb5 | 2015-05-08 17:35:00 | [diff] [blame] | 762 | base::StatisticsRecorder::Initialize(); |
| 763 | |
jcivelli | 15ffa46 | 2017-02-09 18:01:30 | [diff] [blame] | 764 | InitializeV8IfNeeded(command_line, process_type); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 765 | |
sadrul | f6fb2a7 | 2017-02-08 17:59:28 | [diff] [blame] | 766 | #if !defined(OFFICIAL_BUILD) |
| 767 | #if defined(OS_WIN) |
| 768 | bool should_enable_stack_dump = !process_type.empty(); |
| 769 | #else |
| 770 | bool should_enable_stack_dump = true; |
| 771 | #endif |
| 772 | // Print stack traces to stderr when crashes occur. This opens up security |
| 773 | // holes so it should never be enabled for official builds. This needs to |
| 774 | // happen before crash reporting is initialized (which for chrome happens in |
| 775 | // the call to PreSandboxStartup() on the delegate below), because otherwise |
| 776 | // this would interfere with signal handlers used by crash reporting. |
| 777 | if (should_enable_stack_dump && !command_line.HasSwitch( |
| 778 | switches::kDisableInProcessStackTraces)) { |
| 779 | base::debug::EnableInProcessStackDumping(); |
| 780 | } |
| 781 | #endif // !defined(OFFICIAL_BUILD) |
| 782 | |
[email protected] | 8a820c8 | 2014-03-09 18:06:58 | [diff] [blame] | 783 | if (delegate_) |
| 784 | delegate_->PreSandboxStartup(); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 785 | |
| 786 | if (!process_type.empty()) |
sadrul | f6fb2a7 | 2017-02-08 17:59:28 | [diff] [blame] | 787 | CommonSubprocessInit(); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 788 | |
| 789 | #if defined(OS_WIN) |
[email protected] | 8a820c8 | 2014-03-09 18:06:58 | [diff] [blame] | 790 | CHECK(InitializeSandbox(params.sandbox_info)); |
jam | e1f453c | 2016-03-21 15:51:34 | [diff] [blame] | 791 | #elif defined(OS_MACOSX) |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 792 | if (process_type == switches::kRendererProcess || |
| 793 | process_type == switches::kPpapiPluginProcess || |
[email protected] | 8a820c8 | 2014-03-09 18:06:58 | [diff] [blame] | 794 | (delegate_ && delegate_->DelaySandboxInitialization(process_type))) { |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 795 | // On OS X the renderer sandbox needs to be initialized later in the |
| 796 | // startup sequence in RendererMainPlatformDelegate::EnableSandbox(). |
| 797 | } else { |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 798 | CHECK(InitializeSandbox()); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 799 | } |
| 800 | #endif |
| 801 | |
[email protected] | 8a820c8 | 2014-03-09 18:06:58 | [diff] [blame] | 802 | if (delegate_) |
| 803 | delegate_->SandboxInitialized(process_type); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 804 | |
| 805 | // Return -1 to indicate no early termination. |
| 806 | return -1; |
| 807 | } |
| 808 | |
dcheng | e933b3e | 2014-10-21 11:44:09 | [diff] [blame] | 809 | int Run() override { |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 810 | DCHECK(is_initialized_); |
| 811 | DCHECK(!is_shutdown_); |
[email protected] | 47927870 | 2014-08-11 20:32:09 | [diff] [blame] | 812 | const base::CommandLine& command_line = |
| 813 | *base::CommandLine::ForCurrentProcess(); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 814 | std::string process_type = |
[email protected] | 47927870 | 2014-08-11 20:32:09 | [diff] [blame] | 815 | command_line.GetSwitchValueASCII(switches::kProcessType); |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 816 | |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 817 | // Run this logic on all child processes. Zygotes will run this at a later |
| 818 | // point in time when the command line has been updated. |
| 819 | std::unique_ptr<base::FieldTrialList> field_trial_list; |
| 820 | if (!process_type.empty() && process_type != switches::kZygoteProcess) |
| 821 | InitializeFieldTrialAndFeatureList(&field_trial_list); |
| 822 | |
bcwhite | d723c25 | 2016-03-16 17:25:41 | [diff] [blame] | 823 | base::HistogramBase::EnableActivityReportHistogram(process_type); |
| 824 | |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 825 | MainFunctionParams main_params(command_line); |
[email protected] | a88f636 | 2014-03-18 04:25:35 | [diff] [blame] | 826 | main_params.ui_task = ui_task_; |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 827 | #if defined(OS_WIN) |
| 828 | main_params.sandbox_info = &sandbox_info_; |
| 829 | #elif defined(OS_MACOSX) |
| 830 | main_params.autorelease_pool = autorelease_pool_.get(); |
| 831 | #endif |
sky | 89636c3 | 2016-12-13 03:24:33 | [diff] [blame] | 832 | #if defined(USE_AURA) |
| 833 | main_params.env_mode = env_mode_; |
| 834 | #endif |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 835 | |
| 836 | return RunNamedProcessTypeMain(process_type, main_params, delegate_); |
| 837 | } |
| 838 | |
dcheng | e933b3e | 2014-10-21 11:44:09 | [diff] [blame] | 839 | void Shutdown() override { |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 840 | DCHECK(is_initialized_); |
| 841 | DCHECK(!is_shutdown_); |
[email protected] | dab25541 | 2012-02-14 18:38:09 | [diff] [blame] | 842 | |
| 843 | if (completed_basic_startup_ && delegate_) { |
[email protected] | 47927870 | 2014-08-11 20:32:09 | [diff] [blame] | 844 | const base::CommandLine& command_line = |
| 845 | *base::CommandLine::ForCurrentProcess(); |
[email protected] | dab25541 | 2012-02-14 18:38:09 | [diff] [blame] | 846 | std::string process_type = |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 847 | command_line.GetSwitchValueASCII(switches::kProcessType); |
| 848 | |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 849 | delegate_->ProcessExiting(process_type); |
[email protected] | dab25541 | 2012-02-14 18:38:09 | [diff] [blame] | 850 | } |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 851 | |
| 852 | #if defined(OS_WIN) |
| 853 | #ifdef _CRTDBG_MAP_ALLOC |
| 854 | _CrtDumpMemoryLeaks(); |
| 855 | #endif // _CRTDBG_MAP_ALLOC |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 856 | #endif // OS_WIN |
| 857 | |
jam | e1f453c | 2016-03-21 15:51:34 | [diff] [blame] | 858 | #if defined(OS_MACOSX) |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 859 | autorelease_pool_.reset(NULL); |
| 860 | #endif |
| 861 | |
| 862 | exit_manager_.reset(NULL); |
| 863 | |
| 864 | delegate_ = NULL; |
| 865 | is_shutdown_ = true; |
| 866 | } |
| 867 | |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 868 | private: |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 869 | // True if the runner has been initialized. |
| 870 | bool is_initialized_; |
| 871 | |
| 872 | // True if the runner has been shut down. |
| 873 | bool is_shutdown_; |
| 874 | |
[email protected] | dab25541 | 2012-02-14 18:38:09 | [diff] [blame] | 875 | // True if basic startup was completed. |
| 876 | bool completed_basic_startup_; |
| 877 | |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 878 | // Used if the embedder doesn't set one. |
| 879 | ContentClient empty_content_client_; |
| 880 | |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 881 | // The delegate will outlive this object. |
[email protected] | c6681f3 | 2012-06-05 14:43:01 | [diff] [blame] | 882 | ContentMainDelegate* delegate_; |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 883 | |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 884 | std::unique_ptr<base::AtExitManager> exit_manager_; |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 885 | #if defined(OS_WIN) |
| 886 | sandbox::SandboxInterfaceInfo sandbox_info_; |
| 887 | #elif defined(OS_MACOSX) |
dcheng | 6003e0b | 2016-04-09 18:42:34 | [diff] [blame] | 888 | std::unique_ptr<base::mac::ScopedNSAutoreleasePool> autorelease_pool_; |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 889 | #endif |
| 890 | |
[email protected] | a88f636 | 2014-03-18 04:25:35 | [diff] [blame] | 891 | base::Closure* ui_task_; |
| 892 | |
sky | 89636c3 | 2016-12-13 03:24:33 | [diff] [blame] | 893 | #if defined(USE_AURA) |
| 894 | aura::Env::Mode env_mode_ = aura::Env::Mode::LOCAL; |
| 895 | #endif |
| 896 | |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 897 | DISALLOW_COPY_AND_ASSIGN(ContentMainRunnerImpl); |
| 898 | }; |
| 899 | |
[email protected] | f573ed6b | 2012-02-10 15:58:52 | [diff] [blame] | 900 | // static |
| 901 | ContentMainRunner* ContentMainRunner::Create() { |
| 902 | return new ContentMainRunnerImpl(); |
| 903 | } |
| 904 | |
| 905 | } // namespace content |