Wed, 31 Dec 2014 06:09:35 +0100
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 "TestHarness.h" |
michael@0 | 8 | #include "nsMemory.h" |
michael@0 | 9 | #include "nsThreadUtils.h" |
michael@0 | 10 | #include "nsDirectoryServiceDefs.h" |
michael@0 | 11 | #include "mozIStorageService.h" |
michael@0 | 12 | #include "mozIStorageConnection.h" |
michael@0 | 13 | #include "mozIStorageStatementCallback.h" |
michael@0 | 14 | #include "mozIStorageCompletionCallback.h" |
michael@0 | 15 | #include "mozIStorageBindingParamsArray.h" |
michael@0 | 16 | #include "mozIStorageBindingParams.h" |
michael@0 | 17 | #include "mozIStorageAsyncStatement.h" |
michael@0 | 18 | #include "mozIStorageStatement.h" |
michael@0 | 19 | #include "mozIStoragePendingStatement.h" |
michael@0 | 20 | #include "mozIStorageError.h" |
michael@0 | 21 | #include "nsThreadUtils.h" |
michael@0 | 22 | |
michael@0 | 23 | static int gTotalTests = 0; |
michael@0 | 24 | static int gPassedTests = 0; |
michael@0 | 25 | |
michael@0 | 26 | #define do_check_true(aCondition) \ |
michael@0 | 27 | PR_BEGIN_MACRO \ |
michael@0 | 28 | gTotalTests++; \ |
michael@0 | 29 | if (aCondition) { \ |
michael@0 | 30 | gPassedTests++; \ |
michael@0 | 31 | } else { \ |
michael@0 | 32 | fail("%s | Expected true, got false at line %d", __FILE__, __LINE__); \ |
michael@0 | 33 | } \ |
michael@0 | 34 | PR_END_MACRO |
michael@0 | 35 | |
michael@0 | 36 | #define do_check_false(aCondition) \ |
michael@0 | 37 | PR_BEGIN_MACRO \ |
michael@0 | 38 | gTotalTests++; \ |
michael@0 | 39 | if (!aCondition) { \ |
michael@0 | 40 | gPassedTests++; \ |
michael@0 | 41 | } else { \ |
michael@0 | 42 | fail("%s | Expected false, got true at line %d", __FILE__, __LINE__); \ |
michael@0 | 43 | } \ |
michael@0 | 44 | PR_END_MACRO |
michael@0 | 45 | |
michael@0 | 46 | #define do_check_success(aResult) \ |
michael@0 | 47 | do_check_true(NS_SUCCEEDED(aResult)) |
michael@0 | 48 | |
michael@0 | 49 | #ifdef LINUX |
michael@0 | 50 | // XXX Linux opt builds on tinderbox are orange due to linking with stdlib. |
michael@0 | 51 | // This is sad and annoying, but it's a workaround that works. |
michael@0 | 52 | #define do_check_eq(aExpected, aActual) \ |
michael@0 | 53 | do_check_true(aExpected == aActual) |
michael@0 | 54 | #else |
michael@0 | 55 | #include <sstream> |
michael@0 | 56 | |
michael@0 | 57 | // Print nsresult as uint32_t |
michael@0 | 58 | std::ostream& operator<<(std::ostream& aStream, const nsresult aInput) |
michael@0 | 59 | { |
michael@0 | 60 | return aStream << static_cast<uint32_t>(aInput); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | #define do_check_eq(aExpected, aActual) \ |
michael@0 | 64 | PR_BEGIN_MACRO \ |
michael@0 | 65 | gTotalTests++; \ |
michael@0 | 66 | if (aExpected == aActual) { \ |
michael@0 | 67 | gPassedTests++; \ |
michael@0 | 68 | } else { \ |
michael@0 | 69 | std::ostringstream temp; \ |
michael@0 | 70 | temp << __FILE__ << " | Expected '" << aExpected << "', got '"; \ |
michael@0 | 71 | temp << aActual <<"' at line " << __LINE__; \ |
michael@0 | 72 | fail(temp.str().c_str()); \ |
michael@0 | 73 | } \ |
michael@0 | 74 | PR_END_MACRO |
michael@0 | 75 | #endif |
michael@0 | 76 | |
michael@0 | 77 | already_AddRefed<mozIStorageService> |
michael@0 | 78 | getService() |
michael@0 | 79 | { |
michael@0 | 80 | nsCOMPtr<mozIStorageService> ss = |
michael@0 | 81 | do_GetService("@mozilla.org/storage/service;1"); |
michael@0 | 82 | do_check_true(ss); |
michael@0 | 83 | return ss.forget(); |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | already_AddRefed<mozIStorageConnection> |
michael@0 | 87 | getMemoryDatabase() |
michael@0 | 88 | { |
michael@0 | 89 | nsCOMPtr<mozIStorageService> ss = getService(); |
michael@0 | 90 | nsCOMPtr<mozIStorageConnection> conn; |
michael@0 | 91 | nsresult rv = ss->OpenSpecialDatabase("memory", getter_AddRefs(conn)); |
michael@0 | 92 | do_check_success(rv); |
michael@0 | 93 | return conn.forget(); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | already_AddRefed<mozIStorageConnection> |
michael@0 | 97 | getDatabase() |
michael@0 | 98 | { |
michael@0 | 99 | nsCOMPtr<nsIFile> dbFile; |
michael@0 | 100 | (void)NS_GetSpecialDirectory(NS_APP_USER_PROFILE_50_DIR, |
michael@0 | 101 | getter_AddRefs(dbFile)); |
michael@0 | 102 | NS_ASSERTION(dbFile, "The directory doesn't exists?!"); |
michael@0 | 103 | |
michael@0 | 104 | nsresult rv = dbFile->Append(NS_LITERAL_STRING("storage_test_db.sqlite")); |
michael@0 | 105 | do_check_success(rv); |
michael@0 | 106 | |
michael@0 | 107 | nsCOMPtr<mozIStorageService> ss = getService(); |
michael@0 | 108 | nsCOMPtr<mozIStorageConnection> conn; |
michael@0 | 109 | rv = ss->OpenDatabase(dbFile, getter_AddRefs(conn)); |
michael@0 | 110 | do_check_success(rv); |
michael@0 | 111 | return conn.forget(); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | |
michael@0 | 115 | class AsyncStatementSpinner : public mozIStorageStatementCallback |
michael@0 | 116 | , public mozIStorageCompletionCallback |
michael@0 | 117 | { |
michael@0 | 118 | public: |
michael@0 | 119 | NS_DECL_ISUPPORTS |
michael@0 | 120 | NS_DECL_MOZISTORAGESTATEMENTCALLBACK |
michael@0 | 121 | NS_DECL_MOZISTORAGECOMPLETIONCALLBACK |
michael@0 | 122 | |
michael@0 | 123 | AsyncStatementSpinner(); |
michael@0 | 124 | |
michael@0 | 125 | void SpinUntilCompleted(); |
michael@0 | 126 | |
michael@0 | 127 | uint16_t completionReason; |
michael@0 | 128 | |
michael@0 | 129 | protected: |
michael@0 | 130 | virtual ~AsyncStatementSpinner() {} |
michael@0 | 131 | volatile bool mCompleted; |
michael@0 | 132 | }; |
michael@0 | 133 | |
michael@0 | 134 | NS_IMPL_ISUPPORTS(AsyncStatementSpinner, |
michael@0 | 135 | mozIStorageStatementCallback, |
michael@0 | 136 | mozIStorageCompletionCallback) |
michael@0 | 137 | |
michael@0 | 138 | AsyncStatementSpinner::AsyncStatementSpinner() |
michael@0 | 139 | : completionReason(0) |
michael@0 | 140 | , mCompleted(false) |
michael@0 | 141 | { |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | NS_IMETHODIMP |
michael@0 | 145 | AsyncStatementSpinner::HandleResult(mozIStorageResultSet *aResultSet) |
michael@0 | 146 | { |
michael@0 | 147 | return NS_OK; |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | NS_IMETHODIMP |
michael@0 | 151 | AsyncStatementSpinner::HandleError(mozIStorageError *aError) |
michael@0 | 152 | { |
michael@0 | 153 | int32_t result; |
michael@0 | 154 | nsresult rv = aError->GetResult(&result); |
michael@0 | 155 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 156 | nsAutoCString message; |
michael@0 | 157 | rv = aError->GetMessage(message); |
michael@0 | 158 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 159 | |
michael@0 | 160 | nsAutoCString warnMsg; |
michael@0 | 161 | warnMsg.Append("An error occurred while executing an async statement: "); |
michael@0 | 162 | warnMsg.AppendInt(result); |
michael@0 | 163 | warnMsg.Append(" "); |
michael@0 | 164 | warnMsg.Append(message); |
michael@0 | 165 | NS_WARNING(warnMsg.get()); |
michael@0 | 166 | |
michael@0 | 167 | return NS_OK; |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | NS_IMETHODIMP |
michael@0 | 171 | AsyncStatementSpinner::HandleCompletion(uint16_t aReason) |
michael@0 | 172 | { |
michael@0 | 173 | completionReason = aReason; |
michael@0 | 174 | mCompleted = true; |
michael@0 | 175 | return NS_OK; |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | NS_IMETHODIMP |
michael@0 | 179 | AsyncStatementSpinner::Complete(nsresult, nsISupports*) |
michael@0 | 180 | { |
michael@0 | 181 | mCompleted = true; |
michael@0 | 182 | return NS_OK; |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | void AsyncStatementSpinner::SpinUntilCompleted() |
michael@0 | 186 | { |
michael@0 | 187 | nsCOMPtr<nsIThread> thread(::do_GetCurrentThread()); |
michael@0 | 188 | nsresult rv = NS_OK; |
michael@0 | 189 | bool processed = true; |
michael@0 | 190 | while (!mCompleted && NS_SUCCEEDED(rv)) { |
michael@0 | 191 | rv = thread->ProcessNextEvent(true, &processed); |
michael@0 | 192 | } |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | #define NS_DECL_ASYNCSTATEMENTSPINNER \ |
michael@0 | 196 | NS_IMETHOD HandleResult(mozIStorageResultSet *aResultSet); |
michael@0 | 197 | |
michael@0 | 198 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 199 | //// Async Helpers |
michael@0 | 200 | |
michael@0 | 201 | /** |
michael@0 | 202 | * Execute an async statement, blocking the main thread until we get the |
michael@0 | 203 | * callback completion notification. |
michael@0 | 204 | */ |
michael@0 | 205 | void |
michael@0 | 206 | blocking_async_execute(mozIStorageBaseStatement *stmt) |
michael@0 | 207 | { |
michael@0 | 208 | nsRefPtr<AsyncStatementSpinner> spinner(new AsyncStatementSpinner()); |
michael@0 | 209 | |
michael@0 | 210 | nsCOMPtr<mozIStoragePendingStatement> pendy; |
michael@0 | 211 | (void)stmt->ExecuteAsync(spinner, getter_AddRefs(pendy)); |
michael@0 | 212 | spinner->SpinUntilCompleted(); |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | /** |
michael@0 | 216 | * Invoke AsyncClose on the given connection, blocking the main thread until we |
michael@0 | 217 | * get the completion notification. |
michael@0 | 218 | */ |
michael@0 | 219 | void |
michael@0 | 220 | blocking_async_close(mozIStorageConnection *db) |
michael@0 | 221 | { |
michael@0 | 222 | nsRefPtr<AsyncStatementSpinner> spinner(new AsyncStatementSpinner()); |
michael@0 | 223 | |
michael@0 | 224 | db->AsyncClose(spinner); |
michael@0 | 225 | spinner->SpinUntilCompleted(); |
michael@0 | 226 | } |