summaryrefslogtreecommitdiffstats
path: root/ios/chrome/browser/metrics/mobile_session_shutdown_metrics_provider.mm
blob: f13237ba8c5e92af2306dfe24edfe48237cc740b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ios/chrome/browser/metrics/mobile_session_shutdown_metrics_provider.h"

#include <Foundation/Foundation.h>

#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "components/metrics/metrics_service.h"
#include "ios/chrome/browser/crash_report/breakpad_helper.h"
#import "ios/chrome/browser/crash_report/crash_report_background_uploader.h"
#import "ios/chrome/browser/metrics/previous_session_info.h"

namespace {

// Logs |type| in the shutdown type histogram.
void LogShutdownType(MobileSessionShutdownType type) {
  UMA_STABILITY_HISTOGRAM_ENUMERATION("Stability.MobileSessionShutdownType",
                                      type, MOBILE_SESSION_SHUTDOWN_TYPE_COUNT);
}

}  // namespace

MobileSessionShutdownMetricsProvider::MobileSessionShutdownMetricsProvider(
    metrics::MetricsService* metrics_service)
    : metrics_service_(metrics_service) {
  DCHECK(metrics_service_);
}

MobileSessionShutdownMetricsProvider::~MobileSessionShutdownMetricsProvider() {}

bool MobileSessionShutdownMetricsProvider::HasInitialStabilityMetrics() {
  return true;
}

void MobileSessionShutdownMetricsProvider::ProvideInitialStabilityMetrics(
    metrics::SystemProfileProto* system_profile_proto) {
  // If this is the first launch after an upgrade, existing crash reports may
  // have been deleted before this code runs, so log this case in its own
  // bucket.
  if (IsFirstLaunchAfterUpgrade()) {
    LogShutdownType(FIRST_LAUNCH_AFTER_UPGRADE);
    return;
  }

  // If the last app lifetime did not end with a crash, then log it as a normal
  // shutdown while in the background.
  if (metrics_service_->WasLastShutdownClean()) {
    LogShutdownType(SHUTDOWN_IN_BACKGROUND);
    return;
  }

  // If the last app lifetime ended in a crash, log the type of crash.
  MobileSessionShutdownType shutdown_type;
  const bool with_crash_log =
      HasUploadedCrashReportsInBackground() || HasCrashLogs();
  if (ReceivedMemoryWarningBeforeLastShutdown()) {
    if (with_crash_log) {
      shutdown_type = SHUTDOWN_IN_FOREGROUND_WITH_CRASH_LOG_WITH_MEMORY_WARNING;
    } else {
      shutdown_type = SHUTDOWN_IN_FOREGROUND_NO_CRASH_LOG_WITH_MEMORY_WARNING;
    }
  } else {
    if (with_crash_log) {
      shutdown_type = SHUTDOWN_IN_FOREGROUND_WITH_CRASH_LOG_NO_MEMORY_WARNING;
    } else {
      shutdown_type = SHUTDOWN_IN_FOREGROUND_NO_CRASH_LOG_NO_MEMORY_WARNING;
    }
  }
  LogShutdownType(shutdown_type);
}

bool MobileSessionShutdownMetricsProvider::IsFirstLaunchAfterUpgrade() {
  return [[PreviousSessionInfo sharedInstance] isFirstSessionAfterUpgrade];
}

bool MobileSessionShutdownMetricsProvider::HasCrashLogs() {
  return breakpad_helper::HasReportToUpload();
}

bool MobileSessionShutdownMetricsProvider::
    HasUploadedCrashReportsInBackground() {
  return [CrashReportBackgroundUploader hasUploadedCrashReportsInBackground];
}

bool MobileSessionShutdownMetricsProvider::
    ReceivedMemoryWarningBeforeLastShutdown() {
  return [[PreviousSessionInfo sharedInstance]
      didSeeMemoryWarningShortlyBeforeTerminating];
}