Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / chromecast / browser / cast_browser_main_parts.cc
blobcdaaf0b078f534ee8f325529ee9c7cdb44b4de2e
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/net/connectivity_checker.h"
33 #include "content/public/browser/browser_thread.h"
34 #include "content/public/common/content_switches.h"
35 #include "media/base/browser_cdm_factory.h"
36 #include "media/base/media_switches.h"
38 #if defined(OS_ANDROID)
39 #include "chromecast/crash/android/crash_handler.h"
40 #include "components/crash/browser/crash_dump_manager_android.h"
41 #include "net/android/network_change_notifier_factory_android.h"
42 #else
43 #include "chromecast/browser/media/cast_browser_cdm_factory.h"
44 #endif
46 #if defined(USE_AURA)
47 #include "ui/aura/test/test_screen.h"
48 #include "ui/gfx/screen.h"
49 #endif
51 namespace {
53 #if !defined(OS_ANDROID)
54 int kSignalsToRunClosure[] = { SIGTERM, SIGINT, };
56 // Closure to run on SIGTERM and SIGINT.
57 base::Closure* g_signal_closure = NULL;
59 void RunClosureOnSignal(int signum) {
60 LOG(ERROR) << "Got signal " << signum;
61 DCHECK(g_signal_closure);
62 // Expect main thread got this signal. Otherwise, weak_ptr of run_loop will
63 // crash the process.
64 g_signal_closure->Run();
67 void RegisterClosureOnSignal(const base::Closure& closure) {
68 DCHECK(!g_signal_closure);
69 DCHECK_GT(arraysize(kSignalsToRunClosure), 0U);
71 // Allow memory leak by intention.
72 g_signal_closure = new base::Closure(closure);
74 struct sigaction sa_new;
75 memset(&sa_new, 0, sizeof(sa_new));
76 sa_new.sa_handler = RunClosureOnSignal;
77 sigfillset(&sa_new.sa_mask);
78 sa_new.sa_flags = SA_RESTART;
80 for (size_t i = 0; i < arraysize(kSignalsToRunClosure); i++) {
81 struct sigaction sa_old;
82 if (sigaction(kSignalsToRunClosure[i], &sa_new, &sa_old) == -1) {
83 NOTREACHED();
84 } else {
85 DCHECK_EQ(sa_old.sa_handler, SIG_DFL);
89 // Get the first signal to exit when the parent process dies.
90 prctl(PR_SET_PDEATHSIG, kSignalsToRunClosure[0]);
93 const int kKillOnAlarmTimeoutSec = 5; // 5 seconds
95 void KillOnAlarm(int signum) {
96 LOG(ERROR) << "Got alarm signal for termination: " << signum;
97 raise(SIGKILL);
100 void RegisterKillOnAlarm(int timeout_seconds) {
101 struct sigaction sa_new;
102 memset(&sa_new, 0, sizeof(sa_new));
103 sa_new.sa_handler = KillOnAlarm;
104 sigfillset(&sa_new.sa_mask);
105 sa_new.sa_flags = SA_RESTART;
107 struct sigaction sa_old;
108 if (sigaction(SIGALRM, &sa_new, &sa_old) == -1) {
109 NOTREACHED();
110 } else {
111 DCHECK_EQ(sa_old.sa_handler, SIG_DFL);
114 if (alarm(timeout_seconds) > 0)
115 NOTREACHED() << "Previous alarm() was cancelled";
118 void DeregisterKillOnAlarm() {
119 // Explicitly cancel any outstanding alarm() calls.
120 alarm(0);
122 struct sigaction sa_new;
123 memset(&sa_new, 0, sizeof(sa_new));
124 sa_new.sa_handler = SIG_DFL;
125 sigfillset(&sa_new.sa_mask);
126 sa_new.sa_flags = SA_RESTART;
128 struct sigaction sa_old;
129 if (sigaction(SIGALRM, &sa_new, &sa_old) == -1) {
130 NOTREACHED();
131 } else {
132 DCHECK_EQ(sa_old.sa_handler, KillOnAlarm);
135 #endif // !defined(OS_ANDROID)
137 } // namespace
139 namespace chromecast {
140 namespace shell {
142 namespace {
144 struct DefaultCommandLineSwitch {
145 const char* const switch_name;
146 const char* const switch_value;
149 DefaultCommandLineSwitch g_default_switches[] = {
150 // TODO(gunsch): Enable unprefixed EME. See http://crbug.com/471936.
151 { switches::kDisableEncryptedMedia, ""},
152 #if defined(OS_ANDROID)
153 // Disables Chromecast-specific WiFi-related features on ATV for now.
154 { switches::kNoWifi, "" },
155 { switches::kEnableOverlayFullscreenVideo, ""},
156 { switches::kDisableInfobarForProtectedMediaIdentifier, ""},
157 { switches::kDisableGestureRequirementForMediaPlayback, ""},
158 #endif
159 // Always enable HTMLMediaElement logs.
160 { switches::kBlinkPlatformLogChannels, "Media"},
161 #if defined(DISABLE_DISPLAY)
162 { switches::kDisableGpu, "" },
163 #endif
164 #if defined(OS_LINUX)
165 #if defined(ARCH_CPU_X86_FAMILY)
166 // This is needed for now to enable the egltest Ozone platform to work with
167 // current Linux/NVidia OpenGL drivers.
168 { switches::kIgnoreGpuBlacklist, ""},
169 #elif defined(ARCH_CPU_ARM_FAMILY) && !defined(DISABLE_DISPLAY)
170 // On Linux arm, enable CMA pipeline by default.
171 { switches::kEnableCmaMediaPipeline, "" },
172 #endif
173 #endif // defined(OS_LINUX)
174 // Needed to fix a bug where the raster thread doesn't get scheduled for a
175 // substantial time (~5 seconds). See https://crbug.com/441895.
176 { switches::kUseNormalPriorityForTileTaskWorkerThreads, "" },
177 { NULL, NULL }, // Termination
180 void AddDefaultCommandLineSwitches(base::CommandLine* command_line) {
181 int i = 0;
182 while (g_default_switches[i].switch_name != NULL) {
183 command_line->AppendSwitchASCII(
184 std::string(g_default_switches[i].switch_name),
185 std::string(g_default_switches[i].switch_value));
186 ++i;
190 } // namespace
192 CastBrowserMainParts::CastBrowserMainParts(
193 const content::MainFunctionParams& parameters,
194 URLRequestContextFactory* url_request_context_factory)
195 : BrowserMainParts(),
196 cast_browser_process_(new CastBrowserProcess()),
197 parameters_(parameters),
198 url_request_context_factory_(url_request_context_factory) {
199 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
200 AddDefaultCommandLineSwitches(command_line);
203 CastBrowserMainParts::~CastBrowserMainParts() {
206 void CastBrowserMainParts::PreMainMessageLoopStart() {
207 // GroupedHistograms needs to be initialized before any threads are created
208 // to prevent race conditions between calls to Preregister and those threads
209 // attempting to collect metrics.
210 // This call must also be before NetworkChangeNotifier, as it generates
211 // Net/DNS metrics.
212 metrics::PreregisterAllGroupedHistograms();
214 #if defined(OS_ANDROID)
215 net::NetworkChangeNotifier::SetFactory(
216 new net::NetworkChangeNotifierFactoryAndroid());
217 #endif // defined(OS_ANDROID)
220 void CastBrowserMainParts::PostMainMessageLoopStart() {
221 cast_browser_process_->SetMetricsHelper(make_scoped_ptr(
222 new metrics::CastMetricsHelper(base::MessageLoopProxy::current())));
224 #if defined(OS_ANDROID)
225 base::MessageLoopForUI::current()->Start();
226 #endif // defined(OS_ANDROID)
229 int CastBrowserMainParts::PreCreateThreads() {
230 #if defined(OS_ANDROID)
231 // GPU process is started immediately after threads are created, requiring
232 // CrashDumpManager to be initialized beforehand.
233 base::FilePath crash_dumps_dir;
234 if (!chromecast::CrashHandler::GetCrashDumpLocation(&crash_dumps_dir)) {
235 LOG(ERROR) << "Could not find crash dump location.";
237 cast_browser_process_->SetCrashDumpManager(
238 make_scoped_ptr(new breakpad::CrashDumpManager(crash_dumps_dir)));
239 #else
240 base::FilePath home_dir;
241 CHECK(PathService::Get(DIR_CAST_HOME, &home_dir));
242 if (!base::CreateDirectory(home_dir))
243 return 1;
244 #endif
246 #if defined(USE_AURA)
247 // Screen can (and should) exist even with no displays connected. Its presence
248 // is assumed as an interface to access display information, e.g. from metrics
249 // code. See CastContentWindow::CreateWindowTree for update when resolution
250 // is available.
251 DCHECK(!gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_NATIVE));
252 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE,
253 aura::TestScreen::Create(gfx::Size(0, 0)));
254 #endif
255 return 0;
258 void CastBrowserMainParts::PreMainMessageLoopRun() {
259 scoped_refptr<PrefRegistrySimple> pref_registry(new PrefRegistrySimple());
260 metrics::RegisterPrefs(pref_registry.get());
261 cast_browser_process_->SetPrefService(
262 PrefServiceHelper::CreatePrefService(pref_registry.get()));
264 #if !defined(OS_ANDROID)
265 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
266 if (cmd_line->HasSwitch(switches::kEnableCmaMediaPipeline))
267 ::media::SetBrowserCdmFactory(new media::CastBrowserCdmFactory);
268 #endif // !defined(OS_ANDROID)
270 cast_browser_process_->SetConnectivityChecker(
271 make_scoped_refptr(new ConnectivityChecker(
272 content::BrowserThread::GetMessageLoopProxyForThread(
273 content::BrowserThread::FILE))));
275 url_request_context_factory_->InitializeOnUIThread();
277 cast_browser_process_->SetBrowserContext(
278 make_scoped_ptr(new CastBrowserContext(url_request_context_factory_)));
279 cast_browser_process_->SetMetricsServiceClient(
280 metrics::CastMetricsServiceClient::Create(
281 content::BrowserThread::GetBlockingPool(),
282 cast_browser_process_->pref_service(),
283 cast_browser_process_->browser_context()->GetRequestContext()));
285 if (!PlatformClientAuth::Initialize())
286 LOG(ERROR) << "PlatformClientAuth::Initialize failed.";
288 cast_browser_process_->SetRemoteDebuggingServer(
289 make_scoped_ptr(new RemoteDebuggingServer()));
291 cast_browser_process_->SetCastService(CastService::Create(
292 cast_browser_process_->browser_context(),
293 cast_browser_process_->pref_service(),
294 cast_browser_process_->metrics_service_client(),
295 url_request_context_factory_->GetSystemGetter()));
296 cast_browser_process_->cast_service()->Initialize();
298 // Initializing metrics service and network delegates must happen after cast
299 // service is intialized because CastMetricsServiceClient and
300 // CastNetworkDelegate may use components initialized by cast service.
301 cast_browser_process_->metrics_service_client()
302 ->Initialize(cast_browser_process_->cast_service());
303 url_request_context_factory_->InitializeNetworkDelegates();
305 cast_browser_process_->cast_service()->Start();
308 bool CastBrowserMainParts::MainMessageLoopRun(int* result_code) {
309 #if defined(OS_ANDROID)
310 // Android does not use native main MessageLoop.
311 NOTREACHED();
312 return true;
313 #else
314 base::RunLoop run_loop;
315 base::Closure quit_closure(run_loop.QuitClosure());
316 RegisterClosureOnSignal(quit_closure);
318 // If parameters_.ui_task is not NULL, we are running browser tests.
319 if (parameters_.ui_task) {
320 base::MessageLoop* message_loop = base::MessageLoopForUI::current();
321 message_loop->PostTask(FROM_HERE, *parameters_.ui_task);
322 message_loop->PostTask(FROM_HERE, quit_closure);
325 run_loop.Run();
327 // Once the main loop has stopped running, we give the browser process a few
328 // seconds to stop cast service and finalize all resources. If a hang occurs
329 // and cast services refuse to terminate successfully, then we SIGKILL the
330 // current process to avoid indefinte hangs.
331 RegisterKillOnAlarm(kKillOnAlarmTimeoutSec);
333 cast_browser_process_->cast_service()->Stop();
334 return true;
335 #endif
338 void CastBrowserMainParts::PostMainMessageLoopRun() {
339 #if defined(OS_ANDROID)
340 // Android does not use native main MessageLoop.
341 NOTREACHED();
342 #else
343 cast_browser_process_->cast_service()->Finalize();
344 cast_browser_process_->metrics_service_client()->Finalize();
345 cast_browser_process_.reset();
346 DeregisterKillOnAlarm();
347 #endif
350 } // namespace shell
351 } // namespace chromecast