1 /*
2 Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18 */
19
20 #include "config.h"
21 #include "qwebdatabase.h"
22
23 #include "qwebdatabase_p.h"
24 #include "qwebsecurityorigin.h"
25 #include "qwebsecurityorigin_p.h"
26 #include "DatabaseDetails.h"
27 #include "DatabaseTracker.h"
28
29 using namespace WebCore;
30
31 /*!
32 \class QWebDatabase
33 \since 4.5
34 \brief The QWebDatabase class provides access to HTML 5 databases created with JavaScript.
35
36 The upcoming HTML 5 standard includes support for SQL databases that web sites can create and
37 access on a local computer through JavaScript. QWebDatabase is the C++ interface to these
38 databases.
39
40 To get access to all databases defined by a security origin, use QWebSecurityOrigin::databases().
41 Each database has an internal name(), as well as a user-friendly name, provided by displayName().
42
43 WebKit uses SQLite to create and access the local SQL databases. The location of the database
44 file in the local file system is returned by fileName(). You can access the database directly
45 through the QtSql database module.
46
47 For each database the web site can define an expectedSize(). The current size of the database
48 in bytes is returned by size().
49
50 For more information refer to the \l{http://dev.w3.org/html5/webdatabase/}{HTML 5 Draft Standard}.
51
52 \sa QWebSecurityOrigin
53 */
54
55 /*!
56 Constructs a web database from \a other.
57 */
QWebDatabase(const QWebDatabase & other)58 QWebDatabase::QWebDatabase(const QWebDatabase& other)
59 : d(other.d)
60 {
61 }
62
63 /*!
64 Assigns the \a other web database to this.
65 */
operator =(const QWebDatabase & other)66 QWebDatabase& QWebDatabase::operator=(const QWebDatabase& other)
67 {
68 d = other.d;
69 return *this;
70 }
71
72 /*!
73 Returns the name of the database.
74 */
name() const75 QString QWebDatabase::name() const
76 {
77 return d->name;
78 }
79
80 /*!
81 Returns the name of the database as seen by the user.
82 */
displayName() const83 QString QWebDatabase::displayName() const
84 {
85 #if ENABLE(DATABASE)
86 DatabaseDetails details = DatabaseTracker::tracker().detailsForNameAndOrigin(d->name, d->origin.get());
87 return details.displayName();
88 #else
89 return QString();
90 #endif
91 }
92
93 /*!
94 Returns the expected size of the database in bytes as defined by the web author.
95 */
expectedSize() const96 qint64 QWebDatabase::expectedSize() const
97 {
98 #if ENABLE(DATABASE)
99 DatabaseDetails details = DatabaseTracker::tracker().detailsForNameAndOrigin(d->name, d->origin.get());
100 return details.expectedUsage();
101 #else
102 return 0;
103 #endif
104 }
105
106 /*!
107 Returns the current size of the database in bytes.
108 */
size() const109 qint64 QWebDatabase::size() const
110 {
111 #if ENABLE(DATABASE)
112 DatabaseDetails details = DatabaseTracker::tracker().detailsForNameAndOrigin(d->name, d->origin.get());
113 return details.currentUsage();
114 #else
115 return 0;
116 #endif
117 }
118
119 /*!
120 \internal
121 */
QWebDatabase(QWebDatabasePrivate * priv)122 QWebDatabase::QWebDatabase(QWebDatabasePrivate* priv)
123 {
124 d = priv;
125 }
126
127 /*!
128 Returns the file name of the web database.
129
130 The name can be used to access the database through the QtSql database module, for example:
131 \code
132 QWebDatabase webdb = ...
133 QSqlDatabase sqldb = QSqlDatabase::addDatabase("QSQLITE", "myconnection");
134 sqldb.setDatabaseName(webdb.fileName());
135 if (sqldb.open()) {
136 QStringList tables = sqldb.tables();
137 ...
138 }
139 \endcode
140
141 \note Concurrent access to a database from multiple threads or processes
142 is not very efficient because SQLite is used as WebKit's database backend.
143 */
fileName() const144 QString QWebDatabase::fileName() const
145 {
146 #if ENABLE(DATABASE)
147 return DatabaseTracker::tracker().fullPathForDatabase(d->origin.get(), d->name, false);
148 #else
149 return QString();
150 #endif
151 }
152
153 /*!
154 Returns the databases's security origin.
155 */
origin() const156 QWebSecurityOrigin QWebDatabase::origin() const
157 {
158 QWebSecurityOriginPrivate* priv = new QWebSecurityOriginPrivate(d->origin.get());
159 QWebSecurityOrigin origin(priv);
160 return origin;
161 }
162
163 /*!
164 Removes the database \a db from its security origin. All data stored in the
165 database \a db will be destroyed.
166 */
removeDatabase(const QWebDatabase & db)167 void QWebDatabase::removeDatabase(const QWebDatabase& db)
168 {
169 #if ENABLE(DATABASE)
170 DatabaseTracker::tracker().deleteDatabase(db.d->origin.get(), db.d->name);
171 #endif
172 }
173
174 /*!
175 \since 4.6
176
177 Deletes all web databases in the configured offline storage path.
178
179 \sa QWebSettings::setOfflineStoragePath()
180 */
removeAllDatabases()181 void QWebDatabase::removeAllDatabases()
182 {
183 #if ENABLE(DATABASE)
184 DatabaseTracker::tracker().deleteAllDatabases();
185 #endif
186 }
187
188 /*!
189 Destroys the web database object. The data within this database is \b not destroyed.
190 */
~QWebDatabase()191 QWebDatabase::~QWebDatabase()
192 {
193 }
194
195