Home | History | Annotate | Download | only in browser
      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 "chrome/browser/chrome_browser_main_linux.h"
      6 
      7 #include <stdlib.h>
      8 
      9 #include "base/command_line.h"
     10 #include "base/linux_util.h"
     11 #include "base/prefs/pref_service.h"
     12 #include "chrome/app/breakpad_linux.h"
     13 #include "chrome/browser/browser_process.h"
     14 #include "chrome/browser/metrics/metrics_service.h"
     15 #include "chrome/common/chrome_switches.h"
     16 #include "chrome/common/env_vars.h"
     17 #include "chrome/common/pref_names.h"
     18 
     19 #if defined(OS_CHROMEOS)
     20 #include "chrome/browser/chromeos/settings/cros_settings.h"
     21 #include "chrome/browser/chromeos/settings/cros_settings_names.h"
     22 #include "chrome/common/chrome_version_info.h"
     23 #include "chromeos/chromeos_switches.h"
     24 #else
     25 #include "chrome/browser/storage_monitor/storage_monitor_linux.h"
     26 #include "chrome/browser/sxs_linux.h"
     27 #include "content/public/browser/browser_thread.h"
     28 #endif
     29 
     30 namespace {
     31 
     32 #if !defined(OS_CHROMEOS)
     33 void GetLinuxDistroCallback() {
     34   base::GetLinuxDistro();  // Initialize base::linux_distro if needed.
     35 }
     36 #endif
     37 
     38 bool IsCrashReportingEnabled(const PrefService* local_state) {
     39   // Check whether we should initialize the crash reporter. It may be disabled
     40   // through configuration policy or user preference. It must be disabled for
     41   // Guest mode on Chrome OS in Stable channel.
     42   // Also allow crash reporting to be enabled with a command-line flag if the
     43   // crash service is under control of the user. It is used by QA
     44   // testing infrastructure to switch on generation of crash reports.
     45   bool use_switch = true;
     46 
     47   // Convert #define to a variable so that we can use if() rather than
     48   // #if below and so at least compile-test the Chrome code in
     49   // Chromium builds.
     50 #if defined(GOOGLE_CHROME_BUILD)
     51   bool is_chrome_build = true;
     52 #else
     53   bool is_chrome_build = false;
     54 #endif
     55 
     56   // Check these settings in Chrome builds only, to reduce the chance
     57   // that we accidentally upload crash dumps from Chromium builds.
     58   bool breakpad_enabled = false;
     59   if (is_chrome_build) {
     60 #if defined(OS_CHROMEOS)
     61     bool is_guest_session = CommandLine::ForCurrentProcess()->HasSwitch(
     62         chromeos::switches::kGuestSession);
     63     bool is_stable_channel =
     64         chrome::VersionInfo::GetChannel() ==
     65         chrome::VersionInfo::CHANNEL_STABLE;
     66     // TODO(pastarmovj): Consider the TrustedGet here.
     67     bool reporting_enabled;
     68     chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref,
     69                                               &reporting_enabled);
     70     breakpad_enabled =
     71         !(is_guest_session && is_stable_channel) && reporting_enabled;
     72 #else
     73     const PrefService::Preference* metrics_reporting_enabled =
     74         local_state->FindPreference(prefs::kMetricsReportingEnabled);
     75     CHECK(metrics_reporting_enabled);
     76     breakpad_enabled = local_state->GetBoolean(prefs::kMetricsReportingEnabled);
     77     use_switch = metrics_reporting_enabled->IsUserModifiable();
     78 #endif  // defined(OS_CHROMEOS)
     79   }
     80 
     81   if (use_switch) {
     82     // Linux Breakpad interferes with the debug stack traces produced
     83     // by EnableInProcessStackDumping(), used in browser_tests, so we
     84     // do not allow CHROME_HEADLESS=1 to enable Breakpad in Chromium
     85     // because the buildbots have CHROME_HEADLESS set.  However, we
     86     // allow CHROME_HEADLESS to enable Breakpad in Chrome for
     87     // compatibility with Breakpad/Chrome tests that may rely on this.
     88     // TODO(mseaborn): Change tests to use --enable-crash-reporter-for-testing
     89     // instead.
     90     if (is_chrome_build && !breakpad_enabled)
     91       breakpad_enabled = getenv(env_vars::kHeadless) != NULL;
     92     if (!breakpad_enabled)
     93       breakpad_enabled = CommandLine::ForCurrentProcess()->HasSwitch(
     94             switches::kEnableCrashReporterForTesting);
     95   }
     96 
     97   return breakpad_enabled;
     98 }
     99 
    100 }  // namespace
    101 
    102 ChromeBrowserMainPartsLinux::ChromeBrowserMainPartsLinux(
    103     const content::MainFunctionParams& parameters)
    104     : ChromeBrowserMainPartsPosix(parameters) {
    105 }
    106 
    107 ChromeBrowserMainPartsLinux::~ChromeBrowserMainPartsLinux() {
    108 }
    109 
    110 void ChromeBrowserMainPartsLinux::PreProfileInit() {
    111 #if !defined(OS_CHROMEOS)
    112   // Needs to be called after we have chrome::DIR_USER_DATA and
    113   // g_browser_process.  This happens in PreCreateThreads.
    114   content::BrowserThread::PostTask(content::BrowserThread::FILE,
    115                                    FROM_HERE,
    116                                    base::Bind(&GetLinuxDistroCallback));
    117 
    118   content::BrowserThread::PostTask(
    119       content::BrowserThread::FILE,
    120       FROM_HERE,
    121       base::Bind(&sxs_linux::AddChannelMarkToUserDataDir));
    122 #endif
    123 
    124   if (IsCrashReportingEnabled(local_state()))
    125     InitCrashReporter();
    126 
    127   ChromeBrowserMainPartsPosix::PreProfileInit();
    128 }
    129 
    130 void ChromeBrowserMainPartsLinux::PostProfileInit() {
    131   ChromeBrowserMainPartsPosix::PostProfileInit();
    132 
    133   g_browser_process->metrics_service()->RecordBreakpadRegistration(
    134       IsCrashReporterEnabled());
    135 }
    136