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/content/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 settings
.logging_dest
= logging::LOG_TO_SYSTEM_DEBUG_LOG
;
65 #if defined(OS_ANDROID)
66 const base::CommandLine
* command_line(base::CommandLine::ForCurrentProcess());
67 std::string process_type
=
68 command_line
->GetSwitchValueASCII(switches::kProcessType
);
69 // Browser process logs are recorded for attaching with crash dumps.
70 if (process_type
.empty()) {
71 base::FilePath log_file
;
72 PathService::Get(FILE_CAST_ANDROID_LOG
, &log_file
);
73 settings
.logging_dest
= logging::LOG_TO_ALL
;
74 settings
.log_file
= log_file
.value().c_str();
75 settings
.delete_old
= logging::DELETE_OLD_LOG_FILE
;
77 #endif // defined(OS_ANDROID)
78 logging::InitLogging(settings
);
79 // Time, process, and thread ID are available through logcat.
80 logging::SetLogItems(true, true, false, false);
82 #if defined(OS_ANDROID)
83 // Only delete the old crash dumps if the current process is the browser
84 // process. Empty |process_type| signifies browser process.
85 if (process_type
.empty()) {
86 // Get a listing of all of the crash dump files.
87 base::FilePath crash_directory
;
88 if (CastCrashReporterClientAndroid::GetCrashDumpLocation(
89 process_type
, &crash_directory
)) {
90 base::FileEnumerator
crash_directory_list(crash_directory
, false,
91 base::FileEnumerator::FILES
);
92 std::vector
<base::FilePath
> crash_files
;
93 for (base::FilePath file
= crash_directory_list
.Next(); !file
.empty();
94 file
= crash_directory_list
.Next()) {
95 crash_files
.push_back(file
);
97 // Delete crash dumps except for the |kMaxCrashFiles| most recent ones.
98 if (crash_files
.size() > kMaxCrashFiles
) {
100 [](const base::FilePath
& l
, const base::FilePath
& r
) -> bool {
101 base::File::Info l_info
, r_info
;
102 base::GetFileInfo(l
, &l_info
);
103 base::GetFileInfo(r
, &r_info
);
104 return l_info
.last_modified
> r_info
.last_modified
;
106 std::partial_sort(crash_files
.begin(),
107 crash_files
.begin() + kMaxCrashFiles
,
108 crash_files
.end(), newest_first
);
109 for (auto file
= crash_files
.begin() + kMaxCrashFiles
;
110 file
!= crash_files
.end(); ++file
) {
111 base::DeleteFile(*file
, false);
116 #endif // defined(OS_ANDROID)
118 content::SetContentClient(&content_client_
);
122 void CastMainDelegate::PreSandboxStartup() {
123 #if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
124 // Create an instance of the CPU class to parse /proc/cpuinfo and cache the
125 // results. This data needs to be cached when file-reading is still allowed,
126 // since base::CPU expects to be callable later, when file-reading is no
131 const base::CommandLine
* command_line(base::CommandLine::ForCurrentProcess());
132 std::string process_type
=
133 command_line
->GetSwitchValueASCII(switches::kProcessType
);
135 #if defined(OS_ANDROID)
136 base::FilePath log_file
;
137 PathService::Get(FILE_CAST_ANDROID_LOG
, &log_file
);
138 chromecast::CrashHandler::Initialize(process_type
, log_file
);
140 crash_reporter::SetCrashReporterClient(g_crash_reporter_client
.Pointer());
142 if (process_type
!= switches::kZygoteProcess
) {
143 CastCrashReporterClient::InitCrashReporter(process_type
);
145 #endif // defined(OS_ANDROID)
147 InitializeResourceBundle();
150 int CastMainDelegate::RunProcess(
151 const std::string
& process_type
,
152 const content::MainFunctionParams
& main_function_params
) {
153 #if defined(OS_ANDROID)
154 if (!process_type
.empty())
157 // Note: Android must handle running its own browser process.
158 // See ChromeMainDelegateAndroid::RunProcess.
159 browser_runner_
.reset(content::BrowserMainRunner::Create());
160 return browser_runner_
->Initialize(main_function_params
);
163 #endif // defined(OS_ANDROID)
166 void CastMainDelegate::ProcessExiting(const std::string
& process_type
) {
167 if (process_type
.empty())
168 browser_client_
->ProcessExiting();
171 #if !defined(OS_ANDROID)
172 void CastMainDelegate::ZygoteForked() {
173 const base::CommandLine
* command_line(base::CommandLine::ForCurrentProcess());
174 std::string process_type
=
175 command_line
->GetSwitchValueASCII(switches::kProcessType
);
176 CastCrashReporterClient::InitCrashReporter(process_type
);
178 #endif // !defined(OS_ANDROID)
180 void CastMainDelegate::InitializeResourceBundle() {
181 base::FilePath pak_file
;
182 CHECK(PathService::Get(FILE_CAST_PAK
, &pak_file
));
183 #if defined(OS_ANDROID)
184 // On Android, the renderer runs with a different UID and can never access
185 // the file system. Use the file descriptor passed in at launch time.
186 auto global_descriptors
= base::GlobalDescriptors::GetInstance();
187 int pak_fd
= global_descriptors
->MaybeGet(kAndroidPakDescriptor
);
188 base::MemoryMappedFile::Region pak_region
;
190 pak_region
= global_descriptors
->GetRegion(kAndroidPakDescriptor
);
191 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(base::File(pak_fd
),
193 ui::ResourceBundle::GetSharedInstance().AddDataPackFromFileRegion(
194 base::File(pak_fd
), pak_region
, ui::SCALE_FACTOR_100P
);
197 pak_fd
= base::android::OpenApkAsset("assets/cast_shell.pak", &pak_region
);
198 // Loaded from disk for browsertests.
200 int flags
= base::File::FLAG_OPEN
| base::File::FLAG_READ
;
201 pak_fd
= base::File(pak_file
, flags
).TakePlatformFile();
202 pak_region
= base::MemoryMappedFile::Region::kWholeFile
;
204 DCHECK_GE(pak_fd
, 0);
205 global_descriptors
->Set(kAndroidPakDescriptor
, pak_fd
, pak_region
);
207 #endif // defined(OS_ANDROID)
209 resource_delegate_
.reset(new CastResourceDelegate());
210 // TODO(gunsch): Use LOAD_COMMON_RESOURCES once ResourceBundle no longer
211 // hardcodes resource file names.
212 ui::ResourceBundle::InitSharedInstanceWithLocale(
213 "en-US", resource_delegate_
.get(),
214 ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES
);
216 #if defined(OS_ANDROID)
217 ui::ResourceBundle::GetSharedInstance().AddDataPackFromFileRegion(
218 base::File(pak_fd
), pak_region
, ui::SCALE_FACTOR_NONE
);
220 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
221 pak_file
, ui::SCALE_FACTOR_NONE
);
222 #endif // defined(OS_ANDROID)
225 content::ContentBrowserClient
* CastMainDelegate::CreateContentBrowserClient() {
226 browser_client_
= CastContentBrowserClient::Create();
227 return browser_client_
.get();
230 content::ContentRendererClient
*
231 CastMainDelegate::CreateContentRendererClient() {
232 renderer_client_
= CastContentRendererClient::Create();
233 return renderer_client_
.get();
237 } // namespace chromecast