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/common/child_process.h"
17 #include "content/public/common/content_switches.h"
18 #include "content/public/common/main_function_params.h"
21 #include "base/win/metro.h"
22 #include "base/win/windows_version.h"
23 #include "ui/base/win/scoped_ole_initializer.h"
24 #include "ui/base/ime/win/tsf_bridge.h"
27 bool g_exited_main_message_loop
= false;
31 class BrowserMainRunnerImpl
: public BrowserMainRunner
{
33 BrowserMainRunnerImpl()
34 : is_initialized_(false),
36 created_threads_(false) {
39 ~BrowserMainRunnerImpl() {
40 if (is_initialized_
&& !is_shutdown_
)
44 virtual int Initialize(const MainFunctionParams
& parameters
)
46 is_initialized_
= true;
49 // ChildProcess:: is a misnomer unless you consider context. Use
50 // of --wait-for-debugger only makes sense when Chrome itself is a
51 // child process (e.g. when launched by PyAuto).
52 if (parameters
.command_line
.HasSwitch(switches::kWaitForDebugger
))
53 ChildProcess::WaitForDebugger("Browser");
54 #endif // !defined(OS_IOS)
57 if (parameters
.command_line
.HasSwitch(
58 switches::kEnableTextServicesFramework
)) {
59 base::win::SetForceToUseTSF();
60 } else if (base::win::GetVersion() < base::win::VERSION_VISTA
) {
61 // When "Extend support of advanced text services to all programs"
62 // (a.k.a. Cicero Unaware Application Support; CUAS) is enabled on
63 // Windows XP and handwriting modules shipped with Office 2003 are
64 // installed, "penjpn.dll" and "skchui.dll" will be loaded and then crash
65 // unless a user installs Office 2003 SP3. To prevent these modules from
66 // being loaded, disable TSF entirely. crbug/160914.
67 // TODO(yukawa): Add a high-level wrapper for this instead of calling
68 // Win32 API here directly.
69 ImmDisableTextFrameService(static_cast<DWORD
>(-1));
73 base::StatisticsRecorder::Initialize();
75 notification_service_
.reset(new NotificationServiceImpl
);
78 // Ole must be initialized before starting message pump, so that TSF
79 // (Text Services Framework) module can interact with the message pump
80 // on Windows 8 Metro mode.
81 ole_initializer_
.reset(new ui::ScopedOleInitializer
);
84 main_loop_
.reset(new BrowserMainLoop(parameters
));
88 main_loop_
->EarlyInitialization();
90 // Must happen before we try to use a message loop or display any UI.
91 main_loop_
->InitializeToolkit();
93 main_loop_
->MainMessageLoopStart();
95 // WARNING: If we get a WM_ENDSESSION, objects created on the stack here
96 // are NOT deleted. If you need something to run during WM_ENDSESSION add it
97 // to browser_shutdown::Shutdown or BrowserProcess::EndSession.
100 #if !defined(NO_TCMALLOC)
101 // When linking shared libraries, NO_TCMALLOC is defined, and dynamic
102 // allocator selection is not supported.
104 // Make this call before going multithreaded, or spawning any subprocesses.
105 base::allocator::SetupSubprocessAllocator();
107 if (base::win::IsTSFAwareRequired())
108 ui::TSFBridge::Initialize();
111 main_loop_
->CreateThreads();
112 int result_code
= main_loop_
->GetResultCode();
115 created_threads_
= true;
117 // Return -1 to indicate no early termination.
121 virtual int Run() OVERRIDE
{
122 DCHECK(is_initialized_
);
123 DCHECK(!is_shutdown_
);
124 main_loop_
->RunMainMessageLoopParts();
125 return main_loop_
->GetResultCode();
128 virtual void Shutdown() OVERRIDE
{
129 DCHECK(is_initialized_
);
130 DCHECK(!is_shutdown_
);
131 g_exited_main_message_loop
= true;
133 if (created_threads_
)
134 main_loop_
->ShutdownThreadsAndCleanUp();
137 if (base::win::IsTSFAwareRequired())
138 ui::TSFBridge::GetInstance()->Shutdown();
139 ole_initializer_
.reset(NULL
);
142 main_loop_
.reset(NULL
);
144 notification_service_
.reset(NULL
);
150 // True if the runner has been initialized.
151 bool is_initialized_
;
153 // True if the runner has been shut down.
156 // True if the non-UI threads were created.
157 bool created_threads_
;
159 scoped_ptr
<NotificationServiceImpl
> notification_service_
;
160 scoped_ptr
<BrowserMainLoop
> main_loop_
;
162 scoped_ptr
<ui::ScopedOleInitializer
> ole_initializer_
;
165 DISALLOW_COPY_AND_ASSIGN(BrowserMainRunnerImpl
);
169 BrowserMainRunner
* BrowserMainRunner::Create() {
170 return new BrowserMainRunnerImpl();
173 } // namespace content