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. 4 5 #include "chrome/browser/diagnostics/diagnostics_model.h" 6 7 #include <algorithm> 8 #include <vector> 9 10 #include "base/basictypes.h" 11 #include "base/command_line.h" 12 #include "base/file_path.h" 13 #include "base/file_util.h" 14 #include "base/stl_util-inl.h" 15 #include "base/string_util.h" 16 #include "base/path_service.h" 17 #include "chrome/browser/diagnostics/diagnostics_test.h" 18 #include "chrome/browser/diagnostics/recon_diagnostics.h" 19 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" 20 #include "chrome/common/chrome_paths.h" 21 #include "chrome/common/chrome_switches.h" 22 23 namespace { 24 25 // Embodies the commonalities of the model across platforms. It manages the 26 // list of tests and can loop over them. The main job of the platform specific 27 // code becomes: 28 // 1- Inserting the appropiate tests into |tests_| 29 // 2- Overriding RunTest() to wrap it with the appropiate fatal exception 30 // handler for the OS. 31 // This class owns the all the tests and will only delete them upon 32 // destruction. 33 class DiagnosticsModelImpl : public DiagnosticsModel { 34 public: 35 DiagnosticsModelImpl() : tests_run_(0) { 36 } 37 38 ~DiagnosticsModelImpl() { 39 STLDeleteElements(&tests_); 40 } 41 42 virtual int GetTestRunCount() { 43 return tests_run_; 44 } 45 46 virtual int GetTestAvailableCount() { 47 return tests_.size(); 48 } 49 50 virtual void RunAll(DiagnosticsModel::Observer* observer) { 51 size_t test_count = tests_.size(); 52 for (size_t ix = 0; ix != test_count; ++ix) { 53 bool do_next = RunTest(tests_[ix], observer, ix); 54 ++tests_run_; 55 if (!do_next) 56 break; 57 } 58 observer->OnDoneAll(this); 59 } 60 61 virtual TestInfo& GetTest(size_t id) { 62 return *tests_[id]; 63 } 64 65 protected: 66 // Run a particular test. Return false if no other tests should be run. 67 virtual bool RunTest(DiagnosticTest* test, Observer* observer, size_t index) { 68 return test->Execute(observer, this, index); 69 } 70 71 typedef std::vector<DiagnosticTest*> TestArray; 72 TestArray tests_; 73 int tests_run_; 74 75 private: 76 DISALLOW_COPY_AND_ASSIGN(DiagnosticsModelImpl); 77 }; 78 79 // Each platform can have their own tests. For the time being there is only 80 // one test that works on all platforms. 81 #if defined(OS_WIN) 82 class DiagnosticsModelWin : public DiagnosticsModelImpl { 83 public: 84 DiagnosticsModelWin() { 85 tests_.push_back(MakeOperatingSystemTest()); 86 tests_.push_back(MakeConflictingDllsTest()); 87 tests_.push_back(MakeInstallTypeTest()); 88 tests_.push_back(MakeVersionTest()); 89 tests_.push_back(MakeUserDirTest()); 90 tests_.push_back(MakeLocalStateFileTest()); 91 tests_.push_back(MakeDictonaryDirTest()); 92 tests_.push_back(MakeResourcesFileTest()); 93 tests_.push_back(MakeDiskSpaceTest()); 94 tests_.push_back(MakePreferencesTest()); 95 tests_.push_back(MakeLocalStateTest()); 96 tests_.push_back(MakeBookMarksTest()); 97 tests_.push_back(MakeSqliteWebDbTest()); 98 tests_.push_back(MakeSqliteCookiesDbTest()); 99 tests_.push_back(MakeSqliteHistoryDbTest()); 100 tests_.push_back(MakeSqliteArchivedHistoryDbTest()); 101 tests_.push_back(MakeSqliteThumbnailsDbTest()); 102 tests_.push_back(MakeSqliteAppCacheDbTest()); 103 tests_.push_back(MakeSqliteWebDatabaseTrackerDbTest()); 104 } 105 106 private: 107 DISALLOW_COPY_AND_ASSIGN(DiagnosticsModelWin); 108 }; 109 110 #elif defined(OS_MACOSX) 111 class DiagnosticsModelMac : public DiagnosticsModelImpl { 112 public: 113 DiagnosticsModelMac() { 114 tests_.push_back(MakeInstallTypeTest()); 115 tests_.push_back(MakeUserDirTest()); 116 tests_.push_back(MakeLocalStateFileTest()); 117 tests_.push_back(MakeDictonaryDirTest()); 118 tests_.push_back(MakeResourcesFileTest()); 119 tests_.push_back(MakeDiskSpaceTest()); 120 tests_.push_back(MakePreferencesTest()); 121 tests_.push_back(MakeLocalStateTest()); 122 tests_.push_back(MakeBookMarksTest()); 123 tests_.push_back(MakeSqliteWebDbTest()); 124 tests_.push_back(MakeSqliteCookiesDbTest()); 125 tests_.push_back(MakeSqliteHistoryDbTest()); 126 tests_.push_back(MakeSqliteArchivedHistoryDbTest()); 127 tests_.push_back(MakeSqliteThumbnailsDbTest()); 128 tests_.push_back(MakeSqliteAppCacheDbTest()); 129 tests_.push_back(MakeSqliteWebDatabaseTrackerDbTest()); 130 } 131 132 private: 133 DISALLOW_COPY_AND_ASSIGN(DiagnosticsModelMac); 134 }; 135 136 #elif defined(OS_POSIX) 137 class DiagnosticsModelPosix : public DiagnosticsModelImpl { 138 public: 139 DiagnosticsModelPosix() { 140 tests_.push_back(MakeInstallTypeTest()); 141 tests_.push_back(MakeVersionTest()); 142 tests_.push_back(MakeUserDirTest()); 143 tests_.push_back(MakeLocalStateFileTest()); 144 tests_.push_back(MakeDictonaryDirTest()); 145 tests_.push_back(MakeResourcesFileTest()); 146 tests_.push_back(MakeDiskSpaceTest()); 147 tests_.push_back(MakePreferencesTest()); 148 tests_.push_back(MakeLocalStateTest()); 149 tests_.push_back(MakeBookMarksTest()); 150 tests_.push_back(MakeSqliteWebDbTest()); 151 tests_.push_back(MakeSqliteCookiesDbTest()); 152 tests_.push_back(MakeSqliteHistoryDbTest()); 153 tests_.push_back(MakeSqliteArchivedHistoryDbTest()); 154 tests_.push_back(MakeSqliteThumbnailsDbTest()); 155 tests_.push_back(MakeSqliteAppCacheDbTest()); 156 tests_.push_back(MakeSqliteWebDatabaseTrackerDbTest()); 157 } 158 159 private: 160 DISALLOW_COPY_AND_ASSIGN(DiagnosticsModelPosix); 161 }; 162 163 #endif 164 165 } // namespace 166 167 DiagnosticsModel* MakeDiagnosticsModel(const CommandLine& cmdline) { 168 FilePath user_data_dir = cmdline.GetSwitchValuePath(switches::kUserDataDir); 169 if (!user_data_dir.empty()) 170 PathService::Override(chrome::DIR_USER_DATA, user_data_dir); 171 #if defined(OS_WIN) 172 return new DiagnosticsModelWin(); 173 #elif defined(OS_MACOSX) 174 return new DiagnosticsModelMac(); 175 #elif defined(OS_POSIX) 176 return new DiagnosticsModelPosix(); 177 #endif 178 } 179