Home | History | Annotate | Download | only in app
      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 
     65 namespace {
     66 
     67 base::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}
     82 const GUID kContentShellProviderName = {
     83     0x6a3e50a4, 0x7e15, 0x4099,
     84         { 0x84, 0x13, 0xec, 0x94, 0xd8, 0xc2, 0xa4, 0xb6 } };
     85 #endif
     86 
     87 void 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 
    101 namespace content {
    102 
    103 ShellMainDelegate::ShellMainDelegate() {
    104 }
    105 
    106 ShellMainDelegate::~ShellMainDelegate() {
    107 }
    108 
    109 bool 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 (command_line.HasSwitch(switches::kDumpRenderTree)) {
    133     EnableBrowserLayoutTestMode();
    134 
    135     command_line.AppendSwitch(switches::kProcessPerTab);
    136     command_line.AppendSwitch(switches::kEnableLogging);
    137     command_line.AppendSwitch(switches::kAllowFileAccessFromFiles);
    138 #if !defined(OS_ANDROID)
    139     // OSMesa is not yet available for Android. http://crbug.com/248925
    140     command_line.AppendSwitchASCII(
    141         switches::kUseGL, gfx::kGLImplementationOSMesaName);
    142 #endif
    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     // Capturing pixel results does not yet work when implementation-side
    152     // painting is enabled. See http://crbug.com/250777
    153     command_line.AppendSwitch(cc::switches::kDisableImplSidePainting);
    154 #endif
    155 
    156     if (!command_line.HasSwitch(switches::kStableReleaseMode)) {
    157       command_line.AppendSwitch(
    158         switches::kEnableExperimentalWebPlatformFeatures);
    159     }
    160 
    161     if (!command_line.HasSwitch(switches::kEnableThreadedCompositing)) {
    162       command_line.AppendSwitch(switches::kDisableThreadedCompositing);
    163       command_line.AppendSwitch(cc::switches::kDisableThreadedAnimation);
    164     }
    165 
    166     command_line.AppendSwitch(switches::kEnableInbandTextTracks);
    167     command_line.AppendSwitch(switches::kMuteAudio);
    168 
    169 #if defined(USE_AURA)
    170     // TODO: crbug.com/311404 Make layout tests work w/ delegated renderer.
    171     command_line.AppendSwitch(switches::kDisableDelegatedRenderer);
    172 #endif
    173 
    174     net::CookieMonster::EnableFileScheme();
    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 
    192 void ShellMainDelegate::PreSandboxStartup() {
    193   if (CommandLine::ForCurrentProcess()->HasSwitch(
    194           switches::kEnableCrashReporter)) {
    195     std::string process_type =
    196         CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
    197             switches::kProcessType);
    198     breakpad::SetBreakpadClient(g_shell_breakpad_client.Pointer());
    199 #if defined(OS_MACOSX)
    200     base::mac::DisableOSCrashDumps();
    201     breakpad::InitCrashReporter(process_type);
    202     breakpad::InitCrashProcessInfo(process_type);
    203 #elif defined(OS_POSIX) && !defined(OS_MACOSX)
    204     if (process_type != switches::kZygoteProcess) {
    205 #if defined(OS_ANDROID)
    206       if (process_type.empty())
    207         breakpad::InitCrashReporter(process_type);
    208       else
    209         breakpad::InitNonBrowserCrashReporterForAndroid(process_type);
    210 #else
    211       breakpad::InitCrashReporter(process_type);
    212 #endif
    213     }
    214 #elif defined(OS_WIN)
    215     UINT new_flags =
    216         SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX;
    217     UINT existing_flags = SetErrorMode(new_flags);
    218     SetErrorMode(existing_flags | new_flags);
    219     breakpad::InitCrashReporter(process_type);
    220 #endif
    221   }
    222 
    223   InitializeResourceBundle();
    224 }
    225 
    226 int ShellMainDelegate::RunProcess(
    227     const std::string& process_type,
    228     const MainFunctionParams& main_function_params) {
    229   if (!process_type.empty())
    230     return -1;
    231 
    232 #if !defined(OS_ANDROID)
    233   // Android stores the BrowserMainRunner instance as a scoped member pointer
    234   // on the ShellMainDelegate class because of different object lifetime.
    235   scoped_ptr<BrowserMainRunner> browser_runner_;
    236 #endif
    237 
    238   browser_runner_.reset(BrowserMainRunner::Create());
    239   return ShellBrowserMain(main_function_params, browser_runner_);
    240 }
    241 
    242 #if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_MACOSX)
    243 void ShellMainDelegate::ZygoteForked() {
    244   if (CommandLine::ForCurrentProcess()->HasSwitch(
    245           switches::kEnableCrashReporter)) {
    246     std::string process_type =
    247         CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
    248             switches::kProcessType);
    249     breakpad::InitCrashReporter(process_type);
    250   }
    251 }
    252 #endif
    253 
    254 void ShellMainDelegate::InitializeResourceBundle() {
    255 #if defined(OS_ANDROID)
    256   // In the Android case, the renderer runs with a different UID and can never
    257   // access the file system.  So we are passed a file descriptor to the
    258   // ResourceBundle pak at launch time.
    259   int pak_fd =
    260       base::GlobalDescriptors::GetInstance()->MaybeGet(kShellPakDescriptor);
    261   if (pak_fd != base::kInvalidPlatformFileValue) {
    262     ui::ResourceBundle::InitSharedInstanceWithPakFile(pak_fd, false);
    263     ResourceBundle::GetSharedInstance().AddDataPackFromFile(
    264         pak_fd, ui::SCALE_FACTOR_100P);
    265     return;
    266   }
    267 #endif
    268 
    269   base::FilePath pak_file;
    270 #if defined(OS_MACOSX)
    271   pak_file = GetResourcesPakFilePath();
    272 #else
    273   base::FilePath pak_dir;
    274 
    275 #if defined(OS_ANDROID)
    276   bool got_path = PathService::Get(base::DIR_ANDROID_APP_DATA, &pak_dir);
    277   DCHECK(got_path);
    278   pak_dir = pak_dir.Append(FILE_PATH_LITERAL("paks"));
    279 #else
    280   PathService::Get(base::DIR_MODULE, &pak_dir);
    281 #endif
    282 
    283   pak_file = pak_dir.Append(FILE_PATH_LITERAL("content_shell.pak"));
    284 #endif
    285   ui::ResourceBundle::InitSharedInstanceWithPakPath(pak_file);
    286 }
    287 
    288 ContentBrowserClient* ShellMainDelegate::CreateContentBrowserClient() {
    289   browser_client_.reset(new ShellContentBrowserClient);
    290   return browser_client_.get();
    291 }
    292 
    293 ContentRendererClient* ShellMainDelegate::CreateContentRendererClient() {
    294   renderer_client_.reset(new ShellContentRendererClient);
    295   return renderer_client_.get();
    296 }
    297 
    298 }  // namespace content
    299