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"
9 #include "base/command_line.h"
11 #include "base/lazy_instance.h"
12 #include "base/logging.h"
13 #include "base/path_service.h"
14 #include "base/posix/global_descriptors.h"
15 #include "chromecast/base/cast_paths.h"
16 #include "chromecast/browser/cast_content_browser_client.h"
17 #include "chromecast/common/cast_resource_delegate.h"
18 #include "chromecast/common/global_descriptors.h"
19 #include "chromecast/crash/cast_crash_reporter_client.h"
20 #include "chromecast/renderer/cast_content_renderer_client.h"
21 #include "components/crash/app/crash_reporter_client.h"
22 #include "content/public/browser/browser_main_runner.h"
23 #include "content/public/common/content_switches.h"
24 #include "ui/base/resource/resource_bundle.h"
26 #if defined(OS_ANDROID)
27 #include "chromecast/crash/android/crash_handler.h"
28 #endif // defined(OS_ANDROID)
32 #if !defined(OS_ANDROID)
33 base::LazyInstance
<chromecast::CastCrashReporterClient
>::Leaky
34 g_crash_reporter_client
= LAZY_INSTANCE_INITIALIZER
;
35 #endif // !defined(OS_ANDROID)
39 namespace chromecast
{
42 CastMainDelegate::CastMainDelegate() {
45 CastMainDelegate::~CastMainDelegate() {
48 bool CastMainDelegate::BasicStartupComplete(int* exit_code
) {
49 RegisterPathProvider();
51 logging::LoggingSettings settings
;
52 #if defined(OS_ANDROID)
53 base::FilePath log_file
;
54 PathService::Get(FILE_CAST_ANDROID_LOG
, &log_file
);
55 settings
.logging_dest
= logging::LOG_TO_ALL
;
56 settings
.log_file
= log_file
.value().c_str();
57 settings
.delete_old
= logging::DELETE_OLD_LOG_FILE
;
59 settings
.logging_dest
= logging::LOG_TO_SYSTEM_DEBUG_LOG
;
60 #endif // defined(OS_ANDROID)
61 logging::InitLogging(settings
);
62 // Time, process, and thread ID are available through logcat.
63 logging::SetLogItems(true, true, false, false);
65 content::SetContentClient(&content_client_
);
69 void CastMainDelegate::PreSandboxStartup() {
70 #if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
71 // Create an instance of the CPU class to parse /proc/cpuinfo and cache the
72 // results. This data needs to be cached when file-reading is still allowed,
73 // since base::CPU expects to be callable later, when file-reading is no
78 const base::CommandLine
* command_line(base::CommandLine::ForCurrentProcess());
79 std::string process_type
=
80 command_line
->GetSwitchValueASCII(switches::kProcessType
);
82 #if defined(OS_ANDROID)
83 base::FilePath log_file
;
84 PathService::Get(FILE_CAST_ANDROID_LOG
, &log_file
);
85 chromecast::CrashHandler::Initialize(process_type
, log_file
);
87 crash_reporter::SetCrashReporterClient(g_crash_reporter_client
.Pointer());
89 if (process_type
!= switches::kZygoteProcess
) {
90 CastCrashReporterClient::InitCrashReporter(process_type
);
92 #endif // defined(OS_ANDROID)
94 InitializeResourceBundle();
97 int CastMainDelegate::RunProcess(
98 const std::string
& process_type
,
99 const content::MainFunctionParams
& main_function_params
) {
100 #if defined(OS_ANDROID)
101 if (!process_type
.empty())
104 // Note: Android must handle running its own browser process.
105 // See ChromeMainDelegateAndroid::RunProcess.
106 browser_runner_
.reset(content::BrowserMainRunner::Create());
107 return browser_runner_
->Initialize(main_function_params
);
110 #endif // defined(OS_ANDROID)
113 #if !defined(OS_ANDROID)
114 void CastMainDelegate::ZygoteForked() {
115 const base::CommandLine
* command_line(base::CommandLine::ForCurrentProcess());
116 std::string process_type
=
117 command_line
->GetSwitchValueASCII(switches::kProcessType
);
118 CastCrashReporterClient::InitCrashReporter(process_type
);
120 #endif // !defined(OS_ANDROID)
122 void CastMainDelegate::InitializeResourceBundle() {
123 #if defined(OS_ANDROID)
124 // On Android, the renderer runs with a different UID and can never access
125 // the file system. Use the file descriptor passed in at launch time.
127 base::GlobalDescriptors::GetInstance()->MaybeGet(kAndroidPakDescriptor
);
129 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(
130 base::File(pak_fd
), base::MemoryMappedFile::Region::kWholeFile
);
131 ui::ResourceBundle::GetSharedInstance().AddDataPackFromFile(
132 base::File(pak_fd
), ui::SCALE_FACTOR_100P
);
135 #endif // defined(OS_ANDROID)
137 resource_delegate_
.reset(new CastResourceDelegate());
138 // TODO(gunsch): Use LOAD_COMMON_RESOURCES once ResourceBundle no longer
139 // hardcodes resource file names.
140 ui::ResourceBundle::InitSharedInstanceWithLocale(
142 resource_delegate_
.get(),
143 ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES
);
145 base::FilePath pak_file
;
146 CHECK(PathService::Get(FILE_CAST_PAK
, &pak_file
));
147 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
149 ui::SCALE_FACTOR_NONE
);
152 content::ContentBrowserClient
* CastMainDelegate::CreateContentBrowserClient() {
153 browser_client_
.reset(new CastContentBrowserClient
);
154 return browser_client_
.get();
157 content::ContentRendererClient
*
158 CastMainDelegate::CreateContentRendererClient() {
159 renderer_client_
.reset(new CastContentRendererClient
);
160 return renderer_client_
.get();
164 } // namespace chromecast