summaryrefslogtreecommitdiffstats
path: root/chrome/browser/diagnostics/sqlite_diagnostics.cc
blob: f3ca31d8532954e80890a1c1dc25315eddc44f91 (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
// Copyright (c) 2009 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/sqlite_diagnostics.h"

#include "app/sql/connection.h"
#include "base/histogram.h"
#include "base/logging.h"
#include "base/singleton.h"

namespace {

const char* kHistogramNames[] = {
  "Sqlite.Cookie.Error",
  "Sqlite.History.Error",
  "Sqlite.Thumbnail.Error",
  "Sqlite.Text.Error",
  "Sqlite.Web.Error"
};

// This class handles the exceptional sqlite errors that we might encounter
// if for example the db is corrupted. Right now we just generate a UMA
// histogram for release and an assert for debug builds.
//
// Why is it a template you ask? well, that is a funny story. The histograms
// need to be singletons that is why they are always static at the function
// scope, but we cannot use the Singleton class because they are not default
// constructible. The template parameter makes the compiler to create unique
// classes that don't share the same static variable.
template <size_t unique>
class BasicSqliteErrrorHandler : public sql::ErrorDelegate {
 public:

  virtual int OnError(int error, sql::Connection* connection,
                      sql::Statement* stmt) {
    NOTREACHED() << "sqlite error " << error;
    RecordErrorInHistogram(error);
    return error;
  }

 private:
  static void RecordErrorInHistogram(int error) {
    // The histogram values from sqlite result codes go currently from 1 to
    // 26 currently but 50 gives them room to grow.
    static LinearHistogram histogram(kHistogramNames[unique], 1, 50, 51);
    histogram.SetFlags(kUmaTargetedHistogramFlag);
    histogram.Add(error);
  }
};

}  // namespace

sql::ErrorDelegate* GetErrorHandlerForCookieDb() {
  return new BasicSqliteErrrorHandler<0>();
}

sql::ErrorDelegate* GetErrorHandlerForHistoryDb() {
  return new BasicSqliteErrrorHandler<1>();
}

sql::ErrorDelegate* GetErrorHandlerForThumbnailDb() {
  return new BasicSqliteErrrorHandler<2>();
}

sql::ErrorDelegate* GetErrorHandlerForTextDb() {
  return new BasicSqliteErrrorHandler<3>();
}

sql::ErrorDelegate* GetErrorHandlerForWebDb() {
  return new BasicSqliteErrrorHandler<4>();
}