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 "extensions/shell/app/shell_main_delegate.h"
6
7#include "base/command_line.h"
8#include "base/files/file_path.h"
9#include "base/logging.h"
10#include "base/path_service.h"
11#include "content/public/browser/browser_main_runner.h"
12#include "content/public/common/content_switches.h"
13#include "extensions/common/extension_paths.h"
14#include "extensions/shell/browser/default_shell_browser_main_delegate.h"
15#include "extensions/shell/browser/shell_content_browser_client.h"
16#include "extensions/shell/common/shell_content_client.h"
17#include "extensions/shell/renderer/shell_content_renderer_client.h"
18#include "ui/base/resource/resource_bundle.h"
19
20#if defined(OS_CHROMEOS)
21#include "chromeos/chromeos_paths.h"
22#endif
23
24#if !defined(DISABLE_NACL)
25#include "components/nacl/common/nacl_switches.h"
26#if defined(OS_LINUX)
27#include "components/nacl/common/nacl_paths.h"
28#include "components/nacl/zygote/nacl_fork_delegate_linux.h"
29#endif  // OS_LINUX
30#endif  // !DISABLE_NACL
31
32namespace {
33
34void InitLogging() {
35  base::FilePath log_filename;
36  PathService::Get(base::DIR_EXE, &log_filename);
37  log_filename = log_filename.AppendASCII("app_shell.log");
38  logging::LoggingSettings settings;
39  settings.logging_dest = logging::LOG_TO_ALL;
40  settings.log_file = log_filename.value().c_str();
41  settings.delete_old = logging::DELETE_OLD_LOG_FILE;
42  logging::InitLogging(settings);
43  logging::SetLogItems(true, true, true, true);
44}
45
46}  // namespace
47
48namespace extensions {
49
50ShellMainDelegate::ShellMainDelegate() {
51}
52
53ShellMainDelegate::~ShellMainDelegate() {
54}
55
56bool ShellMainDelegate::BasicStartupComplete(int* exit_code) {
57  InitLogging();
58  content_client_.reset(CreateContentClient());
59  SetContentClient(content_client_.get());
60
61#if defined(OS_CHROMEOS)
62  chromeos::RegisterPathProvider();
63#endif
64#if !defined(DISABLE_NACL) && defined(OS_LINUX)
65  nacl::RegisterPathProvider();
66#endif
67  extensions::RegisterPathProvider();
68  return false;
69}
70
71void ShellMainDelegate::PreSandboxStartup() {
72  std::string process_type =
73      CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
74          switches::kProcessType);
75  if (ProcessNeedsResourceBundle(process_type))
76    InitializeResourceBundle();
77}
78
79content::ContentBrowserClient* ShellMainDelegate::CreateContentBrowserClient() {
80  browser_client_.reset(CreateShellContentBrowserClient());
81  return browser_client_.get();
82}
83
84content::ContentRendererClient*
85ShellMainDelegate::CreateContentRendererClient() {
86  renderer_client_.reset(CreateShellContentRendererClient());
87  return renderer_client_.get();
88}
89
90#if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID)
91void ShellMainDelegate::ZygoteStarting(
92    ScopedVector<content::ZygoteForkDelegate>* delegates) {
93#if !defined(DISABLE_NACL)
94  nacl::AddNaClZygoteForkDelegates(delegates);
95#endif  // DISABLE_NACL
96}
97#endif  // OS_POSIX && !OS_MACOSX && !OS_ANDROID
98
99content::ContentClient* ShellMainDelegate::CreateContentClient() {
100  return new ShellContentClient();
101}
102
103content::ContentBrowserClient*
104ShellMainDelegate::CreateShellContentBrowserClient() {
105  return new ShellContentBrowserClient(new DefaultShellBrowserMainDelegate());
106}
107
108content::ContentRendererClient*
109ShellMainDelegate::CreateShellContentRendererClient() {
110  return new ShellContentRendererClient();
111}
112
113void ShellMainDelegate::InitializeResourceBundle() {
114  base::FilePath extensions_shell_and_test_pak_path;
115  PathService::Get(base::DIR_MODULE, &extensions_shell_and_test_pak_path);
116  ui::ResourceBundle::InitSharedInstanceWithPakPath(
117      extensions_shell_and_test_pak_path.AppendASCII(
118          "extensions_shell_and_test.pak"));
119}
120
121// static
122bool ShellMainDelegate::ProcessNeedsResourceBundle(
123    const std::string& process_type) {
124  // The browser process has no process type flag, but needs resources.
125  // On Linux the zygote process opens the resources for the renderers.
126  return process_type.empty() ||
127         process_type == switches::kZygoteProcess ||
128         process_type == switches::kRendererProcess ||
129#if !defined(DISABLE_NACL)
130         process_type == switches::kNaClLoaderProcess ||
131#endif
132         process_type == switches::kUtilityProcess;
133}
134
135}  // namespace extensions
136