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/browser/browser_main.h" | 5 #include "content/public/browser/browser_main_runner.h" |
6 | 6 |
7 #include "base/allocator/allocator_shim.h" | 7 #include "base/allocator/allocator_shim.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/trace_event.h" | 10 #include "base/debug/trace_event.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
13 #include "content/browser/browser_main_loop.h" | 13 #include "content/browser/browser_main_loop.h" |
14 #include "content/browser/notification_service_impl.h" | 14 #include "content/browser/notification_service_impl.h" |
15 #include "content/common/child_process.h" | 15 #include "content/common/child_process.h" |
16 #include "content/public/common/content_switches.h" | 16 #include "content/public/common/content_switches.h" |
17 #include "content/public/common/main_function_params.h" | 17 #include "content/public/common/main_function_params.h" |
18 | 18 |
19 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
20 #include "base/win/scoped_com_initializer.h" | 20 #include "base/win/scoped_com_initializer.h" |
21 #endif | 21 #endif |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 bool g_exited_main_message_loop = false; | 25 bool g_exited_main_message_loop = false; |
26 | 26 |
27 } // namespace | 27 class BrowserMainRunnerImpl : public content::BrowserMainRunner { |
jam
2012/01/31 21:11:29
ditto for inliing these methods
| |
28 public: | |
29 BrowserMainRunnerImpl(); | |
30 ~BrowserMainRunnerImpl(); | |
28 | 31 |
29 namespace content { | 32 virtual int Initialize(const content::MainFunctionParams& parameters) |
33 OVERRIDE; | |
34 virtual int Run() OVERRIDE; | |
35 virtual void Shutdown() OVERRIDE; | |
30 | 36 |
31 bool ExitedMainMessageLoop() { | 37 protected: |
32 return g_exited_main_message_loop; | 38 // True if the runner has been initialized. |
39 bool is_initialized_; | |
40 | |
41 // True if the runner has been shut down. | |
42 bool is_shutdown_; | |
43 | |
44 scoped_ptr<NotificationServiceImpl> notification_service_; | |
45 scoped_ptr<content::BrowserMainLoop> main_loop_; | |
46 #if defined(OS_WIN) | |
47 scoped_ptr<base::win::ScopedCOMInitializer> com_initializer_; | |
48 #endif | |
49 scoped_ptr<base::StatisticsRecorder> statistics_; | |
50 | |
51 DISALLOW_COPY_AND_ASSIGN(BrowserMainRunnerImpl); | |
52 }; | |
53 | |
54 BrowserMainRunnerImpl::BrowserMainRunnerImpl() | |
55 : is_initialized_(false), | |
56 is_shutdown_(false) { | |
33 } | 57 } |
34 | 58 |
35 } // namespace content | 59 BrowserMainRunnerImpl::~BrowserMainRunnerImpl() { |
60 if (is_initialized_ && !is_shutdown_) | |
61 Shutdown(); | |
62 } | |
36 | 63 |
37 // Main routine for running as the Browser process. | 64 int BrowserMainRunnerImpl::Initialize( |
38 int BrowserMain(const content::MainFunctionParams& parameters) { | 65 const content::MainFunctionParams& parameters) { |
39 TRACE_EVENT_BEGIN_ETW("BrowserMain", 0, ""); | 66 is_initialized_ = true; |
40 | 67 |
41 // ChildProcess:: is a misnomer unless you consider context. Use | 68 // ChildProcess:: is a misnomer unless you consider context. Use |
42 // of --wait-for-debugger only makes sense when Chrome itself is a | 69 // of --wait-for-debugger only makes sense when Chrome itself is a |
43 // child process (e.g. when launched by PyAuto). | 70 // child process (e.g. when launched by PyAuto). |
44 if (parameters.command_line.HasSwitch(switches::kWaitForDebugger)) | 71 if (parameters.command_line.HasSwitch(switches::kWaitForDebugger)) |
45 ChildProcess::WaitForDebugger("Browser"); | 72 ChildProcess::WaitForDebugger("Browser"); |
46 | 73 |
47 NotificationServiceImpl main_notification_service; | 74 notification_service_.reset(new NotificationServiceImpl); |
48 | 75 |
49 scoped_ptr<content::BrowserMainLoop> main_loop( | 76 main_loop_.reset(new content::BrowserMainLoop(parameters)); |
50 new content::BrowserMainLoop(parameters)); | |
51 | 77 |
52 main_loop->Init(); | 78 main_loop_->Init(); |
53 | 79 |
54 main_loop->EarlyInitialization(); | 80 main_loop_->EarlyInitialization(); |
55 | 81 |
56 // Must happen before we try to use a message loop or display any UI. | 82 // Must happen before we try to use a message loop or display any UI. |
57 main_loop->InitializeToolkit(); | 83 main_loop_->InitializeToolkit(); |
58 | 84 |
59 main_loop->MainMessageLoopStart(); | 85 main_loop_->MainMessageLoopStart(); |
60 | 86 |
61 // WARNING: If we get a WM_ENDSESSION, objects created on the stack here | 87 // 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 | 88 // are NOT deleted. If you need something to run during WM_ENDSESSION add it |
63 // to browser_shutdown::Shutdown or BrowserProcess::EndSession. | 89 // to browser_shutdown::Shutdown or BrowserProcess::EndSession. |
64 | 90 |
65 // !!!!!!!!!! READ ME !!!!!!!!!! | 91 // !!!!!!!!!! READ ME !!!!!!!!!! |
66 // I (viettrungluu) am in the process of refactoring |BrowserMain()|. If you | 92 // I (viettrungluu) am in the process of refactoring |BrowserMain()|. If you |
67 // need to add something above this comment, read the documentation in | 93 // 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 | 94 // browser_main.h. If you need to add something below, please do the |
69 // following: | 95 // following: |
(...skipping 15 matching lines...) Expand all Loading... | |
85 | 111 |
86 #if defined(OS_WIN) | 112 #if defined(OS_WIN) |
87 #if !defined(NO_TCMALLOC) | 113 #if !defined(NO_TCMALLOC) |
88 // When linking shared libraries, NO_TCMALLOC is defined, and dynamic | 114 // When linking shared libraries, NO_TCMALLOC is defined, and dynamic |
89 // allocator selection is not supported. | 115 // allocator selection is not supported. |
90 | 116 |
91 // Make this call before going multithreaded, or spawning any subprocesses. | 117 // Make this call before going multithreaded, or spawning any subprocesses. |
92 base::allocator::SetupSubprocessAllocator(); | 118 base::allocator::SetupSubprocessAllocator(); |
93 #endif | 119 #endif |
94 | 120 |
95 base::win::ScopedCOMInitializer com_initializer; | 121 com_initializer_.reset(new base::win::ScopedCOMInitializer); |
96 #endif // OS_WIN | 122 #endif // OS_WIN |
97 | 123 |
98 base::StatisticsRecorder statistics; | 124 statistics_.reset(new base::StatisticsRecorder); |
99 | 125 |
100 main_loop->RunMainMessageLoopParts(&g_exited_main_message_loop); | 126 main_loop_->CreateThreads(); |
101 | 127 |
102 TRACE_EVENT_END_ETW("BrowserMain", 0, 0); | 128 // Return -1 to indicate no early termination. |
129 return -1; | |
130 } | |
103 | 131 |
104 return main_loop->GetResultCode(); | 132 int BrowserMainRunnerImpl::Run() { |
133 DCHECK(is_initialized_); | |
134 DCHECK(!is_shutdown_); | |
135 main_loop_->RunMainMessageLoopParts(); | |
136 return main_loop_->GetResultCode(); | |
105 } | 137 } |
138 | |
139 void BrowserMainRunnerImpl::Shutdown() { | |
140 DCHECK(is_initialized_); | |
141 DCHECK(!is_shutdown_); | |
142 g_exited_main_message_loop = true; | |
143 main_loop_->ShutdownThreadsAndCleanUp(); | |
144 | |
145 statistics_.reset(NULL); | |
146 | |
147 #if defined(OS_WIN) | |
148 com_initializer_.reset(NULL); | |
149 #endif | |
150 | |
151 main_loop_.reset(NULL); | |
152 | |
153 notification_service_.reset(NULL); | |
154 | |
155 is_shutdown_ = true; | |
156 } | |
157 | |
158 } // namespace | |
159 | |
160 namespace content { | |
161 | |
162 // static | |
163 BrowserMainRunner* BrowserMainRunner::Create() { | |
164 return new BrowserMainRunnerImpl(); | |
165 } | |
166 | |
167 bool ExitedMainMessageLoop() { | |
168 return g_exited_main_message_loop; | |
169 } | |
170 | |
171 } // namespace content | |
OLD | NEW |