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 "base/command_line.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "chromecast/base/metrics/cast_metrics_helper.h"
11 #include "chromecast/browser/cast_browser_context.h"
12 #include "chromecast/browser/cast_browser_process.h"
13 #include "chromecast/browser/devtools/remote_debugging_server.h"
14 #include "chromecast/browser/metrics/cast_metrics_prefs.h"
15 #include "chromecast/browser/metrics/cast_metrics_service_client.h"
16 #include "chromecast/browser/service/cast_service.h"
17 #include "chromecast/browser/url_request_context_factory.h"
18 #include "chromecast/browser/webui/webui_cast.h"
19 #include "chromecast/common/chromecast_config.h"
20 #include "chromecast/net/network_change_notifier_cast.h"
21 #include "chromecast/net/network_change_notifier_factory_cast.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "content/public/common/content_switches.h"
24 #include "media/base/media_switches.h"
26 #if defined(OS_ANDROID)
27 #include "chromecast/crash/android/crash_handler.h"
28 #include "components/crash/browser/crash_dump_manager_android.h"
29 #include "net/android/network_change_notifier_factory_android.h"
30 #endif // defined(OS_ANDROID)
32 namespace chromecast
{
37 struct DefaultCommandLineSwitch
{
38 const char* const switch_name
;
39 const char* const switch_value
;
42 DefaultCommandLineSwitch g_default_switches
[] = {
43 #if defined(OS_ANDROID)
44 { switches::kMediaDrmEnableNonCompositing
, ""},
45 { switches::kEnableOverlayFullscreenVideo
, ""},
46 { switches::kDisableInfobarForProtectedMediaIdentifier
, ""},
47 { switches::kDisableGestureRequirementForMediaPlayback
, ""},
48 { switches::kForceUseOverlayEmbeddedVideo
, ""},
50 { switches::kDisableApplicationCache
, "" },
51 { switches::kDisablePlugins
, "" },
52 // Always enable HTMLMediaElement logs.
53 { switches::kBlinkPlatformLogChannels
, "Media"},
54 { NULL
, NULL
}, // Termination
57 void AddDefaultCommandLineSwitches(CommandLine
* command_line
) {
59 while (g_default_switches
[i
].switch_name
!= NULL
) {
60 command_line
->AppendSwitchASCII(
61 std::string(g_default_switches
[i
].switch_name
),
62 std::string(g_default_switches
[i
].switch_value
));
69 CastBrowserMainParts::CastBrowserMainParts(
70 const content::MainFunctionParams
& parameters
,
71 URLRequestContextFactory
* url_request_context_factory
)
73 cast_browser_process_(new CastBrowserProcess()),
74 parameters_(parameters
),
75 url_request_context_factory_(url_request_context_factory
) {
76 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
77 AddDefaultCommandLineSwitches(command_line
);
80 CastBrowserMainParts::~CastBrowserMainParts() {
83 void CastBrowserMainParts::PreMainMessageLoopStart() {
84 #if defined(OS_ANDROID)
85 net::NetworkChangeNotifier::SetFactory(
86 new net::NetworkChangeNotifierFactoryAndroid());
88 net::NetworkChangeNotifier::SetFactory(
89 new NetworkChangeNotifierFactoryCast());
90 #endif // defined(OS_ANDROID)
93 void CastBrowserMainParts::PostMainMessageLoopStart() {
94 cast_browser_process_
->SetMetricsHelper(
95 new metrics::CastMetricsHelper(base::MessageLoopProxy::current()));
97 #if defined(OS_ANDROID)
98 base::MessageLoopForUI::current()->Start();
99 #endif // defined(OS_ANDROID)
102 int CastBrowserMainParts::PreCreateThreads() {
103 PrefRegistrySimple
* pref_registry
= new PrefRegistrySimple();
104 metrics::RegisterPrefs(pref_registry
);
105 ChromecastConfig::Create(pref_registry
);
109 void CastBrowserMainParts::PreMainMessageLoopRun() {
110 url_request_context_factory_
->InitializeOnUIThread();
112 cast_browser_process_
->SetBrowserContext(
113 new CastBrowserContext(url_request_context_factory_
));
114 cast_browser_process_
->SetMetricsServiceClient(
115 metrics::CastMetricsServiceClient::Create(
116 content::BrowserThread::GetBlockingPool(),
117 ChromecastConfig::GetInstance()->pref_service(),
118 cast_browser_process_
->browser_context()->GetRequestContext()));
120 #if defined(OS_ANDROID)
121 base::FilePath crash_dumps_dir
;
122 if (!chromecast::CrashHandler::GetCrashDumpLocation(&crash_dumps_dir
)) {
123 LOG(ERROR
) << "Could not find crash dump location.";
125 cast_browser_process_
->SetCrashDumpManager(
126 new breakpad::CrashDumpManager(crash_dumps_dir
));
129 cast_browser_process_
->SetRemoteDebuggingServer(new RemoteDebuggingServer());
133 cast_browser_process_
->SetCastService(CastService::Create(
134 cast_browser_process_
->browser_context(),
135 url_request_context_factory_
->GetSystemGetter(),
137 &metrics::CastMetricsServiceClient::EnableMetricsService
,
138 base::Unretained(cast_browser_process_
->metrics_service_client()))));
140 // Initializing network delegates must happen after Cast service is created.
141 url_request_context_factory_
->InitializeNetworkDelegates();
142 cast_browser_process_
->cast_service()->Start();
145 bool CastBrowserMainParts::MainMessageLoopRun(int* result_code
) {
146 // If parameters_.ui_task is not NULL, we are running browser tests. In this
147 // case, the browser's main message loop will not run.
148 if (parameters_
.ui_task
) {
149 parameters_
.ui_task
->Run();
151 base::MessageLoopForUI::current()->Run();
156 void CastBrowserMainParts::PostMainMessageLoopRun() {
157 cast_browser_process_
->cast_service()->Stop();
158 cast_browser_process_
.reset();
162 } // namespace chromecast