storage/src/mozStoragePrivateHelpers.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "sqlite3.h"
michael@0 8
michael@0 9 #include "jsfriendapi.h"
michael@0 10
michael@0 11 #include "nsPrintfCString.h"
michael@0 12 #include "nsString.h"
michael@0 13 #include "nsError.h"
michael@0 14 #include "mozilla/Mutex.h"
michael@0 15 #include "mozilla/CondVar.h"
michael@0 16 #include "nsThreadUtils.h"
michael@0 17 #include "nsJSUtils.h"
michael@0 18
michael@0 19 #include "Variant.h"
michael@0 20 #include "mozStoragePrivateHelpers.h"
michael@0 21 #include "mozIStorageStatement.h"
michael@0 22 #include "mozIStorageCompletionCallback.h"
michael@0 23 #include "mozIStorageBindingParams.h"
michael@0 24
michael@0 25 #include "prlog.h"
michael@0 26 #ifdef PR_LOGGING
michael@0 27 extern PRLogModuleInfo* gStorageLog;
michael@0 28 #endif
michael@0 29
michael@0 30 namespace mozilla {
michael@0 31 namespace storage {
michael@0 32
michael@0 33 nsresult
michael@0 34 convertResultCode(int aSQLiteResultCode)
michael@0 35 {
michael@0 36 // Drop off the extended result bits of the result code.
michael@0 37 int rc = aSQLiteResultCode & 0xFF;
michael@0 38
michael@0 39 switch (rc) {
michael@0 40 case SQLITE_OK:
michael@0 41 case SQLITE_ROW:
michael@0 42 case SQLITE_DONE:
michael@0 43 return NS_OK;
michael@0 44 case SQLITE_CORRUPT:
michael@0 45 case SQLITE_NOTADB:
michael@0 46 return NS_ERROR_FILE_CORRUPTED;
michael@0 47 case SQLITE_PERM:
michael@0 48 case SQLITE_CANTOPEN:
michael@0 49 return NS_ERROR_FILE_ACCESS_DENIED;
michael@0 50 case SQLITE_BUSY:
michael@0 51 return NS_ERROR_STORAGE_BUSY;
michael@0 52 case SQLITE_LOCKED:
michael@0 53 return NS_ERROR_FILE_IS_LOCKED;
michael@0 54 case SQLITE_READONLY:
michael@0 55 return NS_ERROR_FILE_READ_ONLY;
michael@0 56 case SQLITE_IOERR:
michael@0 57 return NS_ERROR_STORAGE_IOERR;
michael@0 58 case SQLITE_FULL:
michael@0 59 case SQLITE_TOOBIG:
michael@0 60 return NS_ERROR_FILE_NO_DEVICE_SPACE;
michael@0 61 case SQLITE_NOMEM:
michael@0 62 return NS_ERROR_OUT_OF_MEMORY;
michael@0 63 case SQLITE_MISUSE:
michael@0 64 return NS_ERROR_UNEXPECTED;
michael@0 65 case SQLITE_ABORT:
michael@0 66 case SQLITE_INTERRUPT:
michael@0 67 return NS_ERROR_ABORT;
michael@0 68 case SQLITE_CONSTRAINT:
michael@0 69 return NS_ERROR_STORAGE_CONSTRAINT;
michael@0 70 }
michael@0 71
michael@0 72 // generic error
michael@0 73 #ifdef DEBUG
michael@0 74 nsAutoCString message;
michael@0 75 message.AppendLiteral("SQLite returned error code ");
michael@0 76 message.AppendInt(rc);
michael@0 77 message.AppendLiteral(" , Storage will convert it to NS_ERROR_FAILURE");
michael@0 78 NS_WARNING(message.get());
michael@0 79 #endif
michael@0 80 return NS_ERROR_FAILURE;
michael@0 81 }
michael@0 82
michael@0 83 void
michael@0 84 checkAndLogStatementPerformance(sqlite3_stmt *aStatement)
michael@0 85 {
michael@0 86 // Check to see if the query performed sorting operations or not. If it
michael@0 87 // did, it may need to be optimized!
michael@0 88 int count = ::sqlite3_stmt_status(aStatement, SQLITE_STMTSTATUS_SORT, 1);
michael@0 89 if (count <= 0)
michael@0 90 return;
michael@0 91
michael@0 92 const char *sql = ::sqlite3_sql(aStatement);
michael@0 93
michael@0 94 // Check to see if this is marked to not warn
michael@0 95 if (::strstr(sql, "/* do not warn (bug "))
michael@0 96 return;
michael@0 97
michael@0 98 nsAutoCString message;
michael@0 99 message.AppendInt(count);
michael@0 100 if (count == 1)
michael@0 101 message.Append(" sort operation has ");
michael@0 102 else
michael@0 103 message.Append(" sort operations have ");
michael@0 104 message.Append("occurred for the SQL statement '");
michael@0 105 nsPrintfCString address("0x%p", aStatement);
michael@0 106 message.Append(address);
michael@0 107 message.Append("'. See https://developer.mozilla.org/En/Storage/Warnings "
michael@0 108 "details.");
michael@0 109 NS_WARNING(message.get());
michael@0 110 }
michael@0 111
michael@0 112 nsIVariant *
michael@0 113 convertJSValToVariant(
michael@0 114 JSContext *aCtx,
michael@0 115 JS::Value aValue)
michael@0 116 {
michael@0 117 if (aValue.isInt32())
michael@0 118 return new IntegerVariant(aValue.toInt32());
michael@0 119
michael@0 120 if (aValue.isDouble())
michael@0 121 return new FloatVariant(aValue.toDouble());
michael@0 122
michael@0 123 if (aValue.isString()) {
michael@0 124 nsDependentJSString value;
michael@0 125 if (!value.init(aCtx, aValue))
michael@0 126 return nullptr;
michael@0 127 return new TextVariant(value);
michael@0 128 }
michael@0 129
michael@0 130 if (aValue.isBoolean())
michael@0 131 return new IntegerVariant(aValue.isTrue() ? 1 : 0);
michael@0 132
michael@0 133 if (aValue.isNull())
michael@0 134 return new NullVariant();
michael@0 135
michael@0 136 if (aValue.isObject()) {
michael@0 137 JSObject* obj = &aValue.toObject();
michael@0 138 // We only support Date instances, all others fail.
michael@0 139 if (!::js_DateIsValid(obj))
michael@0 140 return nullptr;
michael@0 141
michael@0 142 double msecd = ::js_DateGetMsecSinceEpoch(obj);
michael@0 143 msecd *= 1000.0;
michael@0 144 int64_t msec = msecd;
michael@0 145
michael@0 146 return new IntegerVariant(msec);
michael@0 147 }
michael@0 148
michael@0 149 return nullptr;
michael@0 150 }
michael@0 151
michael@0 152 namespace {
michael@0 153 class CallbackEvent : public nsRunnable
michael@0 154 {
michael@0 155 public:
michael@0 156 CallbackEvent(mozIStorageCompletionCallback *aCallback)
michael@0 157 : mCallback(aCallback)
michael@0 158 {
michael@0 159 }
michael@0 160
michael@0 161 NS_IMETHOD Run()
michael@0 162 {
michael@0 163 (void)mCallback->Complete(NS_OK, nullptr);
michael@0 164 return NS_OK;
michael@0 165 }
michael@0 166 private:
michael@0 167 nsCOMPtr<mozIStorageCompletionCallback> mCallback;
michael@0 168 };
michael@0 169 } // anonymous namespace
michael@0 170 already_AddRefed<nsIRunnable>
michael@0 171 newCompletionEvent(mozIStorageCompletionCallback *aCallback)
michael@0 172 {
michael@0 173 NS_ASSERTION(aCallback, "Passing a null callback is a no-no!");
michael@0 174 nsCOMPtr<nsIRunnable> event = new CallbackEvent(aCallback);
michael@0 175 return event.forget();
michael@0 176 }
michael@0 177
michael@0 178
michael@0 179
michael@0 180 } // namespace storage
michael@0 181 } // namespace mozilla

mercurial