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"
11 #include "base/command_line.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"
35 #include "chromecast/app/linux/cast_crash_reporter_client.h"
36 #endif // defined(OS_ANDROID)
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)
51 namespace chromecast
{
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
;
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
) {
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_
);
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
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
);
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())
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
);
164 #endif // defined(OS_ANDROID)
167 #if !defined(OS_ANDROID)
168 void CastMainDelegate::ZygoteForked() {
169 const base::CommandLine
* command_line(base::CommandLine::ForCurrentProcess());
170 std::string process_type
=
171 command_line
->GetSwitchValueASCII(switches::kProcessType
);
172 CastCrashReporterClient::InitCrashReporter(process_type
);
174 #endif // !defined(OS_ANDROID)
176 void CastMainDelegate::InitializeResourceBundle() {
177 base::FilePath pak_file
;
178 CHECK(PathService::Get(FILE_CAST_PAK
, &pak_file
));
179 #if defined(OS_ANDROID)
180 // On Android, the renderer runs with a different UID and can never access
181 // the file system. Use the file descriptor passed in at launch time.
182 auto global_descriptors
= base::GlobalDescriptors::GetInstance();
183 int pak_fd
= global_descriptors
->MaybeGet(kAndroidPakDescriptor
);
184 base::MemoryMappedFile::Region pak_region
;
186 pak_region
= global_descriptors
->GetRegion(kAndroidPakDescriptor
);
187 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(base::File(pak_fd
),
189 ui::ResourceBundle::GetSharedInstance().AddDataPackFromFileRegion(
190 base::File(pak_fd
), pak_region
, ui::SCALE_FACTOR_100P
);
193 pak_fd
= base::android::OpenApkAsset("assets/cast_shell.pak", &pak_region
);
194 // Loaded from disk for browsertests.
196 int flags
= base::File::FLAG_OPEN
| base::File::FLAG_READ
;
197 pak_fd
= base::File(pak_file
, flags
).TakePlatformFile();
198 pak_region
= base::MemoryMappedFile::Region::kWholeFile
;
200 DCHECK_GE(pak_fd
, 0);
201 global_descriptors
->Set(kAndroidPakDescriptor
, pak_fd
, pak_region
);
203 #endif // defined(OS_ANDROID)
205 resource_delegate_
.reset(new CastResourceDelegate());
206 // TODO(gunsch): Use LOAD_COMMON_RESOURCES once ResourceBundle no longer
207 // hardcodes resource file names.
208 ui::ResourceBundle::InitSharedInstanceWithLocale(
209 "en-US", resource_delegate_
.get(),
210 ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES
);
212 #if defined(OS_ANDROID)
213 ui::ResourceBundle::GetSharedInstance().AddDataPackFromFileRegion(
214 base::File(pak_fd
), pak_region
, ui::SCALE_FACTOR_NONE
);
216 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
217 pak_file
, ui::SCALE_FACTOR_NONE
);
218 #endif // defined(OS_ANDROID)
221 content::ContentBrowserClient
* CastMainDelegate::CreateContentBrowserClient() {
222 browser_client_
= CastContentBrowserClient::Create();
223 return browser_client_
.get();
226 content::ContentRendererClient
*
227 CastMainDelegate::CreateContentRendererClient() {
228 renderer_client_
= CastContentRendererClient::Create();
229 return renderer_client_
.get();
233 } // namespace chromecast