OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/browser_main_runner.h" |
| 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" |
| 11 #include "base/logging.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 class BrowserMainRunnerImpl : public content::BrowserMainRunner { |
| 28 public: |
| 29 BrowserMainRunnerImpl(); |
| 30 ~BrowserMainRunnerImpl(); |
| 31 |
| 32 virtual int Initialize(const content::MainFunctionParams& parameters) |
| 33 OVERRIDE; |
| 34 virtual int Run() OVERRIDE; |
| 35 virtual void Shutdown() OVERRIDE; |
| 36 |
| 37 protected: |
| 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) { |
| 57 } |
| 58 |
| 59 BrowserMainRunnerImpl::~BrowserMainRunnerImpl() { |
| 60 if (is_initialized_ && !is_shutdown_) |
| 61 Shutdown(); |
| 62 } |
| 63 |
| 64 int BrowserMainRunnerImpl::Initialize( |
| 65 const content::MainFunctionParams& parameters) { |
| 66 is_initialized_ = true; |
| 67 |
| 68 // ChildProcess:: is a misnomer unless you consider context. Use |
| 69 // of --wait-for-debugger only makes sense when Chrome itself is a |
| 70 // child process (e.g. when launched by PyAuto). |
| 71 if (parameters.command_line.HasSwitch(switches::kWaitForDebugger)) |
| 72 ChildProcess::WaitForDebugger("Browser"); |
| 73 |
| 74 notification_service_.reset(new NotificationServiceImpl); |
| 75 |
| 76 main_loop_.reset(new content::BrowserMainLoop(parameters)); |
| 77 |
| 78 main_loop_->Init(); |
| 79 |
| 80 main_loop_->EarlyInitialization(); |
| 81 |
| 82 // Must happen before we try to use a message loop or display any UI. |
| 83 main_loop_->InitializeToolkit(); |
| 84 |
| 85 main_loop_->MainMessageLoopStart(); |
| 86 |
| 87 // WARNING: If we get a WM_ENDSESSION, objects created on the stack here |
| 88 // are NOT deleted. If you need something to run during WM_ENDSESSION add it |
| 89 // to browser_shutdown::Shutdown or BrowserProcess::EndSession. |
| 90 |
| 91 // !!!!!!!!!! READ ME !!!!!!!!!! |
| 92 // I (viettrungluu) am in the process of refactoring |BrowserMain()|. If you |
| 93 // need to add something above this comment, read the documentation in |
| 94 // browser_main.h. If you need to add something below, please do the |
| 95 // following: |
| 96 // - Figure out where you should add your code. Do NOT just pick a random |
| 97 // location "which works". |
| 98 // - Document the dependencies apart from compile-time-checkable ones. What |
| 99 // must happen before your new code is executed? Does your new code need to |
| 100 // run before something else? Are there performance reasons for executing |
| 101 // your code at that point? |
| 102 // - If you need to create a (persistent) object, heap allocate it and keep a |
| 103 // |scoped_ptr| to it rather than allocating it on the stack. Otherwise |
| 104 // I'll have to convert your code when I refactor. |
| 105 // - Unless your new code is just a couple of lines, factor it out into a |
| 106 // function with a well-defined purpose. Do NOT just add it inline in |
| 107 // |BrowserMain()|. |
| 108 // Thanks! |
| 109 |
| 110 // TODO(viettrungluu): put the remainder into BrowserMainParts |
| 111 |
| 112 #if defined(OS_WIN) |
| 113 #if !defined(NO_TCMALLOC) |
| 114 // When linking shared libraries, NO_TCMALLOC is defined, and dynamic |
| 115 // allocator selection is not supported. |
| 116 |
| 117 // Make this call before going multithreaded, or spawning any subprocesses. |
| 118 base::allocator::SetupSubprocessAllocator(); |
| 119 #endif |
| 120 |
| 121 com_initializer_.reset(new base::win::ScopedCOMInitializer); |
| 122 #endif // OS_WIN |
| 123 |
| 124 statistics_.reset(new base::StatisticsRecorder); |
| 125 |
| 126 main_loop_->CreateThreads(); |
| 127 |
| 128 // Return -1 to indicate no early termination. |
| 129 return -1; |
| 130 } |
| 131 |
| 132 int BrowserMainRunnerImpl::Run() { |
| 133 DCHECK(is_initialized_); |
| 134 DCHECK(!is_shutdown_); |
| 135 main_loop_->RunMainMessageLoopParts(); |
| 136 return main_loop_->GetResultCode(); |
| 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::CreateMainRunner() { |
| 164 return new BrowserMainRunnerImpl(); |
| 165 } |
| 166 |
| 167 bool ExitedMainMessageLoop() { |
| 168 return g_exited_main_message_loop; |
| 169 } |
| 170 |
| 171 } // namespace content |
OLD | NEW |