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