Chromium Code Reviews| Index: content/browser/browser_main_runner.cc |
| =================================================================== |
| --- content/browser/browser_main_runner.cc (revision 117304) |
| +++ content/browser/browser_main_runner.cc (working copy) |
| @@ -2,7 +2,7 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "content/browser/browser_main.h" |
| +#include "content/public/browser/browser_main_runner.h" |
| #include "base/allocator/allocator_shim.h" |
| #include "base/base_switches.h" |
| @@ -24,82 +24,140 @@ |
| bool g_exited_main_message_loop = false; |
| -} // namespace |
| +class BrowserMainRunnerImpl : public content::BrowserMainRunner { |
| + public: |
| + BrowserMainRunnerImpl() |
| + : is_initialized_(false), |
| + is_shutdown_(false) { |
| + } |
| -namespace content { |
| + ~BrowserMainRunnerImpl() { |
| + if (is_initialized_ && !is_shutdown_) |
| + Shutdown(); |
| + } |
| -bool ExitedMainMessageLoop() { |
| - return g_exited_main_message_loop; |
| -} |
| + virtual int Initialize(const content::MainFunctionParams& parameters) |
| + OVERRIDE { |
| + is_initialized_ = true; |
| -} // namespace content |
| + // ChildProcess:: is a misnomer unless you consider context. Use |
| + // of --wait-for-debugger only makes sense when Chrome itself is a |
| + // child process (e.g. when launched by PyAuto). |
| + if (parameters.command_line.HasSwitch(switches::kWaitForDebugger)) |
| + ChildProcess::WaitForDebugger("Browser"); |
| -// Main routine for running as the Browser process. |
| -int BrowserMain(const content::MainFunctionParams& parameters) { |
| - TRACE_EVENT_BEGIN_ETW("BrowserMain", 0, ""); |
| + notification_service_.reset(new NotificationServiceImpl); |
| - // ChildProcess:: is a misnomer unless you consider context. Use |
| - // of --wait-for-debugger only makes sense when Chrome itself is a |
| - // child process (e.g. when launched by PyAuto). |
| - if (parameters.command_line.HasSwitch(switches::kWaitForDebugger)) |
| - ChildProcess::WaitForDebugger("Browser"); |
| + main_loop_.reset(new content::BrowserMainLoop(parameters)); |
| - NotificationServiceImpl main_notification_service; |
| + main_loop_->Init(); |
| - scoped_ptr<content::BrowserMainLoop> main_loop( |
| - new content::BrowserMainLoop(parameters)); |
| + main_loop_->EarlyInitialization(); |
| - main_loop->Init(); |
| + // Must happen before we try to use a message loop or display any UI. |
| + main_loop_->InitializeToolkit(); |
| - main_loop->EarlyInitialization(); |
| + main_loop_->MainMessageLoopStart(); |
| - // Must happen before we try to use a message loop or display any UI. |
| - main_loop->InitializeToolkit(); |
| + // WARNING: If we get a WM_ENDSESSION, objects created on the stack here |
| + // are NOT deleted. If you need something to run during WM_ENDSESSION add it |
| + // to browser_shutdown::Shutdown or BrowserProcess::EndSession. |
| - main_loop->MainMessageLoopStart(); |
| + // !!!!!!!!!! READ ME !!!!!!!!!! |
|
jam
2012/02/01 17:28:39
actually this comment is so out of date especially
Marshall
2012/02/01 19:37:14
Done.
|
| + // I (viettrungluu) am in the process of refactoring |BrowserMain()|. If you |
| + // need to add something above this comment, read the documentation in |
| + // browser_main.h. If you need to add something below, please do the |
| + // following: |
| + // - Figure out where you should add your code. Do NOT just pick a random |
| + // location "which works". |
| + // - Document the dependencies apart from compile-time-checkable ones. What |
| + // must happen before your new code is executed? Does your new code need |
| + // to run before something else? Are there performance reasons for |
| + // executing your code at that point? |
| + // - If you need to create a (persistent) object, heap allocate it and keep |
| + // a |scoped_ptr| to it rather than allocating it on the stack. Otherwise |
| + // I'll have to convert your code when I refactor. |
| + // - Unless your new code is just a couple of lines, factor it out into a |
| + // function with a well-defined purpose. Do NOT just add it inline in |
| + // |BrowserMain()|. |
| + // Thanks! |
| - // WARNING: If we get a WM_ENDSESSION, objects created on the stack here |
| - // are NOT deleted. If you need something to run during WM_ENDSESSION add it |
| - // to browser_shutdown::Shutdown or BrowserProcess::EndSession. |
| + // TODO(viettrungluu): put the remainder into BrowserMainParts |
| - // !!!!!!!!!! READ ME !!!!!!!!!! |
| - // I (viettrungluu) am in the process of refactoring |BrowserMain()|. If you |
| - // need to add something above this comment, read the documentation in |
| - // browser_main.h. If you need to add something below, please do the |
| - // following: |
| - // - Figure out where you should add your code. Do NOT just pick a random |
| - // location "which works". |
| - // - Document the dependencies apart from compile-time-checkable ones. What |
| - // must happen before your new code is executed? Does your new code need to |
| - // run before something else? Are there performance reasons for executing |
| - // your code at that point? |
| - // - If you need to create a (persistent) object, heap allocate it and keep a |
| - // |scoped_ptr| to it rather than allocating it on the stack. Otherwise |
| - // I'll have to convert your code when I refactor. |
| - // - Unless your new code is just a couple of lines, factor it out into a |
| - // function with a well-defined purpose. Do NOT just add it inline in |
| - // |BrowserMain()|. |
| - // Thanks! |
| - |
| - // TODO(viettrungluu): put the remainder into BrowserMainParts |
| - |
| #if defined(OS_WIN) |
| #if !defined(NO_TCMALLOC) |
| - // When linking shared libraries, NO_TCMALLOC is defined, and dynamic |
| - // allocator selection is not supported. |
| + // When linking shared libraries, NO_TCMALLOC is defined, and dynamic |
| + // allocator selection is not supported. |
| - // Make this call before going multithreaded, or spawning any subprocesses. |
| - base::allocator::SetupSubprocessAllocator(); |
| + // Make this call before going multithreaded, or spawning any subprocesses. |
| + base::allocator::SetupSubprocessAllocator(); |
| #endif |
| - base::win::ScopedCOMInitializer com_initializer; |
| + com_initializer_.reset(new base::win::ScopedCOMInitializer); |
| #endif // OS_WIN |
| - base::StatisticsRecorder statistics; |
| + statistics_.reset(new base::StatisticsRecorder); |
| - main_loop->RunMainMessageLoopParts(&g_exited_main_message_loop); |
| + main_loop_->CreateThreads(); |
| - TRACE_EVENT_END_ETW("BrowserMain", 0, 0); |
| + // Return -1 to indicate no early termination. |
| + return -1; |
| + } |
| - return main_loop->GetResultCode(); |
| + virtual int Run() OVERRIDE { |
| + DCHECK(is_initialized_); |
| + DCHECK(!is_shutdown_); |
| + main_loop_->RunMainMessageLoopParts(); |
| + return main_loop_->GetResultCode(); |
| + } |
| + |
| + virtual void Shutdown() OVERRIDE { |
| + DCHECK(is_initialized_); |
| + DCHECK(!is_shutdown_); |
| + g_exited_main_message_loop = true; |
| + main_loop_->ShutdownThreadsAndCleanUp(); |
| + |
| + statistics_.reset(NULL); |
| + |
| +#if defined(OS_WIN) |
| + com_initializer_.reset(NULL); |
| +#endif |
| + |
| + main_loop_.reset(NULL); |
| + |
| + notification_service_.reset(NULL); |
| + |
| + is_shutdown_ = true; |
| + } |
| + |
| + protected: |
| + // True if the runner has been initialized. |
| + bool is_initialized_; |
| + |
| + // True if the runner has been shut down. |
| + bool is_shutdown_; |
| + |
| + scoped_ptr<NotificationServiceImpl> notification_service_; |
| + scoped_ptr<content::BrowserMainLoop> main_loop_; |
| +#if defined(OS_WIN) |
| + scoped_ptr<base::win::ScopedCOMInitializer> com_initializer_; |
| +#endif |
| + scoped_ptr<base::StatisticsRecorder> statistics_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(BrowserMainRunnerImpl); |
| +}; |
| + |
| +} // namespace |
| + |
| +namespace content { |
| + |
| +// static |
| +BrowserMainRunner* BrowserMainRunner::Create() { |
| + return new BrowserMainRunnerImpl(); |
| } |
| + |
| +bool ExitedMainMessageLoop() { |
| + return g_exited_main_message_loop; |
| +} |
| + |
| +} // namespace content |