shell_main_delegate.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
1// Copyright (c) 2012 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 "content/shell/app/shell_main_delegate.h"
6
7#include "base/base_switches.h"
8#include "base/command_line.h"
9#include "base/cpu.h"
10#include "base/files/file.h"
11#include "base/files/file_path.h"
12#include "base/lazy_instance.h"
13#include "base/logging.h"
14#include "base/path_service.h"
15#include "base/platform_file.h"
16#include "cc/base/switches.h"
17#include "content/public/browser/browser_main_runner.h"
18#include "content/public/common/content_switches.h"
19#include "content/public/common/url_constants.h"
20#include "content/public/test/layouttest_support.h"
21#include "content/shell/app/shell_breakpad_client.h"
22#include "content/shell/app/webkit_test_platform_support.h"
23#include "content/shell/browser/shell_browser_main.h"
24#include "content/shell/browser/shell_content_browser_client.h"
25#include "content/shell/common/shell_switches.h"
26#include "content/shell/renderer/shell_content_renderer_client.h"
27#include "net/cookies/cookie_monster.h"
28#include "ui/base/resource/resource_bundle.h"
29#include "ui/base/ui_base_paths.h"
30#include "ui/base/ui_base_switches.h"
31#include "ui/events/event_switches.h"
32#include "ui/gfx/switches.h"
33#include "ui/gl/gl_switches.h"
34
35#include "ipc/ipc_message.h"  // For IPC_MESSAGE_LOG_ENABLED.
36
37#if defined(IPC_MESSAGE_LOG_ENABLED)
38#define IPC_MESSAGE_MACROS_LOG_ENABLED
39#include "content/public/common/content_ipc_logging.h"
40#define IPC_LOG_TABLE_ADD_ENTRY(msg_id, logger) \
41    content::RegisterIPCLogger(msg_id, logger)
42#include "content/shell/common/shell_messages.h"
43#endif
44
45#if defined(OS_ANDROID)
46#include "base/posix/global_descriptors.h"
47#include "content/shell/android/shell_descriptors.h"
48#endif
49
50#if defined(OS_MACOSX)
51#include "base/mac/os_crash_dumps.h"
52#include "components/breakpad/app/breakpad_mac.h"
53#include "content/shell/app/paths_mac.h"
54#include "content/shell/app/shell_main_delegate_mac.h"
55#endif  // OS_MACOSX
56
57#if defined(OS_WIN)
58#include <initguid.h>
59#include <windows.h>
60#include "base/logging_win.h"
61#include "components/breakpad/app/breakpad_win.h"
62#endif
63
64#if defined(OS_POSIX) && !defined(OS_MACOSX)
65#include "components/breakpad/app/breakpad_linux.h"
66#endif
67
68namespace {
69
70base::LazyInstance<content::ShellBreakpadClient>::Leaky
71    g_shell_breakpad_client = LAZY_INSTANCE_INITIALIZER;
72
73#if defined(OS_WIN)
74// If "Content Shell" doesn't show up in your list of trace providers in
75// Sawbuck, add these registry entries to your machine (NOTE the optional
76// Wow6432Node key for x64 machines):
77// 1. Find:  HKLM\SOFTWARE\[Wow6432Node\]Google\Sawbuck\Providers
78// 2. Add a subkey with the name "{6A3E50A4-7E15-4099-8413-EC94D8C2A4B6}"
79// 3. Add these values:
80//    "default_flags"=dword:00000001
81//    "default_level"=dword:00000004
82//    @="Content Shell"
83
84// {6A3E50A4-7E15-4099-8413-EC94D8C2A4B6}
85const GUID kContentShellProviderName = {
86    0x6a3e50a4, 0x7e15, 0x4099,
87        { 0x84, 0x13, 0xec, 0x94, 0xd8, 0xc2, 0xa4, 0xb6 } };
88#endif
89
90void InitLogging() {
91  base::FilePath log_filename;
92  PathService::Get(base::DIR_EXE, &log_filename);
93  log_filename = log_filename.AppendASCII("content_shell.log");
94  logging::LoggingSettings settings;
95  settings.logging_dest = logging::LOG_TO_ALL;
96  settings.log_file = log_filename.value().c_str();
97  settings.delete_old = logging::DELETE_OLD_LOG_FILE;
98  logging::InitLogging(settings);
99  logging::SetLogItems(true, true, true, true);
100}
101
102}  // namespace
103
104namespace content {
105
106ShellMainDelegate::ShellMainDelegate() {
107}
108
109ShellMainDelegate::~ShellMainDelegate() {
110}
111
112bool ShellMainDelegate::BasicStartupComplete(int* exit_code) {
113#if defined(OS_WIN)
114  // Enable trace control and transport through event tracing for Windows.
115  logging::LogEventProvider::Initialize(kContentShellProviderName);
116#endif
117#if defined(OS_MACOSX)
118  // Needs to happen before InitializeResourceBundle() and before
119  // WebKitTestPlatformInitialize() are called.
120  OverrideFrameworkBundlePath();
121  OverrideChildProcessPath();
122  EnsureCorrectResolutionSettings();
123#endif  // OS_MACOSX
124
125  InitLogging();
126  CommandLine& command_line = *CommandLine::ForCurrentProcess();
127  if (command_line.HasSwitch(switches::kCheckLayoutTestSysDeps)) {
128    if (!CheckLayoutSystemDeps()) {
129      if (exit_code)
130        *exit_code = 1;
131      return true;
132    }
133  }
134
135  if (command_line.HasSwitch(switches::kDumpRenderTree)) {
136    EnableBrowserLayoutTestMode();
137
138    command_line.AppendSwitch(switches::kProcessPerTab);
139    command_line.AppendSwitch(switches::kEnableLogging);
140    command_line.AppendSwitch(switches::kAllowFileAccessFromFiles);
141    command_line.AppendSwitchASCII(switches::kUseGL,
142                                   gfx::kGLImplementationOSMesaName);
143    command_line.AppendSwitch(switches::kSkipGpuDataLoading);
144    command_line.AppendSwitchASCII(switches::kTouchEvents,
145                                   switches::kTouchEventsEnabled);
146    command_line.AppendSwitch(switches::kEnableGestureTapHighlight);
147    command_line.AppendSwitchASCII(switches::kForceDeviceScaleFactor, "1.0");
148#if defined(OS_ANDROID)
149    command_line.AppendSwitch(
150        switches::kDisableGestureRequirementForMediaPlayback);
151#endif
152
153    if (!command_line.HasSwitch(switches::kStableReleaseMode)) {
154      command_line.AppendSwitch(
155        switches::kEnableExperimentalWebPlatformFeatures);
156    }
157
158    if (!command_line.HasSwitch(switches::kEnableThreadedCompositing)) {
159      command_line.AppendSwitch(switches::kDisableThreadedCompositing);
160      command_line.AppendSwitch(cc::switches::kDisableThreadedAnimation);
161    }
162
163    command_line.AppendSwitch(switches::kEnableInbandTextTracks);
164    command_line.AppendSwitch(switches::kMuteAudio);
165
166#if defined(USE_AURA) || defined(OS_ANDROID)
167    // TODO: crbug.com/311404 Make layout tests work w/ delegated renderer.
168    command_line.AppendSwitch(switches::kDisableDelegatedRenderer);
169    command_line.AppendSwitch(cc::switches::kCompositeToMailbox);
170#endif
171
172    command_line.AppendSwitch(switches::kEnableFileCookies);
173
174    command_line.AppendSwitch(switches::kEnablePreciseMemoryInfo);
175
176    // Unless/until WebM files are added to the media layout tests, we need to
177    // avoid removing MP4/H264/AAC so that layout tests can run on Android.
178#if !defined(OS_ANDROID)
179    net::RemoveProprietaryMediaTypesAndCodecsForTests();
180#endif
181
182    if (!WebKitTestPlatformInitialize()) {
183      if (exit_code)
184        *exit_code = 1;
185      return true;
186    }
187  }
188  SetContentClient(&content_client_);
189  return false;
190}
191
192void ShellMainDelegate::PreSandboxStartup() {
193#if defined(ARCH_CPU_ARM_FAMILY) && (defined(OS_ANDROID) || defined(OS_LINUX))
194  // Create an instance of the CPU class to parse /proc/cpuinfo and cache
195  // cpu_brand info.
196  base::CPU cpu_info;
197#endif
198  if (CommandLine::ForCurrentProcess()->HasSwitch(
199          switches::kEnableCrashReporter)) {
200    std::string process_type =
201        CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
202            switches::kProcessType);
203    breakpad::SetBreakpadClient(g_shell_breakpad_client.Pointer());
204#if defined(OS_MACOSX)
205    base::mac::DisableOSCrashDumps();
206    breakpad::InitCrashReporter(process_type);
207    breakpad::InitCrashProcessInfo(process_type);
208#elif defined(OS_POSIX) && !defined(OS_MACOSX)
209    if (process_type != switches::kZygoteProcess) {
210#if defined(OS_ANDROID)
211      if (process_type.empty())
212        breakpad::InitCrashReporter(process_type);
213      else
214        breakpad::InitNonBrowserCrashReporterForAndroid(process_type);
215#else
216      breakpad::InitCrashReporter(process_type);
217#endif
218    }
219#elif defined(OS_WIN)
220    UINT new_flags =
221        SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX;
222    UINT existing_flags = SetErrorMode(new_flags);
223    SetErrorMode(existing_flags | new_flags);
224    breakpad::InitCrashReporter(process_type);
225#endif
226  }
227
228  InitializeResourceBundle();
229}
230
231int ShellMainDelegate::RunProcess(
232    const std::string& process_type,
233    const MainFunctionParams& main_function_params) {
234  if (!process_type.empty())
235    return -1;
236
237#if !defined(OS_ANDROID)
238  // Android stores the BrowserMainRunner instance as a scoped member pointer
239  // on the ShellMainDelegate class because of different object lifetime.
240  scoped_ptr<BrowserMainRunner> browser_runner_;
241#endif
242
243  browser_runner_.reset(BrowserMainRunner::Create());
244  return ShellBrowserMain(main_function_params, browser_runner_);
245}
246
247#if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX)
248void ShellMainDelegate::ZygoteForked() {
249  if (CommandLine::ForCurrentProcess()->HasSwitch(
250          switches::kEnableCrashReporter)) {
251    std::string process_type =
252        CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
253            switches::kProcessType);
254    breakpad::InitCrashReporter(process_type);
255  }
256}
257#endif
258
259void ShellMainDelegate::InitializeResourceBundle() {
260#if defined(OS_ANDROID)
261  // In the Android case, the renderer runs with a different UID and can never
262  // access the file system.  So we are passed a file descriptor to the
263  // ResourceBundle pak at launch time.
264  int pak_fd =
265      base::GlobalDescriptors::GetInstance()->MaybeGet(kShellPakDescriptor);
266  if (pak_fd != base::kInvalidPlatformFileValue) {
267    ui::ResourceBundle::InitSharedInstanceWithPakFile(base::File(pak_fd),
268                                                      false);
269    ResourceBundle::GetSharedInstance().AddDataPackFromFile(
270        base::File(pak_fd), ui::SCALE_FACTOR_100P);
271    return;
272  }
273#endif
274
275  base::FilePath pak_file;
276#if defined(OS_MACOSX)
277  pak_file = GetResourcesPakFilePath();
278#else
279  base::FilePath pak_dir;
280
281#if defined(OS_ANDROID)
282  bool got_path = PathService::Get(base::DIR_ANDROID_APP_DATA, &pak_dir);
283  DCHECK(got_path);
284  pak_dir = pak_dir.Append(FILE_PATH_LITERAL("paks"));
285#else
286  PathService::Get(base::DIR_MODULE, &pak_dir);
287#endif
288
289  pak_file = pak_dir.Append(FILE_PATH_LITERAL("content_shell.pak"));
290#endif
291  ui::ResourceBundle::InitSharedInstanceWithPakPath(pak_file);
292}
293
294ContentBrowserClient* ShellMainDelegate::CreateContentBrowserClient() {
295  browser_client_.reset(new ShellContentBrowserClient);
296  return browser_client_.get();
297}
298
299ContentRendererClient* ShellMainDelegate::CreateContentRendererClient() {
300  renderer_client_.reset(new ShellContentRendererClient);
301  return renderer_client_.get();
302}
303
304}  // namespace content
305