- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / browser_main_runner.cc
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.
4
5 #include "content/public/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 "base/metrics/statistics_recorder.h"
14 #include "content/browser/browser_main_loop.h"
15 #include "content/browser/browser_shutdown_profile_dumper.h"
16 #include "content/browser/notification_service_impl.h"
17 #include "content/public/common/content_switches.h"
18 #include "content/public/common/main_function_params.h"
19 #include "ui/base/ime/input_method_initializer.h"
20
21 #if defined(OS_WIN)
22 #include "base/win/metro.h"
23 #include "base/win/windows_version.h"
24 #include "ui/base/win/scoped_ole_initializer.h"
25 #endif
26
27 bool g_exited_main_message_loop = false;
28
29 namespace content {
30
31 class BrowserMainRunnerImpl : public BrowserMainRunner {
32  public:
33   BrowserMainRunnerImpl()
34       : initialization_started_(false), is_shutdown_(false) {}
35
36   virtual ~BrowserMainRunnerImpl() {
37     if (initialization_started_ && !is_shutdown_)
38       Shutdown();
39   }
40
41   virtual int Initialize(const MainFunctionParams& parameters) OVERRIDE {
42     TRACE_EVENT0("startup", "BrowserMainRunnerImpl::Initialize");
43     // On Android we normally initialize the browser in a series of UI thread
44     // tasks. While this is happening a second request can come from the OS or
45     // another application to start the browser. If this happens then we must
46     // not run these parts of initialization twice.
47     if (!initialization_started_) {
48       initialization_started_ = true;
49
50 #if !defined(OS_IOS)
51       if (parameters.command_line.HasSwitch(switches::kWaitForDebugger))
52         base::debug::WaitForDebugger(60, true);
53 #endif
54
55 #if defined(OS_WIN)
56       if (parameters.command_line.HasSwitch(
57               switches::kEnableTextServicesFramework)) {
58         base::win::SetForceToUseTSF();
59       } else if (base::win::GetVersion() < base::win::VERSION_VISTA) {
60         // When "Extend support of advanced text services to all programs"
61         // (a.k.a. Cicero Unaware Application Support; CUAS) is enabled on
62         // Windows XP and handwriting modules shipped with Office 2003 are
63         // installed, "penjpn.dll" and "skchui.dll" will be loaded and then
64         // 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));
70       }
71 #endif  // OS_WIN
72
73       base::StatisticsRecorder::Initialize();
74
75       notification_service_.reset(new NotificationServiceImpl);
76
77 #if defined(OS_WIN)
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);
82 #endif  // OS_WIN
83
84       main_loop_.reset(new BrowserMainLoop(parameters));
85
86       main_loop_->Init();
87
88       main_loop_->EarlyInitialization();
89
90       // Must happen before we try to use a message loop or display any UI.
91       main_loop_->InitializeToolkit();
92
93       main_loop_->MainMessageLoopStart();
94
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.
98
99 #if defined(OS_WIN) && !defined(NO_TCMALLOC)
100       // When linking shared libraries, NO_TCMALLOC is defined, and dynamic
101       // allocator selection is not supported.
102
103       // Make this call before going multithreaded, or spawning any
104       // subprocesses.
105       base::allocator::SetupSubprocessAllocator();
106 #endif
107       ui::InitializeInputMethod();
108     }
109     main_loop_->CreateStartupTasks();
110     int result_code = main_loop_->GetResultCode();
111     if (result_code > 0)
112       return result_code;
113
114     // Return -1 to indicate no early termination.
115     return -1;
116   }
117
118   virtual int Run() OVERRIDE {
119     DCHECK(initialization_started_);
120     DCHECK(!is_shutdown_);
121     main_loop_->RunMainMessageLoopParts();
122     return main_loop_->GetResultCode();
123   }
124
125   virtual void Shutdown() OVERRIDE {
126     DCHECK(initialization_started_);
127     DCHECK(!is_shutdown_);
128     // The shutdown tracing got enabled in AttemptUserExit earlier, but someone
129     // needs to write the result to disc. For that a dumper needs to get created
130     // which will dump the traces to disc when it gets destroyed.
131     const CommandLine& command_line = *CommandLine::ForCurrentProcess();
132     scoped_ptr<BrowserShutdownProfileDumper> profiler;
133     if (command_line.HasSwitch(switches::kTraceShutdown))
134       profiler.reset(new BrowserShutdownProfileDumper());
135
136     {
137       // The trace event has to stay between profiler creation and destruction.
138       TRACE_EVENT0("shutdown", "BrowserMainRunner");
139       g_exited_main_message_loop = true;
140
141       main_loop_->ShutdownThreadsAndCleanUp();
142
143       ui::ShutdownInputMethod();
144   #if defined(OS_WIN)
145       ole_initializer_.reset(NULL);
146   #endif
147
148       main_loop_.reset(NULL);
149
150       notification_service_.reset(NULL);
151
152       is_shutdown_ = true;
153     }
154   }
155
156  protected:
157   // True if we have started to initialize the runner.
158   bool initialization_started_;
159
160   // True if the runner has been shut down.
161   bool is_shutdown_;
162
163   scoped_ptr<NotificationServiceImpl> notification_service_;
164   scoped_ptr<BrowserMainLoop> main_loop_;
165 #if defined(OS_WIN)
166   scoped_ptr<ui::ScopedOleInitializer> ole_initializer_;
167 #endif
168
169   DISALLOW_COPY_AND_ASSIGN(BrowserMainRunnerImpl);
170 };
171
172 // static
173 BrowserMainRunner* BrowserMainRunner::Create() {
174   return new BrowserMainRunnerImpl();
175 }
176
177 }  // namespace content