Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / chromecast / browser / cast_browser_main_parts.cc
blob1c0ca15f666b4b12fd6f83723ce5ef0a60597811
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 #if !defined(OS_ANDROID)
8 #include <signal.h>
9 #include <sys/prctl.h>
10 #endif
12 #include "base/command_line.h"
13 #include "base/files/file_util.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/path_service.h"
16 #include "base/prefs/pref_registry_simple.h"
17 #include "base/run_loop.h"
18 #include "cc/base/switches.h"
19 #include "chromecast/base/cast_paths.h"
20 #include "chromecast/base/metrics/cast_metrics_helper.h"
21 #include "chromecast/base/metrics/grouped_histogram.h"
22 #include "chromecast/browser/cast_browser_context.h"
23 #include "chromecast/browser/cast_browser_process.h"
24 #include "chromecast/browser/devtools/remote_debugging_server.h"
25 #include "chromecast/browser/metrics/cast_metrics_prefs.h"
26 #include "chromecast/browser/metrics/cast_metrics_service_client.h"
27 #include "chromecast/browser/pref_service_helper.h"
28 #include "chromecast/browser/service/cast_service.h"
29 #include "chromecast/browser/url_request_context_factory.h"
30 #include "chromecast/common/chromecast_switches.h"
31 #include "chromecast/common/platform_client_auth.h"
32 #include "chromecast/media/base/key_systems_common.h"
33 #include "chromecast/net/connectivity_checker.h"
34 #include "content/public/browser/browser_thread.h"
35 #include "content/public/common/content_switches.h"
36 #include "media/base/browser_cdm_factory.h"
37 #include "media/base/media_switches.h"
39 #if defined(OS_ANDROID)
40 #include "chromecast/browser/media/cast_media_client_android.h"
41 #include "chromecast/crash/android/crash_handler.h"
42 #include "components/crash/browser/crash_dump_manager_android.h"
43 #include "media/base/android/media_client_android.h"
44 #include "net/android/network_change_notifier_factory_android.h"
45 #else
46 #include "chromecast/browser/media/cast_browser_cdm_factory.h"
47 #endif
49 #if defined(USE_AURA)
50 #include "ui/aura/test/test_screen.h"
51 #include "ui/gfx/screen.h"
52 #endif
54 namespace {
56 #if !defined(OS_ANDROID)
57 int kSignalsToRunClosure[] = { SIGTERM, SIGINT, };
59 // Closure to run on SIGTERM and SIGINT.
60 base::Closure* g_signal_closure = NULL;
62 void RunClosureOnSignal(int signum) {
63 LOG(ERROR) << "Got signal " << signum;
64 DCHECK(g_signal_closure);
65 // Expect main thread got this signal. Otherwise, weak_ptr of run_loop will
66 // crash the process.
67 g_signal_closure->Run();
70 void RegisterClosureOnSignal(const base::Closure& closure) {
71 DCHECK(!g_signal_closure);
72 DCHECK_GT(arraysize(kSignalsToRunClosure), 0U);
74 // Allow memory leak by intention.
75 g_signal_closure = new base::Closure(closure);
77 struct sigaction sa_new;
78 memset(&sa_new, 0, sizeof(sa_new));
79 sa_new.sa_handler = RunClosureOnSignal;
80 sigfillset(&sa_new.sa_mask);
81 sa_new.sa_flags = SA_RESTART;
83 for (size_t i = 0; i < arraysize(kSignalsToRunClosure); i++) {
84 struct sigaction sa_old;
85 if (sigaction(kSignalsToRunClosure[i], &sa_new, &sa_old) == -1) {
86 NOTREACHED();
87 } else {
88 DCHECK_EQ(sa_old.sa_handler, SIG_DFL);
92 // Get the first signal to exit when the parent process dies.
93 prctl(PR_SET_PDEATHSIG, kSignalsToRunClosure[0]);
96 const int kKillOnAlarmTimeoutSec = 5; // 5 seconds
98 void KillOnAlarm(int signum) {
99 LOG(ERROR) << "Got alarm signal for termination: " << signum;
100 raise(SIGKILL);
103 void RegisterKillOnAlarm(int timeout_seconds) {
104 struct sigaction sa_new;
105 memset(&sa_new, 0, sizeof(sa_new));
106 sa_new.sa_handler = KillOnAlarm;
107 sigfillset(&sa_new.sa_mask);
108 sa_new.sa_flags = SA_RESTART;
110 struct sigaction sa_old;
111 if (sigaction(SIGALRM, &sa_new, &sa_old) == -1) {
112 NOTREACHED();
113 } else {
114 DCHECK_EQ(sa_old.sa_handler, SIG_DFL);
117 if (alarm(timeout_seconds) > 0)
118 NOTREACHED() << "Previous alarm() was cancelled";
121 void DeregisterKillOnAlarm() {
122 // Explicitly cancel any outstanding alarm() calls.
123 alarm(0);
125 struct sigaction sa_new;
126 memset(&sa_new, 0, sizeof(sa_new));
127 sa_new.sa_handler = SIG_DFL;
128 sigfillset(&sa_new.sa_mask);
129 sa_new.sa_flags = SA_RESTART;
131 struct sigaction sa_old;
132 if (sigaction(SIGALRM, &sa_new, &sa_old) == -1) {
133 NOTREACHED();
134 } else {
135 DCHECK_EQ(sa_old.sa_handler, KillOnAlarm);
138 #endif // !defined(OS_ANDROID)
140 } // namespace
142 namespace chromecast {
143 namespace shell {
145 namespace {
147 struct DefaultCommandLineSwitch {
148 const char* const switch_name;
149 const char* const switch_value;
152 DefaultCommandLineSwitch g_default_switches[] = {
153 #if defined(OS_ANDROID)
154 // Disables Chromecast-specific WiFi-related features on ATV for now.
155 { switches::kNoWifi, "" },
156 { switches::kEnableOverlayFullscreenVideo, ""},
157 { switches::kDisableInfobarForProtectedMediaIdentifier, ""},
158 { switches::kDisableGestureRequirementForMediaPlayback, ""},
159 #endif
160 // Always enable HTMLMediaElement logs.
161 { switches::kBlinkPlatformLogChannels, "Media"},
162 #if defined(DISABLE_DISPLAY)
163 { switches::kDisableGpu, "" },
164 #endif
165 #if defined(OS_LINUX)
166 #if defined(ARCH_CPU_X86_FAMILY)
167 // This is needed for now to enable the egltest Ozone platform to work with
168 // current Linux/NVidia OpenGL drivers.
169 { switches::kIgnoreGpuBlacklist, ""},
170 #elif defined(ARCH_CPU_ARM_FAMILY) && !defined(DISABLE_DISPLAY)
171 // On Linux arm, enable CMA pipeline by default.
172 { switches::kEnableCmaMediaPipeline, "" },
173 #endif
174 #endif // defined(OS_LINUX)
175 // Needed to fix a bug where the raster thread doesn't get scheduled for a
176 // substantial time (~5 seconds). See https://crbug.com/441895.
177 { switches::kUseNormalPriorityForTileTaskWorkerThreads, "" },
178 { NULL, NULL }, // Termination
181 void AddDefaultCommandLineSwitches(base::CommandLine* command_line) {
182 int i = 0;
183 while (g_default_switches[i].switch_name != NULL) {
184 command_line->AppendSwitchASCII(
185 std::string(g_default_switches[i].switch_name),
186 std::string(g_default_switches[i].switch_value));
187 ++i;
191 } // namespace
193 CastBrowserMainParts::CastBrowserMainParts(
194 const content::MainFunctionParams& parameters,
195 URLRequestContextFactory* url_request_context_factory)
196 : BrowserMainParts(),
197 cast_browser_process_(new CastBrowserProcess()),
198 parameters_(parameters),
199 url_request_context_factory_(url_request_context_factory) {
200 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
201 AddDefaultCommandLineSwitches(command_line);
204 CastBrowserMainParts::~CastBrowserMainParts() {
207 void CastBrowserMainParts::PreMainMessageLoopStart() {
208 // GroupedHistograms needs to be initialized before any threads are created
209 // to prevent race conditions between calls to Preregister and those threads
210 // attempting to collect metrics.
211 // This call must also be before NetworkChangeNotifier, as it generates
212 // Net/DNS metrics.
213 metrics::PreregisterAllGroupedHistograms();
215 #if defined(OS_ANDROID)
216 net::NetworkChangeNotifier::SetFactory(
217 new net::NetworkChangeNotifierFactoryAndroid());
218 #endif // defined(OS_ANDROID)
221 void CastBrowserMainParts::PostMainMessageLoopStart() {
222 cast_browser_process_->SetMetricsHelper(make_scoped_ptr(
223 new metrics::CastMetricsHelper(base::MessageLoopProxy::current())));
225 #if defined(OS_ANDROID)
226 base::MessageLoopForUI::current()->Start();
227 #endif // defined(OS_ANDROID)
230 int CastBrowserMainParts::PreCreateThreads() {
231 #if defined(OS_ANDROID)
232 // GPU process is started immediately after threads are created, requiring
233 // CrashDumpManager to be initialized beforehand.
234 base::FilePath crash_dumps_dir;
235 if (!chromecast::CrashHandler::GetCrashDumpLocation(&crash_dumps_dir)) {
236 LOG(ERROR) << "Could not find crash dump location.";
238 cast_browser_process_->SetCrashDumpManager(
239 make_scoped_ptr(new breakpad::CrashDumpManager(crash_dumps_dir)));
240 #else
241 base::FilePath home_dir;
242 CHECK(PathService::Get(DIR_CAST_HOME, &home_dir));
243 if (!base::CreateDirectory(home_dir))
244 return 1;
245 #endif
247 #if defined(USE_AURA)
248 // Screen can (and should) exist even with no displays connected. Its presence
249 // is assumed as an interface to access display information, e.g. from metrics
250 // code. See CastContentWindow::CreateWindowTree for update when resolution
251 // is available.
252 DCHECK(!gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE));
253 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE,
254 aura::TestScreen::Create(gfx::Size(0, 0)));
255 #endif
256 return 0;
259 void CastBrowserMainParts::PreMainMessageLoopRun() {
260 scoped_refptr<PrefRegistrySimple> pref_registry(new PrefRegistrySimple());
261 metrics::RegisterPrefs(pref_registry.get());
262 cast_browser_process_->SetPrefService(
263 PrefServiceHelper::CreatePrefService(pref_registry.get()));
265 #if defined(OS_ANDROID)
266 ::media::SetMediaClientAndroid(new media::CastMediaClientAndroid());
267 #else
268 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
269 if (cmd_line->HasSwitch(switches::kEnableCmaMediaPipeline))
270 ::media::SetBrowserCdmFactory(new media::CastBrowserCdmFactory());
271 #endif // defined(OS_ANDROID)
273 cast_browser_process_->SetConnectivityChecker(
274 make_scoped_refptr(new ConnectivityChecker(
275 content::BrowserThread::GetMessageLoopProxyForThread(
276 content::BrowserThread::IO))));
278 url_request_context_factory_->InitializeOnUIThread();
280 cast_browser_process_->SetBrowserContext(
281 make_scoped_ptr(new CastBrowserContext(url_request_context_factory_)));
282 cast_browser_process_->SetMetricsServiceClient(
283 metrics::CastMetricsServiceClient::Create(
284 content::BrowserThread::GetBlockingPool(),
285 cast_browser_process_->pref_service(),
286 cast_browser_process_->browser_context()->GetRequestContext()));
288 if (!PlatformClientAuth::Initialize())
289 LOG(ERROR) << "PlatformClientAuth::Initialize failed.";
291 cast_browser_process_->SetRemoteDebuggingServer(
292 make_scoped_ptr(new RemoteDebuggingServer()));
294 cast_browser_process_->SetCastService(CastService::Create(
295 cast_browser_process_->browser_context(),
296 cast_browser_process_->pref_service(),
297 cast_browser_process_->metrics_service_client(),
298 url_request_context_factory_->GetSystemGetter()));
299 cast_browser_process_->cast_service()->Initialize();
301 // Initializing metrics service and network delegates must happen after cast
302 // service is intialized because CastMetricsServiceClient and
303 // CastNetworkDelegate may use components initialized by cast service.
304 cast_browser_process_->metrics_service_client()
305 ->Initialize(cast_browser_process_->cast_service());
306 url_request_context_factory_->InitializeNetworkDelegates();
308 cast_browser_process_->cast_service()->Start();
311 bool CastBrowserMainParts::MainMessageLoopRun(int* result_code) {
312 #if defined(OS_ANDROID)
313 // Android does not use native main MessageLoop.
314 NOTREACHED();
315 return true;
316 #else
317 base::RunLoop run_loop;
318 base::Closure quit_closure(run_loop.QuitClosure());
319 RegisterClosureOnSignal(quit_closure);
321 // If parameters_.ui_task is not NULL, we are running browser tests.
322 if (parameters_.ui_task) {
323 base::MessageLoop* message_loop = base::MessageLoopForUI::current();
324 message_loop->PostTask(FROM_HERE, *parameters_.ui_task);
325 message_loop->PostTask(FROM_HERE, quit_closure);
328 run_loop.Run();
330 // Once the main loop has stopped running, we give the browser process a few
331 // seconds to stop cast service and finalize all resources. If a hang occurs
332 // and cast services refuse to terminate successfully, then we SIGKILL the
333 // current process to avoid indefinte hangs.
334 RegisterKillOnAlarm(kKillOnAlarmTimeoutSec);
336 cast_browser_process_->cast_service()->Stop();
337 return true;
338 #endif
341 void CastBrowserMainParts::PostMainMessageLoopRun() {
342 #if defined(OS_ANDROID)
343 // Android does not use native main MessageLoop.
344 NOTREACHED();
345 #else
346 cast_browser_process_->cast_service()->Finalize();
347 cast_browser_process_->metrics_service_client()->Finalize();
348 cast_browser_process_.reset();
349 DeregisterKillOnAlarm();
350 #endif
353 } // namespace shell
354 } // namespace chromecast