Chromium Code Reviews| Index: chrome/browser/metrics/metrics_service.cc |
| diff --git a/chrome/browser/metrics/metrics_service.cc b/chrome/browser/metrics/metrics_service.cc |
| index 700cc8a7921359f9235943b64be4a360000ff83c..502ec4070d99348d26f9b684891a7a7c326ae1c4 100644 |
| --- a/chrome/browser/metrics/metrics_service.cc |
| +++ b/chrome/browser/metrics/metrics_service.cc |
| @@ -320,13 +320,12 @@ int MapCrashExitCodeForHistogram(int exit_code) { |
| return std::abs(exit_code); |
| } |
| -void MarkAppCleanShutdownAndCommit() { |
| - PrefService* pref = g_browser_process->local_state(); |
| - pref->SetBoolean(prefs::kStabilityExitedCleanly, true); |
| - pref->SetInteger(prefs::kStabilityExecutionPhase, |
| - MetricsService::SHUTDOWN_COMPLETE); |
| +void MarkAppCleanShutdownAndCommit(PrefService* local_state) { |
| + local_state->SetBoolean(prefs::kStabilityExitedCleanly, true); |
| + local_state->SetInteger(prefs::kStabilityExecutionPhase, |
| + MetricsService::SHUTDOWN_COMPLETE); |
| // Start writing right away (write happens on a different thread). |
| - pref->CommitPendingWrite(); |
| + local_state->CommitPendingWrite(); |
| } |
| } // namespace |
| @@ -459,10 +458,11 @@ void MetricsService::RegisterPrefs(PrefRegistrySimple* registry) { |
| #endif // defined(OS_ANDROID) |
| } |
| -MetricsService::MetricsService(metrics::MetricsStateManager* state_manager) |
| - : MetricsServiceBase(g_browser_process->local_state(), |
| - kUploadLogAvoidRetransmitSize), |
| +MetricsService::MetricsService(metrics::MetricsStateManager* state_manager, |
| + PrefService* local_state) |
| + : MetricsServiceBase(local_state, kUploadLogAvoidRetransmitSize), |
| state_manager_(state_manager), |
| + local_state_(local_state), |
| recording_active_(false), |
| reporting_active_(false), |
| test_mode_active_(false), |
| @@ -725,7 +725,7 @@ void MetricsService::RecordCompletedSessionEnd() { |
| void MetricsService::OnAppEnterBackground() { |
| scheduler_->Stop(); |
| - MarkAppCleanShutdownAndCommit(); |
| + MarkAppCleanShutdownAndCommit(local_state_); |
| // At this point, there's no way of knowing when the process will be |
| // killed, so this has to be treated similar to a shutdown, closing and |
| @@ -741,25 +741,22 @@ void MetricsService::OnAppEnterBackground() { |
| } |
| void MetricsService::OnAppEnterForeground() { |
| - PrefService* pref = g_browser_process->local_state(); |
| - pref->SetBoolean(prefs::kStabilityExitedCleanly, false); |
| - |
| + local_state_->SetBoolean(prefs::kStabilityExitedCleanly, false); |
| StartSchedulerIfNecessary(); |
| } |
| #else |
| -void MetricsService::LogNeedForCleanShutdown() { |
| - PrefService* pref = g_browser_process->local_state(); |
| - pref->SetBoolean(prefs::kStabilityExitedCleanly, false); |
| +void MetricsService::LogNeedForCleanShutdown(PrefService* local_state) { |
| + local_state->SetBoolean(prefs::kStabilityExitedCleanly, false); |
| // Redundant setting to be sure we call for a clean shutdown. |
| clean_shutdown_status_ = NEED_TO_SHUTDOWN; |
| } |
| #endif // defined(OS_ANDROID) || defined(OS_IOS) |
| // static |
| -void MetricsService::SetExecutionPhase(ExecutionPhase execution_phase) { |
| +void MetricsService::SetExecutionPhase(ExecutionPhase execution_phase, |
| + PrefService* local_state) { |
| execution_phase_ = execution_phase; |
| - PrefService* pref = g_browser_process->local_state(); |
| - pref->SetInteger(prefs::kStabilityExecutionPhase, execution_phase_); |
| + local_state->SetInteger(prefs::kStabilityExecutionPhase, execution_phase_); |
| } |
| void MetricsService::RecordBreakpadRegistration(bool success) { |
| @@ -846,28 +843,29 @@ void MetricsService::InitializeMetricsState() { |
| http_pipelining_test_server_ = dist->GetHttpPipeliningTestServer(); |
| #endif |
| - PrefService* pref = g_browser_process->local_state(); |
| - DCHECK(pref); |
| + DCHECK(local_state_); |
| - pref->SetString(prefs::kStabilityStatsVersion, |
| - MetricsLog::GetVersionString()); |
| - pref->SetInt64(prefs::kStabilityStatsBuildTime, MetricsLog::GetBuildTime()); |
| + local_state_->SetString(prefs::kStabilityStatsVersion, |
| + MetricsLog::GetVersionString()); |
| + local_state_->SetInt64(prefs::kStabilityStatsBuildTime, |
| + MetricsLog::GetBuildTime()); |
| - session_id_ = pref->GetInteger(prefs::kMetricsSessionID); |
| + session_id_ = local_state_->GetInteger(prefs::kMetricsSessionID); |
| #if defined(OS_ANDROID) |
| LogAndroidStabilityToPrefs(pref); |
| #endif // defined(OS_ANDROID) |
| - if (!pref->GetBoolean(prefs::kStabilityExitedCleanly)) { |
| + if (!local_state_->GetBoolean(prefs::kStabilityExitedCleanly)) { |
| IncrementPrefValue(prefs::kStabilityCrashCount); |
| // Reset flag, and wait until we call LogNeedForCleanShutdown() before |
| // monitoring. |
| - pref->SetBoolean(prefs::kStabilityExitedCleanly, true); |
| + local_state_->SetBoolean(prefs::kStabilityExitedCleanly, true); |
| // TODO(rtenneti): On windows, consider saving/getting execution_phase from |
| // the registry. |
| - int execution_phase = pref->GetInteger(prefs::kStabilityExecutionPhase); |
| + int execution_phase = |
| + local_state_->GetInteger(prefs::kStabilityExecutionPhase); |
| UMA_HISTOGRAM_SPARSE_SLOWLY("Chrome.Browser.CrashedExecutionPhase", |
| execution_phase); |
| @@ -879,34 +877,34 @@ void MetricsService::InitializeMetricsState() { |
| // Update session ID. |
| ++session_id_; |
| - pref->SetInteger(prefs::kMetricsSessionID, session_id_); |
| + local_state_->SetInteger(prefs::kMetricsSessionID, session_id_); |
| // Stability bookkeeping |
| IncrementPrefValue(prefs::kStabilityLaunchCount); |
| DCHECK_EQ(UNINITIALIZED_PHASE, execution_phase_); |
| - SetExecutionPhase(START_METRICS_RECORDING); |
| + SetExecutionPhase(START_METRICS_RECORDING, local_state_); |
| #if defined(OS_WIN) |
| CountBrowserCrashDumpAttempts(); |
| #endif // defined(OS_WIN) |
| - if (!pref->GetBoolean(prefs::kStabilitySessionEndCompleted)) { |
| + if (!local_state_->GetBoolean(prefs::kStabilitySessionEndCompleted)) { |
| IncrementPrefValue(prefs::kStabilityIncompleteSessionEndCount); |
| // This is marked false when we get a WM_ENDSESSION. |
| - pref->SetBoolean(prefs::kStabilitySessionEndCompleted, true); |
| + local_state_->SetBoolean(prefs::kStabilitySessionEndCompleted, true); |
| } |
| // Call GetUptimes() for the first time, thus allowing all later calls |
| // to record incremental uptimes accurately. |
| base::TimeDelta ignored_uptime_parameter; |
| base::TimeDelta startup_uptime; |
| - GetUptimes(pref, &startup_uptime, &ignored_uptime_parameter); |
| + GetUptimes(local_state_, &startup_uptime, &ignored_uptime_parameter); |
| DCHECK_EQ(0, startup_uptime.InMicroseconds()); |
| // For backwards compatibility, leave this intact in case Omaha is checking |
| // them. prefs::kStabilityLastTimestampSec may also be useless now. |
| // TODO(jar): Delete these if they have no uses. |
| - pref->SetInt64(prefs::kStabilityLaunchTimeSec, Time::Now().ToTimeT()); |
| + local_state_->SetInt64(prefs::kStabilityLaunchTimeSec, Time::Now().ToTimeT()); |
| // Bookkeeping for the uninstall metrics. |
| IncrementLongPrefsValue(prefs::kUninstallLaunchCount); |
| @@ -1036,9 +1034,10 @@ void MetricsService::ReceivedProfilerData( |
| // Upon the first callback, create the initial log so that we can immediately |
| // save the profiler data. |
| if (!initial_metrics_log_.get()) { |
| - initial_metrics_log_.reset( |
| - new MetricsLog(state_manager_->client_id(), session_id_, |
| - MetricsLog::ONGOING_LOG)); |
| + initial_metrics_log_.reset(new MetricsLog(state_manager_->client_id(), |
| + session_id_, |
| + MetricsLog::ONGOING_LOG, |
| + local_state_)); |
| NotifyOnDidCreateMetricsLog(); |
| } |
| @@ -1102,13 +1101,12 @@ void MetricsService::ScheduleNextStateSave() { |
| } |
| void MetricsService::SaveLocalState() { |
| - PrefService* pref = g_browser_process->local_state(); |
| - if (!pref) { |
| + if (!local_state_) { |
|
Alexei Svitkine (slow)
2014/05/19 13:13:54
Nit: Shouldn't be possible - just remove this if.
blundell
2014/05/19 13:49:28
Done.
|
| NOTREACHED(); |
| return; |
| } |
| - RecordCurrentState(pref); |
| + RecordCurrentState(local_state_); |
| // TODO(jar):110021 Does this run down the batteries???? |
| ScheduleNextStateSave(); |
| @@ -1121,9 +1119,10 @@ void MetricsService::SaveLocalState() { |
| void MetricsService::OpenNewLog() { |
| DCHECK(!log_manager_.current_log()); |
| - log_manager_.BeginLoggingWithLog( |
| - new MetricsLog(state_manager_->client_id(), session_id_, |
| - MetricsLog::ONGOING_LOG)); |
| + log_manager_.BeginLoggingWithLog(new MetricsLog(state_manager_->client_id(), |
| + session_id_, |
| + MetricsLog::ONGOING_LOG, |
| + local_state_)); |
| NotifyOnDidCreateMetricsLog(); |
| if (state_ == INITIALIZED) { |
| // We only need to schedule that run once. |
| @@ -1170,10 +1169,9 @@ void MetricsService::CloseCurrentLog() { |
| GetCurrentSyntheticFieldTrials(&synthetic_trials); |
| current_log->RecordEnvironment(plugins_, google_update_metrics_, |
| synthetic_trials); |
| - PrefService* pref = g_browser_process->local_state(); |
| base::TimeDelta incremental_uptime; |
| base::TimeDelta uptime; |
| - GetUptimes(pref, &incremental_uptime, &uptime); |
| + GetUptimes(local_state_, &incremental_uptime, &uptime); |
| current_log->RecordStabilityMetrics(incremental_uptime, uptime); |
| RecordCurrentHistograms(); |
| @@ -1416,12 +1414,13 @@ void MetricsService::StageNewLog() { |
| void MetricsService::PrepareInitialStabilityLog() { |
| DCHECK_EQ(INITIALIZED, state_); |
| - PrefService* pref = g_browser_process->local_state(); |
| - DCHECK_NE(0, pref->GetInteger(prefs::kStabilityCrashCount)); |
| + DCHECK_NE(0, local_state_->GetInteger(prefs::kStabilityCrashCount)); |
| scoped_ptr<MetricsLog> initial_stability_log( |
| - new MetricsLog(state_manager_->client_id(), session_id_, |
| - MetricsLog::INITIAL_STABILITY_LOG)); |
| + new MetricsLog(state_manager_->client_id(), |
| + session_id_, |
| + MetricsLog::INITIAL_STABILITY_LOG, |
| + local_state_)); |
| // Do not call NotifyOnDidCreateMetricsLog here because the stability |
| // log describes stats from the _previous_ session. |
| @@ -1456,10 +1455,9 @@ void MetricsService::PrepareInitialMetricsLog() { |
| GetCurrentSyntheticFieldTrials(&synthetic_trials); |
| initial_metrics_log_->RecordEnvironment(plugins_, google_update_metrics_, |
| synthetic_trials); |
| - PrefService* pref = g_browser_process->local_state(); |
| base::TimeDelta incremental_uptime; |
| base::TimeDelta uptime; |
| - GetUptimes(pref, &incremental_uptime, &uptime); |
| + GetUptimes(local_state_, &incremental_uptime, &uptime); |
| initial_metrics_log_->RecordStabilityMetrics(incremental_uptime, uptime); |
| // Histograms only get written to the current log, so make the new log current |
| @@ -1642,17 +1640,15 @@ void MetricsService::OnURLFetchComplete(const net::URLFetcher* source) { |
| } |
| void MetricsService::IncrementPrefValue(const char* path) { |
| - PrefService* pref = g_browser_process->local_state(); |
| - DCHECK(pref); |
| - int value = pref->GetInteger(path); |
| - pref->SetInteger(path, value + 1); |
| + DCHECK(local_state_); |
| + int value = local_state_->GetInteger(path); |
| + local_state_->SetInteger(path, value + 1); |
| } |
| void MetricsService::IncrementLongPrefsValue(const char* path) { |
| - PrefService* pref = g_browser_process->local_state(); |
| - DCHECK(pref); |
| - int64 value = pref->GetInt64(path); |
| - pref->SetInt64(path, value + 1); |
| + DCHECK(local_state_); |
| + int64 value = local_state_->GetInt64(path); |
| + local_state_->SetInt64(path, value + 1); |
| } |
| void MetricsService::LogLoadStarted(content::WebContents* web_contents) { |
| @@ -1740,16 +1736,15 @@ void MetricsService::GetCurrentSyntheticFieldTrials( |
| void MetricsService::LogCleanShutdown() { |
| // Redundant hack to write pref ASAP. |
| - MarkAppCleanShutdownAndCommit(); |
| + MarkAppCleanShutdownAndCommit(local_state_); |
| // Redundant setting to assure that we always reset this value at shutdown |
| // (and that we don't use some alternate path, and not call LogCleanShutdown). |
| clean_shutdown_status_ = CLEANLY_SHUTDOWN; |
| RecordBooleanPrefValue(prefs::kStabilityExitedCleanly, true); |
| - PrefService* pref = g_browser_process->local_state(); |
| - pref->SetInteger(prefs::kStabilityExecutionPhase, |
| - MetricsService::SHUTDOWN_COMPLETE); |
| + local_state_->SetInteger(prefs::kStabilityExecutionPhase, |
| + MetricsService::SHUTDOWN_COMPLETE); |
| } |
| #if defined(OS_CHROMEOS) |
| @@ -1894,12 +1889,10 @@ bool MetricsService::ShouldLogEvents() { |
| void MetricsService::RecordBooleanPrefValue(const char* path, bool value) { |
| DCHECK(IsSingleThreaded()); |
| + DCHECK(local_state_); |
|
Alexei Svitkine (slow)
2014/05/19 13:13:54
I think we can just have a single DCHECK in the ct
blundell
2014/05/19 13:49:28
Done.
|
| - PrefService* pref = g_browser_process->local_state(); |
| - DCHECK(pref); |
| - |
| - pref->SetBoolean(path, value); |
| - RecordCurrentState(pref); |
| + local_state_->SetBoolean(path, value); |
| + RecordCurrentState(local_state_); |
| } |
| void MetricsService::RecordCurrentState(PrefService* pref) { |
| @@ -1923,9 +1916,9 @@ void MetricsService::StartExternalMetrics() { |
| #endif |
| // static |
| -bool MetricsServiceHelper::IsMetricsReportingEnabled() { |
| +bool MetricsServiceHelper::IsMetricsReportingEnabled( |
| + const PrefService* local_state) { |
| bool result = false; |
| - const PrefService* local_state = g_browser_process->local_state(); |
| if (local_state) { |
| const PrefService::Preference* uma_pref = |
| local_state->FindPreference(prefs::kMetricsReportingEnabled); |
| @@ -1937,7 +1930,8 @@ bool MetricsServiceHelper::IsMetricsReportingEnabled() { |
| return result; |
| } |
| -bool MetricsServiceHelper::IsCrashReportingEnabled() { |
| +bool MetricsServiceHelper::IsCrashReportingEnabled( |
| + const PrefService* local_state) { |
| #if defined(GOOGLE_CHROME_BUILD) |
| #if defined(OS_CHROMEOS) |
| bool reporting_enabled = false; |
| @@ -1946,10 +1940,9 @@ bool MetricsServiceHelper::IsCrashReportingEnabled() { |
| return reporting_enabled; |
| #elif defined(OS_ANDROID) |
| // Android has its own settings for metrics / crash uploading. |
| - const PrefService* prefs = g_browser_process->local_state(); |
| - return prefs->GetBoolean(prefs::kCrashReportingEnabled); |
| + return local_state->GetBoolean(prefs::kCrashReportingEnabled); |
| #else |
| - return MetricsServiceHelper::IsMetricsReportingEnabled(); |
| + return MetricsServiceHelper::IsMetricsReportingEnabled(local_state); |
| #endif |
| #else |
| return false; |