summaryrefslogtreecommitdiffstats
path: root/chrome/browser/diagnostics/recon_diagnostics.cc
blob: 185c9e2b106c98a6257a9d6ea8ed2286aec93278 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/diagnostics/recon_diagnostics.h"

#include <string>

#include "base/file_util.h"
#include "base/json/json_reader.h"
#include "base/json/json_string_value_serializer.h"
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/sys_info.h"
#include "chrome/browser/diagnostics/diagnostics_test.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_version_info.h"

#if defined(OS_WIN)
#include "base/win/windows_version.h"
#include "chrome/browser/enumerate_modules_model_win.h"
#include "chrome/installer/util/install_util.h"
#endif

// Reconnaissance diagnostics. These are the first and most critical
// diagnostic tests. Here we check for the existence of critical files.
// TODO(cpu): Define if it makes sense to localize strings.

// TODO(cpu): There are a few maximum file sizes hard-coded in this file
// that have little or no theoretical or experimental ground. Find a way
// to justify them.

namespace diagnostics {

namespace {

const int64 kOneKilobyte = 1024;
const int64 kOneMegabyte = 1024 * kOneKilobyte;

class InstallTypeTest;
InstallTypeTest* g_install_type = 0;

// Check if any conflicting DLLs are loaded.
class ConflictingDllsTest : public DiagnosticsTest {
 public:
  ConflictingDllsTest()
      : DiagnosticsTest(DIAGNOSTICS_CONFLICTING_DLLS_TEST) {}

  virtual bool ExecuteImpl(DiagnosticsModel::Observer* observer) OVERRIDE {
#if defined(OS_WIN)
    EnumerateModulesModel* model = EnumerateModulesModel::GetInstance();
    model->set_limited_mode(true);
    model->ScanNow();
    scoped_ptr<ListValue> list(model->GetModuleList());
    if (!model->confirmed_bad_modules_detected() &&
        !model->suspected_bad_modules_detected()) {
      RecordSuccess("No conflicting modules found");
      return true;
    }

    std::string failures = "Possibly conflicting modules:";
    DictionaryValue* dictionary;
    for (size_t i = 0; i < list->GetSize(); ++i) {
      if (!list->GetDictionary(i, &dictionary))
        RecordFailure(DIAG_RECON_DICTIONARY_LOOKUP_FAILED,
                      "Dictionary lookup failed");
      int status;
      std::string location;
      std::string name;
      if (!dictionary->GetInteger("status", &status))
        RecordFailure(DIAG_RECON_NO_STATUS_FIELD, "No 'status' field found");
      if (status < ModuleEnumerator::SUSPECTED_BAD)
        continue;

      if (!dictionary->GetString("location", &location)) {
        RecordFailure(DIAG_RECON_NO_LOCATION_FIELD,
                      "No 'location' field found");
        return true;
      }
      if (!dictionary->GetString("name", &name)) {
        RecordFailure(DIAG_RECON_NO_NAME_FIELD, "No 'name' field found");
        return true;
      }

      failures += "\n" + location + name;
    }
    RecordFailure(DIAG_RECON_CONFLICTING_MODULES, failures);
    return true;
#else
    RecordFailure(DIAG_RECON_NOT_IMPLEMENTED, "Not implemented");
    return true;
#endif  // defined(OS_WIN)
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(ConflictingDllsTest);
};

// Check that the disk space in the volume where the user data directory
// normally lives is not dangerously low.
class DiskSpaceTest : public DiagnosticsTest {
 public:
  DiskSpaceTest() : DiagnosticsTest(DIAGNOSTICS_DISK_SPACE_TEST) {}

