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/browser/cast_browser_context.h"
11 #include "chromecast/browser/cast_browser_process.h"
12 #include "chromecast/browser/devtools/remote_debugging_server.h"
13 #include "chromecast/browser/metrics/cast_metrics_prefs.h"
14 #include "chromecast/browser/metrics/cast_metrics_service_client.h"
15 #include "chromecast/browser/service/cast_service.h"
16 #include "chromecast/browser/url_request_context_factory.h"
17 #include "chromecast/browser/webui/webui_cast.h"
18 #include "chromecast/common/chromecast_config.h"
19 #include "chromecast/net/network_change_notifier_cast.h"
20 #include "chromecast/net/network_change_notifier_factory_cast.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/common/content_switches.h"
23 #include "media/base/media_switches.h"
25 #if defined(OS_ANDROID)
26 #include "chromecast/crash/android/crash_handler.h"
27 #include "components/crash/browser/crash_dump_manager_android.h"
28 #include "net/android/network_change_notifier_factory_android.h"
29 #endif // defined(OS_ANDROID)
31 namespace chromecast
{
36 struct DefaultCommandLineSwitch
{
37 const char* const switch_name
;
38 const char* const switch_value
;
41 DefaultCommandLineSwitch g_default_switches
[] = {
42 #if defined(OS_ANDROID)
43 { switches::kMediaDrmEnableNonCompositing
, ""},
44 { switches::kEnableOverlayFullscreenVideo
, ""},
45 { switches::kDisableInfobarForProtectedMediaIdentifier
, ""},
46 { switches::kDisableGestureRequirementForMediaPlayback
, ""},
48 { switches::kDisableApplicationCache
, "" },
49 { switches::kDisablePlugins
, "" },
50 // Always enable HTMLMediaElement logs.
51 { switches::kBlinkPlatformLogChannels
, "Media"},
52 { NULL
, NULL
}, // Termination
55 void AddDefaultCommandLineSwitches(CommandLine
* command_line
) {
57 while (g_default_switches
[i
].switch_name
!= NULL
) {
58 command_line
->AppendSwitchASCII(
59 std::string(g_default_switches
[i
].switch_name
),
60 std::string(g_default_switches
[i
].switch_value
));
67 CastBrowserMainParts::CastBrowserMainParts(
68 const content::MainFunctionParams
& parameters
,
69 URLRequestContextFactory
* url_request_context_factory
)
71 cast_browser_process_(new CastBrowserProcess()),
72 parameters_(parameters
),
73 url_request_context_factory_(url_request_context_factory
) {
74 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
75 AddDefaultCommandLineSwitches(command_line
);
78 CastBrowserMainParts::~CastBrowserMainParts() {
81 void CastBrowserMainParts::PreMainMessageLoopStart() {
82 #if defined(OS_ANDROID)
83 net::NetworkChangeNotifier::SetFactory(
84 new net::NetworkChangeNotifierFactoryAndroid());
86 net::NetworkChangeNotifier::SetFactory(
87 new NetworkChangeNotifierFactoryCast());
88 #endif // defined(OS_ANDROID)
91 void CastBrowserMainParts::PostMainMessageLoopStart() {
92 #if defined(OS_ANDROID)
93 base::MessageLoopForUI::current()->Start();
94 #endif // defined(OS_ANDROID)
97 int CastBrowserMainParts::PreCreateThreads() {
98 PrefRegistrySimple
* pref_registry
= new PrefRegistrySimple();
99 metrics::RegisterPrefs(pref_registry
);
100 ChromecastConfig::Create(pref_registry
);
104 void CastBrowserMainParts::PreMainMessageLoopRun() {
105 url_request_context_factory_
->InitializeOnUIThread();
107 cast_browser_process_
->SetBrowserContext(
108 new CastBrowserContext(url_request_context_factory_
));
109 cast_browser_process_
->SetMetricsServiceClient(
110 metrics::CastMetricsServiceClient::Create(
111 content::BrowserThread::GetBlockingPool(),
112 ChromecastConfig::GetInstance()->pref_service(),
113 cast_browser_process_
->browser_context()->GetRequestContext()));
115 #if defined(OS_ANDROID)
116 base::FilePath crash_dumps_dir
;
117 if (!chromecast::CrashHandler::GetCrashDumpLocation(&crash_dumps_dir
)) {
118 LOG(ERROR
) << "Could not find crash dump location.";
120 cast_browser_process_
->SetCrashDumpManager(
121 new breakpad::CrashDumpManager(crash_dumps_dir
));
124 cast_browser_process_
->SetRemoteDebuggingServer(new RemoteDebuggingServer());
128 cast_browser_process_
->SetCastService(CastService::Create(
129 cast_browser_process_
->browser_context(),
130 url_request_context_factory_
->GetSystemGetter(),
132 &metrics::CastMetricsServiceClient::EnableMetricsService
,
133 base::Unretained(cast_browser_process_
->metrics_service_client()))));
135 // Initializing network delegates must happen after Cast service is created.
136 url_request_context_factory_
->InitializeNetworkDelegates();
137 cast_browser_process_
->cast_service()->Start();
140 bool CastBrowserMainParts::MainMessageLoopRun(int* result_code
) {
141 // If parameters_.ui_task is not NULL, we are running browser tests. In this
142 // case, the browser's main message loop will not run.
143 if (parameters_
.ui_task
) {
144 parameters_
.ui_task
->Run();
146 base::MessageLoopForUI::current()->Run();
151 void CastBrowserMainParts::PostMainMessageLoopRun() {
152 cast_browser_process_
->cast_service()->Stop();
153 cast_browser_process_
.reset();
157 } // namespace chromecast