Home | History | Annotate | Download | only in chromium
      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 using namespace WebCore;
     39 
     40 // Defined in Chromium's codebase in third_party/sqlite/src/os_win.c
     41 extern "C" {
     42 int chromium_sqlite3_initialize_win_sqlite3_file(sqlite3_file* file, HANDLE handle);
     43 }
     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 = ChromiumBridge::databaseOpenFile(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 ChromiumBridge::databaseDeleteFile(fileName);
     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 = ChromiumBridge::databaseGetFileAttributes(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 namespace WebCore {
    142 
    143 void SQLiteFileSystem::registerSQLiteVFS()
    144 {
    145     // FIXME: Make sure there aren't any unintended consequences when VFS code is called in the browser process.
    146     if (!ChromiumBridge::sandboxEnabled()) {
    147         ASSERT_NOT_REACHED();
    148         return;
    149     }
    150 
    151     sqlite3_vfs* win32_vfs = sqlite3_vfs_find("win32");
    152     static sqlite3_vfs chromium_vfs = {
    153         1,
    154         win32_vfs->szOsFile,
    155         win32_vfs->mxPathname,
    156         0,
    157         "chromium_vfs",
    158         win32_vfs->pAppData,
    159         chromiumOpen,
    160         chromiumDelete,
    161         chromiumAccess,
    162         chromiumFullPathname,
    163         chromiumDlOpen,
    164         win32_vfs->xDlError,
    165         win32_vfs->xDlSym,
    166         win32_vfs->xDlClose,
    167         win32_vfs->xRandomness,
    168         win32_vfs->xSleep,
    169         win32_vfs->xCurrentTime,
    170         win32_vfs->xGetLastError
    171     };
    172     sqlite3_vfs_register(&chromium_vfs, 0);
    173 }
    174 
    175 } // namespace WebCore
    176