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