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 <sqlite3.h>
36 #include <windows.h>
37
38 // Defined in Chromium's codebase in third_party/sqlite/src/os_win.c
39 extern "C" {
40 int chromium_sqlite3_initialize_win_sqlite3_file(sqlite3_file* file, HANDLE handle);
41 }
42
43 // Chromium's Windows implementation of SQLite VFS
44 namespace {
45
46 // Opens a file.
47 //
48 // vfs - pointer to the sqlite3_vfs object.
49 // fileName - the name of the file.
50 // id - the structure that will manipulate the newly opened file.
51 // desiredFlags - the desired open mode flags.
52 // usedFlags - the actual open mode flags that were used.
chromiumOpen(sqlite3_vfs *,const char * fileName,sqlite3_file * id,int desiredFlags,int * usedFlags)53 int chromiumOpen(sqlite3_vfs*, const char* fileName,
54 sqlite3_file* id, int desiredFlags, int* usedFlags)
55 {
56 HANDLE h = WebCore::ChromiumBridge::databaseOpenFile(fileName, desiredFlags);
57 if (h == INVALID_HANDLE_VALUE) {
58 if (desiredFlags & SQLITE_OPEN_READWRITE) {
59 int newFlags = (desiredFlags | SQLITE_OPEN_READONLY) & ~SQLITE_OPEN_READWRITE;
60 return chromiumOpen(0, fileName, id, newFlags, usedFlags);
61 } else
62 return SQLITE_CANTOPEN;
63 }
64 if (usedFlags) {
65 if (desiredFlags & SQLITE_OPEN_READWRITE)
66 *usedFlags = SQLITE_OPEN_READWRITE;
67 else
68 *usedFlags = SQLITE_OPEN_READONLY;
69 }
70
71 chromium_sqlite3_initialize_win_sqlite3_file(id, h);
72 return SQLITE_OK;
73 }
74
75 // Deletes the given file.
76 //
77 // vfs - pointer to the sqlite3_vfs object.
78 // fileName - the name of the file.
79 // syncDir - determines if the directory to which this file belongs
80 // should be synched after the file is deleted.
chromiumDelete(sqlite3_vfs *,const char * fileName,int)81 int chromiumDelete(sqlite3_vfs*, const char* fileName, int)
82 {
83 bool deleted = WebCore::ChromiumBridge::databaseDeleteFile(fileName);
84 DWORD rc = WebCore::ChromiumBridge::databaseGetFileAttributes(fileName);
85 return ((rc == INVALID_FILE_ATTRIBUTES) && deleted ?
86 SQLITE_OK : SQLITE_IOERR_DELETE);
87 }
88
89 // Check the existance and status of the given file.
90 //
91 // vfs - pointer to the sqlite3_vfs object.
92 // fileName - the name of the file.
93 // flag - the type of test to make on this file.
94 // res - the result.
chromiumAccess(sqlite3_vfs *,const char * fileName,int flag,int * res)95 int chromiumAccess(sqlite3_vfs*, const char* fileName, int flag, int* res)
96 {
97 DWORD attr = WebCore::ChromiumBridge::databaseGetFileAttributes(fileName);
98 switch (flag) {
99 case SQLITE_ACCESS_READ:
100 case SQLITE_ACCESS_EXISTS:
101 *res = (attr != INVALID_FILE_ATTRIBUTES);
102 break;
103 case SQLITE_ACCESS_READWRITE:
104 *res = ((attr & FILE_ATTRIBUTE_READONLY) == 0);
105 break;
106 default:
107 return SQLITE_ERROR;
108 }
109
110 return SQLITE_OK;
111 }
112
113 // Turns a relative pathname into a full pathname.
114 //
115 // vfs - pointer to the sqlite3_vfs object.
116 // relativePath - the relative path.
117 // bufSize - the size of the output buffer in bytes.
118 // absolutePath - the output buffer where the absolute path will be stored.
chromiumFullPathname(sqlite3_vfs * vfs,const char * relativePath,int,char * absolutePath)119 int chromiumFullPathname(sqlite3_vfs* vfs, const char* relativePath,
120 int, char* absolutePath)
121 {
122 // The renderer process doesn't need to know the absolute path of the file
123 sqlite3_snprintf(vfs->mxPathname, absolutePath, "%s", relativePath);
124 return SQLITE_OK;
125 }
126
127 #ifndef SQLITE_OMIT_LOAD_EXTENSION
128 // Returns NULL, thus disallowing loading libraries in the renderer process.
129 //
130 // vfs - pointer to the sqlite3_vfs object.
131 // fileName - the name of the shared library file.
chromiumDlOpen(sqlite3_vfs *,const char *)132 void* chromiumDlOpen(sqlite3_vfs*, const char*)
133 {
134 return 0;
135 }
136 #else
137 #define chromiumDlOpen 0
138 #endif // SQLITE_OMIT_LOAD_EXTENSION
139
140 } // namespace
141
142 namespace WebCore {
143
registerSQLiteVFS()144 void SQLiteFileSystem::registerSQLiteVFS()
145 {
146 // FIXME: Make sure there aren't any unintended consequences when VFS code is called in the browser process.
147 if (!ChromiumBridge::sandboxEnabled()) {
148 ASSERT_NOT_REACHED();
149 return;
150 }
151
152 sqlite3_vfs* win32_vfs = sqlite3_vfs_find("win32");
153 static sqlite3_vfs chromium_vfs = {
154 1,
155 win32_vfs->szOsFile,
156 win32_vfs->mxPathname,
157 0,
158 "chromium_vfs",
159 0,
160 chromiumOpen,
161 chromiumDelete,
162 chromiumAccess,
163 chromiumFullPathname,
164 chromiumDlOpen,
165 win32_vfs->xDlError,
166 win32_vfs->xDlSym,
167 win32_vfs->xDlClose,
168 win32_vfs->xRandomness,
169 win32_vfs->xSleep,
170 win32_vfs->xCurrentTime,
171 win32_vfs->xGetLastError
172 };
173 sqlite3_vfs_register(&chromium_vfs, 1);
174 }
175
176 } // namespace WebCore
177