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