Bug 1831567 [wpt PR 39864] - Reduce calls to pip, a=testonly
[gecko.git] / storage / mozStoragePrivateHelpers.h
blob9dca95392e5e02b39156e487fe9c730a76ab9daf
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/. */
7 #ifndef mozStoragePrivateHelpers_h
8 #define mozStoragePrivateHelpers_h
10 /**
11 * This file contains convenience methods for mozStorage.
14 #include "sqlite3.h"
15 #include "nsISerialEventTarget.h"
16 #include "nsIVariant.h"
17 #include "nsError.h"
18 #include "js/TypeDecls.h"
19 #include "Variant.h"
21 class mozIStorageCompletionCallback;
22 class nsIRunnable;
24 namespace mozilla {
25 namespace storage {
27 ////////////////////////////////////////////////////////////////////////////////
28 //// Macros
30 #define ENSURE_INDEX_VALUE(aIndex, aCount) \
31 NS_ENSURE_TRUE(aIndex < aCount, NS_ERROR_INVALID_ARG)
33 ////////////////////////////////////////////////////////////////////////////////
34 //// Functions
36 /**
37 * Returns true if the given SQLite result is an error of come kind.
39 * @param aSQLiteResultCode
40 * The SQLite return code to check.
41 * @returns true if the result represents an error.
43 bool isErrorCode(int aSQLiteResultCode);
45 /**
46 * Converts a SQLite return code to an nsresult return code.
48 * @param aSQLiteResultCode
49 * The SQLite return code to convert.
50 * @returns the corresponding nsresult code for aSQLiteResultCode.
52 nsresult convertResultCode(int aSQLiteResultCode);
54 /**
55 * Checks the performance of a SQLite statement and logs a warning with
56 * NS_WARNING. Currently this only checks the number of sort operations done
57 * on a statement, and if more than zero have been done, the statement can be
58 * made faster with the careful use of an index.
60 * @param aStatement
61 * The sqlite3_stmt object to check.
63 void checkAndLogStatementPerformance(sqlite3_stmt* aStatement);
65 /**
66 * Convert the provided JS::Value into a variant representation if possible.
68 * @param aCtx
69 * The JSContext the value is from.
70 * @param aValue
71 * The JavaScript value to convert. All primitive types are supported,
72 * but only Date objects are supported from the Date family. Date
73 * objects are coerced to PRTime (nanoseconds since epoch) values.
74 * @return the variant if conversion was successful, nullptr if conversion
75 * failed. The caller is responsible for addref'ing if non-null.
77 nsIVariant* convertJSValToVariant(JSContext* aCtx, const JS::Value& aValue);
79 /**
80 * Convert a provided nsIVariant implementation to our own thread-safe
81 * refcounting implementation, if needed.
83 * @param aValue
84 * The original nsIVariant to be converted.
85 * @return a thread-safe refcounting nsIVariant implementation.
87 Variant_base* convertVariantToStorageVariant(nsIVariant* aVariant);
89 /**
90 * Obtains an event that will notify a completion callback about completion.
92 * @param aCallback
93 * The callback to be notified.
94 * @return an nsIRunnable that can be dispatched to the calling thread.
96 already_AddRefed<nsIRunnable> newCompletionEvent(
97 mozIStorageCompletionCallback* aCallback);
99 /**
100 * Utility method to get a Blob as a string value. The string expects
101 * the interface exposed by nsAString/nsACString/etc.
103 template <class T, class V>
104 nsresult DoGetBlobAsString(T* aThis, uint32_t aIndex, V& aValue) {
105 typedef typename V::char_type char_type;
107 uint32_t size;
108 char_type* blob;
109 nsresult rv =
110 aThis->GetBlob(aIndex, &size, reinterpret_cast<uint8_t**>(&blob));
111 NS_ENSURE_SUCCESS(rv, rv);
113 aValue.Assign(blob, size / sizeof(char_type));
114 delete[] blob;
115 return NS_OK;
119 * Utility method to bind a string value as a Blob. The string expects
120 * the interface exposed by nsAString/nsACString/etc.
122 template <class T, class V>
123 nsresult DoBindStringAsBlobByName(T* aThis, const nsACString& aName,
124 const V& aValue) {
125 typedef typename V::char_type char_type;
126 return aThis->BindBlobByName(
127 aName, reinterpret_cast<const uint8_t*>(aValue.BeginReading()),
128 aValue.Length() * sizeof(char_type));
132 * Utility method to bind a string value as a Blob. The string expects
133 * the interface exposed by nsAString/nsACString/etc.
135 template <class T, class V>
136 nsresult DoBindStringAsBlobByIndex(T* aThis, uint32_t aIndex, const V& aValue) {
137 typedef typename V::char_type char_type;
138 return aThis->BindBlobByIndex(
139 aIndex, reinterpret_cast<const uint8_t*>(aValue.BeginReading()),
140 aValue.Length() * sizeof(char_type));
144 * Utility function to check if a serial event target may run runnables
145 * on the current thread.
147 inline bool IsOnCurrentSerialEventTarget(nsISerialEventTarget* aTarget) {
148 return aTarget->IsOnCurrentThread();
151 } // namespace storage
152 } // namespace mozilla
154 #endif // mozStoragePrivateHelpers_h