  virtual bool ExecuteImpl(DiagnosticsModel::Observer* observer) OVERRIDE {
    base::FilePath data_dir;
    if (!PathService::Get(chrome::DIR_USER_DATA, &data_dir))
      return false;
    int64 disk_space = base::SysInfo::AmountOfFreeDiskSpace(data_dir);
    if (disk_space < 0) {
      RecordFailure(DIAG_RECON_UNABLE_TO_QUERY, "Unable to query free space");
      return true;
    }
    std::string printable_size = base::Int64ToString(disk_space);
    if (disk_space < 80 * kOneMegabyte) {
      RecordFailure(DIAG_RECON_LOW_DISK_SPACE,
                    "Low disk space: " + printable_size);
      return true;
    }
    RecordSuccess("Free space: " + printable_size);
    return true;
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(DiskSpaceTest);
};

// Check if it is system install or per-user install.
class InstallTypeTest : public DiagnosticsTest {
 public:
  InstallTypeTest()
      : DiagnosticsTest(DIAGNOSTICS_INSTALL_TYPE_TEST), user_level_(false) {}

  virtual bool ExecuteImpl(DiagnosticsModel::Observer* observer) OVERRIDE {
#if defined(OS_WIN)
    base::FilePath chrome_exe;
    if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
      RecordFailure(DIAG_RECON_INSTALL_PATH_PROVIDER, "Path provider failure");
      return false;
    }
    user_level_ = InstallUtil::IsPerUserInstall(chrome_exe.value().c_str());
    const char* type = user_level_ ? "User Level" : "System Level";
    std::string install_type(type);
#else
    std::string install_type("System Level");
#endif  // defined(OS_WIN)
    RecordSuccess(install_type);
    g_install_type = this;
    return true;
  }

  bool system_level() const { return !user_level_; }

 private:
  bool user_level_;
  DISALLOW_COPY_AND_ASSIGN(InstallTypeTest);
};

// Checks that a given JSON file can be correctly parsed.
class JSONTest : public DiagnosticsTest {
 public:
  enum FileImportance {
    NON_CRITICAL,
    CRITICAL
  };

  JSONTest(const base::FilePath& path,
           DiagnosticsTestId id,
           int64 max_file_size,
           FileImportance importance)
      : DiagnosticsTest(id),
        path_(path),
        max_file_size_(max_file_size),
        importance_(importance) {}

  virtual bool ExecuteImpl(DiagnosticsModel::Observer* observer) OVERRIDE {
    if (!base::PathExists(path_)) {
      if (importance_ == CRITICAL) {
        RecordOutcome(DIAG_RECON_FILE_NOT_FOUND,
                      "File not found",
                      DiagnosticsModel::TEST_FAIL_CONTINUE);
      } else {
        RecordOutcome(DIAG_RECON_FILE_NOT_FOUND_OK,
                      "File not found (but that is OK)",
                      DiagnosticsModel::TEST_OK);
      }
      return true;
    }
    int64 file_size;
    if (!file_util::GetFileSize(path_, &file_size)) {
      RecordFailure(DIAG_RECON_CANNOT_OBTAIN_FILE_SIZE,
                    "Cannot obtain file size");
      return true;
    }

    if (file_size > max_file_size_) {
      RecordFailure(DIAG_RECON_FILE_TOO_BIG, "File too big");
      return true;
    }
    // Being small enough, we can process it in-memory.
    std::string json_data;
    if (!base::ReadFileToString(path_, &json_data)) {
      RecordFailure(DIAG_RECON_UNABLE_TO_OPEN_FILE,
                    "Could not open file. Possibly locked by another process");
      return true;
    }

    JSONStringValueSerializer json(json_data);
    int error_code = base::JSONReader::JSON_NO_ERROR;
    std::string error_message;
    scoped_ptr<Value> json_root(json.Deserialize(&error_code, &error_message));
    if (base::JSONReader::JSON_NO_ERROR != error_code) {
      if (error_message.empty()) {
        error_message = "Parse error " + base::IntToString(error_code);
      }
      RecordFailure(DIAG_RECON_PARSE_ERROR, error_message);
      return true;
    }

    RecordSuccess("File parsed OK");
    return true;
  }

