summaryrefslogtreecommitdiffstats
path: root/webkit/support/simple_database_system.cc
blob: 8786c82cecfbb87091911fb6e4232a6bb55ede6b (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
// 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 "webkit/support/simple_database_system.h"

#include "base/auto_reset.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/file_util.h"
#include "base/message_loop.h"
#include "base/message_loop_proxy.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/platform_thread.h"
#include "base/utf_string_conversions.h"
#include "third_party/WebKit/public/platform/WebString.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebDatabase.h"
#include "third_party/sqlite/sqlite3.h"
#include "webkit/browser/database/database_util.h"
#include "webkit/browser/database/vfs_backend.h"

using webkit_database::DatabaseTracker;
using webkit_database::DatabaseUtil;
using webkit_database::OriginInfo;
using webkit_database::VfsBackend;

SimpleDatabaseSystem* SimpleDatabaseSystem::instance_ = NULL;

SimpleDatabaseSystem* SimpleDatabaseSystem::GetInstance() {
  DCHECK(instance_);
  return instance_;
}

SimpleDatabaseSystem::SimpleDatabaseSystem()
    : db_thread_("SimpleDBThread"),
      quota_per_origin_(5 * 1024 * 1024),
      open_connections_(new webkit_database::DatabaseConnectionsWrapper) {
  DCHECK(!instance_);
  instance_ = this;
  CHECK(temp_dir_.CreateUniqueTempDir());
  db_tracker_ =
      new DatabaseTracker(temp_dir_.path(), false, NULL, NULL, NULL);
  db_tracker_->AddObserver(this);
  db_thread_.Start();
  db_thread_proxy_ = db_thread_.message_loop_proxy();
}

SimpleDatabaseSystem::~SimpleDatabaseSystem() {
  base::WaitableEvent done_event(false, false);
  db_thread_proxy_->PostTask(
      FROM_HERE,
      base::Bind(&SimpleDatabaseSystem::ThreadCleanup,
                 base::Unretained(this), &done_event));
  done_event.Wait();
  instance_ = NULL;
}

void SimpleDatabaseSystem::databaseOpened(const WebKit::WebDatabase& database) {
  base::string16 origin_identifier =
      database.securityOrigin().databaseIdentifier();
  base::string16 database_name = database.name();
  open_connections_->AddOpenConnection(origin_identifier, database_name);
  db_thread_proxy_->PostTask(
      FROM_HERE,
      base::Bind(&SimpleDatabaseSystem::DatabaseOpened,
                 base::Unretained(this),
                 origin_identifier,
                 database_name, database.displayName(),
                 database.estimatedSize()));
}

void SimpleDatabaseSystem::databaseModified(
    const WebKit::WebDatabase& database) {
  db_thread_proxy_->PostTask(
      FROM_HERE,
      base::Bind(&SimpleDatabaseSystem::DatabaseModified,
                 base::Unretained(this),
                 database.securityOrigin().databaseIdentifier(),
                 database.name()));
}

void SimpleDatabaseSystem::databaseClosed(const WebKit::WebDatabase& database) {
  base::string16 origin_identifier =
      database.securityOrigin().databaseIdentifier();
  base::string16 database_name = database.name();
  db_thread_proxy_->PostTask(
      FROM_HERE,
      base::Bind(&SimpleDatabaseSystem::DatabaseClosed,
                 base::Unretained(this), origin_identifier, database_name));
}

base::PlatformFile SimpleDatabaseSystem::OpenFile(
    const base::string16& vfs_file_name, int desired_flags) {
  base::PlatformFile result = base::kInvalidPlatformFileValue;
  base::WaitableEvent done_event(false, false);
  db_thread_proxy_->PostTask(
      FROM_HERE,
      base::Bind(&SimpleDatabaseSystem::VfsOpenFile,
                 base::Unretained(this),
                 vfs_file_name, desired_flags,
                 &result, &done_event));
  done_event.Wait();
  return result;
}

int SimpleDatabaseSystem::DeleteFile(
    const base::string16& vfs_file_name, bool sync_dir) {
  int result = SQLITE_OK;
  base::WaitableEvent done_event(false, false);
  db_thread_proxy_->PostTask(
      FROM_HERE,
      base::Bind(&SimpleDatabaseSystem::VfsDeleteFile,
                 base::Unretained(this),
                 vfs_file_name, sync_dir,
                 &result, &done_event));
  done_event.Wait();
  return result;
}

uint32 SimpleDatabaseSystem::GetFileAttributes(
    const base::string16& vfs_file_name) {
  uint32 result = 0;
  base::WaitableEvent done_event(false, false);
  db_thread_proxy_->PostTask(
      FROM_HERE,
      base::Bind(&SimpleDatabaseSystem::VfsGetFileAttributes,
                 base::Unretained(this), vfs_file_name, &result, &done_event));
  done_event.Wait();
  return result;
}

int64 SimpleDatabaseSystem::GetFileSize(const base::string16& vfs_file_name) {
  int64 result = 0;
  base::WaitableEvent done_event(false, false);
  db_thread_proxy_->PostTask(
      FROM_HERE,
      base::Bind(&SimpleDatabaseSystem::VfsGetFileSize,
                 base::Unretained(this), vfs_file_name, &result, &done_event));
  done_event.Wait();
  return result;
}

int64 SimpleDatabaseSystem::GetSpaceAvailable(
    const base::string16& origin_identifier) {
  int64 result = 0;
  base::WaitableEvent done_event(false, false);
  db_thread_proxy_->PostTask(
      FROM_HERE,
      base::Bind(&SimpleDatabaseSystem::VfsGetSpaceAvailable,
                 base::Unretained(this), origin_identifier,
                 &result, &done_event));
  done_event.Wait();
  return result;
}

void SimpleDatabaseSystem::ClearAllDatabases() {
  open_connections_->WaitForAllDatabasesToClose();
  db_thread_proxy_->PostTask(
      FROM_HERE,
      base::Bind(&SimpleDatabaseSystem::ResetTracker, base::Unretained(this)));
}

void SimpleDatabaseSystem::SetDatabaseQuota(int64 quota) {
  if (!db_thread_proxy_->BelongsToCurrentThread()) {
    db_thread_proxy_->PostTask(
        FROM_HERE,
        base::Bind(&SimpleDatabaseSystem::SetDatabaseQuota,
                   base::Unretained(this), quota));
    return;
  }
  quota_per_origin_ = quota;
}

void SimpleDatabaseSystem::DatabaseOpened(
    const base::string16& origin_identifier,
    const base::string16& database_name,
    const base::string16& description,
    int64 estimated_size) {
  DCHECK(db_thread_proxy_->BelongsToCurrentThread());
  int64 database_size = 0;
  db_tracker_->DatabaseOpened(
      origin_identifier, database_name, description,
      estimated_size, &database_size);
  OnDatabaseSizeChanged(origin_identifier, database_name,
                        database_size);
}

void SimpleDatabaseSystem::DatabaseModified(
    const base::string16& origin_identifier,
    const base::string16& database_name) {
  DCHECK(db_thread_proxy_->BelongsToCurrentThread());
  db_tracker_->DatabaseModified(origin_identifier, database_name);
}

void SimpleDatabaseSystem::DatabaseClosed(
    const base::string16& origin_identifier,
    const base::string16& database_name) {
  DCHECK(db_thread_proxy_->BelongsToCurrentThread());
  db_tracker_->DatabaseClosed(origin_identifier, database_name);
  open_connections_->RemoveOpenConnection(origin_identifier, database_name);
}

void SimpleDatabaseSystem::OnDatabaseSizeChanged(
    const base::string16& origin_identifier,
    const base::string16& database_name,
    int64 database_size) {
  DCHECK(db_thread_proxy_->BelongsToCurrentThread());
  // We intentionally call into webkit on our background db_thread_
  // to better emulate what happens in chrome where this method is
  // invoked on the background ipc thread.
  WebKit::WebDatabase::updateDatabaseSize(
      origin_identifier, database_name, database_size);
}

void SimpleDatabaseSystem::OnDatabaseScheduledForDeletion(
    const base::string16& origin_identifier,
    const base::string16& database_name) {
  DCHECK(db_thread_proxy_->BelongsToCurrentThread());
  // We intentionally call into webkit on our background db_thread_
  // to better emulate what happens in chrome where this method is
  // invoked on the background ipc thread.
  WebKit::WebDatabase::closeDatabaseImmediately(
      origin_identifier, database_name);
}

void SimpleDatabaseSystem::VfsOpenFile(
    const base::string16& vfs_file_name, int desired_flags,
    base::PlatformFile* file_handle, base::WaitableEvent* done_event ) {
  DCHECK(db_thread_proxy_->BelongsToCurrentThread());
  base::FilePath file_name = GetFullFilePathForVfsFile(vfs_file_name);
  if (file_name.empty()) {
    VfsBackend::OpenTempFileInDirectory(
        db_tracker_->DatabaseDirectory(), desired_flags, file_handle);
  } else {
    VfsBackend::OpenFile(file_name, desired_flags, file_handle);
  }
  done_event->Signal();
}

void SimpleDatabaseSystem::VfsDeleteFile(
    const base::string16& vfs_file_name, bool sync_dir,
    int* result, base::WaitableEvent* done_event) {
  DCHECK(db_thread_proxy_->BelongsToCurrentThread());
  // We try to delete the file multiple times, because that's what the default
  // VFS does (apparently deleting a file can sometimes fail on Windows).
  // We sleep for 10ms between retries for the same reason.
  const int kNumDeleteRetries = 3;
  int num_retries = 0;
  *result = SQLITE_OK;
  base::FilePath file_name = GetFullFilePathForVfsFile(vfs_file_name);
  do {
    *result = VfsBackend::DeleteFile(file_name, sync_dir);
  } while ((++num_retries < kNumDeleteRetries) &&
           (*result == SQLITE_IOERR_DELETE) &&
           (base::PlatformThread::Sleep(
               base::TimeDelta::FromMilliseconds(10)),
            1));

  done_event->Signal();
}

void SimpleDatabaseSystem::VfsGetFileAttributes(
    const base::string16& vfs_file_name,
    uint32* result, base::WaitableEvent* done_event) {
  DCHECK(db_thread_proxy_->BelongsToCurrentThread());
  *result = VfsBackend::GetFileAttributes(
      GetFullFilePathForVfsFile(vfs_file_name));
  done_event->Signal();
}

void SimpleDatabaseSystem::VfsGetFileSize(
    const base::string16& vfs_file_name,
    int64* result, base::WaitableEvent* done_event) {
  DCHECK(db_thread_proxy_->BelongsToCurrentThread());
  *result = VfsBackend::GetFileSize(GetFullFilePathForVfsFile(vfs_file_name));
  done_event->Signal();
}

void SimpleDatabaseSystem::VfsGetSpaceAvailable(
    const base::string16& origin_identifier,
    int64* result, base::WaitableEvent* done_event) {
  DCHECK(db_thread_proxy_->BelongsToCurrentThread());
  // This method isn't actually part of the "vfs" interface, but it is
  // used from within webcore and handled here in the same fashion.
  OriginInfo info;
  if (db_tracker_->GetOriginInfo(origin_identifier, &info)) {
    int64 space_available = quota_per_origin_ - info.TotalSize();
    *result = space_available < 0 ? 0 : space_available;
  } else {
    NOTREACHED();
    *result = 0;
  }
  done_event->Signal();
}

base::FilePath SimpleDatabaseSystem::GetFullFilePathForVfsFile(
    const base::string16& vfs_file_name) {
  DCHECK(db_thread_proxy_->BelongsToCurrentThread());
  if (vfs_file_name.empty())  // temp file, used for vacuuming
    return base::FilePath();
  return DatabaseUtil::GetFullFilePathForVfsFile(
      db_tracker_.get(), vfs_file_name);
}

void SimpleDatabaseSystem::ResetTracker() {
  DCHECK(db_thread_proxy_->BelongsToCurrentThread());
  db_tracker_->CloseTrackerDatabaseAndClearCaches();
  file_util::Delete(db_tracker_->DatabaseDirectory(), true);
}

void SimpleDatabaseSystem::ThreadCleanup(base::WaitableEvent* done_event) {
  ResetTracker();
  db_tracker_->RemoveObserver(this);
  db_tracker_ = NULL;
  done_event->Signal();
}