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/shell/app/cast_main_delegate.h"
7 #include "base/command_line.h"
9 #include "base/logging.h"
10 #include "base/path_service.h"
11 #include "base/posix/global_descriptors.h"
12 #include "chromecast/common/cast_paths.h"
13 #include "chromecast/common/cast_resource_delegate.h"
14 #include "chromecast/common/global_descriptors.h"
15 #include "chromecast/shell/browser/cast_content_browser_client.h"
16 #include "chromecast/shell/renderer/cast_content_renderer_client.h"
17 #include "content/public/browser/browser_main_runner.h"
18 #include "content/public/common/content_switches.h"
19 #include "ui/base/resource/resource_bundle.h"
21 #if defined(OS_ANDROID)
22 #include "chromecast/crash/android/crash_handler.h"
23 #endif // defined(OS_ANDROID)
25 namespace chromecast
{
28 CastMainDelegate::CastMainDelegate() {
31 CastMainDelegate::~CastMainDelegate() {
34 bool CastMainDelegate::BasicStartupComplete(int* exit_code
) {
35 RegisterPathProvider();
37 logging::LoggingSettings settings
;
38 #if defined(OS_ANDROID)
39 base::FilePath log_file
;
40 PathService::Get(FILE_CAST_ANDROID_LOG
, &log_file
);
41 settings
.logging_dest
= logging::LOG_TO_ALL
;
42 settings
.log_file
= log_file
.value().c_str();
43 settings
.delete_old
= logging::DELETE_OLD_LOG_FILE
;
45 settings
.logging_dest
= logging::LOG_TO_SYSTEM_DEBUG_LOG
;
46 #endif // defined(OS_ANDROID)
47 logging::InitLogging(settings
);
48 // Time, process, and thread ID are available through logcat.
49 logging::SetLogItems(true, true, false, false);
51 content::SetContentClient(&content_client_
);
55 void CastMainDelegate::PreSandboxStartup() {
56 #if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
57 // Create an instance of the CPU class to parse /proc/cpuinfo and cache the
58 // results. This data needs to be cached when file-reading is still allowed,
59 // since base::CPU expects to be callable later, when file-reading is no
64 const base::CommandLine
* command_line(base::CommandLine::ForCurrentProcess());
65 std::string process_type
=
66 command_line
->GetSwitchValueASCII(switches::kProcessType
);
68 #if defined(OS_ANDROID)
69 base::FilePath log_file
;
70 PathService::Get(FILE_CAST_ANDROID_LOG
, &log_file
);
71 chromecast::CrashHandler::Initialize(process_type
, log_file
);
72 #endif // defined(OS_ANDROID)
74 InitializeResourceBundle();
77 int CastMainDelegate::RunProcess(
78 const std::string
& process_type
,
79 const content::MainFunctionParams
& main_function_params
) {
80 #if defined(OS_ANDROID)
81 if (!process_type
.empty())
84 // Note: Android must handle running its own browser process.
85 // See ChromeMainDelegateAndroid::RunProcess.
86 browser_runner_
.reset(content::BrowserMainRunner::Create());
87 return browser_runner_
->Initialize(main_function_params
);
90 #endif // defined(OS_ANDROID)
93 #if !defined(OS_ANDROID)
94 void CastMainDelegate::ZygoteForked() {
96 #endif // !defined(OS_ANDROID)
98 void CastMainDelegate::InitializeResourceBundle() {
99 #if defined(OS_ANDROID)
100 // On Android, the renderer runs with a different UID and can never access
101 // the file system. Use the file descriptor passed in at launch time.
103 base::GlobalDescriptors::GetInstance()->MaybeGet(kAndroidPakDescriptor
);
105 ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(
106 base::File(pak_fd
), base::MemoryMappedFile::Region::kWholeFile
);
107 ui::ResourceBundle::GetSharedInstance().AddDataPackFromFile(
108 base::File(pak_fd
), ui::SCALE_FACTOR_100P
);
111 #endif // defined(OS_ANDROID)
113 resource_delegate_
.reset(new CastResourceDelegate());
114 // TODO(gunsch): Use LOAD_COMMON_RESOURCES once ResourceBundle no longer
115 // hardcodes resource file names.
116 ui::ResourceBundle::InitSharedInstanceWithLocale(
118 resource_delegate_
.get(),
119 ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES
);
121 base::FilePath pak_file
;
122 CHECK(PathService::Get(FILE_CAST_PAK
, &pak_file
));
123 ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
125 ui::SCALE_FACTOR_NONE
);
128 content::ContentBrowserClient
* CastMainDelegate::CreateContentBrowserClient() {
129 browser_client_
.reset(new CastContentBrowserClient
);
130 return browser_client_
.get();
133 content::ContentRendererClient
*
134 CastMainDelegate::CreateContentRendererClient() {
135 renderer_client_
.reset(new CastContentRendererClient
);
136 return renderer_client_
.get();
140 } // namespace chromecast