tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

mozStoragePrivateHelpers.h (4924B)


      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/. */
      6 
      7 #ifndef mozStoragePrivateHelpers_h
      8 #define mozStoragePrivateHelpers_h
      9 
     10 /**
     11 * This file contains convenience methods for mozStorage.
     12 */
     13 
     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"
     20 
     21 class mozIStorageCompletionCallback;
     22 class nsIRunnable;
     23 
     24 namespace mozilla {
     25 namespace storage {
     26 
     27 ////////////////////////////////////////////////////////////////////////////////
     28 //// Macros
     29 
     30 #define ENSURE_INDEX_VALUE(aIndex, aCount) \
     31  NS_ENSURE_TRUE(aIndex < aCount, NS_ERROR_INVALID_ARG)
     32 
     33 ////////////////////////////////////////////////////////////////////////////////
     34 //// Functions
     35 
     36 /**
     37 * Returns true if the given SQLite result is an error of come kind.
     38 *
     39 * @param aSQLiteResultCode
     40 *        The SQLite return code to check.
     41 * @returns true if the result represents an error.
     42 */
     43 bool isErrorCode(int aSQLiteResultCode);
     44 
     45 /**
     46 * Converts a SQLite return code to an nsresult return code.
     47 *
     48 * @param aSQLiteResultCode
     49 *        The SQLite return code to convert.
     50 * @returns the corresponding nsresult code for aSQLiteResultCode.
     51 */
     52 nsresult convertResultCode(int aSQLiteResultCode);
     53 
     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.
     59 *
     60 * @param aStatement
     61 *        The sqlite3_stmt object to check.
     62 */
     63 void checkAndLogStatementPerformance(sqlite3_stmt* aStatement);
     64 
     65 /**
     66 * Convert the provided JS::Value into a variant representation if possible.
     67 *
     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.
     76 */
     77 nsIVariant* convertJSValToVariant(JSContext* aCtx, const JS::Value& aValue);
     78 
     79 /**
     80 * Convert a provided nsIVariant implementation to our own thread-safe
     81 * refcounting implementation, if needed.
     82 *
     83 * @param aValue
     84 *        The original nsIVariant to be converted.
     85 * @return a thread-safe refcounting nsIVariant implementation.
     86 */
     87 Variant_base* convertVariantToStorageVariant(nsIVariant* aVariant);
     88 
     89 /**
     90 * Obtains an event that will notify a completion callback about completion.
     91 *
     92 * @param aCallback
     93 *        The callback to be notified.
     94 * @return an nsIRunnable that can be dispatched to the calling thread.
     95 */
     96 already_AddRefed<nsIRunnable> newCompletionEvent(
     97    mozIStorageCompletionCallback* aCallback);
     98 
     99 /**
    100 * Utility method to get a Blob as a string value.  The string expects
    101 * the interface exposed by nsAString/nsACString/etc.
    102 */
    103 template <class T, class V>
    104 nsresult DoGetBlobAsString(T* aThis, uint32_t aIndex, V& aValue) {
    105  typedef typename V::char_type char_type;
    106 
    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);
    112 
    113  aValue.Assign(blob, size / sizeof(char_type));
    114  delete[] blob;
    115  return NS_OK;
    116 }
    117 
    118 /**
    119 * Utility method to bind a string value as a Blob.  The string expects
    120 * the interface exposed by nsAString/nsACString/etc.
    121 */
    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));
    129 }
    130 
    131 /**
    132 * Utility method to bind a string value as a Blob.  The string expects
    133 * the interface exposed by nsAString/nsACString/etc.
    134 */
    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));
    141 }
    142 
    143 /**
    144 * Utility function to check if a serial event target may run runnables
    145 * on the current thread.
    146 */
    147 inline bool IsOnCurrentSerialEventTarget(nsISerialEventTarget* aTarget) {
    148  return aTarget->IsOnCurrentThread();
    149 }
    150 
    151 }  // namespace storage
    152 }  // namespace mozilla
    153 
    154 #endif  // mozStoragePrivateHelpers_h