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"
10 #include "base/command_line.h"
11 #include "base/files/file_util.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/path_service.h"
14 #include "base/prefs/pref_registry_simple.h"
15 #include "base/run_loop.h"
16 #include "cc/base/switches.h"
17 #include "chromecast/base/metrics/cast_metrics_helper.h"
18 #include "chromecast/base/metrics/grouped_histogram.h"
19 #include "chromecast/browser/cast_browser_context.h"
20 #include "chromecast/browser/cast_browser_process.h"
21 #include "chromecast/browser/devtools/remote_debugging_server.h"
22 #include "chromecast/browser/media/cast_browser_cdm_factory.h"
23 #include "chromecast/browser/metrics/cast_metrics_prefs.h"
24 #include "chromecast/browser/metrics/cast_metrics_service_client.h"
25 #include "chromecast/browser/pref_service_helper.h"
26 #include "chromecast/browser/service/cast_service.h"
27 #include "chromecast/browser/url_request_context_factory.h"
28 #include "chromecast/common/cast_paths.h"
29 #include "chromecast/common/chromecast_switches.h"
30 #include "chromecast/common/platform_client_auth.h"
31 #include "chromecast/net/connectivity_checker.h"
32 #include "content/public/browser/browser_thread.h"
33 #include "content/public/common/content_switches.h"
34 #include "media/base/browser_cdm_factory.h"
35 #include "media/base/media_switches.h"
37 #if defined(OS_ANDROID)
38 #include "chromecast/crash/android/crash_handler.h"
39 #include "components/crash/browser/crash_dump_manager_android.h"
40 #include "net/android/network_change_notifier_factory_android.h"
45 #if !defined(OS_ANDROID)
46 int kSignalsToRunClosure
[] = { SIGTERM
, SIGINT
, };
48 // Closure to run on SIGTERM and SIGINT.
49 base::Closure
* g_signal_closure
= NULL
;
51 void RunClosureOnSignal(int signum
) {
52 LOG(ERROR
) << "Got signal " << signum
;
53 DCHECK(g_signal_closure
);
54 // Expect main thread got this signal. Otherwise, weak_ptr of run_loop will
56 g_signal_closure
->Run();
59 void RegisterClosureOnSignal(const base::Closure
& closure
) {
60 DCHECK(!g_signal_closure
);
61 DCHECK_GT(arraysize(kSignalsToRunClosure
), 0U);
63 // Allow memory leak by intention.
64 g_signal_closure
= new base::Closure(closure
);
66 struct sigaction sa_new
;
67 memset(&sa_new
, 0, sizeof(sa_new
));
68 sa_new
.sa_handler
= RunClosureOnSignal
;
69 sigfillset(&sa_new
.sa_mask
);
70 sa_new
.sa_flags
= SA_RESTART
;
72 for (size_t i
= 0; i
< arraysize(kSignalsToRunClosure
); i
++) {
73 struct sigaction sa_old
;
74 if (sigaction(kSignalsToRunClosure
[i
], &sa_new
, &sa_old
) == -1) {
77 DCHECK_EQ(sa_old
.sa_handler
, SIG_DFL
);
81 // Get the first signal to exit when the parent process dies.
82 prctl(PR_SET_PDEATHSIG
, kSignalsToRunClosure
[0]);
84 #endif // !defined(OS_ANDROID)
88 namespace chromecast
{
93 struct DefaultCommandLineSwitch
{
94 const char* const switch_name
;
95 const char* const switch_value
;
98 DefaultCommandLineSwitch g_default_switches
[] = {
99 // TODO(ddorwin): Develop a permanent solution. See http://crbug.com/394926.
100 // For now, disable unprefixed EME because the video behavior not be
101 // consistent with other clients.
102 { switches::kDisableEncryptedMedia
, ""},
103 #if defined(OS_ANDROID)
104 // Disables Chromecast-specific WiFi-related features on ATV for now.
105 { switches::kNoWifi
, "" },
106 { switches::kMediaDrmEnableNonCompositing
, ""},
107 { switches::kEnableOverlayFullscreenVideo
, ""},
108 { switches::kDisableInfobarForProtectedMediaIdentifier
, ""},
109 { switches::kDisableGestureRequirementForMediaPlayback
, ""},
110 { switches::kForceUseOverlayEmbeddedVideo
, ""},
112 // Always enable HTMLMediaElement logs.
113 { switches::kBlinkPlatformLogChannels
, "Media"},
114 #if defined(DISABLE_DISPLAY)
115 { switches::kDisableGpu
, "" },
117 #if defined(OS_LINUX)
118 #if defined(ARCH_CPU_X86_FAMILY)
119 // This is needed for now to enable the egltest Ozone platform to work with
120 // current Linux/NVidia OpenGL drivers.
121 { switches::kIgnoreGpuBlacklist
, ""},
122 #elif defined(ARCH_CPU_ARM_FAMILY) && !defined(DISABLE_DISPLAY)
123 // On Linux arm, enable CMA pipeline by default.
124 { switches::kEnableCmaMediaPipeline
, "" },
126 #endif // defined(OS_LINUX)
127 // Needed to fix a bug where the raster thread doesn't get scheduled for a
128 // substantial time (~5 seconds). See https://crbug.com/441895.
129 { switches::kUseNormalPriorityForTileTaskWorkerThreads
, "" },
130 { NULL
, NULL
}, // Termination
133 void AddDefaultCommandLineSwitches(base::CommandLine
* command_line
) {
135 while (g_default_switches
[i
].switch_name
!= NULL
) {
136 command_line
->AppendSwitchASCII(
137 std::string(g_default_switches
[i
].switch_name
),
138 std::string(g_default_switches
[i
].switch_value
));
145 CastBrowserMainParts::CastBrowserMainParts(
146 const content::MainFunctionParams
& parameters
,
147 URLRequestContextFactory
* url_request_context_factory
)
148 : BrowserMainParts(),
149 cast_browser_process_(new CastBrowserProcess()),
150 parameters_(parameters
),
151 url_request_context_factory_(url_request_context_factory
) {
152 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
153 AddDefaultCommandLineSwitches(command_line
);
156 CastBrowserMainParts::~CastBrowserMainParts() {
159 void CastBrowserMainParts::PreMainMessageLoopStart() {
160 // GroupedHistograms needs to be initialized before any threads are created
161 // to prevent race conditions between calls to Preregister and those threads
162 // attempting to collect metrics.
163 // This call must also be before NetworkChangeNotifier, as it generates
165 metrics::PreregisterAllGroupedHistograms();
167 #if defined(OS_ANDROID)
168 net::NetworkChangeNotifier::SetFactory(
169 new net::NetworkChangeNotifierFactoryAndroid());
170 #endif // defined(OS_ANDROID)
173 void CastBrowserMainParts::PostMainMessageLoopStart() {
174 cast_browser_process_
->SetMetricsHelper(make_scoped_ptr(
175 new metrics::CastMetricsHelper(base::MessageLoopProxy::current())));
177 #if defined(OS_ANDROID)
178 base::MessageLoopForUI::current()->Start();
179 #endif // defined(OS_ANDROID)
182 int CastBrowserMainParts::PreCreateThreads() {
183 #if defined(OS_ANDROID)
184 // GPU process is started immediately after threads are created, requiring
185 // CrashDumpManager to be initialized beforehand.
186 base::FilePath crash_dumps_dir
;
187 if (!chromecast::CrashHandler::GetCrashDumpLocation(&crash_dumps_dir
)) {
188 LOG(ERROR
) << "Could not find crash dump location.";
190 cast_browser_process_
->SetCrashDumpManager(
191 make_scoped_ptr(new breakpad::CrashDumpManager(crash_dumps_dir
)));
193 base::FilePath home_dir
;
194 CHECK(PathService::Get(DIR_CAST_HOME
, &home_dir
));
195 if (!base::CreateDirectory(home_dir
))
201 void CastBrowserMainParts::PreMainMessageLoopRun() {
202 scoped_refptr
<PrefRegistrySimple
> pref_registry(new PrefRegistrySimple());
203 metrics::RegisterPrefs(pref_registry
.get());
204 cast_browser_process_
->SetPrefService(
205 PrefServiceHelper::CreatePrefService(pref_registry
.get()));
207 #if !defined(OS_ANDROID)
208 const base::CommandLine
* cmd_line
= base::CommandLine::ForCurrentProcess();
209 if (cmd_line
->HasSwitch(switches::kEnableCmaMediaPipeline
))
210 ::media::SetBrowserCdmFactory(new media::CastBrowserCdmFactory
);
211 #endif // !defined(OS_ANDROID)
213 cast_browser_process_
->SetConnectivityChecker(
214 make_scoped_refptr(new ConnectivityChecker(
215 content::BrowserThread::GetMessageLoopProxyForThread(
216 content::BrowserThread::FILE))));
218 url_request_context_factory_
->InitializeOnUIThread();
220 cast_browser_process_
->SetBrowserContext(
221 make_scoped_ptr(new CastBrowserContext(url_request_context_factory_
)));
222 cast_browser_process_
->SetMetricsServiceClient(
223 metrics::CastMetricsServiceClient::Create(
224 content::BrowserThread::GetBlockingPool(),
225 cast_browser_process_
->pref_service(),
226 cast_browser_process_
->browser_context()->GetRequestContext()));
228 if (!PlatformClientAuth::Initialize())
229 LOG(ERROR
) << "PlatformClientAuth::Initialize failed.";
231 cast_browser_process_
->SetRemoteDebuggingServer(
232 make_scoped_ptr(new RemoteDebuggingServer()));
234 cast_browser_process_
->SetCastService(CastService::Create(
235 cast_browser_process_
->browser_context(),
236 cast_browser_process_
->pref_service(),
237 cast_browser_process_
->metrics_service_client(),
238 url_request_context_factory_
->GetSystemGetter()));
239 cast_browser_process_
->cast_service()->Initialize();
241 // Initializing metrics service and network delegates must happen after cast
242 // service is intialized because CastMetricsServiceClient and
243 // CastNetworkDelegate may use components initialized by cast service.
244 cast_browser_process_
->metrics_service_client()
245 ->Initialize(cast_browser_process_
->cast_service());
246 url_request_context_factory_
->InitializeNetworkDelegates();
248 cast_browser_process_
->cast_service()->Start();
251 bool CastBrowserMainParts::MainMessageLoopRun(int* result_code
) {
252 #if defined(OS_ANDROID)
253 // Android does not use native main MessageLoop.
257 base::RunLoop run_loop
;
258 base::Closure
quit_closure(run_loop
.QuitClosure());
259 RegisterClosureOnSignal(quit_closure
);
261 // If parameters_.ui_task is not NULL, we are running browser tests.
262 if (parameters_
.ui_task
) {
263 base::MessageLoop
* message_loop
= base::MessageLoopForUI::current();
264 message_loop
->PostTask(FROM_HERE
, *parameters_
.ui_task
);
265 message_loop
->PostTask(FROM_HERE
, quit_closure
);
270 cast_browser_process_
->cast_service()->Stop();
275 void CastBrowserMainParts::PostMainMessageLoopRun() {
276 #if defined(OS_ANDROID)
277 // Android does not use native main MessageLoop.
280 cast_browser_process_
->cast_service()->Finalize();
281 cast_browser_process_
->metrics_service_client()->Finalize();
282 cast_browser_process_
.reset();
287 } // namespace chromecast