1 /*
2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "SQLiteFileSystem.h"
33
34 #include "ChromiumBridge.h"
35 #include "CString.h"
36 #include "SQLiteDatabase.h"
37 #include <sqlite3.h>
38
39 #ifndef SQLITE_OPEN_FULLMUTEX
40 #define SQLITE_OPEN_FULLMUTEX 0x00010000
41 #endif
42
43 // SQLiteFileSystem::registerSQLiteVFS() is implemented in the
44 // platform-specific files SQLiteFileSystemChromium{Win|Posix}.cpp
45 namespace WebCore {
46
SQLiteFileSystem()47 SQLiteFileSystem::SQLiteFileSystem()
48 {
49 }
50
openDatabase(const String & fileName,sqlite3 ** database)51 int SQLiteFileSystem::openDatabase(const String& fileName, sqlite3** database)
52 {
53 if (!ChromiumBridge::sandboxEnabled()) {
54 String path = fileName;
55 return sqlite3_open16(path.charactersWithNullTermination(), database);
56 }
57
58 // open databases using Chromium's VFS
59 return sqlite3_open_v2(fileName.utf8().data(), database,
60 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_FULLMUTEX,
61 "chromium_vfs");
62 }
63
getFileNameForNewDatabase(const String &,const String & dbName,const String & originIdentifier,SQLiteDatabase *)64 String SQLiteFileSystem::getFileNameForNewDatabase(
65 const String&, const String& dbName, const String &originIdentifier, SQLiteDatabase*)
66 {
67 // Chromium names DB files based on origin and DB name only
68 return originIdentifier + "_" + dbName + ".db";
69 }
70
appendDatabaseFileNameToPath(const String &,const String & fileName)71 String SQLiteFileSystem::appendDatabaseFileNameToPath(const String&, const String& fileName)
72 {
73 // Chromium saves all DB files in the same directory (known by
74 // the browser process only); as far as the renderer processes
75 // are concerned, all DB files are saved in the "current" directory
76 return fileName;
77 }
78
ensureDatabaseDirectoryExists(const String &)79 bool SQLiteFileSystem::ensureDatabaseDirectoryExists(const String&)
80 {
81 // if the directory where Chromium stores the databases does not exist,
82 // it will be automatically created by the browser process;
83 // so as far as the WebKit code is concerned, this directory always exists
84 return true;
85 }
86
ensureDatabaseFileExists(const String &,bool)87 bool SQLiteFileSystem::ensureDatabaseFileExists(const String&, bool)
88 {
89 // all database directories will be created as needed by the browser process
90 return true;
91 }
92
deleteEmptyDatabaseDirectory(const String &)93 bool SQLiteFileSystem::deleteEmptyDatabaseDirectory(const String&)
94 {
95 // Chromium does not use a separate directory for each database,
96 // so there's nothing to do here
97 return true;
98 }
99
deleteDatabaseFile(const String & fileName)100 bool SQLiteFileSystem::deleteDatabaseFile(const String& fileName)
101 {
102 // return true if and only if the error code returned by
103 // ChromiumBridge::deleteDatabase() is 0
104 return (!ChromiumBridge::databaseDeleteFile(fileName));
105 }
106
getDatabaseFileSize(const String & fileName)107 long long SQLiteFileSystem::getDatabaseFileSize(const String& fileName)
108 {
109 return ChromiumBridge::databaseGetFileSize(fileName);
110 }
111
112 } // namespace WebCore
113