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
);
77 main_loop_
.reset(new BrowserMainLoop(parameters
));
81 main_loop_
->EarlyInitialization();
83 // Must happen before we try to use a message loop or display any UI.
84 main_loop_
->InitializeToolkit();
86 main_loop_
->MainMessageLoopStart();
88 // WARNING: If we get a WM_ENDSESSION, objects created on the stack here
89 // are NOT deleted. If you need something to run during WM_ENDSESSION add it
90 // to browser_shutdown::Shutdown or BrowserProcess::EndSession.
93 #if !defined(NO_TCMALLOC)
94 // When linking shared libraries, NO_TCMALLOC is defined, and dynamic
95 // allocator selection is not supported.
97 // Make this call before going multithreaded, or spawning any subprocesses.
98 base::allocator::SetupSubprocessAllocator();
100 ole_initializer_
.reset(new ui::ScopedOleInitializer
);
101 if (base::win::IsTSFAwareRequired())
102 ui::TSFBridge::Initialize();
105 main_loop_
->CreateThreads();
106 int result_code
= main_loop_
->GetResultCode();
109 created_threads_
= true;
111 // Return -1 to indicate no early termination.
115 virtual int Run() OVERRIDE
{
116 DCHECK(is_initialized_
);
117 DCHECK(!is_shutdown_
);
118 main_loop_
->RunMainMessageLoopParts();
119 return main_loop_
->GetResultCode();
122 virtual void Shutdown() OVERRIDE
{
123 DCHECK(is_initialized_
);
124 DCHECK(!is_shutdown_
);
125 g_exited_main_message_loop
= true;
127 if (created_threads_
)
128 main_loop_
->ShutdownThreadsAndCleanUp();
131 if (base::win::IsTSFAwareRequired())
132 ui::TSFBridge::GetInstance()->Shutdown();
133 ole_initializer_
.reset(NULL
);
136 main_loop_
.reset(NULL
);
138 notification_service_
.reset(NULL
);
144 // True if the runner has been initialized.
145 bool is_initialized_
;
147 // True if the runner has been shut down.
150 // True if the non-UI threads were created.
151 bool created_threads_
;
153 scoped_ptr
<NotificationServiceImpl
> notification_service_
;
154 scoped_ptr
<BrowserMainLoop
> main_loop_
;
156 scoped_ptr
<ui::ScopedOleInitializer
> ole_initializer_
;
159 DISALLOW_COPY_AND_ASSIGN(BrowserMainRunnerImpl
);
163 BrowserMainRunner
* BrowserMainRunner::Create() {
164 return new BrowserMainRunnerImpl();
167 } // namespace content