1424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// found in the LICENSE file.
4424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
5424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "chrome/browser/diagnostics/diagnostics_metrics.h"
6424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
7424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include <string>
8424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
9424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "base/basictypes.h"
10424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "base/metrics/histogram.h"
11424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "chrome/browser/diagnostics/diagnostics_test.h"
12424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
13424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)namespace diagnostics {
14424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
15424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)namespace {
16424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
17424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// A struct to hold information about the tests.
18424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)struct TestNameInfo {
19424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Should only contain characters [A-Za-z0-9] (no spaces).
20424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  const char* name;
21424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
22424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // A non-localized description only meant for developer consumption.
23424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  const char* description;
24424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)};
25424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
26424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// This structure MUST have DIAGNOSTICS_TEST_COUNT entries in it: one for each
27424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// value of DiagnosticsTestId.  Note that the values in the "name" fields are
28424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// used for UMA metrics names (with "Diagnostics.Test." or
29424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// "Diagnostics.Recovery." prepended), so do not change them without
30424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// understanding the consequences.
31424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)const TestNameInfo kTestNameInfo[] = {
32424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    {"ConflictingDlls", "Conflicting modules"},
33424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    {"DiskSpace", "Available disk space"},
34424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    {"InstallType", "Install type"},
35424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    {"JSONBookmarks", "Bookmark file"},
36424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    {"JSONLocalState", "Local state integrity"},
37424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    {"JSONPreferences", "User preferences integrity"},
38424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    {"OperatingSystem", "Operating system supported version"},
39424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    {"PathDictionaries", "App dictionaries directory path"},
40424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    {"PathLocalState", "Local state path"},
41424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    {"PathResources", "Resources path"},
42424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    {"PathUserData", "User data path"},
43424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    {"Version", "Chrome version test"},
44424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    {"SQLiteIntegrityAppCache", "Application cache database"},
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    {"SQLiteIntegrityArchivedHistory", "Archived history database (obsolete)"},
46424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    {"SQLiteIntegrityCookie", "Cookie database"},
47424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    {"SQLiteIntegrityDatabaseTracker", "Database tracker database"},
48424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    {"SQLiteIntegrityHistory", "History database"},
49424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    {"SQLiteIntegrityNSSCert", "NSS certificate database"},
50424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    {"SQLiteIntegrityNSSKey", "NSS Key database"},
51424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    {"SQLiteIntegrityThumbnails", "Thumbnails database"},
52424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    {"SQLiteIntegrityWebData", "Web Data database"},
53424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // Add new entries in the same order as DiagnosticsTestId.
54424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)};
55424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
56424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)COMPILE_ASSERT(arraysize(kTestNameInfo) == DIAGNOSTICS_TEST_ID_COUNT,
57424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)               diagnostics_test_info_mismatch);
58424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
59424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)const TestNameInfo* FindTestInfo(DiagnosticsTestId id) {
60424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  DCHECK(id < DIAGNOSTICS_TEST_ID_COUNT);
61424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return &kTestNameInfo[id];
62424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
63424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
64424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}  // namespace
65424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
66424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)std::string GetTestName(DiagnosticsTestId id) {
67424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return std::string(FindTestInfo(id)->name);
68424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
69424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
70424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)std::string GetTestDescription(DiagnosticsTestId id) {
71424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return std::string(FindTestInfo(id)->description);
72424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
73424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
74424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#define TEST_CASE(name, id)                                \
75424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  case id:                                                 \
76424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    UMA_HISTOGRAM_ENUMERATION(name, result, RESULT_COUNT); \
77424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    break
78424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
79424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// These must each have their own complete case so that the UMA macros create
80424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// a unique static pointer block for each individual metric.  This is done as a
81424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// macro to prevent errors where the ID is added to one function below, but not
82424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// the other, because they must match.
83424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#define TEST_CASES(name)                                               \
84424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TEST_CASE(name, DIAGNOSTICS_CONFLICTING_DLLS_TEST);                  \
85424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TEST_CASE(name, DIAGNOSTICS_DISK_SPACE_TEST);                        \
86424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TEST_CASE(name, DIAGNOSTICS_INSTALL_TYPE_TEST);                      \
87424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TEST_CASE(name, DIAGNOSTICS_JSON_BOOKMARKS_TEST);                    \
88424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TEST_CASE(name, DIAGNOSTICS_JSON_LOCAL_STATE_TEST);                  \
89424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TEST_CASE(name, DIAGNOSTICS_JSON_PREFERENCES_TEST);                  \
90424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TEST_CASE(name, DIAGNOSTICS_OPERATING_SYSTEM_TEST);                  \
91424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TEST_CASE(name, DIAGNOSTICS_PATH_DICTIONARIES_TEST);                 \
92424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TEST_CASE(name, DIAGNOSTICS_PATH_LOCAL_STATE_TEST);                  \
93424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TEST_CASE(name, DIAGNOSTICS_PATH_RESOURCES_TEST);                    \
94424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TEST_CASE(name, DIAGNOSTICS_PATH_USER_DATA_TEST);                    \
95424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TEST_CASE(name, DIAGNOSTICS_VERSION_TEST);                           \
96424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TEST_CASE(name, DIAGNOSTICS_SQLITE_INTEGRITY_APP_CACHE_TEST);        \
97116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  TEST_CASE(name, DIAGNOSTICS_SQLITE_INTEGRITY_ARCHIVED_HISTORY_TEST_OBSOLETE);\
98424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TEST_CASE(name, DIAGNOSTICS_SQLITE_INTEGRITY_COOKIE_TEST);           \
99424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TEST_CASE(name, DIAGNOSTICS_SQLITE_INTEGRITY_DATABASE_TRACKER_TEST); \
100424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TEST_CASE(name, DIAGNOSTICS_SQLITE_INTEGRITY_HISTORY_TEST);          \
101424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TEST_CASE(name, DIAGNOSTICS_SQLITE_INTEGRITY_NSS_CERT_TEST);         \
102424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TEST_CASE(name, DIAGNOSTICS_SQLITE_INTEGRITY_NSS_KEY_TEST);          \
103424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TEST_CASE(name, DIAGNOSTICS_SQLITE_INTEGRITY_THUMBNAILS_TEST);       \
104424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TEST_CASE(name, DIAGNOSTICS_SQLITE_INTEGRITY_WEB_DATA_TEST)
105424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
106424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void RecordUMARecoveryResult(DiagnosticsTestId id, RunResultMetrics result) {
107424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  const std::string name("Diagnostics.Recovery." +
108424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                         GetTestName(static_cast<DiagnosticsTestId>(id)));
109424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  switch (id) {
110424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    TEST_CASES(name);  // See above
111424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    default:
112424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      NOTREACHED() << "Unhandled UMA Metric type" << id;
113424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
114424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
115424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
116424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void RecordUMATestResult(DiagnosticsTestId id, RunResultMetrics result) {
117424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  const std::string name("Diagnostics.Test." +
118424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                         GetTestName(static_cast<DiagnosticsTestId>(id)));
119424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  switch (id) {
120424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    TEST_CASES(name);  // See above
121424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    default:
122424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      NOTREACHED() << "Unhandled UMA Metric type" << id;
123424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
124424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
125424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#undef TEST_CASE
126424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#undef TEST_CASES
127424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
128424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}  // namespace diagnostics
129