 private:
  base::FilePath path_;
  int64 max_file_size_;
  FileImportance importance_;
  DISALLOW_COPY_AND_ASSIGN(JSONTest);
};

// Check that the flavor of the operating system is supported.
class OperatingSystemTest : public DiagnosticsTest {
 public:
  OperatingSystemTest()
      : DiagnosticsTest(DIAGNOSTICS_OPERATING_SYSTEM_TEST) {}

  virtual bool ExecuteImpl(DiagnosticsModel::Observer* observer) OVERRIDE {
#if defined(OS_WIN)
    base::win::Version version = base::win::GetVersion();
    if ((version < base::win::VERSION_XP) ||
        ((version == base::win::VERSION_XP) &&
         (base::win::OSInfo::GetInstance()->service_pack().major < 2))) {
      RecordFailure(DIAG_RECON_PRE_WINDOW_XP_SP2,
                    "Must have Windows XP SP2 or later");
      return false;
    }
#else
// TODO(port): define the OS criteria for Linux and Mac.
#endif  // defined(OS_WIN)
    RecordSuccess(
        base::StringPrintf("%s %s",
                           base::SysInfo::OperatingSystemName().c_str(),
                           base::SysInfo::OperatingSystemVersion().c_str()));
    return true;
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(OperatingSystemTest);
};

struct TestPathInfo {
  DiagnosticsTestId test_id;
  int path_id;
  bool is_directory;
  bool is_optional;
  bool test_writable;
  int64 max_size;
};

const TestPathInfo kPathsToTest[] = {
    {DIAGNOSTICS_PATH_DICTIONARIES_TEST, chrome::DIR_APP_DICTIONARIES, true,
     true, false, 0},
    {DIAGNOSTICS_PATH_LOCAL_STATE_TEST, chrome::FILE_LOCAL_STATE, false, false,
     true, 500 * kOneKilobyte},
    {DIAGNOSTICS_PATH_RESOURCES_TEST, chrome::FILE_RESOURCES_PACK, false, false,
     false, 0},
    {DIAGNOSTICS_PATH_USER_DATA_TEST, chrome::DIR_USER_DATA, true, false, true,
     850 * kOneMegabyte},
};

// Check that the user's data directory exists and the paths are writable.
// If it is a system-wide install some paths are not expected to be writable.
// This test depends on |InstallTypeTest| having run successfully.
class PathTest : public DiagnosticsTest {
 public:
  explicit PathTest(const TestPathInfo& path_info)
      : DiagnosticsTest(path_info.test_id),
        path_info_(path_info) {}

  virtual bool ExecuteImpl(DiagnosticsModel::Observer* observer) OVERRIDE {
    if (!g_install_type) {
      RecordStopFailure(DIAG_RECON_DEPENDENCY, "Install dependency failure");
      return false;
    }
    base::FilePath dir_or_file;
    if (!PathService::Get(path_info_.path_id, &dir_or_file)) {
      RecordStopFailure(DIAG_RECON_PATH_PROVIDER, "Path provider failure");
      return false;
    }
    if (!base::PathExists(dir_or_file)) {
      RecordFailure(
          DIAG_RECON_PATH_NOT_FOUND,
          "Path not found: " + UTF16ToUTF8(dir_or_file.LossyDisplayName()));
      return true;
    }

    int64 dir_or_file_size = 0;
    if (path_info_.is_directory) {
      dir_or_file_size = base::ComputeDirectorySize(dir_or_file);
    } else {
      file_util::GetFileSize(dir_or_file, &dir_or_file_size);
    }
    if (!dir_or_file_size && !path_info_.is_optional) {
      RecordFailure(DIAG_RECON_CANNOT_OBTAIN_SIZE,
                    "Cannot obtain size for: " +
                        UTF16ToUTF8(dir_or_file.LossyDisplayName()));
      return true;
    }
    std::string printable_size = base::Int64ToString(dir_or_file_size);

    if (path_info_.max_size > 0) {
      if (dir_or_file_size > path_info_.max_size) {
        RecordFailure(DIAG_RECON_FILE_TOO_LARGE,
                      "Path contents too large (" + printable_size + ") for: " +
                          UTF16ToUTF8(dir_or_file.LossyDisplayName()));
        return true;
      }
    }
    if (g_install_type->system_level() && !path_info_.test_writable) {
      RecordSuccess("Path exists");
      return true;
    }
    if (!base::PathIsWritable(dir_or_file)) {
      RecordFailure(DIAG_RECON_NOT_WRITABLE,
                    "Path is not writable: " +
                        UTF16ToUTF8(dir_or_file.LossyDisplayName()));
      return true;
    }
    RecordSuccess("Path exists and is writable: " + printable_size);
    return true;
  }

