[email protected] | 26f4647 | 2009-12-20 03:57:10 | [diff] [blame] | 1 | // Copyright (c) 2009 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 "base/file_util.h" |
| 6 | #include "base/string_util.h" |
| 7 | #include "base/path_service.h" |
| 8 | #include "chrome/browser/diagnostics/diagnostics_test.h" |
| 9 | #include "chrome/common/chrome_paths.h" |
| 10 | #include "app/app_paths.h" |
| 11 | |
| 12 | #if defined(OS_WIN) |
| 13 | #include "base/win_util.h" |
| 14 | #include "chrome/installer/util/install_util.h" |
| 15 | #endif |
| 16 | |
| 17 | // Reconnaissance diagnostics. These are the first and most critical |
| 18 | // diagnostic tests. Here we check for the existence of critical files. |
| 19 | // TODO(cpu): Define if it makes sense to localize strings. |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | class InstallTypeTest; |
| 24 | InstallTypeTest* g_install_type = 0; |
| 25 | |
| 26 | #if defined(OS_WIN) |
| 27 | // Check that we know what flavor of windows is and that is not an |
| 28 | // unsuported operating system. |
| 29 | class WinOSIdTest : public DiagnosticTest { |
| 30 | public: |
| 31 | WinOSIdTest() : DiagnosticTest(ASCIIToUTF16("Operating System")) {} |
| 32 | |
| 33 | virtual int GetId() { return 0; } |
| 34 | |
| 35 | virtual bool ExecuteImpl(DiagnosticsModel::Observer* observer) { |
| 36 | win_util::WinVersion version = win_util::GetWinVersion(); |
| 37 | if (version < win_util::WINVERSION_XP) { |
| 38 | RecordFailure(ASCIIToUTF16("Windows 2000 or earlier")); |
| 39 | return false; |
| 40 | } |
| 41 | int major = 0; |
| 42 | int minor = 0; |
| 43 | string16 os_name; |
| 44 | win_util::GetServicePackLevel(&major, &minor); |
| 45 | if ((version == win_util::WINVERSION_XP) && (major < 2)) { |
| 46 | RecordFailure(ASCIIToUTF16("XP Service Pack 1 or earlier")); |
| 47 | return false; |
| 48 | } |
| 49 | RecordSuccess(ASCIIToUTF16(StringPrintf("Windows %d [%d:%d]", |
| 50 | version, major, minor))); |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | private: |
| 55 | DISALLOW_COPY_AND_ASSIGN(WinOSIdTest); |
| 56 | }; |
| 57 | |
| 58 | // Check if it is system install or per-user install. |
| 59 | class InstallTypeTest : public DiagnosticTest { |
| 60 | public: |
| 61 | InstallTypeTest() : DiagnosticTest(ASCIIToUTF16("Install Type")), |
| 62 | user_level_(false) {} |
| 63 | |
| 64 | virtual int GetId() { return 0; } |
| 65 | |
| 66 | virtual bool ExecuteImpl(DiagnosticsModel::Observer* observer) { |
| 67 | FilePath chrome_exe; |
| 68 | if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 69 | RecordFailure(ASCIIToUTF16("Path provider failure")); |
| 70 | return false; |
| 71 | } |
| 72 | user_level_ = InstallUtil::IsPerUserInstall( |
| 73 | chrome_exe.ToWStringHack().c_str()); |
| 74 | string16 install_type(ASCIIToUTF16(user_level_ ? "User Level" : |
| 75 | "System Level")); |
| 76 | RecordSuccess(install_type); |
| 77 | g_install_type = this; |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | bool system_level() const { return !user_level_; } |
| 82 | |
| 83 | private: |
| 84 | bool user_level_; |
| 85 | DISALLOW_COPY_AND_ASSIGN(InstallTypeTest); |
| 86 | }; |
| 87 | |
| 88 | #else |
| 89 | // For Mac and Linux we just assume is a system level install. |
| 90 | class InstallTypeTest : public DiagnosticTest { |
| 91 | public: |
| 92 | InstallTypeTest() : DiagnosticTest(ASCIIToUTF16("Install Type")) {} |
| 93 | |
| 94 | virtual int GetId() { return 0; } |
| 95 | |
| 96 | virtual bool ExecuteImpl(DiagnosticsModel::Observer* observer) { |
| 97 | g_install_type = this; |
| 98 | RecordSuccess(ASCIIToUTF16("OK")); |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | bool system_level() const { return true; } |
| 103 | |
| 104 | private: |
| 105 | DISALLOW_COPY_AND_ASSIGN(InstallTypeTest); |
| 106 | }; |
| 107 | #endif // defined(OS_WIN) |
| 108 | |
| 109 | struct TestPathInfo { |
| 110 | const char* test_name; |
| 111 | int dir_id; |
| 112 | bool test_writable; |
[email protected] | 6d745ac | 2010-01-14 22:12:59 | [diff] [blame^] | 113 | int64 max_size; |
[email protected] | 26f4647 | 2009-12-20 03:57:10 | [diff] [blame] | 114 | }; |
| 115 | |
[email protected] | 6d745ac | 2010-01-14 22:12:59 | [diff] [blame^] | 116 | const int64 kOneKilo = 1024; |
| 117 | const int64 kOneMeg = 1024 * kOneKilo; |
| 118 | |
[email protected] | 26f4647 | 2009-12-20 03:57:10 | [diff] [blame] | 119 | const TestPathInfo kPathsToTest[] = { |
[email protected] | 6d745ac | 2010-01-14 22:12:59 | [diff] [blame^] | 120 | {"User data Directory", chrome::DIR_USER_DATA, true, 250 * kOneMeg}, |
| 121 | {"Local state file", chrome::FILE_LOCAL_STATE, true, 200 * kOneKilo}, |
| 122 | {"Resources module", chrome::FILE_RESOURCE_MODULE, false, 0}, |
| 123 | {"Dictionaries Directory", chrome::DIR_APP_DICTIONARIES, false, 0}, |
| 124 | {"Inspector Directory", chrome::DIR_INSPECTOR, false, 0} |
[email protected] | 26f4647 | 2009-12-20 03:57:10 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | // Check that the user's data directory exists and the paths are writeable. |
| 128 | // If it is a systemwide install some paths are not expected to be writeable. |
| 129 | // This test depends on |InstallTypeTest| having run succesfuly. |
| 130 | class PathTest : public DiagnosticTest { |
| 131 | public: |
| 132 | explicit PathTest(const TestPathInfo& path_info) |
| 133 | : DiagnosticTest(ASCIIToUTF16(path_info.test_name)), |
| 134 | path_info_(path_info) {} |
| 135 | |
| 136 | virtual int GetId() { return 0; } |
| 137 | |
| 138 | virtual bool ExecuteImpl(DiagnosticsModel::Observer* observer) { |
| 139 | if (!g_install_type) { |
| 140 | RecordStopFailure(ASCIIToUTF16("dependency failure")); |
| 141 | return false; |
| 142 | } |
[email protected] | 6d745ac | 2010-01-14 22:12:59 | [diff] [blame^] | 143 | FilePath dir_or_file; |
| 144 | if (!PathService::Get(path_info_.dir_id, &dir_or_file)) { |
[email protected] | 26f4647 | 2009-12-20 03:57:10 | [diff] [blame] | 145 | RecordStopFailure(ASCIIToUTF16("Path provider failure")); |
| 146 | return false; |
| 147 | } |
[email protected] | 6d745ac | 2010-01-14 22:12:59 | [diff] [blame^] | 148 | if (!file_util::PathExists(dir_or_file)) { |
[email protected] | 26f4647 | 2009-12-20 03:57:10 | [diff] [blame] | 149 | RecordFailure(ASCIIToUTF16("Path not found")); |
| 150 | return true; |
| 151 | } |
[email protected] | 6d745ac | 2010-01-14 22:12:59 | [diff] [blame^] | 152 | |
| 153 | int64 dir_or_file_size; |
| 154 | if (!file_util::GetFileSize(dir_or_file, &dir_or_file_size)) { |
| 155 | RecordFailure(ASCIIToUTF16("Cannot obtain size")); |
| 156 | return true; |
| 157 | } |
| 158 | DataUnits units = GetByteDisplayUnits(dir_or_file_size); |
| 159 | string16 printable_size = |
| 160 | WideToUTF16(FormatBytes(dir_or_file_size, units, true)); |
| 161 | |
| 162 | if (path_info_.max_size > 0) { |
| 163 | if (dir_or_file_size > path_info_.max_size) { |
| 164 | RecordFailure(ASCIIToUTF16("Path is too big: ") + printable_size); |
| 165 | return true; |
| 166 | } |
| 167 | } |
[email protected] | 26f4647 | 2009-12-20 03:57:10 | [diff] [blame] | 168 | if (g_install_type->system_level() && !path_info_.test_writable) { |
| 169 | RecordSuccess(ASCIIToUTF16("Path exists")); |
| 170 | return true; |
| 171 | } |
[email protected] | 6d745ac | 2010-01-14 22:12:59 | [diff] [blame^] | 172 | if (!file_util::PathIsWritable(dir_or_file)) { |
[email protected] | 26f4647 | 2009-12-20 03:57:10 | [diff] [blame] | 173 | RecordFailure(ASCIIToUTF16("Path is not writable")); |
| 174 | return true; |
| 175 | } |
[email protected] | 6d745ac | 2010-01-14 22:12:59 | [diff] [blame^] | 176 | RecordSuccess(ASCIIToUTF16("Path exists and is writable: ") + printable_size); |
[email protected] | 26f4647 | 2009-12-20 03:57:10 | [diff] [blame] | 177 | return true; |
| 178 | } |
| 179 | |
| 180 | private: |
| 181 | TestPathInfo path_info_; |
| 182 | DISALLOW_COPY_AND_ASSIGN(PathTest); |
| 183 | }; |
| 184 | |
| 185 | } // namespace |
| 186 | |
| 187 | DiagnosticTest* MakeUserDirTest() { |
| 188 | return new PathTest(kPathsToTest[0]); |
| 189 | } |
| 190 | |
| 191 | DiagnosticTest* MakeLocalStateFileTest() { |
| 192 | return new PathTest(kPathsToTest[1]); |
| 193 | } |
| 194 | |
| 195 | DiagnosticTest* MakeResourceFileTest() { |
| 196 | return new PathTest(kPathsToTest[2]); |
| 197 | } |
| 198 | |
| 199 | DiagnosticTest* MakeDictonaryDirTest() { |
| 200 | return new PathTest(kPathsToTest[3]); |
| 201 | } |
| 202 | |
| 203 | DiagnosticTest* MakeInspectorDirTest() { |
| 204 | return new PathTest(kPathsToTest[4]); |
| 205 | } |
| 206 | |
| 207 | #if defined(OS_WIN) |
| 208 | DiagnosticTest* MakeWinOsIdTest() { |
| 209 | return new WinOSIdTest(); |
| 210 | } |
| 211 | #endif // defined(OS_WIN) |
| 212 | |
| 213 | DiagnosticTest* MakeInstallTypeTest() { |
| 214 | return new InstallTypeTest(); |
| 215 | } |
| 216 | |