OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/diagnostics/diagnostics_model.h" | 5 #include "chrome/browser/diagnostics/diagnostics_model.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <memory> |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
10 #include "base/command_line.h" | 11 #include "base/command_line.h" |
11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
12 #include "base/macros.h" | 13 #include "base/macros.h" |
13 #include "base/path_service.h" | 14 #include "base/path_service.h" |
14 #include "base/stl_util.h" | |
15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
17 #include "chrome/browser/diagnostics/diagnostics_test.h" | 17 #include "chrome/browser/diagnostics/diagnostics_test.h" |
18 #include "chrome/browser/diagnostics/recon_diagnostics.h" | 18 #include "chrome/browser/diagnostics/recon_diagnostics.h" |
19 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" | 19 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" |
20 #include "chrome/common/chrome_paths.h" | 20 #include "chrome/common/chrome_paths.h" |
21 #include "chrome/common/chrome_switches.h" | 21 #include "chrome/common/chrome_switches.h" |
22 | 22 |
23 namespace diagnostics { | 23 namespace diagnostics { |
24 | 24 |
(...skipping 18 matching lines...) Expand all Loading... |
43 // code becomes: | 43 // code becomes: |
44 // 1- Inserting the appropriate tests into |tests_| | 44 // 1- Inserting the appropriate tests into |tests_| |
45 // 2- Overriding RunTest() to wrap it with the appropriate fatal exception | 45 // 2- Overriding RunTest() to wrap it with the appropriate fatal exception |
46 // handler for the OS. | 46 // handler for the OS. |
47 // This class owns the all the tests and will only delete them upon | 47 // This class owns the all the tests and will only delete them upon |
48 // destruction. | 48 // destruction. |
49 class DiagnosticsModelImpl : public DiagnosticsModel { | 49 class DiagnosticsModelImpl : public DiagnosticsModel { |
50 public: | 50 public: |
51 DiagnosticsModelImpl() : tests_run_(0) {} | 51 DiagnosticsModelImpl() : tests_run_(0) {} |
52 | 52 |
53 ~DiagnosticsModelImpl() override { base::STLDeleteElements(&tests_); } | 53 ~DiagnosticsModelImpl() override {} |
54 | 54 |
55 int GetTestRunCount() const override { return tests_run_; } | 55 int GetTestRunCount() const override { return tests_run_; } |
56 | 56 |
57 int GetTestAvailableCount() const override { return tests_.size(); } | 57 int GetTestAvailableCount() const override { return tests_.size(); } |
58 | 58 |
59 void RunAll(DiagnosticsModel::Observer* observer) override { | 59 void RunAll(DiagnosticsModel::Observer* observer) override { |
60 size_t test_count = tests_.size(); | 60 size_t test_count = tests_.size(); |
61 bool continue_running = true; | 61 bool continue_running = true; |
62 for (size_t i = 0; i != test_count; ++i) { | 62 for (size_t i = 0; i != test_count; ++i) { |
63 // If one of the diagnostic steps returns false, we want to | 63 // If one of the diagnostic steps returns false, we want to |
64 // mark the rest of them as "skipped" in the UMA stats. | 64 // mark the rest of them as "skipped" in the UMA stats. |
65 if (continue_running) { | 65 if (continue_running) { |
66 continue_running = RunTest(tests_[i], observer, i); | 66 continue_running = RunTest(tests_[i].get(), observer, i); |
67 ++tests_run_; | 67 ++tests_run_; |
68 } else { | 68 } else { |
69 #if defined(OS_CHROMEOS) // Only collecting UMA stats on ChromeOS | 69 #if defined(OS_CHROMEOS) // Only collecting UMA stats on ChromeOS |
70 RecordUMATestResult(static_cast<DiagnosticsTestId>(tests_[i]->GetId()), | 70 RecordUMATestResult(static_cast<DiagnosticsTestId>(tests_[i]->GetId()), |
71 RESULT_SKIPPED); | 71 RESULT_SKIPPED); |
72 #else | 72 #else |
73 // On other platforms, we can just bail out if a diagnostic step returns | 73 // On other platforms, we can just bail out if a diagnostic step returns |
74 // false. | 74 // false. |
75 break; | 75 break; |
76 #endif | 76 #endif |
77 } | 77 } |
78 } | 78 } |
79 if (observer) | 79 if (observer) |
80 observer->OnAllTestsDone(this); | 80 observer->OnAllTestsDone(this); |
81 } | 81 } |
82 | 82 |
83 void RecoverAll(DiagnosticsModel::Observer* observer) override { | 83 void RecoverAll(DiagnosticsModel::Observer* observer) override { |
84 size_t test_count = tests_.size(); | 84 size_t test_count = tests_.size(); |
85 bool continue_running = true; | 85 bool continue_running = true; |
86 for (size_t i = 0; i != test_count; ++i) { | 86 for (size_t i = 0; i != test_count; ++i) { |
87 // If one of the recovery steps returns false, we want to | 87 // If one of the recovery steps returns false, we want to |
88 // mark the rest of them as "skipped" in the UMA stats. | 88 // mark the rest of them as "skipped" in the UMA stats. |
89 if (continue_running) { | 89 if (continue_running) { |
90 continue_running = RunRecovery(tests_[i], observer, i); | 90 continue_running = RunRecovery(tests_[i].get(), observer, i); |
91 } else { | 91 } else { |
92 #if defined(OS_CHROMEOS) // Only collecting UMA stats on ChromeOS | 92 #if defined(OS_CHROMEOS) // Only collecting UMA stats on ChromeOS |
93 RecordUMARecoveryResult( | 93 RecordUMARecoveryResult( |
94 static_cast<DiagnosticsTestId>(tests_[i]->GetId()), RESULT_SKIPPED); | 94 static_cast<DiagnosticsTestId>(tests_[i]->GetId()), RESULT_SKIPPED); |
95 #else | 95 #else |
96 // On other platforms, we can just bail out if a recovery step returns | 96 // On other platforms, we can just bail out if a recovery step returns |
97 // false. | 97 // false. |
98 break; | 98 break; |
99 #endif | 99 #endif |
100 } | 100 } |
101 } | 101 } |
102 if (observer) | 102 if (observer) |
103 observer->OnAllRecoveryDone(this); | 103 observer->OnAllRecoveryDone(this); |
104 } | 104 } |
105 | 105 |
106 const TestInfo& GetTest(size_t index) const override { | 106 const TestInfo& GetTest(size_t index) const override { |
107 return *tests_[index]; | 107 return *tests_[index]; |
108 } | 108 } |
109 | 109 |
110 bool GetTestInfo(int id, const TestInfo** result) const override { | 110 bool GetTestInfo(int id, const TestInfo** result) const override { |
111 DCHECK(id < DIAGNOSTICS_TEST_ID_COUNT); | 111 DCHECK(id < DIAGNOSTICS_TEST_ID_COUNT); |
112 DCHECK(id >= 0); | 112 DCHECK(id >= 0); |
113 for (size_t i = 0; i < tests_.size(); i++) { | 113 for (const auto& test : tests_) { |
114 if (tests_[i]->GetId() == id) { | 114 if (test->GetId() == id) { |
115 *result = tests_[i]; | 115 *result = test.get(); |
116 return true; | 116 return true; |
117 } | 117 } |
118 } | 118 } |
119 return false; | 119 return false; |
120 } | 120 } |
121 | 121 |
122 protected: | 122 protected: |
123 // Run a particular diagnostic test. Return false if no other tests should be | 123 // Run a particular diagnostic test. Return false if no other tests should be |
124 // run. | 124 // run. |
125 virtual bool RunTest(DiagnosticsTest* test, | 125 virtual bool RunTest(DiagnosticsTest* test, |
126 Observer* observer, | 126 Observer* observer, |
127 size_t index) { | 127 size_t index) { |
128 return test->Execute(observer, this, index); | 128 return test->Execute(observer, this, index); |
129 } | 129 } |
130 | 130 |
131 // Recover from a particular diagnostic test. Return false if no further | 131 // Recover from a particular diagnostic test. Return false if no further |
132 // recovery should be run. | 132 // recovery should be run. |
133 virtual bool RunRecovery(DiagnosticsTest* test, | 133 virtual bool RunRecovery(DiagnosticsTest* test, |
134 Observer* observer, | 134 Observer* observer, |
135 size_t index) { | 135 size_t index) { |
136 return test->Recover(observer, this, index); | 136 return test->Recover(observer, this, index); |
137 } | 137 } |
138 | 138 |
139 typedef std::vector<DiagnosticsTest*> TestArray; | 139 std::vector<std::unique_ptr<DiagnosticsTest>> tests_; |
140 TestArray tests_; | |
141 int tests_run_; | 140 int tests_run_; |
142 | 141 |
143 private: | 142 private: |
144 DISALLOW_COPY_AND_ASSIGN(DiagnosticsModelImpl); | 143 DISALLOW_COPY_AND_ASSIGN(DiagnosticsModelImpl); |
145 }; | 144 }; |
146 | 145 |
147 // Each platform can have their own tests. For the time being there is only | 146 // Each platform can have their own tests. For the time being there is only |
148 // one test that works on all platforms. | 147 // one test that works on all platforms. |
149 #if defined(OS_WIN) | 148 #if defined(OS_WIN) |
150 class DiagnosticsModelWin : public DiagnosticsModelImpl { | 149 class DiagnosticsModelWin : public DiagnosticsModelImpl { |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 #if defined(OS_WIN) | 238 #if defined(OS_WIN) |
240 return new DiagnosticsModelWin(); | 239 return new DiagnosticsModelWin(); |
241 #elif defined(OS_MACOSX) | 240 #elif defined(OS_MACOSX) |
242 return new DiagnosticsModelMac(); | 241 return new DiagnosticsModelMac(); |
243 #elif defined(OS_POSIX) | 242 #elif defined(OS_POSIX) |
244 return new DiagnosticsModelPosix(); | 243 return new DiagnosticsModelPosix(); |
245 #endif | 244 #endif |
246 } | 245 } |
247 | 246 |
248 } // namespace diagnostics | 247 } // namespace diagnostics |
OLD | NEW |