Update CrOS OOBE throbber to MD throbber; delete old asset
[chromium-blink-merge.git] / chromecast / app / cast_main_delegate.cc
blobf28ef11ec8217588d95b0fc7163c6cf7c1702a81
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/app/cast_main_delegate.h"
7 #include <algorithm>
8 #include <string>
9 #include <vector>
11 #include "base/command_line.h"
12 #include "base/cpu.h"
13 #include "base/files/file.h"
14 #include "base/files/file_enumerator.h"
15 #include "base/files/file_util.h"
16 #include "base/lazy_instance.h"
17 #include "base/logging.h"
18 #include "base/path_service.h"
19 #include "base/posix/global_descriptors.h"
20 #include "chromecast/base/cast_paths.h"
21 #include "chromecast/browser/cast_content_browser_client.h"
22 #include "chromecast/common/cast_resource_delegate.h"
23 #include "chromecast/common/global_descriptors.h"
24 #include "chromecast/renderer/cast_content_renderer_client.h"
25 #include "components/crash/app/crash_reporter_client.h"
26 #include "content/public/browser/browser_main_runner.h"
27 #include "content/public/common/content_switches.h"
28 #include "ui/base/resource/resource_bundle.h"
30 #if defined(OS_ANDROID)
31 #include "base/android/apk_assets.h"
32 #include "chromecast/app/android/cast_crash_reporter_client_android.h"
33 #include "chromecast/app/android/crash_handler.h"
34 #else
35 #include "chromecast/app/linux/cast_crash_reporter_client.h"
36 #endif // defined(OS_ANDROID)
38 namespace {
40 #if !defined(OS_ANDROID)
41 base::LazyInstance<chromecast::CastCrashReporterClient>::Leaky
42 g_crash_reporter_client = LAZY_INSTANCE_INITIALIZER;
43 #endif // !defined(OS_ANDROID)
45 #if defined(OS_ANDROID)
46 const int kMaxCrashFiles = 10;
47 #endif // defined(OS_ANDROID)
49 } // namespace
51 namespace chromecast {
52 namespace shell {
54 CastMainDelegate::CastMainDelegate() {
57 CastMainDelegate::~CastMainDelegate() {
60 bool CastMainDelegate::BasicStartupComplete(int* exit_code) {
61 RegisterPathProvider();
63 logging::LoggingSettings settings;
64 #if defined(OS_ANDROID)
65 base::FilePath log_file;
66 PathService::Get(FILE_CAST_ANDROID_LOG, &log_file);
67 settings.logging_dest = logging::LOG_TO_ALL;
68 settings.log_file = log_file.value().c_str();
69 const base::CommandLine* command_line(base::CommandLine::ForCurrentProcess());
70 std::string process_type =
71 command_line->GetSwitchValueASCII(switches::kProcessType);
72 // Only delete the old logs if the current process is the browser process.
73 // Empty process_type signifies browser process.
74 settings.delete_old = process_type.empty() ? logging::DELETE_OLD_LOG_FILE
75 : logging::APPEND_TO_OLD_LOG_FILE;
76 #else
77 settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
78 #endif // defined(OS_ANDROID)
79 logging::InitLogging(settings);
80 // Time, process, and thread ID are available through logcat.
81 logging::SetLogItems(true, true, false, false);
83 #if defined(OS_ANDROID)
84 // Only delete the old crash dumps if the current process is the browser
85 // process. Empty |process_type| signifies browser process.
86 if (process_type.empty()) {
87 // Get a listing of all of the crash dump files.
88 base::FilePath crash_directory;
89 if (CastCrashReporterClientAndroid::GetCrashDumpLocation(
90 process_type, &crash_directory)) {
91 base::FileEnumerator crash_directory_list(crash_directory, false,
92 base::FileEnumerator::FILES);
93 std::vector<base::FilePath> crash_files;
94 for (base::FilePath file = crash_directory_list.Next(); !file.empty();
95 file = crash_directory_list.Next()) {
96 crash_files.push_back(file);
98 // Delete crash dumps except for the |kMaxCrashFiles| most recent ones.
99 if (crash_files.size() > kMaxCrashFiles) {
100 auto newest_first =
101 [](const base::FilePath& l, const base::FilePath& r) -> bool {
102 base::File::Info l_info, r_info;
103 base::GetFileInfo(l, &l_info);
104 base::GetFileInfo(r, &r_info);
105 return l_info.last_modified > r_info.last_modified;
107 std::partial_sort(crash_files.begin(),
108 crash_files.begin() + kMaxCrashFiles,
109 crash_files.end(), newest_first);
110 for (auto file = crash_files.begin() + kMaxCrashFiles;
111 file != crash_files.end(); ++file) {
112 base::DeleteFile(*file, false);
117 #endif // defined(OS_ANDROID)
119 content::SetContentClient(&content_client_);
120 return false;
123 void CastMainDelegate::PreSandboxStartup() {
124 #if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
125 // Create an instance of the CPU class to parse /proc/cpuinfo and cache the
126 // results. This data needs to be cached when file-reading is still allowed,
127 // since base::CPU expects to be callable later, when file-reading is no
128 // longer allowed.
129 base::CPU cpu_info;
130 #endif
132 const base::CommandLine* command_line(base::CommandLine::ForCurrentProcess());
133 std::string process_type =
134 command_line->GetSwitchValueASCII(switches::kProcessType);
136 #if defined(OS_ANDROID)
137 base::FilePath log_file;
138 PathService::Get(FILE_CAST_ANDROID_LOG, &log_file);
139 chromecast::CrashHandler::Initialize(process_type, log_file);
140 #else
141 crash_reporter::SetCrashReporterClient(g_crash_reporter_client.Pointer());
143 if (process_type != switches::kZygoteProcess) {
144 CastCrashReporterClient::InitCrashReporter(process_type);
146 #endif // defined(OS_ANDROID)
148 InitializeResourceBundle();
151 int CastMainDelegate::RunProcess(
152 const std::string& process_type,
153 const content::MainFunctionParams& main_function_params) {
154 #if defined(OS_ANDROID)
155 if (!process_type.empty())
156 return -1;
158 // Note: Android must handle running its own browser process.
159 // See ChromeMainDelegateAndroid::RunProcess.
160 browser_runner_.reset(content::BrowserMainRunner::Create());
161 return browser_runner_->Initialize(main_function_params);
162 #else
163 return -1;
164 #endif // defined(OS_ANDROID)
167 void CastMainDelegate::ProcessExiting(const std::string& process_type) {
168 if (process_type.empty())
169 browser_client_->ProcessExiting();
172 #if !defined(OS_ANDROID)
173 void CastMainDelegate::ZygoteForked() {
174 const base::CommandLine* command_line(base::CommandLine::ForCurrentProcess());
175 std::string process_type =
176 command_line->GetSwitchValueASCII(switches::kProcessType);
177 CastCrashReporterClient::InitCrashReporter(process_type);
179 #endif // !defined(OS_ANDROID)
181 void CastMainDelegate::InitializeResourceBundle() {
182 base::FilePath pak_file;
183 CHECK(PathService::Get(FILE_CAST_PAK, &pak_file));
184 #if defined(OS_ANDROID)
185 // On Android, the renderer runs with a different UID and can never access
186 // the file system. Use the file descriptor passed in at launch time.
187 auto global_descriptors = base::GlobalDescriptors::GetInstance();
188 int pak_fd = global_descriptors->MaybeGet(kAndroidPakDescriptor);
189 base::MemoryMappedFile::Region pak_region;
190 if (pak_fd >= 0) {
191 pak_region = global_descriptors->GetRegion(kAndroidPakDescriptor);
192 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(base::File(pak_fd),
193 pak_region);
194 ui::ResourceBundle::GetSharedInstance().AddDataPackFromFileRegion(
195 base::File(pak_fd), pak_region, ui::SCALE_FACTOR_100P);
196 return;
197 } else {
198 pak_fd = base::android::OpenApkAsset("assets/cast_shell.pak", &pak_region);
199 // Loaded from disk for browsertests.
200 if (pak_fd < 0) {
201 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ;
202 pak_fd = base::File(pak_file, flags).TakePlatformFile();
203 pak_region = base::MemoryMappedFile::Region::kWholeFile;
205 DCHECK_GE(pak_fd, 0);
206 global_descriptors->Set(kAndroidPakDescriptor, pak_fd, pak_region);
208 #endif // defined(OS_ANDROID)
210 resource_delegate_.reset(new CastResourceDelegate());
211 // TODO(gunsch): Use LOAD_COMMON_RESOURCES once ResourceBundle no longer
212 // hardcodes resource file names.
213 ui::ResourceBundle::InitSharedInstanceWithLocale(
214 "en-US", resource_delegate_.get(),
215 ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES);
217 #if defined(OS_ANDROID)
218 ui::ResourceBundle::GetSharedInstance().AddDataPackFromFileRegion(
219 base::File(pak_fd), pak_region, ui::SCALE_FACTOR_NONE);
220 #else
221 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
222 pak_file, ui::SCALE_FACTOR_NONE);
223 #endif // defined(OS_ANDROID)
226 content::ContentBrowserClient* CastMainDelegate::CreateContentBrowserClient() {
227 browser_client_ = CastContentBrowserClient::Create();
228 return browser_client_.get();
231 content::ContentRendererClient*
232 CastMainDelegate::CreateContentRendererClient() {
233 renderer_client_ = CastContentRendererClient::Create();
234 return renderer_client_.get();
237 } // namespace shell
238 } // namespace chromecast