Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / webdatabase / sqlite / SQLiteFileSystemWin.cpp
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 "modules/webdatabase/sqlite/SQLiteFileSystem.h"
33
34 #include <windows.h>
35 #include <sqlite3.h>
36 #include "public/platform/Platform.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 namespace blink {
44
45 // Chromium's Windows implementation of SQLite VFS
46 namespace {
47
48 // Opens a file.
49 //
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)
57 {
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);
63         } else
64             return SQLITE_CANTOPEN;
65     }
66     if (usedFlags) {
67         if (desiredFlags & SQLITE_OPEN_READWRITE)
68             *usedFlags = SQLITE_OPEN_READWRITE;
69         else
70             *usedFlags = SQLITE_OPEN_READONLY;
71     }
72
73     chromium_sqlite3_initialize_win_sqlite3_file(id, h);
74     return SQLITE_OK;
75 }
76
77 // Deletes the given file.
78 //
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)
84 {
85     return Platform::current()->databaseDeleteFile(String(fileName), false);
86 }
87
88 // Check the existance and status of the given file.
89 //
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.
93 // res - the result.
94 int chromiumAccess(sqlite3_vfs*, const char* fileName, int flag, int* res)
95 {
96     DWORD attr = Platform::current()->databaseGetFileAttributes(String(fileName));
97     switch (flag) {
98     case SQLITE_ACCESS_READ:
99     case SQLITE_ACCESS_EXISTS:
100         *res = (attr != INVALID_FILE_ATTRIBUTES);
101         break;
102     case SQLITE_ACCESS_READWRITE:
103         *res = ((attr & FILE_ATTRIBUTE_READONLY) == 0);
104         break;
105     default:
106         return SQLITE_ERROR;
107     }
108
109     return SQLITE_OK;
110 }
111
112 // Turns a relative pathname into a full pathname.
113 //
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)
120 {
121     // The renderer process doesn't need to know the absolute path of the file
122     sqlite3_snprintf(vfs->mxPathname, absolutePath, "%s", relativePath);
123     return SQLITE_OK;
124 }
125
126 #ifndef SQLITE_OMIT_LOAD_EXTENSION
127 // Returns NULL, thus disallowing loading libraries in the renderer process.
128 //
129 // vfs - pointer to the sqlite3_vfs object.
130 // fileName - the name of the shared library file.
131 void* chromiumDlOpen(sqlite3_vfs*, const char*)
132 {
133     return 0;
134 }
135 #else
136 #define chromiumDlOpen 0
137 #endif // SQLITE_OMIT_LOAD_EXTENSION
138
139 } // namespace
140
141 void SQLiteFileSystem::registerSQLiteVFS()
142 {
143     sqlite3_vfs* win32_vfs = sqlite3_vfs_find("win32");
144     static sqlite3_vfs chromium_vfs = {
145         1,
146         win32_vfs->szOsFile,
147         win32_vfs->mxPathname,
148         0,
149         "chromium_vfs",
150         win32_vfs->pAppData,
151         chromiumOpen,
152         chromiumDelete,
153         chromiumAccess,
154         chromiumFullPathname,
155         chromiumDlOpen,
156         win32_vfs->xDlError,
157         win32_vfs->xDlSym,
158         win32_vfs->xDlClose,
159         win32_vfs->xRandomness,
160         win32_vfs->xSleep,
161         win32_vfs->xCurrentTime,
162         win32_vfs->xGetLastError
163     };
164     sqlite3_vfs_register(&chromium_vfs, 0);
165 }
166
167 } // namespace blink