Allow overlapping sync and async startup requests
[chromium-blink-merge.git] / content / browser / browser_main_runner.cc
blobf97318a690657892c4676b1b1d454f9dd75f8a1b
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
3 // found in the LICENSE file.
5 #include "content/public/browser/browser_main_runner.h"
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 "base/metrics/statistics_recorder.h"
14 #include "content/browser/browser_main_loop.h"
15 #include "content/browser/notification_service_impl.h"
16 #include "content/public/common/content_switches.h"
17 #include "content/public/common/main_function_params.h"
18 #include "ui/base/ime/input_method_initializer.h"
20 #if defined(OS_WIN)
21 #include "base/win/metro.h"
22 #include "base/win/windows_version.h"
23 #include "ui/base/win/scoped_ole_initializer.h"
24 #endif
26 bool g_exited_main_message_loop = false;
28 namespace content {
30 class BrowserMainRunnerImpl : public BrowserMainRunner {
31 public:
32 BrowserMainRunnerImpl()
33 : initialization_started_(false), is_shutdown_(false) {}
35 virtual ~BrowserMainRunnerImpl() {
36 if (initialization_started_ && !is_shutdown_)
37 Shutdown();
40 virtual int Initialize(const MainFunctionParams& parameters) OVERRIDE {
41 TRACE_EVENT0("startup", "BrowserMainRunnerImpl::Initialize");
42 // On Android we normally initialize the browser in a series of UI thread
43 // tasks. While this is happening a second request can come from the OS or
44 // another application to start the browser. If this happens then we must
45 // not run these parts of initialization twice.
46 if (!initialization_started_) {
47 initialization_started_ = true;
49 #if !defined(OS_IOS)
50 if (parameters.command_line.HasSwitch(switches::kWaitForDebugger))
51 base::debug::WaitForDebugger(60, true);
52 #endif
54 #if defined(OS_WIN)
55 if (parameters.command_line.HasSwitch(
56 switches::kEnableTextServicesFramework)) {
57 base::win::SetForceToUseTSF();
58 } else if (base::win::GetVersion() < base::win::VERSION_VISTA) {
59 // When "Extend support of advanced text services to all programs"
60 // (a.k.a. Cicero Unaware Application Support; CUAS) is enabled on
61 // Windows XP and handwriting modules shipped with Office 2003 are
62 // installed, "penjpn.dll" and "skchui.dll" will be loaded and then
63 // crash
64 // unless a user installs Office 2003 SP3. To prevent these modules from
65 // being loaded, disable TSF entirely. crbug/160914.
66 // TODO(yukawa): Add a high-level wrapper for this instead of calling
67 // Win32 API here directly.
68 ImmDisableTextFrameService(static_cast<DWORD>(-1));
70 #endif // OS_WIN
72 base::StatisticsRecorder::Initialize();
74 notification_service_.reset(new NotificationServiceImpl);
76 #if defined(OS_WIN)
77 // Ole must be initialized before starting message pump, so that TSF
78 // (Text Services Framework) module can interact with the message pump
79 // on Windows 8 Metro mode.
80 ole_initializer_.reset(new ui::ScopedOleInitializer);
81 #endif // OS_WIN
83 main_loop_.reset(new BrowserMainLoop(parameters));
85 main_loop_->Init();
87 main_loop_->EarlyInitialization();
89 // Must happen before we try to use a message loop or display any UI.
90 main_loop_->InitializeToolkit();
92 main_loop_->MainMessageLoopStart();
94 // WARNING: If we get a WM_ENDSESSION, objects created on the stack here
95 // are NOT deleted. If you need something to run during WM_ENDSESSION add it
96 // to browser_shutdown::Shutdown or BrowserProcess::EndSession.
98 #if defined(OS_WIN) && !defined(NO_TCMALLOC)
99 // When linking shared libraries, NO_TCMALLOC is defined, and dynamic
100 // allocator selection is not supported.
102 // Make this call before going multithreaded, or spawning any
103 // subprocesses.
104 base::allocator::SetupSubprocessAllocator();
105 #endif
106 ui::InitializeInputMethod();
108 main_loop_->CreateStartupTasks();
109 int result_code = main_loop_->GetResultCode();
110 if (result_code > 0)
111 return result_code;
113 // Return -1 to indicate no early termination.
114 return -1;
117 virtual int Run() OVERRIDE {
118 DCHECK(initialization_started_);
119 DCHECK(!is_shutdown_);
120 main_loop_->RunMainMessageLoopParts();
121 return main_loop_->GetResultCode();
124 virtual void Shutdown() OVERRIDE {
125 DCHECK(initialization_started_);
126 DCHECK(!is_shutdown_);
127 g_exited_main_message_loop = true;
129 main_loop_->ShutdownThreadsAndCleanUp();
131 ui::ShutdownInputMethod();
132 #if defined(OS_WIN)
133 ole_initializer_.reset(NULL);
134 #endif
136 main_loop_.reset(NULL);
138 notification_service_.reset(NULL);
140 is_shutdown_ = true;
143 protected:
144 // True if we have started to initialize the runner.
145 bool initialization_started_;
147 // True if the runner has been shut down.
148 bool is_shutdown_;
150 scoped_ptr<NotificationServiceImpl> notification_service_;
151 scoped_ptr<BrowserMainLoop> main_loop_;
152 #if defined(OS_WIN)
153 scoped_ptr<ui::ScopedOleInitializer> ole_initializer_;
154 #endif
156 DISALLOW_COPY_AND_ASSIGN(BrowserMainRunnerImpl);
159 // static
160 BrowserMainRunner* BrowserMainRunner::Create() {
161 return new BrowserMainRunnerImpl();
164 } // namespace content