 private:
  TestPathInfo path_info_;
  DISALLOW_COPY_AND_ASSIGN(PathTest);
};

// Check the version of Chrome.
class VersionTest : public DiagnosticsTest {
 public:
  VersionTest() : DiagnosticsTest(DIAGNOSTICS_VERSION_TEST) {}

  virtual bool ExecuteImpl(DiagnosticsModel::Observer* observer) OVERRIDE {
    chrome::VersionInfo version_info;
    if (!version_info.is_valid()) {
      RecordFailure(DIAG_RECON_NO_VERSION, "No Version");
      return true;
    }
    std::string current_version = version_info.Version();
    if (current_version.empty()) {
      RecordFailure(DIAG_RECON_EMPTY_VERSION, "Empty Version");
      return true;
    }
    std::string version_modifier =
        chrome::VersionInfo::GetVersionStringModifier();
    if (!version_modifier.empty())
      current_version += " " + version_modifier;
#if defined(GOOGLE_CHROME_BUILD)
    current_version += " GCB";
#endif  // defined(GOOGLE_CHROME_BUILD)
    RecordSuccess(current_version);
    return true;
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(VersionTest);
};

}  // namespace

DiagnosticsTest* MakeConflictingDllsTest() { return new ConflictingDllsTest(); }

DiagnosticsTest* MakeDiskSpaceTest() { return new DiskSpaceTest(); }

DiagnosticsTest* MakeInstallTypeTest() { return new InstallTypeTest(); }

DiagnosticsTest* MakeBookMarksTest() {
  base::FilePath path = DiagnosticsTest::GetUserDefaultProfileDir();
  path = path.Append(chrome::kBookmarksFileName);
  return new JSONTest(path,
                      DIAGNOSTICS_JSON_BOOKMARKS_TEST,
                      2 * kOneMegabyte,
                      JSONTest::NON_CRITICAL);
}

DiagnosticsTest* MakeLocalStateTest() {
  base::FilePath path;
  PathService::Get(chrome::DIR_USER_DATA, &path);
  path = path.Append(chrome::kLocalStateFilename);
  return new JSONTest(path,
                      DIAGNOSTICS_JSON_LOCAL_STATE_TEST,
                      50 * kOneKilobyte,
                      JSONTest::CRITICAL);
}

DiagnosticsTest* MakePreferencesTest() {
  base::FilePath path = DiagnosticsTest::GetUserDefaultProfileDir();
  path = path.Append(chrome::kPreferencesFilename);
  return new JSONTest(path,
                      DIAGNOSTICS_JSON_PREFERENCES_TEST,
                      100 * kOneKilobyte,
                      JSONTest::CRITICAL);
}


DiagnosticsTest* MakeOperatingSystemTest() { return new OperatingSystemTest(); }

DiagnosticsTest* MakeDictonaryDirTest() {
  return new PathTest(kPathsToTest[0]);
}

DiagnosticsTest* MakeLocalStateFileTest() {
  return new PathTest(kPathsToTest[1]);
}

DiagnosticsTest* MakeResourcesFileTest() {
  return new PathTest(kPathsToTest[2]);
}

DiagnosticsTest* MakeUserDirTest() { return new PathTest(kPathsToTest[3]); }

DiagnosticsTest* MakeVersionTest() { return new VersionTest(); }

}  // namespace diagnostics