Bug 628949 - Update visible region / glass regions after we paint. r=roc a=2.0.
[mozilla-central.git] / storage / src / mozStoragePrivateHelpers.cpp
blob033e4e7dfa7e64912e7e2ba309faca93b07f9d2f
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
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 mozilla.org code.
18 * The Initial Developer of the Original Code is
19 * Mozilla Corporation
20 * Portions created by the Initial Developer are Copyright (C) 2009
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
25 * Shawn Wilsher <me@shawnwilsher.com>
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #include "sqlite3.h"
43 #include "jsapi.h"
44 #include "jsdate.h"
46 #include "nsPrintfCString.h"
47 #include "nsString.h"
48 #include "nsError.h"
49 #include "mozilla/Mutex.h"
50 #include "mozilla/CondVar.h"
51 #include "nsThreadUtils.h"
52 #include "nsJSUtils.h"
54 #include "Variant.h"
55 #include "mozStoragePrivateHelpers.h"
56 #include "mozIStorageStatement.h"
57 #include "mozIStorageCompletionCallback.h"
58 #include "mozIStorageBindingParams.h"
60 namespace mozilla {
61 namespace storage {
63 nsresult
64 convertResultCode(int aSQLiteResultCode)
66 // Drop off the extended result bits of the result code.
67 int rc = aSQLiteResultCode & 0xFF;
69 switch (rc) {
70 case SQLITE_OK:
71 case SQLITE_ROW:
72 case SQLITE_DONE:
73 return NS_OK;
74 case SQLITE_CORRUPT:
75 case SQLITE_NOTADB:
76 return NS_ERROR_FILE_CORRUPTED;
77 case SQLITE_PERM:
78 case SQLITE_CANTOPEN:
79 return NS_ERROR_FILE_ACCESS_DENIED;
80 case SQLITE_BUSY:
81 return NS_ERROR_STORAGE_BUSY;
82 case SQLITE_LOCKED:
83 return NS_ERROR_FILE_IS_LOCKED;
84 case SQLITE_READONLY:
85 return NS_ERROR_FILE_READ_ONLY;
86 case SQLITE_IOERR:
87 return NS_ERROR_STORAGE_IOERR;
88 case SQLITE_FULL:
89 case SQLITE_TOOBIG:
90 return NS_ERROR_FILE_NO_DEVICE_SPACE;
91 case SQLITE_NOMEM:
92 return NS_ERROR_OUT_OF_MEMORY;
93 case SQLITE_MISUSE:
94 return NS_ERROR_UNEXPECTED;
95 case SQLITE_ABORT:
96 case SQLITE_INTERRUPT:
97 return NS_ERROR_ABORT;
98 case SQLITE_CONSTRAINT:
99 return NS_ERROR_STORAGE_CONSTRAINT;
102 // generic error
103 #ifdef DEBUG
104 nsCAutoString message;
105 message.AppendLiteral("SQLite returned error code ");
106 message.AppendInt(rc);
107 message.AppendLiteral(" , Storage will convert it to NS_ERROR_FAILURE");
108 NS_WARNING(message.get());
109 #endif
110 return NS_ERROR_FAILURE;
113 void
114 checkAndLogStatementPerformance(sqlite3_stmt *aStatement)
116 // Check to see if the query performed sorting operations or not. If it
117 // did, it may need to be optimized!
118 int count = ::sqlite3_stmt_status(aStatement, SQLITE_STMTSTATUS_SORT, 1);
119 if (count <= 0)
120 return;
122 const char *sql = ::sqlite3_sql(aStatement);
124 // Check to see if this is marked to not warn
125 if (::strstr(sql, "/* do not warn (bug "))
126 return;
128 nsCAutoString message;
129 message.AppendInt(count);
130 if (count == 1)
131 message.Append(" sort operation has ");
132 else
133 message.Append(" sort operations have ");
134 message.Append("occurred for the SQL statement '");
135 nsPrintfCString address("0x%p", aStatement);
136 message.Append(address);
137 message.Append("'. See https://developer.mozilla.org/En/Storage/Warnings "
138 "details.");
139 NS_WARNING(message.get());
142 nsIVariant *
143 convertJSValToVariant(
144 JSContext *aCtx,
145 jsval aValue)
147 if (JSVAL_IS_INT(aValue))
148 return new IntegerVariant(JSVAL_TO_INT(aValue));
150 if (JSVAL_IS_DOUBLE(aValue))
151 return new FloatVariant(JSVAL_TO_DOUBLE(aValue));
153 if (JSVAL_IS_STRING(aValue)) {
154 JSString *str = JSVAL_TO_STRING(aValue);
155 nsDependentJSString value;
156 if (!value.init(aCtx, str))
157 return nsnull;
158 return new TextVariant(value);
161 if (JSVAL_IS_BOOLEAN(aValue))
162 return new IntegerVariant((aValue == JSVAL_TRUE) ? 1 : 0);
164 if (JSVAL_IS_NULL(aValue))
165 return new NullVariant();
167 if (JSVAL_IS_OBJECT(aValue)) {
168 JSObject *obj = JSVAL_TO_OBJECT(aValue);
169 // We only support Date instances, all others fail.
170 if (!::js_DateIsValid(aCtx, obj))
171 return nsnull;
173 double msecd = ::js_DateGetMsecSinceEpoch(aCtx, obj);
174 msecd *= 1000.0;
175 PRInt64 msec;
176 LL_D2L(msec, msecd);
178 return new IntegerVariant(msec);
181 return nsnull;
185 namespace {
186 class CallbackEvent : public nsRunnable
188 public:
189 CallbackEvent(mozIStorageCompletionCallback *aCallback)
190 : mCallback(aCallback)
194 NS_IMETHOD Run()
196 (void)mCallback->Complete();
197 return NS_OK;
199 private:
200 nsCOMPtr<mozIStorageCompletionCallback> mCallback;
202 } // anonymous namespace
203 already_AddRefed<nsIRunnable>
204 newCompletionEvent(mozIStorageCompletionCallback *aCallback)
206 NS_ASSERTION(aCallback, "Passing a null callback is a no-no!");
207 nsCOMPtr<nsIRunnable> event = new CallbackEvent(aCallback);
208 return event.forget();
212 * This code is heavily based on the sample at:
213 * http://www.sqlite.org/unlock_notify.html
215 namespace {
217 class UnlockNotification
219 public:
220 UnlockNotification()
221 : mMutex("UnlockNotification mMutex")
222 , mCondVar(mMutex, "UnlockNotification condVar")
223 , mSignaled(false)
227 void Wait()
229 mozilla::MutexAutoLock lock(mMutex);
230 while (!mSignaled) {
231 (void)mCondVar.Wait();
235 void Signal()
237 mozilla::MutexAutoLock lock(mMutex);
238 mSignaled = true;
239 (void)mCondVar.Notify();
242 private:
243 mozilla::Mutex mMutex;
244 mozilla::CondVar mCondVar;
245 bool mSignaled;
248 void
249 UnlockNotifyCallback(void **aArgs,
250 int aArgsSize)
252 for (int i = 0; i < aArgsSize; i++) {
253 UnlockNotification *notification =
254 static_cast<UnlockNotification *>(aArgs[i]);
255 notification->Signal();
260 WaitForUnlockNotify(sqlite3* aDatabase)
262 UnlockNotification notification;
263 int srv = ::sqlite3_unlock_notify(aDatabase, UnlockNotifyCallback,
264 &notification);
265 NS_ASSERTION(srv == SQLITE_LOCKED || srv == SQLITE_OK, "Bad result!");
266 if (srv == SQLITE_OK)
267 notification.Wait();
269 return srv;
272 } // anonymous namespace
275 stepStmt(sqlite3_stmt* aStatement)
277 bool checkedMainThread = false;
279 sqlite3* db = ::sqlite3_db_handle(aStatement);
280 (void)::sqlite3_extended_result_codes(db, 1);
282 int srv;
283 while ((srv = ::sqlite3_step(aStatement)) == SQLITE_LOCKED_SHAREDCACHE) {
284 if (!checkedMainThread) {
285 checkedMainThread = true;
286 if (NS_IsMainThread()) {
287 NS_WARNING("We won't allow blocking on the main thread!");
288 break;
292 srv = WaitForUnlockNotify(sqlite3_db_handle(aStatement));
293 if (srv != SQLITE_OK)
294 break;
296 ::sqlite3_reset(aStatement);
299 (void)::sqlite3_extended_result_codes(db, 0);
300 // Drop off the extended result bits of the result code.
301 return srv & 0xFF;
305 prepareStmt(sqlite3* aDatabase,
306 const nsCString &aSQL,
307 sqlite3_stmt **_stmt)
309 bool checkedMainThread = false;
311 (void)::sqlite3_extended_result_codes(aDatabase, 1);
313 int srv;
314 while((srv = ::sqlite3_prepare_v2(aDatabase, aSQL.get(), -1, _stmt, NULL)) ==
315 SQLITE_LOCKED_SHAREDCACHE) {
316 if (!checkedMainThread) {
317 checkedMainThread = true;
318 if (NS_IsMainThread()) {
319 NS_WARNING("We won't allow blocking on the main thread!");
320 break;
324 srv = WaitForUnlockNotify(aDatabase);
325 if (srv != SQLITE_OK)
326 break;
329 (void)::sqlite3_extended_result_codes(aDatabase, 0);
330 // Drop off the extended result bits of the result code.
331 return srv & 0xFF;
334 } // namespace storage
335 } // namespace mozilla