OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chromecast/shell/browser/cast_browser_main_parts.h" | |
6 | |
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/common/chromecast_config.h" | |
11 #include "chromecast/metrics/cast_metrics_prefs.h" | |
12 #include "chromecast/metrics/cast_metrics_service_client.h" | |
13 #include "chromecast/net/network_change_notifier_cast.h" | |
14 #include "chromecast/net/network_change_notifier_factory_cast.h" | |
15 #include "chromecast/service/cast_service.h" | |
16 #include "chromecast/shell/browser/cast_browser_context.h" | |
17 #include "chromecast/shell/browser/cast_browser_process.h" | |
18 #include "chromecast/shell/browser/devtools/remote_debugging_server.h" | |
19 #include "chromecast/shell/browser/url_request_context_factory.h" | |
20 #include "chromecast/shell/browser/webui/webui_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" | |
24 | |
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) | |
30 | |
31 namespace chromecast { | |
32 namespace shell { | |
33 | |
34 namespace { | |
35 | |
36 struct DefaultCommandLineSwitch { | |
37 const char* const switch_name; | |
38 const char* const switch_value; | |
39 }; | |
40 | |
41 DefaultCommandLineSwitch g_default_switches[] = { | |
42 #if defined(OS_ANDROID) | |
43 { switches::kMediaDrmEnableNonCompositing, ""}, | |
44 { switches::kEnableOverlayFullscreenVideo, ""}, | |
45 { switches::kDisableInfobarForProtectedMediaIdentifier, ""}, | |
46 { switches::kDisableGestureRequirementForMediaPlayback, ""}, | |
47 #endif | |
48 { switches::kDisableApplicationCache, "" }, | |
49 { switches::kDisablePlugins, "" }, | |
50 // Always enable HTMLMediaElement logs. | |
51 { switches::kBlinkPlatformLogChannels, "Media"}, | |
52 { NULL, NULL }, // Termination | |
53 }; | |
54 | |
55 void AddDefaultCommandLineSwitches(CommandLine* command_line) { | |
56 int i = 0; | |
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)); | |
61 ++i; | |
62 } | |
63 } | |
64 | |
65 } // namespace | |
66 | |
67 CastBrowserMainParts::CastBrowserMainParts( | |
68 const content::MainFunctionParams& parameters, | |
69 URLRequestContextFactory* url_request_context_factory) | |
70 : BrowserMainParts(), | |
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); | |
76 } | |
77 | |
78 CastBrowserMainParts::~CastBrowserMainParts() { | |
79 } | |
80 | |
81 void CastBrowserMainParts::PreMainMessageLoopStart() { | |
82 #if defined(OS_ANDROID) | |
83 net::NetworkChangeNotifier::SetFactory( | |
84 new net::NetworkChangeNotifierFactoryAndroid()); | |
85 #else | |
86 net::NetworkChangeNotifier::SetFactory( | |
87 new NetworkChangeNotifierFactoryCast()); | |
88 #endif // defined(OS_ANDROID) | |
89 } | |
90 | |
91 void CastBrowserMainParts::PostMainMessageLoopStart() { | |
92 #if defined(OS_ANDROID) | |
93 base::MessageLoopForUI::current()->Start(); | |
94 #endif // defined(OS_ANDROID) | |
95 } | |
96 | |
97 int CastBrowserMainParts::PreCreateThreads() { | |
98 PrefRegistrySimple* pref_registry = new PrefRegistrySimple(); | |
99 metrics::RegisterPrefs(pref_registry); | |
100 ChromecastConfig::Create(pref_registry); | |
101 return 0; | |
102 } | |
103 | |
104 void CastBrowserMainParts::PreMainMessageLoopRun() { | |
105 url_request_context_factory_->InitializeOnUIThread(); | |
106 | |
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())); | |
114 | |
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."; | |
119 } | |
120 cast_browser_process_->SetCrashDumpManager( | |
121 new breakpad::CrashDumpManager(crash_dumps_dir)); | |
122 #endif | |
123 | |
124 cast_browser_process_->SetRemoteDebuggingServer(new RemoteDebuggingServer()); | |
125 | |
126 InitializeWebUI(); | |
127 | |
128 cast_browser_process_->SetCastService(CastService::Create( | |
129 cast_browser_process_->browser_context(), | |
130 url_request_context_factory_->GetSystemGetter(), | |
131 base::Bind( | |
132 &metrics::CastMetricsServiceClient::EnableMetricsService, | |
133 base::Unretained(cast_browser_process_->metrics_service_client())))); | |
134 | |
135 // Initializing network delegates must happen after Cast service is created. | |
136 url_request_context_factory_->InitializeNetworkDelegates(); | |
137 cast_browser_process_->cast_service()->Start(); | |
138 } | |
139 | |
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(); | |
145 } else { | |
146 base::MessageLoopForUI::current()->Run(); | |
147 } | |
148 return true; | |
149 } | |
150 | |
151 void CastBrowserMainParts::PostMainMessageLoopRun() { | |
152 cast_browser_process_->cast_service()->Stop(); | |
153 cast_browser_process_.reset(); | |
154 } | |
155 | |
156 } // namespace shell | |
157 } // namespace chromecast | |
OLD | NEW |