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
3 // found in the LICENSE file.
5 #include "content/common/web_database_observer_impl.h"
7 #include "base/metrics/histogram.h"
8 #include "base/string16.h"
9 #include "content/common/database_messages.h"
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDatabase.h"
11 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
13 using WebKit::WebDatabase
;
17 const int kResultHistogramSize
= 50;
18 const int kCallsiteHistogramSize
= 10;
20 int DetermineHistogramResult(int websql_error
, int sqlite_error
) {
21 // If we have a sqlite error, log it after trimming the extended bits.
22 // There are 26 possible values, but we leave room for some new ones.
24 return std::min(sqlite_error
& 0xff, 30);
26 // Otherwise, websql_error may be an SQLExceptionCode, SQLErrorCode
27 // or a DOMExceptionCode, or -1 for success.
28 if (websql_error
== -1)
31 // SQLExceptionCode starts at 1000
32 if (websql_error
>= 1000)
35 return std::min(websql_error
+ 30, kResultHistogramSize
- 1);
38 #define HISTOGRAM_WEBSQL_RESULT(name, database, callsite, \
39 websql_error, sqlite_error) \
41 DCHECK(callsite < kCallsiteHistogramSize); \
42 int result = DetermineHistogramResult(websql_error, sqlite_error); \
43 if (database.isSyncDatabase()) { \
44 UMA_HISTOGRAM_ENUMERATION("websql.Sync." name, \
45 result, kResultHistogramSize); \
47 UMA_HISTOGRAM_ENUMERATION("websql.Sync." name ".ErrorSite", \
48 callsite, kCallsiteHistogramSize); \
51 UMA_HISTOGRAM_ENUMERATION("websql.Async." name, \
52 result, kResultHistogramSize); \
54 UMA_HISTOGRAM_ENUMERATION("websql.Async." name ".ErrorSite", \
55 callsite, kCallsiteHistogramSize); \
62 WebDatabaseObserverImpl::WebDatabaseObserverImpl(
63 IPC::Message::Sender
* sender
)
65 open_connections_(new webkit_database::DatabaseConnectionsWrapper
) {
68 WebDatabaseObserverImpl::~WebDatabaseObserverImpl() {
71 void WebDatabaseObserverImpl::databaseOpened(
72 const WebDatabase
& database
) {
73 string16 origin_identifier
= database
.securityOrigin().databaseIdentifier();
74 string16 database_name
= database
.name();
75 open_connections_
->AddOpenConnection(origin_identifier
, database_name
);
76 sender_
->Send(new DatabaseHostMsg_Opened(
77 origin_identifier
, database_name
,
78 database
.displayName(), database
.estimatedSize()));
81 void WebDatabaseObserverImpl::databaseModified(
82 const WebDatabase
& database
) {
83 sender_
->Send(new DatabaseHostMsg_Modified(
84 database
.securityOrigin().databaseIdentifier(), database
.name()));
87 void WebDatabaseObserverImpl::databaseClosed(
88 const WebDatabase
& database
) {
89 string16 origin_identifier
= database
.securityOrigin().databaseIdentifier();
90 string16 database_name
= database
.name();
91 sender_
->Send(new DatabaseHostMsg_Closed(
92 origin_identifier
, database_name
));
93 open_connections_
->RemoveOpenConnection(origin_identifier
, database_name
);
96 void WebDatabaseObserverImpl::reportOpenDatabaseResult(
97 const WebDatabase
& database
, int callsite
, int websql_error
,
99 HISTOGRAM_WEBSQL_RESULT("OpenResult", database
, callsite
,
100 websql_error
, sqlite_error
);
103 void WebDatabaseObserverImpl::reportChangeVersionResult(
104 const WebDatabase
& database
, int callsite
, int websql_error
,
106 HISTOGRAM_WEBSQL_RESULT("ChangeVersionResult", database
, callsite
,
107 websql_error
, sqlite_error
);
110 void WebDatabaseObserverImpl::reportStartTransactionResult(
111 const WebDatabase
& database
, int callsite
, int websql_error
,
113 HISTOGRAM_WEBSQL_RESULT("BeginResult", database
, callsite
,
114 websql_error
, sqlite_error
);
117 void WebDatabaseObserverImpl::reportCommitTransactionResult(
118 const WebDatabase
& database
, int callsite
, int websql_error
,
120 HISTOGRAM_WEBSQL_RESULT("CommitResult", database
, callsite
,
121 websql_error
, sqlite_error
);
124 void WebDatabaseObserverImpl::reportExecuteStatementResult(
125 const WebDatabase
& database
, int callsite
, int websql_error
,
127 HISTOGRAM_WEBSQL_RESULT("StatementResult", database
, callsite
,
128 websql_error
, sqlite_error
);
131 void WebDatabaseObserverImpl::reportVacuumDatabaseResult(
132 const WebDatabase
& database
, int sqlite_error
) {
133 int result
= DetermineHistogramResult(-1, sqlite_error
);
134 if (database
.isSyncDatabase()) {
135 UMA_HISTOGRAM_ENUMERATION("websql.Sync.VacuumResult",
136 result
, kResultHistogramSize
);
138 UMA_HISTOGRAM_ENUMERATION("websql.Async.VacuumResult",
139 result
, kResultHistogramSize
);
143 void WebDatabaseObserverImpl::WaitForAllDatabasesToClose() {
144 open_connections_
->WaitForAllDatabasesToClose();