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 "cc/base/switches.h"
11 #include "chromecast/base/metrics/cast_metrics_helper.h"
12 #include "chromecast/browser/cast_browser_context.h"
13 #include "chromecast/browser/cast_browser_process.h"
14 #include "chromecast/browser/devtools/remote_debugging_server.h"
15 #include "chromecast/browser/metrics/cast_metrics_prefs.h"
16 #include "chromecast/browser/metrics/cast_metrics_service_client.h"
17 #include "chromecast/browser/service/cast_service.h"
18 #include "chromecast/browser/url_request_context_factory.h"
19 #include "chromecast/browser/webui/webui_cast.h"
20 #include "chromecast/common/chromecast_config.h"
21 #include "chromecast/common/platform_client_auth.h"
22 #include "chromecast/net/network_change_notifier_cast.h"
23 #include "chromecast/net/network_change_notifier_factory_cast.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/common/content_switches.h"
26 #include "media/base/media_switches.h"
28 #if defined(OS_ANDROID)
29 #include "chromecast/crash/android/crash_handler.h"
30 #include "components/crash/browser/crash_dump_manager_android.h"
31 #include "net/android/network_change_notifier_factory_android.h"
32 #endif // defined(OS_ANDROID)
34 namespace chromecast {
39 struct DefaultCommandLineSwitch {
40 const char* const switch_name;
41 const char* const switch_value;
44 DefaultCommandLineSwitch g_default_switches[] = {
45 #if defined(OS_ANDROID)
46 { switches::kMediaDrmEnableNonCompositing, ""},
47 { switches::kEnableOverlayFullscreenVideo, ""},
48 { switches::kDisableInfobarForProtectedMediaIdentifier, ""},
49 { switches::kDisableGestureRequirementForMediaPlayback, ""},
50 { switches::kForceUseOverlayEmbeddedVideo, ""},
52 { switches::kDisableApplicationCache, "" },
53 { switches::kDisablePlugins, "" },
54 // Always enable HTMLMediaElement logs.
55 { switches::kBlinkPlatformLogChannels, "Media"},
56 #if defined(OS_LINUX) && defined(ARCH_CPU_X86_FAMILY)
57 // This is needed for now to enable the egltest Ozone platform to work with
58 // current Linux/NVidia OpenGL drivers.
59 { switches::kIgnoreGpuBlacklist, ""},
60 // TODO(gusfernandez): This is needed to fix a bug with
61 // glPostSubBufferCHROMIUM (crbug.com/429200)
62 { cc::switches::kUIDisablePartialSwap, ""},
64 { NULL, NULL }, // Termination
67 void AddDefaultCommandLineSwitches(CommandLine* command_line) {
69 while (g_default_switches[i].switch_name != NULL) {
70 command_line->AppendSwitchASCII(
71 std::string(g_default_switches[i].switch_name),
72 std::string(g_default_switches[i].switch_value));
79 CastBrowserMainParts::CastBrowserMainParts(
80 const content::MainFunctionParams& parameters,
81 URLRequestContextFactory* url_request_context_factory)
83 cast_browser_process_(new CastBrowserProcess()),
84 parameters_(parameters),
85 url_request_context_factory_(url_request_context_factory) {
86 CommandLine* command_line = CommandLine::ForCurrentProcess();
87 AddDefaultCommandLineSwitches(command_line);
90 CastBrowserMainParts::~CastBrowserMainParts() {
93 void CastBrowserMainParts::PreMainMessageLoopStart() {
94 #if defined(OS_ANDROID)
95 net::NetworkChangeNotifier::SetFactory(
96 new net::NetworkChangeNotifierFactoryAndroid());
98 net::NetworkChangeNotifier::SetFactory(
99 new NetworkChangeNotifierFactoryCast());
100 #endif // defined(OS_ANDROID)
103 void CastBrowserMainParts::PostMainMessageLoopStart() {
104 cast_browser_process_->SetMetricsHelper(
105 new metrics::CastMetricsHelper(base::MessageLoopProxy::current()));
107 #if defined(OS_ANDROID)
108 base::MessageLoopForUI::current()->Start();
109 #endif // defined(OS_ANDROID)
112 int CastBrowserMainParts::PreCreateThreads() {
113 PrefRegistrySimple* pref_registry = new PrefRegistrySimple();
114 metrics::RegisterPrefs(pref_registry);
115 ChromecastConfig::Create(pref_registry);
119 void CastBrowserMainParts::PreMainMessageLoopRun() {
120 url_request_context_factory_->InitializeOnUIThread();
122 cast_browser_process_->SetBrowserContext(
123 new CastBrowserContext(url_request_context_factory_));
124 cast_browser_process_->SetMetricsServiceClient(
125 metrics::CastMetricsServiceClient::Create(
126 content::BrowserThread::GetBlockingPool(),
127 ChromecastConfig::GetInstance()->pref_service(),
128 cast_browser_process_->browser_context()->GetRequestContext()));
130 #if defined(OS_ANDROID)
131 base::FilePath crash_dumps_dir;
132 if (!chromecast::CrashHandler::GetCrashDumpLocation(&crash_dumps_dir)) {
133 LOG(ERROR) << "Could not find crash dump location.";
135 cast_browser_process_->SetCrashDumpManager(
136 new breakpad::CrashDumpManager(crash_dumps_dir));
139 if (!PlatformClientAuth::Initialize()) {
140 LOG(ERROR) << "PlatformClientAuth::Initialize failed.";
143 cast_browser_process_->SetRemoteDebuggingServer(new RemoteDebuggingServer());
147 cast_browser_process_->SetCastService(CastService::Create(
148 cast_browser_process_->browser_context(),
149 url_request_context_factory_->GetSystemGetter(),
151 &metrics::CastMetricsServiceClient::EnableMetricsService,
152 base::Unretained(cast_browser_process_->metrics_service_client()))));
154 // Initializing network delegates must happen after Cast service is created.
155 url_request_context_factory_->InitializeNetworkDelegates();
156 cast_browser_process_->cast_service()->Start();
159 bool CastBrowserMainParts::MainMessageLoopRun(int* result_code) {
160 // If parameters_.ui_task is not NULL, we are running browser tests. In this
161 // case, the browser's main message loop will not run.
162 if (parameters_.ui_task) {
163 parameters_.ui_task->Run();
165 base::MessageLoopForUI::current()->Run();
170 void CastBrowserMainParts::PostMainMessageLoopRun() {
171 cast_browser_process_->cast_service()->Stop();
172 cast_browser_process_.reset();
176 } // namespace chromecast