2 * Copyright (C) 2009 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "modules/webdatabase/sqlite/SQLiteFileSystem.h"
36 #include "public/platform/Platform.h"
38 // Defined in Chromium's codebase in third_party/sqlite/src/os_win.c
40 int chromium_sqlite3_initialize_win_sqlite3_file(sqlite3_file* file, HANDLE handle);
45 // Chromium's Windows implementation of SQLite VFS
50 // vfs - pointer to the sqlite3_vfs object.
51 // fileName - the name of the file.
52 // id - the structure that will manipulate the newly opened file.
53 // desiredFlags - the desired open mode flags.
54 // usedFlags - the actual open mode flags that were used.
55 int chromiumOpen(sqlite3_vfs*, const char* fileName,
56 sqlite3_file* id, int desiredFlags, int* usedFlags)
58 HANDLE h = Platform::current()->databaseOpenFile(String(fileName), desiredFlags);
59 if (h == INVALID_HANDLE_VALUE) {
60 if (desiredFlags & SQLITE_OPEN_READWRITE) {
61 int newFlags = (desiredFlags | SQLITE_OPEN_READONLY) & ~SQLITE_OPEN_READWRITE;
62 return chromiumOpen(0, fileName, id, newFlags, usedFlags);
64 return SQLITE_CANTOPEN;
67 if (desiredFlags & SQLITE_OPEN_READWRITE)
68 *usedFlags = SQLITE_OPEN_READWRITE;
70 *usedFlags = SQLITE_OPEN_READONLY;
73 chromium_sqlite3_initialize_win_sqlite3_file(id, h);
77 // Deletes the given file.
79 // vfs - pointer to the sqlite3_vfs object.
80 // fileName - the name of the file.
81 // syncDir - determines if the directory to which this file belongs
82 // should be synched after the file is deleted.
83 int chromiumDelete(sqlite3_vfs*, const char* fileName, int)
85 return Platform::current()->databaseDeleteFile(String(fileName), false);
88 // Check the existance and status of the given file.
90 // vfs - pointer to the sqlite3_vfs object.
91 // fileName - the name of the file.
92 // flag - the type of test to make on this file.
94 int chromiumAccess(sqlite3_vfs*, const char* fileName, int flag, int* res)
96 DWORD attr = Platform::current()->databaseGetFileAttributes(String(fileName));
98 case SQLITE_ACCESS_READ:
99 case SQLITE_ACCESS_EXISTS:
100 *res = (attr != INVALID_FILE_ATTRIBUTES);
102 case SQLITE_ACCESS_READWRITE:
103 *res = ((attr & FILE_ATTRIBUTE_READONLY) == 0);
112 // Turns a relative pathname into a full pathname.
114 // vfs - pointer to the sqlite3_vfs object.
115 // relativePath - the relative path.
116 // bufSize - the size of the output buffer in bytes.
117 // absolutePath - the output buffer where the absolute path will be stored.
118 int chromiumFullPathname(sqlite3_vfs* vfs, const char* relativePath,
119 int, char* absolutePath)
121 // The renderer process doesn't need to know the absolute path of the file
122 sqlite3_snprintf(vfs->mxPathname, absolutePath, "%s", relativePath);
126 #ifndef SQLITE_OMIT_LOAD_EXTENSION
127 // Returns NULL, thus disallowing loading libraries in the renderer process.
129 // vfs - pointer to the sqlite3_vfs object.
130 // fileName - the name of the shared library file.
131 void* chromiumDlOpen(sqlite3_vfs*, const char*)
136 #define chromiumDlOpen 0
137 #endif // SQLITE_OMIT_LOAD_EXTENSION
141 void SQLiteFileSystem::registerSQLiteVFS()
143 sqlite3_vfs* win32_vfs = sqlite3_vfs_find("win32");
144 static sqlite3_vfs chromium_vfs = {
147 win32_vfs->mxPathname,
154 chromiumFullPathname,
159 win32_vfs->xRandomness,
161 win32_vfs->xCurrentTime,
162 win32_vfs->xGetLastError
164 sqlite3_vfs_register(&chromium_vfs, 0);