shell_main_delegate.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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#endif
168
169    command_line.AppendSwitch(switches::kEnableFileCookies);
170
171    // Unless/until WebM files are added to the media layout tests, we need to
172    // avoid removing MP4/H264/AAC so that layout tests can run on Android.
173#if !defined(OS_ANDROID)
174    net::RemoveProprietaryMediaTypesAndCodecsForTests();
175#endif
176
177    if (!WebKitTestPlatformInitialize()) {
178      if (exit_code)
179        *exit_code = 1;
180      return true;
181    }
182  }
183  SetContentClient(&content_client_);
184  return false;
185}
186
187void ShellMainDelegate::PreSandboxStartup() {
188  if (CommandLine::ForCurrentProcess()->HasSwitch(
189          switches::kEnableCrashReporter)) {
190    std::string process_type =
191        CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
192            switches::kProcessType);
193    breakpad::SetBreakpadClient(g_shell_breakpad_client.Pointer());
194#if defined(OS_MACOSX)
195    base::mac::DisableOSCrashDumps();
196    breakpad::InitCrashReporter(process_type);
197    breakpad::InitCrashProcessInfo(process_type);
198#elif defined(OS_POSIX) && !defined(OS_MACOSX)
199    if (process_type != switches::kZygoteProcess) {
200#if defined(OS_ANDROID)
201      if (process_type.empty())
202        breakpad::InitCrashReporter(process_type);
203      else
204        breakpad::InitNonBrowserCrashReporterForAndroid(process_type);
205#else
206      breakpad::InitCrashReporter(process_type);
207#endif
208    }
209#elif defined(OS_WIN)
210    UINT new_flags =
211        SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX;
212    UINT existing_flags = SetErrorMode(new_flags);
213    SetErrorMode(existing_flags | new_flags);
214    breakpad::InitCrashReporter(process_type);
215#endif
216  }
217
218  InitializeResourceBundle();
219}
220
221int ShellMainDelegate::RunProcess(
222    const std::string& process_type,
223    const MainFunctionParams& main_function_params) {
224  if (!process_type.empty())
225    return -1;
226
227#if !defined(OS_ANDROID)
228  // Android stores the BrowserMainRunner instance as a scoped member pointer
229  // on the ShellMainDelegate class because of different object lifetime.
230  scoped_ptr<BrowserMainRunner> browser_runner_;
231#endif
232
233  browser_runner_.reset(BrowserMainRunner::Create());
234  return ShellBrowserMain(main_function_params, browser_runner_);
235}
236
237#if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX)
238void ShellMainDelegate::ZygoteForked() {
239  if (CommandLine::ForCurrentProcess()->HasSwitch(
240          switches::kEnableCrashReporter)) {
241    std::string process_type =
242        CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
243            switches::kProcessType);
244    breakpad::InitCrashReporter(process_type);
245  }
246}
247#endif
248
249void ShellMainDelegate::InitializeResourceBundle() {
250#if defined(OS_ANDROID)
251  // In the Android case, the renderer runs with a different UID and can never
252  // access the file system.  So we are passed a file descriptor to the
253  // ResourceBundle pak at launch time.
254  int pak_fd =
255      base::GlobalDescriptors::GetInstance()->MaybeGet(kShellPakDescriptor);
256  if (pak_fd != base::kInvalidPlatformFileValue) {
257    ui::ResourceBundle::InitSharedInstanceWithPakFile(base::File(pak_fd),
258                                                      false);
259    ResourceBundle::GetSharedInstance().AddDataPackFromFile(
260        base::File(pak_fd), ui::SCALE_FACTOR_100P);
261    return;
262  }
263#endif
264
265  base::FilePath pak_file;
266#if defined(OS_MACOSX)
267  pak_file = GetResourcesPakFilePath();
268#else
269  base::FilePath pak_dir;
270
271#if defined(OS_ANDROID)
272  bool got_path = PathService::Get(base::DIR_ANDROID_APP_DATA, &pak_dir);
273  DCHECK(got_path);
274  pak_dir = pak_dir.Append(FILE_PATH_LITERAL("paks"));
275#else
276  PathService::Get(base::DIR_MODULE, &pak_dir);
277#endif
278
279  pak_file = pak_dir.Append(FILE_PATH_LITERAL("content_shell.pak"));
280#endif
281  ui::ResourceBundle::InitSharedInstanceWithPakPath(pak_file);
282}
283
284ContentBrowserClient* ShellMainDelegate::CreateContentBrowserClient() {
285  browser_client_.reset(new ShellContentBrowserClient);
286  return browser_client_.get();
287}
288
289ContentRendererClient* ShellMainDelegate::CreateContentRendererClient() {
290  renderer_client_.reset(new ShellContentRendererClient);
291  return renderer_client_.get();
292}
293
294}  // namespace content
295