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 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 #include "jsfriendapi.h"
11 #include "nsPrintfCString.h"
14 #include "mozilla/Mutex.h"
15 #include "mozilla/CondVar.h"
16 #include "nsQueryObject.h"
17 #include "nsThreadUtils.h"
18 #include "nsJSUtils.h"
21 #include "mozStoragePrivateHelpers.h"
22 #include "mozIStorageCompletionCallback.h"
24 #include "mozilla/Logging.h"
25 extern mozilla::LazyLogModule gStorageLog
;
30 nsresult
convertResultCode(int aSQLiteResultCode
) {
31 // Drop off the extended result bits of the result code.
32 int rc
= aSQLiteResultCode
& 0xFF;
41 return NS_ERROR_FILE_CORRUPTED
;
44 return NS_ERROR_FILE_ACCESS_DENIED
;
46 return NS_ERROR_STORAGE_BUSY
;
48 return NS_ERROR_FILE_IS_LOCKED
;
50 return NS_ERROR_FILE_READ_ONLY
;
52 return NS_ERROR_STORAGE_IOERR
;
55 return NS_ERROR_FILE_NO_DEVICE_SPACE
;
57 return NS_ERROR_OUT_OF_MEMORY
;
59 return NS_ERROR_UNEXPECTED
;
61 case SQLITE_INTERRUPT
:
62 return NS_ERROR_ABORT
;
63 case SQLITE_CONSTRAINT
:
64 return NS_ERROR_STORAGE_CONSTRAINT
;
69 nsAutoCString message
;
70 message
.AppendLiteral("SQLite returned error code ");
71 message
.AppendInt(rc
);
72 message
.AppendLiteral(" , Storage will convert it to NS_ERROR_FAILURE");
73 NS_WARNING_ASSERTION(rc
== SQLITE_ERROR
, message
.get());
75 return NS_ERROR_FAILURE
;
78 void checkAndLogStatementPerformance(sqlite3_stmt
* aStatement
) {
79 // Check to see if the query performed sorting operations or not. If it
80 // did, it may need to be optimized!
81 int count
= ::sqlite3_stmt_status(aStatement
, SQLITE_STMTSTATUS_SORT
, 1);
82 if (count
<= 0) return;
84 const char* sql
= ::sqlite3_sql(aStatement
);
86 // Check to see if this is marked to not warn
87 if (::strstr(sql
, "/* do not warn (bug ")) return;
89 // CREATE INDEX always sorts (sorting is a necessary step in creating
90 // an index). So ignore the warning there.
91 if (::strstr(sql
, "CREATE INDEX") || ::strstr(sql
, "CREATE UNIQUE INDEX"))
94 nsAutoCString
message("Suboptimal indexes for the SQL statement ");
95 #ifdef MOZ_STORAGE_SORTWARNING_SQL_DUMP
98 message
.AppendLiteral("` [");
99 message
.AppendInt(count
);
100 message
.AppendLiteral(" sort operation(s)]");
102 nsPrintfCString
address("0x%p", aStatement
);
103 message
.Append(address
);
105 message
.AppendLiteral(" (http://mzl.la/1FuID0j).");
106 NS_WARNING(message
.get());
109 nsIVariant
* convertJSValToVariant(JSContext
* aCtx
, const JS::Value
& aValue
) {
110 if (aValue
.isInt32()) return new IntegerVariant(aValue
.toInt32());
112 if (aValue
.isDouble()) return new FloatVariant(aValue
.toDouble());
114 if (aValue
.isString()) {
115 nsAutoJSString value
;
116 if (!value
.init(aCtx
, aValue
.toString())) return nullptr;
117 return new TextVariant(value
);
120 if (aValue
.isBoolean()) return new IntegerVariant(aValue
.isTrue() ? 1 : 0);
122 if (aValue
.isNull()) return new NullVariant();
124 if (aValue
.isObject()) {
125 JS::Rooted
<JSObject
*> obj(aCtx
, &aValue
.toObject());
126 // We only support Date instances, all others fail.
128 if (!js::DateIsValid(aCtx
, obj
, &valid
) || !valid
) return nullptr;
131 if (!js::DateGetMsecSinceEpoch(aCtx
, obj
, &msecd
)) return nullptr;
134 int64_t msec
= msecd
;
136 return new IntegerVariant(msec
);
142 Variant_base
* convertVariantToStorageVariant(nsIVariant
* aVariant
) {
143 RefPtr
<Variant_base
> variant
= do_QueryObject(aVariant
);
145 // JS helpers already convert the JS representation to a Storage Variant,
146 // in such a case there's nothing left to do here, so just pass-through.
150 if (!aVariant
) return new NullVariant();
152 uint16_t dataType
= aVariant
->GetDataType();
155 case nsIDataType::VTYPE_BOOL
:
156 case nsIDataType::VTYPE_INT8
:
157 case nsIDataType::VTYPE_INT16
:
158 case nsIDataType::VTYPE_INT32
:
159 case nsIDataType::VTYPE_UINT8
:
160 case nsIDataType::VTYPE_UINT16
:
161 case nsIDataType::VTYPE_UINT32
:
162 case nsIDataType::VTYPE_INT64
:
163 case nsIDataType::VTYPE_UINT64
: {
165 nsresult rv
= aVariant
->GetAsInt64(&v
);
166 NS_ENSURE_SUCCESS(rv
, nullptr);
167 return new IntegerVariant(v
);
169 case nsIDataType::VTYPE_FLOAT
:
170 case nsIDataType::VTYPE_DOUBLE
: {
172 nsresult rv
= aVariant
->GetAsDouble(&v
);
173 NS_ENSURE_SUCCESS(rv
, nullptr);
174 return new FloatVariant(v
);
176 case nsIDataType::VTYPE_CHAR
:
177 case nsIDataType::VTYPE_CHAR_STR
:
178 case nsIDataType::VTYPE_STRING_SIZE_IS
:
179 case nsIDataType::VTYPE_UTF8STRING
:
180 case nsIDataType::VTYPE_CSTRING
: {
182 nsresult rv
= aVariant
->GetAsAUTF8String(v
);
183 NS_ENSURE_SUCCESS(rv
, nullptr);
184 return new UTF8TextVariant(v
);
186 case nsIDataType::VTYPE_WCHAR
:
187 case nsIDataType::VTYPE_WCHAR_STR
:
188 case nsIDataType::VTYPE_WSTRING_SIZE_IS
:
189 case nsIDataType::VTYPE_ASTRING
: {
191 nsresult rv
= aVariant
->GetAsAString(v
);
192 NS_ENSURE_SUCCESS(rv
, nullptr);
193 return new TextVariant(v
);
195 case nsIDataType::VTYPE_ARRAY
: {
200 // Note this copies the array data.
201 nsresult rv
= aVariant
->GetAsArray(&type
, &iid
, &len
, &rawArray
);
202 NS_ENSURE_SUCCESS(rv
, nullptr);
203 if (type
== nsIDataType::VTYPE_UINT8
) {
204 std::pair
<uint8_t*, int> v(static_cast<uint8_t*>(rawArray
), len
);
205 // Take ownership of the data avoiding a further copy.
206 return new AdoptedBlobVariant(v
);
210 case nsIDataType::VTYPE_EMPTY
:
211 case nsIDataType::VTYPE_EMPTY_ARRAY
:
212 case nsIDataType::VTYPE_VOID
:
213 return new NullVariant();
214 case nsIDataType::VTYPE_ID
:
215 case nsIDataType::VTYPE_INTERFACE
:
216 case nsIDataType::VTYPE_INTERFACE_IS
:
218 NS_WARNING("Unsupported variant type");
226 class CallbackEvent
: public Runnable
{
228 explicit CallbackEvent(mozIStorageCompletionCallback
* aCallback
)
229 : Runnable("storage::CallbackEvent"), mCallback(aCallback
) {}
231 NS_IMETHOD
Run() override
{
232 (void)mCallback
->Complete(NS_OK
, nullptr);
237 nsCOMPtr
<mozIStorageCompletionCallback
> mCallback
;
240 already_AddRefed
<nsIRunnable
> newCompletionEvent(
241 mozIStorageCompletionCallback
* aCallback
) {
242 NS_ASSERTION(aCallback
, "Passing a null callback is a no-no!");
243 nsCOMPtr
<nsIRunnable
> event
= new CallbackEvent(aCallback
);
244 return event
.forget();
247 } // namespace storage
248 } // namespace mozilla