Upstream version 5.34.92.0
[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/leak_annotations.h"
11 #include "base/debug/trace_event.h"
12 #include "base/logging.h"
13 #include "base/metrics/histogram.h"
14 #include "base/metrics/statistics_recorder.h"
15 #include "content/browser/browser_main_loop.h"
16 #include "content/browser/browser_shutdown_profile_dumper.h"
17 #include "content/browser/notification_service_impl.h"
18 #include "content/public/common/content_switches.h"
19 #include "content/public/common/main_function_params.h"
20 #include "ui/base/ime/input_method_initializer.h"
21
22 #if defined(OS_WIN)
23 #include "base/win/metro.h"
24 #include "base/win/windows_version.h"
25 #include "ui/base/win/scoped_ole_initializer.h"
26 #endif
27
28 bool g_exited_main_message_loop = false;
29
30 namespace content {
31
32 class BrowserMainRunnerImpl : public BrowserMainRunner {
33  public:
34   BrowserMainRunnerImpl()
35       : initialization_started_(false), is_shutdown_(false) {}
36
37   virtual ~BrowserMainRunnerImpl() {
38     if (initialization_started_ && !is_shutdown_)
39       Shutdown();
40   }
41
42   virtual int Initialize(const MainFunctionParams& parameters) OVERRIDE {
43     TRACE_EVENT0("startup", "BrowserMainRunnerImpl::Initialize");
44     // On Android we normally initialize the browser in a series of UI thread
45     // tasks. While this is happening a second request can come from the OS or
46     // another application to start the browser. If this happens then we must
47     // not run these parts of initialization twice.
48     if (!initialization_started_) {
49       initialization_started_ = true;
50
51 #if !defined(OS_IOS)
52       if (parameters.command_line.HasSwitch(switches::kWaitForDebugger))
53         base::debug::WaitForDebugger(60, true);
54 #endif
55
56 #if defined(OS_WIN)
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
65         // crash
66         // unless a user installs Office 2003 SP3. To prevent these modules from
67         // being loaded, disable TSF entirely. crbug/160914.
68         // TODO(yukawa): Add a high-level wrapper for this instead of calling
69         // Win32 API here directly.
70         ImmDisableTextFrameService(static_cast<DWORD>(-1));
71       }
72 #endif  // OS_WIN
73
74       base::StatisticsRecorder::Initialize();
75
76       notification_service_.reset(new NotificationServiceImpl);
77
78 #if defined(OS_WIN)
79       // Ole must be initialized before starting message pump, so that TSF
80       // (Text Services Framework) module can interact with the message pump
81       // on Windows 8 Metro mode.
82       ole_initializer_.reset(new ui::ScopedOleInitializer);
83 #endif  // OS_WIN
84
85       main_loop_.reset(new BrowserMainLoop(parameters));
86
87       main_loop_->Init();
88
89       main_loop_->EarlyInitialization();
90
91       // Must happen before we try to use a message loop or display any UI.
92       main_loop_->InitializeToolkit();
93
94       main_loop_->MainMessageLoopStart();
95
96 // WARNING: If we get a WM_ENDSESSION, objects created on the stack here
97 // are NOT deleted. If you need something to run during WM_ENDSESSION add it
98 // to browser_shutdown::Shutdown or BrowserProcess::EndSession.
99
100 #if defined(OS_WIN) && !defined(NO_TCMALLOC)
101       // When linking shared libraries, NO_TCMALLOC is defined, and dynamic
102       // allocator selection is not supported.
103
104       // Make this call before going multithreaded, or spawning any
105       // subprocesses.
106       base::allocator::SetupSubprocessAllocator();
107 #endif
108       ui::InitializeInputMethod();
109     }
110     main_loop_->CreateStartupTasks();
111     int result_code = main_loop_->GetResultCode();
112     if (result_code > 0)
113       return result_code;
114
115     // Return -1 to indicate no early termination.
116     return -1;
117   }
118
119   virtual int Run() OVERRIDE {
120     DCHECK(initialization_started_);
121     DCHECK(!is_shutdown_);
122     main_loop_->RunMainMessageLoopParts();
123     return main_loop_->GetResultCode();
124   }
125
126   virtual void Shutdown() OVERRIDE {
127     DCHECK(initialization_started_);
128     DCHECK(!is_shutdown_);
129 #ifdef LEAK_SANITIZER
130     // Invoke leak detection now, to avoid dealing with shutdown-only leaks.
131     // Normally this will have already happened in
132     // BroserProcessImpl::ReleaseModule(), so this call has no effect. This is
133     // only for processes which do not instantiate a BrowserProcess.
134     // If leaks are found, the process will exit here.
135     __lsan_do_leak_check();
136 #endif
137     // The shutdown tracing got enabled in AttemptUserExit earlier, but someone
138     // needs to write the result to disc. For that a dumper needs to get created
139     // which will dump the traces to disc when it gets destroyed.
140     const CommandLine& command_line = *CommandLine::ForCurrentProcess();
141     scoped_ptr<BrowserShutdownProfileDumper> profiler;
142     if (command_line.HasSwitch(switches::kTraceShutdown))
143       profiler.reset(new BrowserShutdownProfileDumper());
144
145     {
146       // The trace event has to stay between profiler creation and destruction.
147       TRACE_EVENT0("shutdown", "BrowserMainRunner");
148       g_exited_main_message_loop = true;
149
150       main_loop_->ShutdownThreadsAndCleanUp();
151
152       ui::ShutdownInputMethod();
153   #if defined(OS_WIN)
154       ole_initializer_.reset(NULL);
155   #endif
156
157       main_loop_.reset(NULL);
158
159       notification_service_.reset(NULL);
160
161       is_shutdown_ = true;
162     }
163   }
164
165  protected:
166   // True if we have started to initialize the runner.
167   bool initialization_started_;
168
169   // True if the runner has been shut down.
170   bool is_shutdown_;
171
172   scoped_ptr<NotificationServiceImpl> notification_service_;
173   scoped_ptr<BrowserMainLoop> main_loop_;
174 #if defined(OS_WIN)
175   scoped_ptr<ui::ScopedOleInitializer> ole_initializer_;
176 #endif
177
178   DISALLOW_COPY_AND_ASSIGN(BrowserMainRunnerImpl);
179 };
180
181 // static
182 BrowserMainRunner* BrowserMainRunner::Create() {
183   return new BrowserMainRunnerImpl();
184 }
185
186 }  // namespace content