OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/app/content_main.h" | 5 #include "content/app/content_main.h" |
6 | 6 |
7 #include "base/at_exit.h" | 7 #include "base/at_exit.h" |
8 #include "base/base_switches.h" | 8 #include "base/base_switches.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/debug/debugger.h" | 10 #include "base/debug/debugger.h" |
11 #include "base/i18n/icu_util.h" | 11 #include "base/i18n/icu_util.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/mac/scoped_nsautorelease_pool.h" | 13 #include "base/mac/scoped_nsautorelease_pool.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/metrics/stats_table.h" |
15 #include "base/process_util.h" | 16 #include "base/process_util.h" |
16 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
| 18 #include "base/string_number_conversions.h" |
17 #include "content/app/content_main_delegate.h" | 19 #include "content/app/content_main_delegate.h" |
| 20 #include "content/common/content_constants.h" |
18 #include "content/common/content_paths.h" | 21 #include "content/common/content_paths.h" |
19 #include "content/common/content_switches.h" | 22 #include "content/common/content_switches.h" |
20 #include "content/common/main_function_params.h" | 23 #include "content/common/main_function_params.h" |
21 #include "content/common/sandbox_init_wrapper.h" | 24 #include "content/common/sandbox_init_wrapper.h" |
22 #include "content/common/set_process_title.h" | 25 #include "content/common/set_process_title.h" |
23 #include "crypto/nss_util.h" | 26 #include "crypto/nss_util.h" |
| 27 #include "ipc/ipc_switches.h" |
24 #include "ui/base/ui_base_switches.h" | 28 #include "ui/base/ui_base_switches.h" |
25 #include "ui/base/ui_base_paths.h" | 29 #include "ui/base/ui_base_paths.h" |
26 | 30 |
27 #if defined(OS_WIN) | 31 #if defined(OS_WIN) |
28 #include <atlbase.h> | 32 #include <atlbase.h> |
29 #include <atlapp.h> | 33 #include <atlapp.h> |
30 #include <new.h> | 34 #include <new.h> |
31 #include <malloc.h> | 35 #include <malloc.h> |
32 #elif defined(OS_MACOSX) | 36 #elif defined(OS_MACOSX) |
33 #include "base/mach_ipc_mac.h" | 37 #include "base/mach_ipc_mac.h" |
(...skipping 12 matching lines...) Expand all Loading... |
46 #endif | 50 #endif |
47 | 51 |
48 #endif // OS_POSIX | 52 #endif // OS_POSIX |
49 | 53 |
50 #if !defined(OS_MACOSX) && defined(USE_TCMALLOC) | 54 #if !defined(OS_MACOSX) && defined(USE_TCMALLOC) |
51 extern "C" { | 55 extern "C" { |
52 int tc_set_new_mode(int mode); | 56 int tc_set_new_mode(int mode); |
53 } | 57 } |
54 #endif | 58 #endif |
55 | 59 |
| 60 extern int BrowserMain(const MainFunctionParams&); |
56 extern int RendererMain(const MainFunctionParams&); | 61 extern int RendererMain(const MainFunctionParams&); |
57 extern int GpuMain(const MainFunctionParams&); | 62 extern int GpuMain(const MainFunctionParams&); |
58 extern int PluginMain(const MainFunctionParams&); | 63 extern int PluginMain(const MainFunctionParams&); |
59 extern int PpapiPluginMain(const MainFunctionParams&); | 64 extern int PpapiPluginMain(const MainFunctionParams&); |
60 extern int PpapiBrokerMain(const MainFunctionParams&); | 65 extern int PpapiBrokerMain(const MainFunctionParams&); |
61 extern int WorkerMain(const MainFunctionParams&); | 66 extern int WorkerMain(const MainFunctionParams&); |
62 extern int UtilityMain(const MainFunctionParams&); | 67 extern int UtilityMain(const MainFunctionParams&); |
63 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 68 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
64 extern int ZygoteMain(const MainFunctionParams&, | 69 extern int ZygoteMain(const MainFunctionParams&, |
65 ZygoteForkDelegate* forkdelegate); | 70 ZygoteForkDelegate* forkdelegate); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 // all processes except the browser process (where we call system | 182 // all processes except the browser process (where we call system |
178 // APIs that may rely on the correct locale for formatting numbers | 183 // APIs that may rely on the correct locale for formatting numbers |
179 // when presenting them to the user), reset the locale for numeric | 184 // when presenting them to the user), reset the locale for numeric |
180 // formatting. | 185 // formatting. |
181 // Note that this is not correct for plugin processes -- they can | 186 // Note that this is not correct for plugin processes -- they can |
182 // surface UI -- but it's likely they get this wrong too so why not. | 187 // surface UI -- but it's likely they get this wrong too so why not. |
183 setlocale(LC_NUMERIC, "C"); | 188 setlocale(LC_NUMERIC, "C"); |
184 #endif | 189 #endif |
185 } | 190 } |
186 | 191 |
| 192 void InitializeStatsTable(base::ProcessId browser_pid, |
| 193 const CommandLine& command_line) { |
| 194 // Initialize the Stats Counters table. With this initialized, |
| 195 // the StatsViewer can be utilized to read counters outside of |
| 196 // Chrome. These lines can be commented out to effectively turn |
| 197 // counters 'off'. The table is created and exists for the life |
| 198 // of the process. It is not cleaned up. |
| 199 if (command_line.HasSwitch(switches::kEnableStatsTable) || |
| 200 command_line.HasSwitch(switches::kEnableBenchmarking)) { |
| 201 // NOTIMPLEMENTED: we probably need to shut this down correctly to avoid |
| 202 // leaking shared memory regions on posix platforms. |
| 203 std::string statsfile = |
| 204 base::StringPrintf("%s-%u", |
| 205 content::kStatsFilename, |
| 206 static_cast<unsigned int>(browser_pid)); |
| 207 base::StatsTable* stats_table = new base::StatsTable(statsfile, |
| 208 content::kStatsMaxThreads, content::kStatsMaxCounters); |
| 209 base::StatsTable::set_current(stats_table); |
| 210 } |
| 211 } |
| 212 |
187 // We dispatch to a process-type-specific FooMain() based on a command-line | 213 // We dispatch to a process-type-specific FooMain() based on a command-line |
188 // flag. This struct is used to build a table of (flag, main function) pairs. | 214 // flag. This struct is used to build a table of (flag, main function) pairs. |
189 struct MainFunction { | 215 struct MainFunction { |
190 const char* name; | 216 const char* name; |
191 int (*function)(const MainFunctionParams&); | 217 int (*function)(const MainFunctionParams&); |
192 }; | 218 }; |
193 | 219 |
194 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 220 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
195 // On platforms that use the zygote, we have a special subset of | 221 // On platforms that use the zygote, we have a special subset of |
196 // subprocesses that are launched via the zygote. This function | 222 // subprocesses that are launched via the zygote. This function |
(...skipping 13 matching lines...) Expand all Loading... |
210 // This function call can return multiple times, once per fork(). | 236 // This function call can return multiple times, once per fork(). |
211 if (!ZygoteMain(main_function_params, zygote_fork_delegate.get())) | 237 if (!ZygoteMain(main_function_params, zygote_fork_delegate.get())) |
212 return 1; | 238 return 1; |
213 | 239 |
214 if (delegate) delegate->ZygoteForked(); | 240 if (delegate) delegate->ZygoteForked(); |
215 | 241 |
216 // Zygote::HandleForkRequest may have reallocated the command | 242 // Zygote::HandleForkRequest may have reallocated the command |
217 // line so update it here with the new version. | 243 // line so update it here with the new version. |
218 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 244 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
219 | 245 |
| 246 // The StatsTable must be initialized in each process; we already |
| 247 // initialized for the browser process, now we need to initialize |
| 248 // within the new processes as well. |
| 249 pid_t browser_pid = base::GetParentProcessId( |
| 250 base::GetParentProcessId(base::GetCurrentProcId())); |
| 251 InitializeStatsTable(browser_pid, command_line); |
| 252 |
220 MainFunctionParams main_params(command_line, | 253 MainFunctionParams main_params(command_line, |
221 main_function_params.sandbox_info_, | 254 main_function_params.sandbox_info_, |
222 main_function_params.autorelease_pool_); | 255 main_function_params.autorelease_pool_); |
223 // Get the new process type from the new command line. | 256 // Get the new process type from the new command line. |
224 std::string process_type = | 257 std::string process_type = |
225 command_line.GetSwitchValueASCII(switches::kProcessType); | 258 command_line.GetSwitchValueASCII(switches::kProcessType); |
226 | 259 |
227 for (size_t i = 0; i < arraysize(kMainFunctions); ++i) { | 260 for (size_t i = 0; i < arraysize(kMainFunctions); ++i) { |
228 if (process_type == kMainFunctions[i].name) | 261 if (process_type == kMainFunctions[i].name) |
229 return kMainFunctions[i].function(main_params); | 262 return kMainFunctions[i].function(main_params); |
230 } | 263 } |
231 | 264 |
232 if (delegate) | 265 if (delegate) |
233 return delegate->RunProcess(process_type, main_function_params); | 266 return delegate->RunProcess(process_type, main_function_params); |
234 | 267 |
235 NOTREACHED() << "Unknown zygote process type: " << process_type; | 268 NOTREACHED() << "Unknown zygote process type: " << process_type; |
236 return 1; | 269 return 1; |
237 } | 270 } |
238 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) | 271 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) |
239 | 272 |
240 // Run the FooMain() for a given process type. | 273 // Run the FooMain() for a given process type. |
241 // If |process_type| is empty, runs BrowserMain(). | 274 // If |process_type| is empty, runs BrowserMain(). |
242 // Returns the exit code for this process. | 275 // Returns the exit code for this process. |
243 int RunNamedProcessTypeMain(const std::string& process_type, | 276 int RunNamedProcessTypeMain(const std::string& process_type, |
244 const MainFunctionParams& main_function_params, | 277 const MainFunctionParams& main_function_params, |
245 content::ContentMainDelegate* delegate) { | 278 content::ContentMainDelegate* delegate) { |
246 static const MainFunction kMainFunctions[] = { | 279 static const MainFunction kMainFunctions[] = { |
| 280 { "", BrowserMain }, |
247 { switches::kRendererProcess, RendererMain }, | 281 { switches::kRendererProcess, RendererMain }, |
248 { switches::kPluginProcess, PluginMain }, | 282 { switches::kPluginProcess, PluginMain }, |
249 { switches::kWorkerProcess, WorkerMain }, | 283 { switches::kWorkerProcess, WorkerMain }, |
250 { switches::kPpapiPluginProcess, PpapiPluginMain }, | 284 { switches::kPpapiPluginProcess, PpapiPluginMain }, |
251 { switches::kPpapiBrokerProcess, PpapiBrokerMain }, | 285 { switches::kPpapiBrokerProcess, PpapiBrokerMain }, |
252 { switches::kUtilityProcess, UtilityMain }, | 286 { switches::kUtilityProcess, UtilityMain }, |
253 { switches::kGpuProcess, GpuMain }, | 287 { switches::kGpuProcess, GpuMain }, |
254 }; | 288 }; |
255 | 289 |
256 for (size_t i = 0; i < arraysize(kMainFunctions); ++i) { | 290 for (size_t i = 0; i < arraysize(kMainFunctions); ++i) { |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 | 416 |
383 #if defined(USE_NSS) | 417 #if defined(USE_NSS) |
384 crypto::EarlySetupForNSSInit(); | 418 crypto::EarlySetupForNSSInit(); |
385 #endif | 419 #endif |
386 | 420 |
387 ui::RegisterPathProvider(); | 421 ui::RegisterPathProvider(); |
388 content::RegisterPathProvider(); | 422 content::RegisterPathProvider(); |
389 | 423 |
390 CHECK(icu_util::Initialize()); | 424 CHECK(icu_util::Initialize()); |
391 | 425 |
| 426 base::ProcessId browser_pid = base::GetCurrentProcId(); |
| 427 if (command_line.HasSwitch(switches::kProcessChannelID)) { |
| 428 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 429 std::string channel_name = |
| 430 command_line.GetSwitchValueASCII(switches::kProcessChannelID); |
| 431 |
| 432 int browser_pid_int; |
| 433 base::StringToInt(channel_name, &browser_pid_int); |
| 434 browser_pid = static_cast<base::ProcessId>(browser_pid_int); |
| 435 DCHECK_NE(browser_pid_int, 0); |
| 436 #elif defined(OS_POSIX) |
| 437 // On linux, we're in the zygote here; so we need the parent process' id. |
| 438 browser_pid = base::GetParentProcessId(base::GetCurrentProcId()); |
| 439 #endif |
| 440 } |
| 441 |
| 442 InitializeStatsTable(browser_pid, command_line); |
| 443 |
392 if (delegate) delegate->PreSandboxStartup(); | 444 if (delegate) delegate->PreSandboxStartup(); |
393 | 445 |
394 if (!process_type.empty()) | 446 if (!process_type.empty()) |
395 CommonSubprocessInit(process_type); | 447 CommonSubprocessInit(process_type); |
396 | 448 |
397 // Initialize the sandbox for this process. | 449 // Initialize the sandbox for this process. |
398 SandboxInitWrapper sandbox_wrapper; | 450 SandboxInitWrapper sandbox_wrapper; |
399 bool initialize_sandbox = true; | 451 bool initialize_sandbox = true; |
400 | 452 |
401 #if defined(OS_WIN) | 453 #if defined(OS_WIN) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 _CrtDumpMemoryLeaks(); | 488 _CrtDumpMemoryLeaks(); |
437 #endif // _CRTDBG_MAP_ALLOC | 489 #endif // _CRTDBG_MAP_ALLOC |
438 | 490 |
439 _Module.Term(); | 491 _Module.Term(); |
440 #endif // OS_WIN | 492 #endif // OS_WIN |
441 | 493 |
442 return exit_code; | 494 return exit_code; |
443 } | 495 } |
444 | 496 |
445 } // namespace content | 497 } // namespace content |
OLD | NEW |