cast_main_delegate.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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.
4
5#include "chromecast/shell/app/cast_main_delegate.h"
6
7#include "base/cpu.h"
8#include "base/logging.h"
9#include "base/path_service.h"
10#include "base/posix/global_descriptors.h"
11#include "chromecast/common/cast_paths.h"
12#include "chromecast/common/cast_resource_delegate.h"
13#include "chromecast/common/global_descriptors.h"
14#include "chromecast/shell/browser/cast_content_browser_client.h"
15#include "chromecast/shell/renderer/cast_content_renderer_client.h"
16#include "content/public/browser/browser_main_runner.h"
17#include "content/public/common/content_switches.h"
18#include "ui/base/resource/resource_bundle.h"
19
20namespace chromecast {
21namespace shell {
22
23CastMainDelegate::CastMainDelegate() {
24}
25
26CastMainDelegate::~CastMainDelegate() {
27}
28
29bool CastMainDelegate::BasicStartupComplete(int* exit_code) {
30  RegisterPathProvider();
31
32  logging::LoggingSettings settings;
33#if defined(OS_ANDROID)
34  base::FilePath log_file;
35  PathService::Get(FILE_CAST_ANDROID_LOG, &log_file);
36  settings.logging_dest = logging::LOG_TO_ALL;
37  settings.log_file = log_file.value().c_str();
38  settings.delete_old = logging::DELETE_OLD_LOG_FILE;
39#else
40  settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
41#endif  // defined(OS_ANDROID)
42  logging::InitLogging(settings);
43  // Time, process, and thread ID are available through logcat.
44  logging::SetLogItems(true, true, false, false);
45
46  content::SetContentClient(&content_client_);
47  return false;
48}
49
50void CastMainDelegate::PreSandboxStartup() {
51#if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
52  // Create an instance of the CPU class to parse /proc/cpuinfo and cache the
53  // results. This data needs to be cached when file-reading is still allowed,
54  // since base::CPU expects to be callable later, when file-reading is no
55  // longer allowed.
56  base::CPU cpu_info;
57#endif
58
59  InitializeResourceBundle();
60}
61
62int CastMainDelegate::RunProcess(
63    const std::string& process_type,
64    const content::MainFunctionParams& main_function_params) {
65#if defined(OS_ANDROID)
66  if (!process_type.empty())
67    return -1;
68
69  // Note: Android must handle running its own browser process.
70  // See ChromeMainDelegateAndroid::RunProcess.
71  browser_runner_.reset(content::BrowserMainRunner::Create());
72  return browser_runner_->Initialize(main_function_params);
73#else
74  return -1;
75#endif  // defined(OS_ANDROID)
76}
77
78#if !defined(OS_ANDROID)
79void CastMainDelegate::ZygoteForked() {
80}
81#endif  // !defined(OS_ANDROID)
82
83void CastMainDelegate::InitializeResourceBundle() {
84#if defined(OS_ANDROID)
85  // On Android, the renderer runs with a different UID and can never access
86  // the file system. Use the file descriptor passed in at launch time.
87  int pak_fd =
88      base::GlobalDescriptors::GetInstance()->MaybeGet(kAndroidPakDescriptor);
89  if (pak_fd >= 0) {
90    ui::ResourceBundle::InitSharedInstanceWithPakFileRegion(
91        base::File(pak_fd), base::MemoryMappedFile::Region::kWholeFile);
92    ui::ResourceBundle::GetSharedInstance().AddDataPackFromFile(
93        base::File(pak_fd), ui::SCALE_FACTOR_100P);
94    return;
95  }
96#endif  // defined(OS_ANDROID)
97
98  resource_delegate_.reset(new CastResourceDelegate());
99  // TODO(gunsch): Use LOAD_COMMON_RESOURCES once ResourceBundle no longer
100  // hardcodes resource file names.
101  ui::ResourceBundle::InitSharedInstanceWithLocale(
102      "en-US",
103      resource_delegate_.get(),
104      ui::ResourceBundle::DO_NOT_LOAD_COMMON_RESOURCES);
105
106  base::FilePath pak_file;
107  CHECK(PathService::Get(FILE_CAST_PAK, &pak_file));
108  ui::ResourceBundle::GetSharedInstance().AddDataPackFromPath(
109      pak_file,
110      ui::SCALE_FACTOR_NONE);
111}
112
113content::ContentBrowserClient* CastMainDelegate::CreateContentBrowserClient() {
114  browser_client_.reset(new CastContentBrowserClient);
115  return browser_client_.get();
116}
117
118content::ContentRendererClient*
119CastMainDelegate::CreateContentRendererClient() {
120  renderer_client_.reset(new CastContentRendererClient);
121  return renderer_client_.get();
122}
123
124}  // namespace shell
125}  // namespace chromecast
126