OLD | NEW |
1 // Copyright (c) 2011 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.h" | 5 #include "content/browser/browser_main.h" |
6 | 6 |
7 #include "base/allocator/allocator_shim.h" | |
8 #include "base/base_switches.h" | |
9 #include "base/command_line.h" | |
10 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
11 #include "base/logging.h" | 8 #include "content/public/browser/browser_main_runner.h" |
12 #include "base/metrics/histogram.h" | |
13 #include "content/browser/browser_main_loop.h" | |
14 #include "content/browser/notification_service_impl.h" | |
15 #include "content/common/child_process.h" | |
16 #include "content/public/common/content_switches.h" | |
17 #include "content/public/common/main_function_params.h" | |
18 | |
19 #if defined(OS_WIN) | |
20 #include "base/win/scoped_com_initializer.h" | |
21 #endif | |
22 | |
23 namespace { | |
24 | |
25 bool g_exited_main_message_loop = false; | |
26 | |
27 } // namespace | |
28 | |
29 namespace content { | |
30 | |
31 bool ExitedMainMessageLoop() { | |
32 return g_exited_main_message_loop; | |
33 } | |
34 | |
35 } // namespace content | |
36 | 9 |
37 // Main routine for running as the Browser process. | 10 // Main routine for running as the Browser process. |
38 int BrowserMain(const content::MainFunctionParams& parameters) { | 11 int BrowserMain(const content::MainFunctionParams& parameters) { |
39 TRACE_EVENT_BEGIN_ETW("BrowserMain", 0, ""); | 12 TRACE_EVENT_BEGIN_ETW("BrowserMain", 0, ""); |
40 | 13 |
41 // ChildProcess:: is a misnomer unless you consider context. Use | 14 scoped_ptr<content::BrowserMainRunner> main_runner_( |
42 // of --wait-for-debugger only makes sense when Chrome itself is a | 15 content::BrowserMainRunner::Create()); |
43 // child process (e.g. when launched by PyAuto). | |
44 if (parameters.command_line.HasSwitch(switches::kWaitForDebugger)) | |
45 ChildProcess::WaitForDebugger("Browser"); | |
46 | 16 |
47 NotificationServiceImpl main_notification_service; | 17 int exit_code = main_runner_->Initialize(parameters); |
| 18 if (exit_code >= 0) |
| 19 return exit_code; |
48 | 20 |
49 scoped_ptr<content::BrowserMainLoop> main_loop( | 21 exit_code = main_runner_->Run(); |
50 new content::BrowserMainLoop(parameters)); | |
51 | 22 |
52 main_loop->Init(); | 23 main_runner_->Shutdown(); |
53 | |
54 main_loop->EarlyInitialization(); | |
55 | |
56 // Must happen before we try to use a message loop or display any UI. | |
57 main_loop->InitializeToolkit(); | |
58 | |
59 main_loop->MainMessageLoopStart(); | |
60 | |
61 // WARNING: If we get a WM_ENDSESSION, objects created on the stack here | |
62 // are NOT deleted. If you need something to run during WM_ENDSESSION add it | |
63 // to browser_shutdown::Shutdown or BrowserProcess::EndSession. | |
64 | |
65 // !!!!!!!!!! READ ME !!!!!!!!!! | |
66 // I (viettrungluu) am in the process of refactoring |BrowserMain()|. If you | |
67 // need to add something above this comment, read the documentation in | |
68 // browser_main.h. If you need to add something below, please do the | |
69 // following: | |
70 // - Figure out where you should add your code. Do NOT just pick a random | |
71 // location "which works". | |
72 // - Document the dependencies apart from compile-time-checkable ones. What | |
73 // must happen before your new code is executed? Does your new code need to | |
74 // run before something else? Are there performance reasons for executing | |
75 // your code at that point? | |
76 // - If you need to create a (persistent) object, heap allocate it and keep a | |
77 // |scoped_ptr| to it rather than allocating it on the stack. Otherwise | |
78 // I'll have to convert your code when I refactor. | |
79 // - Unless your new code is just a couple of lines, factor it out into a | |
80 // function with a well-defined purpose. Do NOT just add it inline in | |
81 // |BrowserMain()|. | |
82 // Thanks! | |
83 | |
84 // TODO(viettrungluu): put the remainder into BrowserMainParts | |
85 | |
86 #if defined(OS_WIN) | |
87 #if !defined(NO_TCMALLOC) | |
88 // When linking shared libraries, NO_TCMALLOC is defined, and dynamic | |
89 // allocator selection is not supported. | |
90 | |
91 // Make this call before going multithreaded, or spawning any subprocesses. | |
92 base::allocator::SetupSubprocessAllocator(); | |
93 #endif | |
94 | |
95 base::win::ScopedCOMInitializer com_initializer; | |
96 #endif // OS_WIN | |
97 | |
98 base::StatisticsRecorder statistics; | |
99 | |
100 main_loop->RunMainMessageLoopParts(&g_exited_main_message_loop); | |
101 | 24 |
102 TRACE_EVENT_END_ETW("BrowserMain", 0, 0); | 25 TRACE_EVENT_END_ETW("BrowserMain", 0, 0); |
103 | 26 |
104 return main_loop->GetResultCode(); | 27 return exit_code; |
105 } | 28 } |
OLD | NEW |