Clear webapp storage when site data is cleared
[chromium-blink-merge.git] / chromecast / browser / cast_browser_main_parts.cc
blob6345cede8ae56cc8aabdbc16a13d2617ba12ac8c
1 // Copyright 2014 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 "chromecast/browser/cast_browser_main_parts.h"
7 #include <string>
8 #if !defined(OS_ANDROID)
9 #include <signal.h>
10 #include <sys/prctl.h>
11 #endif
13 #include "base/command_line.h"
14 #include "base/files/file_util.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/path_service.h"
17 #include "base/prefs/pref_registry_simple.h"
18 #include "base/run_loop.h"
19 #include "base/thread_task_runner_handle.h"
20 #include "cc/base/switches.h"
21 #include "chromecast/base/cast_paths.h"
22 #include "chromecast/base/cast_sys_info_util.h"
23 #include "chromecast/base/chromecast_switches.h"
24 #include "chromecast/base/metrics/cast_metrics_helper.h"
25 #include "chromecast/base/metrics/grouped_histogram.h"
26 #include "chromecast/browser/cast_browser_context.h"
27 #include "chromecast/browser/cast_browser_process.h"
28 #include "chromecast/browser/cast_content_browser_client.h"
29 #include "chromecast/browser/cast_net_log.h"
30 #include "chromecast/browser/devtools/remote_debugging_server.h"
31 #include "chromecast/browser/metrics/cast_metrics_prefs.h"
32 #include "chromecast/browser/metrics/cast_metrics_service_client.h"
33 #include "chromecast/browser/pref_service_helper.h"
34 #include "chromecast/browser/url_request_context_factory.h"
35 #include "chromecast/common/platform_client_auth.h"
36 #include "chromecast/media/base/key_systems_common.h"
37 #include "chromecast/media/base/media_message_loop.h"
38 #include "chromecast/net/connectivity_checker.h"
39 #include "chromecast/public/cast_media_shlib.h"
40 #include "chromecast/public/cast_sys_info.h"
41 #include "chromecast/service/cast_service.h"
42 #include "content/public/browser/browser_thread.h"
43 #include "content/public/browser/gpu_data_manager.h"
44 #include "content/public/common/content_switches.h"
45 #include "media/audio/audio_manager.h"
46 #include "media/audio/audio_manager_factory.h"
47 #include "media/base/browser_cdm_factory.h"
48 #include "media/base/media.h"
49 #include "ui/compositor/compositor_switches.h"
51 #if defined(OS_ANDROID)
52 #include "chromecast/app/android/crash_handler.h"
53 #include "chromecast/browser/media/cast_media_client_android.h"
54 #include "components/crash/browser/crash_dump_manager_android.h"
55 #include "media/base/android/media_client_android.h"
56 #include "net/android/network_change_notifier_factory_android.h"
57 #else
58 #include "chromecast/net/network_change_notifier_factory_cast.h"
59 #endif
61 #if defined(USE_AURA)
62 #include "chromecast/graphics/cast_screen.h"
63 #include "ui/aura/env.h"
64 #include "ui/gfx/screen.h"
65 #endif
67 namespace {
69 #if !defined(OS_ANDROID)
70 int kSignalsToRunClosure[] = { SIGTERM, SIGINT, };
72 // Closure to run on SIGTERM and SIGINT.
73 base::Closure* g_signal_closure = NULL;
75 void RunClosureOnSignal(int signum) {
76 LOG(ERROR) << "Got signal " << signum;
77 DCHECK(g_signal_closure);
78 // Expect main thread got this signal. Otherwise, weak_ptr of run_loop will
79 // crash the process.
80 g_signal_closure->Run();
83 void RegisterClosureOnSignal(const base::Closure& closure) {
84 DCHECK(!g_signal_closure);
85 DCHECK_GT(arraysize(kSignalsToRunClosure), 0U);
87 // Allow memory leak by intention.
88 g_signal_closure = new base::Closure(closure);
90 struct sigaction sa_new;
91 memset(&sa_new, 0, sizeof(sa_new));
92 sa_new.sa_handler = RunClosureOnSignal;
93 sigfillset(&sa_new.sa_mask);
94 sa_new.sa_flags = SA_RESTART;
96 for (size_t i = 0; i < arraysize(kSignalsToRunClosure); i++) {
97 struct sigaction sa_old;
98 if (sigaction(kSignalsToRunClosure[i], &sa_new, &sa_old) == -1) {
99 NOTREACHED();
100 } else {
101 DCHECK_EQ(sa_old.sa_handler, SIG_DFL);
105 // Get the first signal to exit when the parent process dies.
106 prctl(PR_SET_PDEATHSIG, kSignalsToRunClosure[0]);
109 const int kKillOnAlarmTimeoutSec = 5; // 5 seconds
111 void KillOnAlarm(int signum) {
112 LOG(ERROR) << "Got alarm signal for termination: " << signum;
113 raise(SIGKILL);
116 void RegisterKillOnAlarm(int timeout_seconds) {
117 struct sigaction sa_new;
118 memset(&sa_new, 0, sizeof(sa_new));
119 sa_new.sa_handler = KillOnAlarm;
120 sigfillset(&sa_new.sa_mask);
121 sa_new.sa_flags = SA_RESTART;
123 struct sigaction sa_old;
124 if (sigaction(SIGALRM, &sa_new, &sa_old) == -1) {
125 NOTREACHED();
126 } else {
127 DCHECK_EQ(sa_old.sa_handler, SIG_DFL);
130 if (alarm(timeout_seconds) > 0)
131 NOTREACHED() << "Previous alarm() was cancelled";
134 void DeregisterKillOnAlarm() {
135 // Explicitly cancel any outstanding alarm() calls.
136 alarm(0);
138 struct sigaction sa_new;
139 memset(&sa_new, 0, sizeof(sa_new));
140 sa_new.sa_handler = SIG_DFL;
141 sigfillset(&sa_new.sa_mask);
142 sa_new.sa_flags = SA_RESTART;
144 struct sigaction sa_old;
145 if (sigaction(SIGALRM, &sa_new, &sa_old) == -1) {
146 NOTREACHED();
147 } else {
148 DCHECK_EQ(sa_old.sa_handler, KillOnAlarm);
151 #endif // !defined(OS_ANDROID)
153 } // namespace
155 namespace chromecast {
156 namespace shell {
158 namespace {
160 struct DefaultCommandLineSwitch {
161 const char* const switch_name;
162 const char* const switch_value;
165 DefaultCommandLineSwitch g_default_switches[] = {
166 #if defined(OS_ANDROID)
167 // Disables Chromecast-specific WiFi-related features on ATV for now.
168 { switches::kNoWifi, "" },
169 { switches::kDisableGestureRequirementForMediaPlayback, ""},
170 #endif
171 // Always enable HTMLMediaElement logs.
172 { switches::kBlinkPlatformLogChannels, "Media"},
173 #if defined(DISABLE_DISPLAY)
174 { switches::kDisableGpu, "" },
175 #endif
176 #if defined(OS_LINUX)
177 #if defined(ARCH_CPU_X86_FAMILY)
178 // This is needed for now to enable the egltest Ozone platform to work with
179 // current Linux/NVidia OpenGL drivers.
180 { switches::kIgnoreGpuBlacklist, ""},
181 #elif defined(ARCH_CPU_ARM_FAMILY)
182 // On Linux arm, enable CMA pipeline by default.
183 { switches::kEnableCmaMediaPipeline, "" },
184 #if !defined(DISABLE_DISPLAY)
185 { switches::kEnableHardwareOverlays, "" },
186 #endif
187 #endif
188 #endif // defined(OS_LINUX)
189 // Needed to fix a bug where the raster thread doesn't get scheduled for a
190 // substantial time (~5 seconds). See https://crbug.com/441895.
191 { switches::kUseNormalPriorityForTileTaskWorkerThreads, "" },
192 // Needed so that our call to GpuDataManager::SetGLStrings doesn't race
193 // against GPU process creation (which is otherwise triggered from
194 // BrowserThreadsStarted). The GPU process will be created as soon as a
195 // renderer needs it, which always happens after main loop starts.
196 { switches::kDisableGpuEarlyInit, "" },
197 { NULL, NULL }, // Termination
200 void AddDefaultCommandLineSwitches(base::CommandLine* command_line) {
201 int i = 0;
202 while (g_default_switches[i].switch_name != NULL) {
203 command_line->AppendSwitchASCII(
204 std::string(g_default_switches[i].switch_name),
205 std::string(g_default_switches[i].switch_value));
206 ++i;
210 } // namespace
212 CastBrowserMainParts::CastBrowserMainParts(
213 const content::MainFunctionParams& parameters,
214 URLRequestContextFactory* url_request_context_factory,
215 scoped_ptr<::media::AudioManagerFactory> audio_manager_factory)
216 : BrowserMainParts(),
217 cast_browser_process_(new CastBrowserProcess()),
218 parameters_(parameters),
219 url_request_context_factory_(url_request_context_factory),
220 audio_manager_factory_(audio_manager_factory.Pass()),
221 net_log_(new CastNetLog()) {
222 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
223 AddDefaultCommandLineSwitches(command_line);
226 CastBrowserMainParts::~CastBrowserMainParts() {
229 void CastBrowserMainParts::PreMainMessageLoopStart() {
230 // GroupedHistograms needs to be initialized before any threads are created
231 // to prevent race conditions between calls to Preregister and those threads
232 // attempting to collect metrics.
233 // This call must also be before NetworkChangeNotifier, as it generates
234 // Net/DNS metrics.
235 metrics::PreregisterAllGroupedHistograms();
237 // Set the platform's implementation of AudioManagerFactory.
238 if (audio_manager_factory_)
239 ::media::AudioManager::SetFactory(audio_manager_factory_.release());
241 #if defined(OS_ANDROID)
242 net::NetworkChangeNotifier::SetFactory(
243 new net::NetworkChangeNotifierFactoryAndroid());
244 #else
245 net::NetworkChangeNotifier::SetFactory(
246 new NetworkChangeNotifierFactoryCast());
247 #endif // defined(OS_ANDROID)
250 void CastBrowserMainParts::PostMainMessageLoopStart() {
251 cast_browser_process_->SetMetricsHelper(make_scoped_ptr(
252 new metrics::CastMetricsHelper(base::ThreadTaskRunnerHandle::Get())));
254 #if defined(OS_ANDROID)
255 base::MessageLoopForUI::current()->Start();
256 #endif // defined(OS_ANDROID)
259 int CastBrowserMainParts::PreCreateThreads() {
260 #if defined(OS_ANDROID)
261 // GPU process is started immediately after threads are created, requiring
262 // CrashDumpManager to be initialized beforehand.
263 base::FilePath crash_dumps_dir;
264 if (!chromecast::CrashHandler::GetCrashDumpLocation(&crash_dumps_dir)) {
265 LOG(ERROR) << "Could not find crash dump location.";
267 cast_browser_process_->SetCrashDumpManager(
268 make_scoped_ptr(new breakpad::CrashDumpManager(crash_dumps_dir)));
269 #else
270 base::FilePath home_dir;
271 CHECK(PathService::Get(DIR_CAST_HOME, &home_dir));
272 if (!base::CreateDirectory(home_dir))
273 return 1;
274 #endif
276 #if defined(USE_AURA)
277 // Screen can (and should) exist even with no displays connected. Its presence
278 // is assumed as an interface to access display information, e.g. from metrics
279 // code. See CastContentWindow::CreateWindowTree for update when resolution
280 // is available.
281 cast_browser_process_->SetCastScreen(make_scoped_ptr(new CastScreen));
282 DCHECK(!gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE));
283 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE,
284 cast_browser_process_->cast_screen());
285 #endif
286 return 0;
289 void CastBrowserMainParts::PreMainMessageLoopRun() {
290 #if !defined(OS_ANDROID)
291 // Set GL strings so GPU config code can make correct feature blacklisting/
292 // whitelisting decisions.
293 // Note: SetGLStrings MUST be called after GpuDataManager::Initialize.
294 scoped_ptr<CastSysInfo> sys_info = CreateSysInfo();
295 content::GpuDataManager::GetInstance()->SetGLStrings(
296 sys_info->GetGlVendor(), sys_info->GetGlRenderer(),
297 sys_info->GetGlVersion());
298 #endif // !defined(OS_ANDROID)
300 scoped_refptr<PrefRegistrySimple> pref_registry(new PrefRegistrySimple());
301 metrics::RegisterPrefs(pref_registry.get());
302 cast_browser_process_->SetPrefService(
303 PrefServiceHelper::CreatePrefService(pref_registry.get()));
305 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
306 #if defined(OS_ANDROID)
307 ::media::SetMediaClientAndroid(new media::CastMediaClientAndroid());
308 #else
309 if (cmd_line->HasSwitch(switches::kEnableCmaMediaPipeline)) {
310 scoped_ptr<::media::BrowserCdmFactory> cdm_factory =
311 cast_browser_process_->browser_client()->CreateBrowserCdmFactory();
312 ::media::SetBrowserCdmFactory(cdm_factory.release());
314 #endif // defined(OS_ANDROID)
316 cast_browser_process_->SetConnectivityChecker(
317 ConnectivityChecker::Create(
318 content::BrowserThread::GetMessageLoopProxyForThread(
319 content::BrowserThread::IO)));
321 cast_browser_process_->SetNetLog(net_log_.get());
323 url_request_context_factory_->InitializeOnUIThread(net_log_.get());
325 cast_browser_process_->SetBrowserContext(
326 make_scoped_ptr(new CastBrowserContext(url_request_context_factory_)));
327 cast_browser_process_->SetMetricsServiceClient(
328 metrics::CastMetricsServiceClient::Create(
329 content::BrowserThread::GetBlockingPool(),
330 cast_browser_process_->pref_service(),
331 cast_browser_process_->browser_context()->GetRequestContext()));
333 if (!PlatformClientAuth::Initialize())
334 LOG(ERROR) << "PlatformClientAuth::Initialize failed.";
336 cast_browser_process_->SetRemoteDebuggingServer(make_scoped_ptr(
337 new RemoteDebuggingServer(cast_browser_process_->browser_client()->
338 EnableRemoteDebuggingImmediately())));
340 media::MediaMessageLoop::GetTaskRunner()->PostTask(
341 FROM_HERE,
342 base::Bind(&media::CastMediaShlib::Initialize, cmd_line->argv()));
343 ::media::InitializeMediaLibrary();
345 cast_browser_process_->SetCastService(
346 cast_browser_process_->browser_client()->CreateCastService(
347 cast_browser_process_->browser_context(),
348 cast_browser_process_->pref_service(),
349 url_request_context_factory_->GetSystemGetter()));
350 cast_browser_process_->cast_service()->Initialize();
352 // Initializing metrics service and network delegates must happen after cast
353 // service is intialized because CastMetricsServiceClient and
354 // CastNetworkDelegate may use components initialized by cast service.
355 cast_browser_process_->metrics_service_client()
356 ->Initialize(cast_browser_process_->cast_service());
357 url_request_context_factory_->InitializeNetworkDelegates();
359 cast_browser_process_->cast_service()->Start();
362 bool CastBrowserMainParts::MainMessageLoopRun(int* result_code) {
363 #if defined(OS_ANDROID)
364 // Android does not use native main MessageLoop.
365 NOTREACHED();
366 return true;
367 #else
368 base::RunLoop run_loop;
369 base::Closure quit_closure(run_loop.QuitClosure());
370 RegisterClosureOnSignal(quit_closure);
372 // If parameters_.ui_task is not NULL, we are running browser tests.
373 if (parameters_.ui_task) {
374 base::MessageLoop* message_loop = base::MessageLoopForUI::current();
375 message_loop->PostTask(FROM_HERE, *parameters_.ui_task);
376 message_loop->PostTask(FROM_HERE, quit_closure);
379 run_loop.Run();
381 // Once the main loop has stopped running, we give the browser process a few
382 // seconds to stop cast service and finalize all resources. If a hang occurs
383 // and cast services refuse to terminate successfully, then we SIGKILL the
384 // current process to avoid indefinte hangs.
385 RegisterKillOnAlarm(kKillOnAlarmTimeoutSec);
387 cast_browser_process_->cast_service()->Stop();
388 return true;
389 #endif
392 void CastBrowserMainParts::PostMainMessageLoopRun() {
393 #if defined(OS_ANDROID)
394 // Android does not use native main MessageLoop.
395 NOTREACHED();
396 #else
397 cast_browser_process_->cast_service()->Finalize();
398 cast_browser_process_->metrics_service_client()->Finalize();
399 cast_browser_process_.reset();
401 #if defined(USE_AURA)
402 aura::Env::DeleteInstance();
403 #endif
405 DeregisterKillOnAlarm();
406 #endif
409 } // namespace shell
410 } // namespace chromecast