summaryrefslogtreecommitdiffstats
path: root/content/common/web_database_observer_impl.cc
blob: d8483cd01ff6d9aa90acd462b312317c002a8eaa (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
// 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 "content/common/web_database_observer_impl.h"

#include "base/metrics/histogram.h"
#include "base/string16.h"
#include "content/common/database_messages.h"
#include "third_party/sqlite/sqlite3.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDatabase.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"

using WebKit::WebDatabase;

namespace content {
namespace {

const int kResultHistogramSize = 50;
const int kCallsiteHistogramSize = 10;

int DetermineHistogramResult(int websql_error, int sqlite_error) {
  // If we have a sqlite error, log it after trimming the extended bits.
  // There are 26 possible values, but we leave room for some new ones.
  if (sqlite_error)
    return std::min(sqlite_error & 0xff, 30);

  // Otherwise, websql_error may be an SQLExceptionCode, SQLErrorCode
  // or a DOMExceptionCode, or -1 for success.
  if (websql_error == -1)
    return 0;  // no error

  // SQLExceptionCode starts at 1000
  if (websql_error >= 1000)
    websql_error -= 1000;

  return std::min(websql_error + 30, kResultHistogramSize - 1);
}

#define HISTOGRAM_WEBSQL_RESULT(name, database, callsite, \
                                websql_error, sqlite_error) \
  do { \
    DCHECK(callsite < kCallsiteHistogramSize); \
    int result = DetermineHistogramResult(websql_error, sqlite_error); \
    if (database.isSyncDatabase()) { \
      UMA_HISTOGRAM_ENUMERATION("websql.Sync." name, \
                                result, kResultHistogramSize); \
      if (result) { \
        UMA_HISTOGRAM_ENUMERATION("websql.Sync." name ".ErrorSite", \
                                  callsite, kCallsiteHistogramSize); \
      } \
    } else { \
      UMA_HISTOGRAM_ENUMERATION("websql.Async." name, \
                                result, kResultHistogramSize); \
      if (result) { \
        UMA_HISTOGRAM_ENUMERATION("websql.Async." name ".ErrorSite", \
                                  callsite, kCallsiteHistogramSize); \
      } \
    } \
  } while (0)

}  // namespace

WebDatabaseObserverImpl::WebDatabaseObserverImpl(
    IPC::SyncMessageFilter* sender)
    : sender_(sender),
      open_connections_(new webkit_database::DatabaseConnectionsWrapper) {
  DCHECK(sender);
}

WebDatabaseObserverImpl::~WebDatabaseObserverImpl() {
}

void WebDatabaseObserverImpl::databaseOpened(
    const WebDatabase& database) {
  string16 origin_identifier = database.securityOrigin().databaseIdentifier();
  string16 database_name = database.name();
  open_connections_->AddOpenConnection(origin_identifier, database_name);
  sender_->Send(new DatabaseHostMsg_Opened(
      origin_identifier, database_name,
      database.displayName(), database.estimatedSize()));
}

void WebDatabaseObserverImpl::databaseModified(
    const WebDatabase& database) {
  sender_->Send(new DatabaseHostMsg_Modified(
      database.securityOrigin().databaseIdentifier(), database.name()));
}

void WebDatabaseObserverImpl::databaseClosed(
    const WebDatabase& database) {
  string16 origin_identifier = database.securityOrigin().databaseIdentifier();
  string16 database_name = database.name();
  sender_->Send(new DatabaseHostMsg_Closed(
      origin_identifier, database_name));
  open_connections_->RemoveOpenConnection(origin_identifier, database_name);
}

void WebDatabaseObserverImpl::reportOpenDatabaseResult(
    const WebDatabase& database, int callsite, int websql_error,
    int sqlite_error) {
  HISTOGRAM_WEBSQL_RESULT("OpenResult", database, callsite,
                          websql_error, sqlite_error);
  HandleSqliteError(database, sqlite_error);
}

void WebDatabaseObserverImpl::reportChangeVersionResult(
    const WebDatabase& database, int callsite, int websql_error,
    int sqlite_error) {
  HISTOGRAM_WEBSQL_RESULT("ChangeVersionResult", database, callsite,
                          websql_error, sqlite_error);
  HandleSqliteError(database, sqlite_error);
}

void WebDatabaseObserverImpl::reportStartTransactionResult(
    const WebDatabase& database, int callsite, int websql_error,
    int sqlite_error) {
  HISTOGRAM_WEBSQL_RESULT("BeginResult", database, callsite,
                          websql_error, sqlite_error);
  HandleSqliteError(database, sqlite_error);
}

void WebDatabaseObserverImpl::reportCommitTransactionResult(
    const WebDatabase& database, int callsite, int websql_error,
    int sqlite_error) {
  HISTOGRAM_WEBSQL_RESULT("CommitResult", database, callsite,
                          websql_error, sqlite_error);
  HandleSqliteError(database, sqlite_error);
}

void WebDatabaseObserverImpl::reportExecuteStatementResult(
    const WebDatabase& database, int callsite, int websql_error,
    int sqlite_error) {
  HISTOGRAM_WEBSQL_RESULT("StatementResult", database, callsite,
                          websql_error, sqlite_error);
  HandleSqliteError(database, sqlite_error);
}

void WebDatabaseObserverImpl::reportVacuumDatabaseResult(
    const WebDatabase& database, int sqlite_error) {
  int result = DetermineHistogramResult(-1, sqlite_error);
  if (database.isSyncDatabase()) {
    UMA_HISTOGRAM_ENUMERATION("websql.Sync.VacuumResult",
                              result, kResultHistogramSize);
  } else {
    UMA_HISTOGRAM_ENUMERATION("websql.Async.VacuumResult",
                              result, kResultHistogramSize);
  }
  HandleSqliteError(database, sqlite_error);
}

void WebDatabaseObserverImpl::WaitForAllDatabasesToClose() {
  open_connections_->WaitForAllDatabasesToClose();
}

void WebDatabaseObserverImpl::HandleSqliteError(
    const WebDatabase& database, int error) {
  // We filter out errors which the backend doesn't act on to avoid
  // a unnecessary ipc traffic, this method can get called at a fairly
  // high frequency (per-sqlstatement).
  if (error == SQLITE_CORRUPT || error == SQLITE_NOTADB) {
    sender_->Send(new DatabaseHostMsg_HandleSqliteError(
        database.securityOrigin().databaseIdentifier(),
        database.name(),
        error));
  }
}

}  // namespace content