Bug 435531 ? problem saving login when form or existing login is password-only. r...
[wine-gecko.git] / storage / src / mozStorageService.cpp
blobc3ef24e418a71c18c2a3735fc887421c787c7f7d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: sw=4 ts=4 sts=4
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is Oracle Corporation code.
18 * The Initial Developer of the Original Code is
19 * Oracle Corporation
20 * Portions created by the Initial Developer are Copyright (C) 2004
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
25 * Brett Wilson <brettw@gmail.com>
26 * Shawn Wilsher <me@shawnwilsher.com>
28 * Alternatively, the contents of this file may be used under the terms of
29 * either the GNU General Public License Version 2 or later (the "GPL"), or
30 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 * in which case the provisions of the GPL or the LGPL are applicable instead
32 * of those above. If you wish to allow use of your version of this file only
33 * under the terms of either the GPL or the LGPL, and not to allow others to
34 * use your version of this file under the terms of the MPL, indicate your
35 * decision by deleting the provisions above and replace them with the notice
36 * and other provisions required by the GPL or the LGPL. If you do not delete
37 * the provisions above, a recipient may use your version of this file under
38 * the terms of any one of the MPL, the GPL or the LGPL.
40 * ***** END LICENSE BLOCK ***** */
42 #include "mozStorageService.h"
43 #include "mozStorageConnection.h"
44 #include "nsCRT.h"
45 #include "plstr.h"
46 #include "prinit.h"
47 #include "nsAutoLock.h"
48 #include "nsAutoPtr.h"
49 #include "mozStorage.h"
51 #include "sqlite3.h"
53 NS_IMPL_THREADSAFE_ISUPPORTS1(mozStorageService, mozIStorageService)
55 /**
56 * Lock used in mozStorageService::GetSingleton to ensure that we only create
57 * one instance of the storage service. This lock is created in SingetonInit()
58 * and destroyed in mozStorageService::~mozStorageService().
60 static PRLock *gSingletonLock;
62 /**
63 * Creates the lock used in the singleton getter. This lock is destroyed in
64 * the services destructor.
66 * @returns PR_SUCCESS if creating the lock was successful, PR_FAILURE otherwise
68 static
69 PRStatus
70 SingletonInit()
72 gSingletonLock = PR_NewLock();
73 NS_ENSURE_TRUE(gSingletonLock, PR_FAILURE);
74 return PR_SUCCESS;
77 mozStorageService *mozStorageService::gStorageService = nsnull;
79 mozStorageService *
80 mozStorageService::GetSingleton()
82 // Since this service can be called by multiple threads, we have to use a
83 // a lock to test and possibly create gStorageService
84 static PRCallOnceType sInitOnce;
85 PRStatus rc = PR_CallOnce(&sInitOnce, SingletonInit);
86 if (rc != PR_SUCCESS)
87 return nsnull;
89 // If someone managed to start us twice, error out early.
90 if (!gSingletonLock)
91 return nsnull;
93 nsAutoLock lock(gSingletonLock);
94 if (gStorageService) {
95 NS_ADDREF(gStorageService);
96 return gStorageService;
99 gStorageService = new mozStorageService();
100 if (gStorageService) {
101 NS_ADDREF(gStorageService);
102 if (NS_FAILED(gStorageService->Init()))
103 NS_RELEASE(gStorageService);
106 return gStorageService;
109 mozStorageService::~mozStorageService()
111 gStorageService = nsnull;
112 PR_DestroyLock(mLock);
113 PR_DestroyLock(gSingletonLock);
114 gSingletonLock = nsnull;
117 nsresult
118 mozStorageService::Init()
120 mLock = PR_NewLock();
121 if (!mLock)
122 return NS_ERROR_OUT_OF_MEMORY;
124 nsresult rv = mBackground.initialize();
125 NS_ENSURE_SUCCESS(rv, rv);
127 // This makes multiple connections to the same database share the same pager
128 // cache. We do not need to lock here with mLock because this function is
129 // only ever called from mozStorageService::GetSingleton, which will only
130 // call this function once, and will not return until this function returns.
131 int rc = sqlite3_enable_shared_cache(1);
132 if (rc != SQLITE_OK)
133 return ConvertResultCode(rc);
135 return NS_OK;
138 #ifndef NS_APP_STORAGE_50_FILE
139 #define NS_APP_STORAGE_50_FILE "UStor"
140 #endif
142 /* mozIStorageConnection openSpecialDatabase(in string aStorageKey); */
143 NS_IMETHODIMP
144 mozStorageService::OpenSpecialDatabase(const char *aStorageKey, mozIStorageConnection **_retval)
146 nsresult rv;
148 nsCOMPtr<nsIFile> storageFile;
149 if (PL_strcmp(aStorageKey, "memory") == 0) {
150 // just fall through with NULL storageFile, this will cause the storage
151 // connection to use a memory DB.
152 } else if (PL_strcmp(aStorageKey, "profile") == 0) {
154 rv = NS_GetSpecialDirectory(NS_APP_STORAGE_50_FILE, getter_AddRefs(storageFile));
155 if (NS_FAILED(rv)) {
156 // teh wtf?
157 return rv;
160 nsString filename;
161 storageFile->GetPath(filename);
162 nsCString filename8 = NS_ConvertUTF16toUTF8(filename.get());
163 // fall through to DB initialization
164 } else {
165 return NS_ERROR_INVALID_ARG;
168 mozStorageConnection *msc = new mozStorageConnection(this);
169 if (!msc)
170 return NS_ERROR_OUT_OF_MEMORY;
172 rv = msc->Initialize (storageFile);
173 NS_ENSURE_SUCCESS(rv, rv);
175 NS_ADDREF(*_retval = msc);
176 return NS_OK;
179 /* mozIStorageConnection openDatabase(in nsIFile aDatabaseFile); */
180 NS_IMETHODIMP
181 mozStorageService::OpenDatabase(nsIFile *aDatabaseFile, mozIStorageConnection **_retval)
183 nsRefPtr<mozStorageConnection> msc = new mozStorageConnection(this);
184 if (!msc)
185 return NS_ERROR_OUT_OF_MEMORY;
188 nsAutoLock lock(mLock);
189 nsresult rv = msc->Initialize(aDatabaseFile);
190 NS_ENSURE_SUCCESS(rv, rv);
193 NS_ADDREF(*_retval = msc);
194 return NS_OK;
197 /* mozIStorageConnection openUnsharedDatabase(in nsIFile aDatabaseFile); */
198 NS_IMETHODIMP
199 mozStorageService::OpenUnsharedDatabase(nsIFile *aDatabaseFile, mozIStorageConnection **_retval)
201 nsRefPtr<mozStorageConnection> msc = new mozStorageConnection(this);
202 if (!msc)
203 return NS_ERROR_OUT_OF_MEMORY;
205 // Initialize the connection, temporarily turning off shared caches so the
206 // new connection gets its own cache. Database connections are assigned
207 // caches when they are opened, and they retain those caches for their
208 // lifetimes, unaffected by changes to the shared caches setting, so we can
209 // disable shared caches temporarily while we initialize the new connection
210 // without affecting the caches currently in use by other connections.
211 nsresult rv;
213 nsAutoLock lock(mLock);
214 int rc = sqlite3_enable_shared_cache(0);
215 if (rc != SQLITE_OK)
216 return ConvertResultCode(rc);
218 rv = msc->Initialize(aDatabaseFile);
220 rc = sqlite3_enable_shared_cache(1);
221 if (rc != SQLITE_OK)
222 return ConvertResultCode(rc);
224 NS_ENSURE_SUCCESS(rv, rv);
226 NS_ADDREF(*_retval = msc);
227 return NS_OK;
231 ** Utilities
234 NS_IMETHODIMP
235 mozStorageService::BackupDatabaseFile(nsIFile *aDBFile,
236 const nsAString &aBackupFileName,
237 nsIFile *aBackupParentDirectory,
238 nsIFile **backup)
240 nsresult rv;
241 nsCOMPtr<nsIFile> parentDir = aBackupParentDirectory;
242 if (!parentDir) {
243 // This argument is optional, and defaults to the same parent directory
244 // as the current file.
245 rv = aDBFile->GetParent(getter_AddRefs(parentDir));
246 NS_ENSURE_SUCCESS(rv, rv);
249 nsCOMPtr<nsIFile> backupDB;
250 rv = parentDir->Clone(getter_AddRefs(backupDB));
251 NS_ENSURE_SUCCESS(rv, rv);
253 rv = backupDB->Append(aBackupFileName);
254 NS_ENSURE_SUCCESS(rv, rv);
256 rv = backupDB->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
257 NS_ENSURE_SUCCESS(rv, rv);
259 nsAutoString fileName;
260 rv = backupDB->GetLeafName(fileName);
261 NS_ENSURE_SUCCESS(rv, rv);
263 rv = backupDB->Remove(PR_FALSE);
264 NS_ENSURE_SUCCESS(rv, rv);
266 backupDB.swap(*backup);
268 return aDBFile->CopyTo(parentDir, fileName);