Back to index

lightning-sunbird  0.9+nobinonly
mozStorageService.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  * vim: sw=4 ts=4 sts=4
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Oracle Corporation code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  *  Oracle Corporation
00020  * Portions created by the Initial Developer are Copyright (C) 2004
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
00025  *   Brett Wilson <brettw@gmail.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #include "mozStorageService.h"
00042 #include "mozStorageConnection.h"
00043 #include "nsCRT.h"
00044 #include "nsIThread.h"
00045 #include "plstr.h"
00046 
00047 #include "sqlite3.h"
00048 #include "sqlite3file.h"
00049 
00050 NS_IMPL_THREADSAFE_ISUPPORTS2(mozStorageService, mozIStorageService, nsIObserver)
00051 
00052 // XXX this sucks that we have to pull in nsIFile and all that
00053 // just to use NS_GetSpecialDirectory
00054 
00055 static const char* gQuitApplicationMessage = "quit-application";
00056 
00057 mozStorageService::mozStorageService()
00058 {
00059     
00060 }
00061 
00062 mozStorageService::~mozStorageService()
00063 {
00064     FreeLocks();
00065 }
00066 
00067 nsresult
00068 mozStorageService::Init()
00069 {
00070     // The service must be initialized on the main thread. The
00071     // InitStorageAsyncIO function creates a thread which is joined with the
00072     // main thread during shutdown. If the thread is created from a random
00073     // thread, we'll join to the wrong parent.
00074     NS_ENSURE_STATE(nsIThread::IsMainThread());
00075 
00076     // this makes multiple connections to the same database share the same pager
00077     // cache.
00078     sqlite3_enable_shared_cache(1);
00079 
00080     nsresult rv;
00081     nsCOMPtr<nsIObserverService> observerService = 
00082             do_GetService("@mozilla.org/observer-service;1", &rv);
00083     NS_ENSURE_SUCCESS(rv, rv);
00084 
00085     rv = InitStorageAsyncIO();
00086     NS_ENSURE_SUCCESS(rv, rv);
00087 
00088     rv = observerService->AddObserver(this, gQuitApplicationMessage, PR_FALSE);
00089     NS_ENSURE_SUCCESS(rv, rv);
00090 
00091     return NS_OK;
00092 }
00093 
00094 #ifndef NS_APP_STORAGE_50_FILE
00095 #define NS_APP_STORAGE_50_FILE "UStor"
00096 #endif
00097 
00098 /* mozIStorageConnection openSpecialDatabase(in string aStorageKey); */
00099 NS_IMETHODIMP
00100 mozStorageService::OpenSpecialDatabase(const char *aStorageKey, mozIStorageConnection **_retval)
00101 {
00102     nsresult rv;
00103 
00104     nsCOMPtr<nsIFile> storageFile;
00105     if (PL_strcmp(aStorageKey, "memory") == 0) {
00106         // just fall through with NULL storageFile, this will cause the storage
00107         // connection to use a memory DB.
00108     } else if (PL_strcmp(aStorageKey, "profile") == 0) {
00109 
00110         rv = NS_GetSpecialDirectory(NS_APP_STORAGE_50_FILE, getter_AddRefs(storageFile));
00111         if (NS_FAILED(rv)) {
00112             // teh wtf?
00113             return rv;
00114         }
00115 
00116         nsString filename;
00117         storageFile->GetPath(filename);
00118         nsCString filename8 = NS_ConvertUTF16toUTF8(filename.get());
00119         // fall through to DB initialization
00120     } else {
00121         return NS_ERROR_INVALID_ARG;
00122     }
00123 
00124     mozStorageConnection *msc = new mozStorageConnection(this);
00125     if (! msc)
00126         return NS_ERROR_OUT_OF_MEMORY;
00127     nsCOMPtr<mozIStorageConnection> conn = msc;
00128     rv = msc->Initialize (storageFile);
00129     if (NS_FAILED(rv)) return rv;
00130 
00131     *_retval = conn;
00132     NS_ADDREF(*_retval);
00133     return NS_OK;
00134 }
00135 
00136 /* mozIStorageConnection openDatabase(in nsIFile aDatabaseFile); */
00137 NS_IMETHODIMP
00138 mozStorageService::OpenDatabase(nsIFile *aDatabaseFile, mozIStorageConnection **_retval)
00139 {
00140     nsresult rv;
00141 
00142     mozStorageConnection *msc = new mozStorageConnection(this);
00143     if (! msc)
00144         return NS_ERROR_OUT_OF_MEMORY;
00145     nsCOMPtr<mozIStorageConnection> conn = msc;
00146     rv = msc->Initialize (aDatabaseFile);
00147     if (NS_FAILED(rv)) return rv;
00148 
00149     *_retval = conn;
00150     NS_ADDREF(*_retval);
00151     return NS_OK;
00152 }
00153 
00154 NS_IMETHODIMP
00155 mozStorageService::Observe(nsISupports *aSubject, const char *aTopic,
00156                            const PRUnichar *aData)
00157 {
00158     nsresult rv;
00159     if (nsCRT::strcmp(aTopic, gQuitApplicationMessage) == 0) {
00160         rv = FinishAsyncIO();
00161         NS_ENSURE_SUCCESS(rv, rv);
00162     }
00163     return NS_OK;
00164 }