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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
// 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/renderer_host/database_dispatcher_host.h"
#if defined(OS_POSIX)
#include "base/file_descriptor_posix.h"
#endif
#if defined(USE_SYSTEM_SQLITE)
#include <sqlite3.h>
#else
#include "third_party/sqlite/preprocessed/sqlite3.h"
#endif
#include "base/string_util.h"
#include "base/thread.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/renderer_host/browser_render_process_host.h"
#include "chrome/common/render_messages.h"
#include "webkit/database/vfs_backend.h"
using webkit_database::DatabaseTracker;
using webkit_database::VfsBackend;
const int kNumDeleteRetries = 2;
const int kDelayDeleteRetryMs = 100;
DatabaseDispatcherHost::DatabaseDispatcherHost(
DatabaseTracker* db_tracker,
IPC::Message::Sender* message_sender,
base::ProcessHandle process_handle)
: db_tracker_(db_tracker),
message_sender_(message_sender),
process_handle_(process_handle),
observer_added_(false),
shutdown_(false) {
DCHECK(db_tracker_);
DCHECK(message_sender_);
}
void DatabaseDispatcherHost::Shutdown() {
shutdown_ = true;
message_sender_ = NULL;
if (observer_added_) {
ChromeThread::PostTask(
ChromeThread::FILE, FROM_HERE,
NewRunnableMethod(this, &DatabaseDispatcherHost::RemoveObserver));
}
}
void DatabaseDispatcherHost::AddObserver() {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
db_tracker_->AddObserver(this);
}
void DatabaseDispatcherHost::RemoveObserver() {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
db_tracker_->RemoveObserver(this);
}
FilePath DatabaseDispatcherHost::GetDBFileFullPath(
const FilePath& vfs_file_name) {
// 'vfs_file_name' can be one of 3 things:
// 1. Empty string: It means the VFS wants to open a temp file. In this case
// we need to return the path to the directory that stores all databases.
// 2. origin_identifier/database_name: In this case, we need to extract
// 'origin_identifier' and 'database_name' and pass them to
// DatabaseTracker::GetFullDBFilePath().
// 3. origin_identifier/database_name-suffix: '-suffix' could be '-journal',
// for example. In this case, we need to extract 'origin_identifier' and
// 'database_name-suffix' and pass them to
// DatabaseTracker::GetFullDBFilePath(). 'database_name-suffix' is not
// a database name as expected by DatabaseTracker::GetFullDBFilePath(),
// but due to its implementation, it's OK to pass in 'database_name-suffix'
// too.
//
// We also check that the given string doesn't contain invalid characters
// that would result in a DB file stored outside of the directory where
// all DB files are supposed to be stored.
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
if (vfs_file_name.empty())
return db_tracker_->DatabaseDirectory();
std::wstring str = vfs_file_name.ToWStringHack();
size_t slashIndex = str.find('/');
if (slashIndex == std::wstring::npos)
return FilePath(); // incorrect format
std::wstring origin_identifier = str.substr(0, slashIndex);
std::wstring database_name =
str.substr(slashIndex + 1, str.length() - slashIndex);
if ((origin_identifier.find('\\') != std::wstring::npos) ||
(origin_identifier.find('/') != std::wstring::npos) ||
(origin_identifier.find(':') != std::wstring::npos) ||
(database_name.find('\\') != std::wstring::npos) ||
(database_name.find('/') != std::wstring::npos) ||
(database_name.find(':') != std::wstring::npos)) {
return FilePath();
}
return db_tracker_->GetFullDBFilePath(
WideToUTF16(origin_identifier), WideToUTF16(database_name));
}
bool DatabaseDispatcherHost::OnMessageReceived(
const IPC::Message& message, bool* message_was_ok) {
DCHECK(!shutdown_);
*message_was_ok = true;
bool handled = true;
IPC_BEGIN_MESSAGE_MAP_EX(DatabaseDispatcherHost, message, *message_was_ok)
IPC_MESSAGE_HANDLER(ViewHostMsg_DatabaseOpenFile, OnDatabaseOpenFile)
IPC_MESSAGE_HANDLER(ViewHostMsg_DatabaseDeleteFile, OnDatabaseDeleteFile)
IPC_MESSAGE_HANDLER(ViewHostMsg_DatabaseGetFileAttributes,
OnDatabaseGetFileAttributes)
IPC_MESSAGE_HANDLER(ViewHostMsg_DatabaseGetFileSize,
OnDatabaseGetFileSize)
IPC_MESSAGE_HANDLER(ViewHostMsg_DatabaseOpened, OnDatabaseOpened)
IPC_MESSAGE_HANDLER(ViewHostMsg_DatabaseModified, OnDatabaseModified)
IPC_MESSAGE_HANDLER(ViewHostMsg_DatabaseClosed, OnDatabaseClosed)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP_EX()
return handled;
}
void DatabaseDispatcherHost::ReceivedBadMessage(uint16 msg_type) {
BrowserRenderProcessHost::BadMessageTerminateProcess(
msg_type, process_handle_);
}
// Scheduled by the file thread on the IO thread.
// Sends back to the renderer process the given message.
void DatabaseDispatcherHost::SendMessage(IPC::Message* message) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
if (!shutdown_)
message_sender_->Send(message);
else
delete message;
}
void DatabaseDispatcherHost::OnDatabaseOpenFile(const FilePath& vfs_file_name,
int desired_flags,
int32 message_id) {
if (!observer_added_) {
observer_added_ = true;
ChromeThread::PostTask(
ChromeThread::FILE, FROM_HERE,
NewRunnableMethod(this, &DatabaseDispatcherHost::AddObserver));
}
ChromeThread::PostTask(
ChromeThread::FILE, FROM_HERE,
NewRunnableMethod(this,
&DatabaseDispatcherHost::DatabaseOpenFile,
vfs_file_name,
desired_flags,
message_id));
}
static void SetOpenFileResponseParams(
ViewMsg_DatabaseOpenFileResponse_Params* params,
base::PlatformFile file_handle,
base::PlatformFile dir_handle) {
#if defined(OS_WIN)
params->file_handle = file_handle;
#elif defined(OS_POSIX)
params->file_handle = base::FileDescriptor(file_handle, true);
params->dir_handle = base::FileDescriptor(dir_handle, true);
#endif
}
// Scheduled by the IO thread on the file thread.
// Opens the given database file, then schedules
// a task on the IO thread's message loop to send an IPC back to
// corresponding renderer process with the file handle.
void DatabaseDispatcherHost::DatabaseOpenFile(const FilePath& vfs_file_name,
int desired_flags,
int32 message_id) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
base::PlatformFile target_handle = base::kInvalidPlatformFileValue;
base::PlatformFile target_dir_handle = base::kInvalidPlatformFileValue;
FilePath db_file_name = GetDBFileFullPath(vfs_file_name);
if (!db_file_name.empty()) {
FilePath db_dir = db_tracker_->DatabaseDirectory();
VfsBackend::OpenFile(db_file_name, db_dir, desired_flags,
process_handle_, &target_handle, &target_dir_handle);
}
ViewMsg_DatabaseOpenFileResponse_Params response_params;
SetOpenFileResponseParams(&response_params, target_handle, target_dir_handle);
ChromeThread::PostTask(
ChromeThread::IO, FROM_HERE,
NewRunnableMethod(this,
&DatabaseDispatcherHost::SendMessage,
new ViewMsg_DatabaseOpenFileResponse(
message_id, response_params)));
}
void DatabaseDispatcherHost::OnDatabaseDeleteFile(const FilePath& vfs_file_name,
const bool& sync_dir,
int32 message_id) {
ChromeThread::PostTask(
ChromeThread::FILE, FROM_HERE,
NewRunnableMethod(this,
&DatabaseDispatcherHost::DatabaseDeleteFile,
vfs_file_name,
sync_dir,
message_id,
kNumDeleteRetries));
}
// Scheduled by the IO thread on the file thread.
// Deletes the given database file, then schedules
// a task on the IO thread's message loop to send an IPC back to
// corresponding renderer process with the error code.
void DatabaseDispatcherHost::DatabaseDeleteFile(const FilePath& vfs_file_name,
bool sync_dir,
int32 message_id,
int reschedule_count) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
// Return an error if the file name is invalid or if the file could not
// be deleted after kNumDeleteRetries attempts.
int error_code = SQLITE_IOERR_DELETE;
FilePath db_file_name = GetDBFileFullPath(vfs_file_name);
if (!db_file_name.empty()) {
FilePath db_dir = db_tracker_->DatabaseDirectory();
error_code = VfsBackend::DeleteFile(db_file_name, db_dir, sync_dir);
if ((error_code == SQLITE_IOERR_DELETE) && reschedule_count) {
// If the file could not be deleted, try again.
ChromeThread::PostDelayedTask(
ChromeThread::FILE, FROM_HERE,
NewRunnableMethod(this,
&DatabaseDispatcherHost::DatabaseDeleteFile,
vfs_file_name,
sync_dir,
message_id,
reschedule_count - 1),
kDelayDeleteRetryMs);
return;
}
}
ChromeThread::PostTask(
ChromeThread::IO, FROM_HERE,
NewRunnableMethod(this,
&DatabaseDispatcherHost::SendMessage,
new ViewMsg_DatabaseDeleteFileResponse(
message_id, error_code)));
}
void DatabaseDispatcherHost::OnDatabaseGetFileAttributes(
const FilePath& vfs_file_name,
int32 message_id) {
ChromeThread::PostTask(
ChromeThread::FILE, FROM_HERE,
NewRunnableMethod(this,
&DatabaseDispatcherHost::DatabaseGetFileAttributes,
vfs_file_name,
message_id));
}
// Scheduled by the IO thread on the file thread.
// Gets the attributes of the given database file, then schedules
// a task on the IO thread's message loop to send an IPC back to
// corresponding renderer process.
void DatabaseDispatcherHost::DatabaseGetFileAttributes(
const FilePath& vfs_file_name,
int32 message_id) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
int32 attributes = -1;
FilePath db_file_name = GetDBFileFullPath(vfs_file_name);
if (!db_file_name.empty())
attributes = VfsBackend::GetFileAttributes(db_file_name);
ChromeThread::PostTask(
ChromeThread::IO, FROM_HERE,
NewRunnableMethod(this,
&DatabaseDispatcherHost::SendMessage,
new ViewMsg_DatabaseGetFileAttributesResponse(
message_id, attributes)));
}
void DatabaseDispatcherHost::OnDatabaseGetFileSize(
const FilePath& vfs_file_name, int32 message_id) {
ChromeThread::PostTask(
ChromeThread::FILE, FROM_HERE,
NewRunnableMethod(this,
&DatabaseDispatcherHost::DatabaseGetFileSize,
vfs_file_name,
message_id));
}
// Scheduled by the IO thread on the file thread.
// Gets the size of the given file, then schedules a task
// on the IO thread's message loop to send an IPC back to
// the corresponding renderer process.
void DatabaseDispatcherHost::DatabaseGetFileSize(const FilePath& vfs_file_name,
int32 message_id) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
int64 size = 0;
FilePath db_file_name = GetDBFileFullPath(vfs_file_name);
if (!db_file_name.empty())
size = VfsBackend::GetFileSize(db_file_name);
ChromeThread::PostTask(
ChromeThread::IO, FROM_HERE,
NewRunnableMethod(this,
&DatabaseDispatcherHost::SendMessage,
new ViewMsg_DatabaseGetFileSizeResponse(
message_id, size)));
}
void DatabaseDispatcherHost::OnDatabaseOpened(const string16& origin_identifier,
const string16& database_name,
const string16& description,
int64 estimated_size) {
ChromeThread::PostTask(
ChromeThread::FILE, FROM_HERE,
NewRunnableMethod(this,
&DatabaseDispatcherHost::DatabaseOpened,
origin_identifier,
database_name,
description,
estimated_size));
}
void DatabaseDispatcherHost::DatabaseOpened(const string16& origin_identifier,
const string16& database_name,
const string16& description,
int64 estimated_size) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
int64 database_size = 0;
int64 space_available = 0;
AddAccessedOrigin(origin_identifier);
db_tracker_->DatabaseOpened(origin_identifier, database_name, description,
estimated_size, &database_size, &space_available);
ChromeThread::PostTask(
ChromeThread::IO, FROM_HERE,
NewRunnableMethod(this,
&DatabaseDispatcherHost::SendMessage,
new ViewMsg_DatabaseUpdateSize(
origin_identifier, database_name,
database_size, space_available)));
}
void DatabaseDispatcherHost::OnDatabaseModified(
const string16& origin_identifier,
const string16& database_name) {
ChromeThread::PostTask(
ChromeThread::FILE, FROM_HERE,
NewRunnableMethod(this,
&DatabaseDispatcherHost::DatabaseModified,
origin_identifier,
database_name));
}
void DatabaseDispatcherHost::DatabaseModified(const string16& origin_identifier,
const string16& database_name) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
if (!HasAccessedOrigin(origin_identifier)) {
ReceivedBadMessage(ViewHostMsg_DatabaseModified::ID);
return;
}
db_tracker_->DatabaseModified(origin_identifier, database_name);
}
void DatabaseDispatcherHost::OnDatabaseClosed(const string16& origin_identifier,
const string16& database_name) {
ChromeThread::PostTask(
ChromeThread::FILE, FROM_HERE,
NewRunnableMethod(this,
&DatabaseDispatcherHost::DatabaseClosed,
origin_identifier,
database_name));
}
void DatabaseDispatcherHost::DatabaseClosed(const string16& origin_identifier,
const string16& database_name) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
if (!HasAccessedOrigin(origin_identifier)) {
ReceivedBadMessage(ViewHostMsg_DatabaseClosed::ID);
return;
}
db_tracker_->DatabaseClosed(origin_identifier, database_name);
}
void DatabaseDispatcherHost::OnDatabaseSizeChanged(
const string16& origin_identifier,
const string16& database_name,
int64 database_size,
int64 space_available) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
if (HasAccessedOrigin(origin_identifier)) {
ChromeThread::PostTask(
ChromeThread::IO, FROM_HERE,
NewRunnableMethod(this,
&DatabaseDispatcherHost::SendMessage,
new ViewMsg_DatabaseUpdateSize(
origin_identifier, database_name,
database_size, space_available)));
}
}
void DatabaseDispatcherHost::AddAccessedOrigin(
const string16& origin_identifier) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
accessed_origins_.insert(origin_identifier);
}
bool DatabaseDispatcherHost::HasAccessedOrigin(
const string16& origin_identifier) {
DCHECK(ChromeThread::CurrentlyOn(ChromeThread::FILE));
return (accessed_origins_.find(origin_identifier) != accessed_origins_.end());
}
|