Introduce ProfilerMetricsProvider
[chromium-blink-merge.git] / chrome / browser / diagnostics / diagnostics_model.cc
blob1216bf08c539297fd0fb63cc257a38af5d94cdd4
1 // Copyright (c) 2011 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.
5 #include "chrome/browser/diagnostics/diagnostics_model.h"
7 #include <algorithm>
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/command_line.h"
12 #include "base/files/file_path.h"
13 #include "base/path_service.h"
14 #include "base/stl_util.h"
15 #include "base/strings/string_util.h"
16 #include "chrome/browser/diagnostics/diagnostics_test.h"
17 #include "chrome/browser/diagnostics/recon_diagnostics.h"
18 #include "chrome/browser/diagnostics/sqlite_diagnostics.h"
19 #include "chrome/common/chrome_paths.h"
20 #include "chrome/common/chrome_switches.h"
22 namespace diagnostics {
24 // This is the count of diagnostic tests on each platform. This should
25 // only be used by testing code.
26 #if defined(OS_WIN)
27 const int DiagnosticsModel::kDiagnosticsTestCount = 19;
28 #elif defined(OS_MACOSX)
29 const int DiagnosticsModel::kDiagnosticsTestCount = 15;
30 #elif defined(OS_POSIX)
31 #if defined(OS_CHROMEOS)
32 const int DiagnosticsModel::kDiagnosticsTestCount = 19;
33 #else
34 const int DiagnosticsModel::kDiagnosticsTestCount = 17;
35 #endif
36 #endif
38 namespace {
40 // Embodies the commonalities of the model across platforms. It manages the
41 // list of tests and can loop over them. The main job of the platform specific
42 // code becomes:
43 // 1- Inserting the appropriate tests into |tests_|
44 // 2- Overriding RunTest() to wrap it with the appropriate fatal exception
45 // handler for the OS.
46 // This class owns the all the tests and will only delete them upon
47 // destruction.
48 class DiagnosticsModelImpl : public DiagnosticsModel {
49 public:
50 DiagnosticsModelImpl() : tests_run_(0) {}
52 virtual ~DiagnosticsModelImpl() { STLDeleteElements(&tests_); }
54 virtual int GetTestRunCount() const OVERRIDE { return tests_run_; }
56 virtual int GetTestAvailableCount() const OVERRIDE { return tests_.size(); }
58 virtual void RunAll(DiagnosticsModel::Observer* observer) OVERRIDE {
59 size_t test_count = tests_.size();
60 bool continue_running = true;
61 for (size_t i = 0; i != test_count; ++i) {
62 // If one of the diagnostic steps returns false, we want to
63 // mark the rest of them as "skipped" in the UMA stats.
64 if (continue_running) {
65 continue_running = RunTest(tests_[i], observer, i);
66 ++tests_run_;
67 } else {
68 #if defined(OS_CHROMEOS) // Only collecting UMA stats on ChromeOS
69 RecordUMATestResult(static_cast<DiagnosticsTestId>(tests_[i]->GetId()),
70 RESULT_SKIPPED);
71 #else
72 // On other platforms, we can just bail out if a diagnostic step returns
73 // false.
74 break;
75 #endif
78 if (observer)
79 observer->OnAllTestsDone(this);
82 virtual void RecoverAll(DiagnosticsModel::Observer* observer) OVERRIDE {
83 size_t test_count = tests_.size();
84 bool continue_running = true;
85 for (size_t i = 0; i != test_count; ++i) {
86 // If one of the recovery steps returns false, we want to
87 // mark the rest of them as "skipped" in the UMA stats.
88 if (continue_running) {
89 continue_running = RunRecovery(tests_[i], observer, i);
90 } else {
91 #if defined(OS_CHROMEOS) // Only collecting UMA stats on ChromeOS
92 RecordUMARecoveryResult(
93 static_cast<DiagnosticsTestId>(tests_[i]->GetId()), RESULT_SKIPPED);
94 #else
95 // On other platforms, we can just bail out if a recovery step returns
96 // false.
97 break;
98 #endif
101 if (observer)
102 observer->OnAllRecoveryDone(this);
105 virtual const TestInfo& GetTest(size_t index) const OVERRIDE {
106 return *tests_[index];
109 virtual bool GetTestInfo(int id, const TestInfo** result) const OVERRIDE {
110 DCHECK(id < DIAGNOSTICS_TEST_ID_COUNT);
111 DCHECK(id >= 0);
112 for (size_t i = 0; i < tests_.size(); i++) {
113 if (tests_[i]->GetId() == id) {
114 *result = tests_[i];
115 return true;
118 return false;
121 protected:
122 // Run a particular diagnostic test. Return false if no other tests should be
123 // run.
124 virtual bool RunTest(DiagnosticsTest* test,
125 Observer* observer,
126 size_t index) {
127 return test->Execute(observer, this, index);
130 // Recover from a particular diagnostic test. Return false if no further
131 // recovery should be run.
132 virtual bool RunRecovery(DiagnosticsTest* test,
133 Observer* observer,
134 size_t index) {
135 return test->Recover(observer, this, index);
138 typedef std::vector<DiagnosticsTest*> TestArray;
139 TestArray tests_;
140 int tests_run_;
142 private:
143 DISALLOW_COPY_AND_ASSIGN(DiagnosticsModelImpl);
146 // Each platform can have their own tests. For the time being there is only
147 // one test that works on all platforms.
148 #if defined(OS_WIN)
149 class DiagnosticsModelWin : public DiagnosticsModelImpl {
150 public:
151 DiagnosticsModelWin() {
152 tests_.push_back(MakeOperatingSystemTest());
153 tests_.push_back(MakeConflictingDllsTest());
154 tests_.push_back(MakeInstallTypeTest());
155 tests_.push_back(MakeVersionTest());
156 tests_.push_back(MakeUserDirTest());
157 tests_.push_back(MakeLocalStateFileTest());
158 tests_.push_back(MakeDictonaryDirTest());
159 tests_.push_back(MakeResourcesFileTest());
160 tests_.push_back(MakeDiskSpaceTest());
161 tests_.push_back(MakePreferencesTest());
162 tests_.push_back(MakeLocalStateTest());
163 tests_.push_back(MakeBookMarksTest());
164 tests_.push_back(MakeSqliteWebDataDbTest());
165 tests_.push_back(MakeSqliteCookiesDbTest());
166 tests_.push_back(MakeSqliteHistoryDbTest());
167 tests_.push_back(MakeSqliteArchivedHistoryDbTest());
168 tests_.push_back(MakeSqliteThumbnailsDbTest());
169 tests_.push_back(MakeSqliteAppCacheDbTest());
170 tests_.push_back(MakeSqliteWebDatabaseTrackerDbTest());
173 private:
174 DISALLOW_COPY_AND_ASSIGN(DiagnosticsModelWin);
177 #elif defined(OS_MACOSX)
178 class DiagnosticsModelMac : public DiagnosticsModelImpl {
179 public:
180 DiagnosticsModelMac() {
181 tests_.push_back(MakeInstallTypeTest());
182 tests_.push_back(MakeUserDirTest());
183 tests_.push_back(MakeLocalStateFileTest());
184 tests_.push_back(MakeDictonaryDirTest());
185 tests_.push_back(MakeDiskSpaceTest());
186 tests_.push_back(MakePreferencesTest());
187 tests_.push_back(MakeLocalStateTest());
188 tests_.push_back(MakeBookMarksTest());
189 tests_.push_back(MakeSqliteWebDataDbTest());
190 tests_.push_back(MakeSqliteCookiesDbTest());
191 tests_.push_back(MakeSqliteHistoryDbTest());
192 tests_.push_back(MakeSqliteArchivedHistoryDbTest());
193 tests_.push_back(MakeSqliteThumbnailsDbTest());
194 tests_.push_back(MakeSqliteAppCacheDbTest());
195 tests_.push_back(MakeSqliteWebDatabaseTrackerDbTest());
198 private:
199 DISALLOW_COPY_AND_ASSIGN(DiagnosticsModelMac);
202 #elif defined(OS_POSIX)
203 class DiagnosticsModelPosix : public DiagnosticsModelImpl {
204 public:
205 DiagnosticsModelPosix() {
206 tests_.push_back(MakeInstallTypeTest());
207 tests_.push_back(MakeVersionTest());
208 tests_.push_back(MakeUserDirTest());
209 tests_.push_back(MakeLocalStateFileTest());
210 tests_.push_back(MakeDictonaryDirTest());
211 tests_.push_back(MakeResourcesFileTest());
212 tests_.push_back(MakeDiskSpaceTest());
213 tests_.push_back(MakePreferencesTest());
214 tests_.push_back(MakeLocalStateTest());
215 tests_.push_back(MakeBookMarksTest());
216 tests_.push_back(MakeSqliteWebDataDbTest());
217 tests_.push_back(MakeSqliteCookiesDbTest());
218 tests_.push_back(MakeSqliteHistoryDbTest());
219 tests_.push_back(MakeSqliteArchivedHistoryDbTest());
220 tests_.push_back(MakeSqliteThumbnailsDbTest());
221 tests_.push_back(MakeSqliteAppCacheDbTest());
222 tests_.push_back(MakeSqliteWebDatabaseTrackerDbTest());
223 #if defined(OS_CHROMEOS)
224 tests_.push_back(MakeSqliteNssCertDbTest());
225 tests_.push_back(MakeSqliteNssKeyDbTest());
226 #endif
229 private:
230 DISALLOW_COPY_AND_ASSIGN(DiagnosticsModelPosix);
233 #endif
235 } // namespace
237 DiagnosticsModel* MakeDiagnosticsModel(const CommandLine& cmdline) {
238 base::FilePath user_data_dir =
239 cmdline.GetSwitchValuePath(switches::kUserDataDir);
240 if (!user_data_dir.empty())
241 PathService::Override(chrome::DIR_USER_DATA, user_data_dir);
242 #if defined(OS_WIN)
243 return new DiagnosticsModelWin();
244 #elif defined(OS_MACOSX)
245 return new DiagnosticsModelMac();
246 #elif defined(OS_POSIX)
247 return new DiagnosticsModelPosix();
248 #endif
251 } // namespace diagnostics