storage/src/mozStorageAsyncStatement.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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 <limits.h>
michael@0 8 #include <stdio.h>
michael@0 9
michael@0 10 #include "nsError.h"
michael@0 11 #include "nsMemory.h"
michael@0 12 #include "nsProxyRelease.h"
michael@0 13 #include "nsThreadUtils.h"
michael@0 14 #include "nsIClassInfoImpl.h"
michael@0 15 #include "nsIProgrammingLanguage.h"
michael@0 16 #include "Variant.h"
michael@0 17
michael@0 18 #include "mozIStorageError.h"
michael@0 19
michael@0 20 #include "mozStorageBindingParams.h"
michael@0 21 #include "mozStorageConnection.h"
michael@0 22 #include "mozStorageAsyncStatementJSHelper.h"
michael@0 23 #include "mozStorageAsyncStatementParams.h"
michael@0 24 #include "mozStoragePrivateHelpers.h"
michael@0 25 #include "mozStorageStatementRow.h"
michael@0 26 #include "mozStorageStatement.h"
michael@0 27 #include "nsDOMClassInfo.h"
michael@0 28
michael@0 29 #include "prlog.h"
michael@0 30
michael@0 31 #ifdef PR_LOGGING
michael@0 32 extern PRLogModuleInfo *gStorageLog;
michael@0 33 #endif
michael@0 34
michael@0 35 namespace mozilla {
michael@0 36 namespace storage {
michael@0 37
michael@0 38 ////////////////////////////////////////////////////////////////////////////////
michael@0 39 //// nsIClassInfo
michael@0 40
michael@0 41 NS_IMPL_CI_INTERFACE_GETTER(AsyncStatement,
michael@0 42 mozIStorageAsyncStatement,
michael@0 43 mozIStorageBaseStatement,
michael@0 44 mozIStorageBindingParams,
michael@0 45 mozilla::storage::StorageBaseStatementInternal)
michael@0 46
michael@0 47 class AsyncStatementClassInfo : public nsIClassInfo
michael@0 48 {
michael@0 49 public:
michael@0 50 MOZ_CONSTEXPR AsyncStatementClassInfo() {}
michael@0 51
michael@0 52 NS_DECL_ISUPPORTS_INHERITED
michael@0 53
michael@0 54 NS_IMETHODIMP
michael@0 55 GetInterfaces(uint32_t *_count, nsIID ***_array)
michael@0 56 {
michael@0 57 return NS_CI_INTERFACE_GETTER_NAME(AsyncStatement)(_count, _array);
michael@0 58 }
michael@0 59
michael@0 60 NS_IMETHODIMP
michael@0 61 GetHelperForLanguage(uint32_t aLanguage, nsISupports **_helper)
michael@0 62 {
michael@0 63 if (aLanguage == nsIProgrammingLanguage::JAVASCRIPT) {
michael@0 64 static AsyncStatementJSHelper sJSHelper;
michael@0 65 *_helper = &sJSHelper;
michael@0 66 return NS_OK;
michael@0 67 }
michael@0 68
michael@0 69 *_helper = nullptr;
michael@0 70 return NS_OK;
michael@0 71 }
michael@0 72
michael@0 73 NS_IMETHODIMP
michael@0 74 GetContractID(char **_contractID)
michael@0 75 {
michael@0 76 *_contractID = nullptr;
michael@0 77 return NS_OK;
michael@0 78 }
michael@0 79
michael@0 80 NS_IMETHODIMP
michael@0 81 GetClassDescription(char **_desc)
michael@0 82 {
michael@0 83 *_desc = nullptr;
michael@0 84 return NS_OK;
michael@0 85 }
michael@0 86
michael@0 87 NS_IMETHODIMP
michael@0 88 GetClassID(nsCID **_id)
michael@0 89 {
michael@0 90 *_id = nullptr;
michael@0 91 return NS_OK;
michael@0 92 }
michael@0 93
michael@0 94 NS_IMETHODIMP
michael@0 95 GetImplementationLanguage(uint32_t *_language)
michael@0 96 {
michael@0 97 *_language = nsIProgrammingLanguage::CPLUSPLUS;
michael@0 98 return NS_OK;
michael@0 99 }
michael@0 100
michael@0 101 NS_IMETHODIMP
michael@0 102 GetFlags(uint32_t *_flags)
michael@0 103 {
michael@0 104 *_flags = 0;
michael@0 105 return NS_OK;
michael@0 106 }
michael@0 107
michael@0 108 NS_IMETHODIMP
michael@0 109 GetClassIDNoAlloc(nsCID *_cid)
michael@0 110 {
michael@0 111 return NS_ERROR_NOT_AVAILABLE;
michael@0 112 }
michael@0 113 };
michael@0 114
michael@0 115 NS_IMETHODIMP_(MozExternalRefCountType) AsyncStatementClassInfo::AddRef() { return 2; }
michael@0 116 NS_IMETHODIMP_(MozExternalRefCountType) AsyncStatementClassInfo::Release() { return 1; }
michael@0 117 NS_IMPL_QUERY_INTERFACE(AsyncStatementClassInfo, nsIClassInfo)
michael@0 118
michael@0 119 static AsyncStatementClassInfo sAsyncStatementClassInfo;
michael@0 120
michael@0 121 ////////////////////////////////////////////////////////////////////////////////
michael@0 122 //// AsyncStatement
michael@0 123
michael@0 124 AsyncStatement::AsyncStatement()
michael@0 125 : StorageBaseStatementInternal()
michael@0 126 , mFinalized(false)
michael@0 127 {
michael@0 128 }
michael@0 129
michael@0 130 nsresult
michael@0 131 AsyncStatement::initialize(Connection *aDBConnection,
michael@0 132 sqlite3 *aNativeConnection,
michael@0 133 const nsACString &aSQLStatement)
michael@0 134 {
michael@0 135 MOZ_ASSERT(aDBConnection, "No database connection given!");
michael@0 136 MOZ_ASSERT(!aDBConnection->isClosed(), "Database connection should be valid");
michael@0 137 MOZ_ASSERT(aNativeConnection, "No native connection given!");
michael@0 138
michael@0 139 mDBConnection = aDBConnection;
michael@0 140 mNativeConnection = aNativeConnection;
michael@0 141 mSQLString = aSQLStatement;
michael@0 142
michael@0 143 PR_LOG(gStorageLog, PR_LOG_NOTICE, ("Inited async statement '%s' (0x%p)",
michael@0 144 mSQLString.get()));
michael@0 145
michael@0 146 #ifdef DEBUG
michael@0 147 // We want to try and test for LIKE and that consumers are using
michael@0 148 // escapeStringForLIKE instead of just trusting user input. The idea to
michael@0 149 // check to see if they are binding a parameter after like instead of just
michael@0 150 // using a string. We only do this in debug builds because it's expensive!
michael@0 151 const nsCaseInsensitiveCStringComparator c;
michael@0 152 nsACString::const_iterator start, end, e;
michael@0 153 aSQLStatement.BeginReading(start);
michael@0 154 aSQLStatement.EndReading(end);
michael@0 155 e = end;
michael@0 156 while (::FindInReadable(NS_LITERAL_CSTRING(" LIKE"), start, e, c)) {
michael@0 157 // We have a LIKE in here, so we perform our tests
michael@0 158 // FindInReadable moves the iterator, so we have to get a new one for
michael@0 159 // each test we perform.
michael@0 160 nsACString::const_iterator s1, s2, s3;
michael@0 161 s1 = s2 = s3 = start;
michael@0 162
michael@0 163 if (!(::FindInReadable(NS_LITERAL_CSTRING(" LIKE ?"), s1, end, c) ||
michael@0 164 ::FindInReadable(NS_LITERAL_CSTRING(" LIKE :"), s2, end, c) ||
michael@0 165 ::FindInReadable(NS_LITERAL_CSTRING(" LIKE @"), s3, end, c))) {
michael@0 166 // At this point, we didn't find a LIKE statement followed by ?, :,
michael@0 167 // or @, all of which are valid characters for binding a parameter.
michael@0 168 // We will warn the consumer that they may not be safely using LIKE.
michael@0 169 NS_WARNING("Unsafe use of LIKE detected! Please ensure that you "
michael@0 170 "are using mozIStorageAsyncStatement::escapeStringForLIKE "
michael@0 171 "and that you are binding that result to the statement "
michael@0 172 "to prevent SQL injection attacks.");
michael@0 173 }
michael@0 174
michael@0 175 // resetting start and e
michael@0 176 start = e;
michael@0 177 e = end;
michael@0 178 }
michael@0 179 #endif
michael@0 180
michael@0 181 return NS_OK;
michael@0 182 }
michael@0 183
michael@0 184 mozIStorageBindingParams *
michael@0 185 AsyncStatement::getParams()
michael@0 186 {
michael@0 187 nsresult rv;
michael@0 188
michael@0 189 // If we do not have an array object yet, make it.
michael@0 190 if (!mParamsArray) {
michael@0 191 nsCOMPtr<mozIStorageBindingParamsArray> array;
michael@0 192 rv = NewBindingParamsArray(getter_AddRefs(array));
michael@0 193 NS_ENSURE_SUCCESS(rv, nullptr);
michael@0 194
michael@0 195 mParamsArray = static_cast<BindingParamsArray *>(array.get());
michael@0 196 }
michael@0 197
michael@0 198 // If there isn't already any rows added, we'll have to add one to use.
michael@0 199 if (mParamsArray->length() == 0) {
michael@0 200 nsRefPtr<AsyncBindingParams> params(new AsyncBindingParams(mParamsArray));
michael@0 201 NS_ENSURE_TRUE(params, nullptr);
michael@0 202
michael@0 203 rv = mParamsArray->AddParams(params);
michael@0 204 NS_ENSURE_SUCCESS(rv, nullptr);
michael@0 205
michael@0 206 // We have to unlock our params because AddParams locks them. This is safe
michael@0 207 // because no reference to the params object was, or ever will be given out.
michael@0 208 params->unlock(nullptr);
michael@0 209
michael@0 210 // We also want to lock our array at this point - we don't want anything to
michael@0 211 // be added to it.
michael@0 212 mParamsArray->lock();
michael@0 213 }
michael@0 214
michael@0 215 return *mParamsArray->begin();
michael@0 216 }
michael@0 217
michael@0 218 /**
michael@0 219 * If we are here then we know there are no pending async executions relying on
michael@0 220 * us (StatementData holds a reference to us; this also goes for our own
michael@0 221 * AsyncStatementFinalizer which proxies its release to the calling thread) and
michael@0 222 * so it is always safe to destroy our sqlite3_stmt if one exists. We can be
michael@0 223 * destroyed on the caller thread by garbage-collection/reference counting or on
michael@0 224 * the async thread by the last execution of a statement that already lost its
michael@0 225 * main-thread refs.
michael@0 226 */
michael@0 227 AsyncStatement::~AsyncStatement()
michael@0 228 {
michael@0 229 destructorAsyncFinalize();
michael@0 230 cleanupJSHelpers();
michael@0 231
michael@0 232 // If we are getting destroyed on the wrong thread, proxy the connection
michael@0 233 // release to the right thread. I'm not sure why we do this.
michael@0 234 bool onCallingThread = false;
michael@0 235 (void)mDBConnection->threadOpenedOn->IsOnCurrentThread(&onCallingThread);
michael@0 236 if (!onCallingThread) {
michael@0 237 // NS_ProxyRelase only magic forgets for us if mDBConnection is an
michael@0 238 // nsCOMPtr. Which it is not; it's an nsRefPtr.
michael@0 239 Connection *forgottenConn = nullptr;
michael@0 240 mDBConnection.swap(forgottenConn);
michael@0 241 (void)::NS_ProxyRelease(forgottenConn->threadOpenedOn,
michael@0 242 static_cast<mozIStorageConnection *>(forgottenConn));
michael@0 243 }
michael@0 244 }
michael@0 245
michael@0 246 void
michael@0 247 AsyncStatement::cleanupJSHelpers()
michael@0 248 {
michael@0 249 // We are considered dead at this point, so any wrappers for row or params
michael@0 250 // need to lose their reference to us.
michael@0 251 if (mStatementParamsHolder) {
michael@0 252 nsCOMPtr<nsIXPConnectWrappedNative> wrapper =
michael@0 253 do_QueryInterface(mStatementParamsHolder);
michael@0 254 nsCOMPtr<mozIStorageStatementParams> iParams =
michael@0 255 do_QueryWrappedNative(wrapper);
michael@0 256 AsyncStatementParams *params =
michael@0 257 static_cast<AsyncStatementParams *>(iParams.get());
michael@0 258 params->mStatement = nullptr;
michael@0 259 mStatementParamsHolder = nullptr;
michael@0 260 }
michael@0 261 }
michael@0 262
michael@0 263 ////////////////////////////////////////////////////////////////////////////////
michael@0 264 //// nsISupports
michael@0 265
michael@0 266 NS_IMPL_ADDREF(AsyncStatement)
michael@0 267 NS_IMPL_RELEASE(AsyncStatement)
michael@0 268
michael@0 269 NS_INTERFACE_MAP_BEGIN(AsyncStatement)
michael@0 270 NS_INTERFACE_MAP_ENTRY(mozIStorageAsyncStatement)
michael@0 271 NS_INTERFACE_MAP_ENTRY(mozIStorageBaseStatement)
michael@0 272 NS_INTERFACE_MAP_ENTRY(mozIStorageBindingParams)
michael@0 273 NS_INTERFACE_MAP_ENTRY(mozilla::storage::StorageBaseStatementInternal)
michael@0 274 if (aIID.Equals(NS_GET_IID(nsIClassInfo))) {
michael@0 275 foundInterface = static_cast<nsIClassInfo *>(&sAsyncStatementClassInfo);
michael@0 276 }
michael@0 277 else
michael@0 278 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozIStorageAsyncStatement)
michael@0 279 NS_INTERFACE_MAP_END
michael@0 280
michael@0 281
michael@0 282 ////////////////////////////////////////////////////////////////////////////////
michael@0 283 //// StorageBaseStatementInternal
michael@0 284
michael@0 285 Connection *
michael@0 286 AsyncStatement::getOwner()
michael@0 287 {
michael@0 288 return mDBConnection;
michael@0 289 }
michael@0 290
michael@0 291 int
michael@0 292 AsyncStatement::getAsyncStatement(sqlite3_stmt **_stmt)
michael@0 293 {
michael@0 294 #ifdef DEBUG
michael@0 295 // Make sure we are never called on the connection's owning thread.
michael@0 296 bool onOpenedThread = false;
michael@0 297 (void)mDBConnection->threadOpenedOn->IsOnCurrentThread(&onOpenedThread);
michael@0 298 NS_ASSERTION(!onOpenedThread,
michael@0 299 "We should only be called on the async thread!");
michael@0 300 #endif
michael@0 301
michael@0 302 if (!mAsyncStatement) {
michael@0 303 int rc = mDBConnection->prepareStatement(mNativeConnection, mSQLString,
michael@0 304 &mAsyncStatement);
michael@0 305 if (rc != SQLITE_OK) {
michael@0 306 PR_LOG(gStorageLog, PR_LOG_ERROR,
michael@0 307 ("Sqlite statement prepare error: %d '%s'", rc,
michael@0 308 ::sqlite3_errmsg(mNativeConnection)));
michael@0 309 PR_LOG(gStorageLog, PR_LOG_ERROR,
michael@0 310 ("Statement was: '%s'", mSQLString.get()));
michael@0 311 *_stmt = nullptr;
michael@0 312 return rc;
michael@0 313 }
michael@0 314 PR_LOG(gStorageLog, PR_LOG_NOTICE, ("Initialized statement '%s' (0x%p)",
michael@0 315 mSQLString.get(),
michael@0 316 mAsyncStatement));
michael@0 317 }
michael@0 318
michael@0 319 *_stmt = mAsyncStatement;
michael@0 320 return SQLITE_OK;
michael@0 321 }
michael@0 322
michael@0 323 nsresult
michael@0 324 AsyncStatement::getAsynchronousStatementData(StatementData &_data)
michael@0 325 {
michael@0 326 if (mFinalized)
michael@0 327 return NS_ERROR_UNEXPECTED;
michael@0 328
michael@0 329 // Pass null for the sqlite3_stmt; it will be requested on demand from the
michael@0 330 // async thread.
michael@0 331 _data = StatementData(nullptr, bindingParamsArray(), this);
michael@0 332
michael@0 333 return NS_OK;
michael@0 334 }
michael@0 335
michael@0 336 already_AddRefed<mozIStorageBindingParams>
michael@0 337 AsyncStatement::newBindingParams(mozIStorageBindingParamsArray *aOwner)
michael@0 338 {
michael@0 339 if (mFinalized)
michael@0 340 return nullptr;
michael@0 341
michael@0 342 nsCOMPtr<mozIStorageBindingParams> params(new AsyncBindingParams(aOwner));
michael@0 343 return params.forget();
michael@0 344 }
michael@0 345
michael@0 346
michael@0 347 ////////////////////////////////////////////////////////////////////////////////
michael@0 348 //// mozIStorageAsyncStatement
michael@0 349
michael@0 350 // (nothing is specific to mozIStorageAsyncStatement)
michael@0 351
michael@0 352 ////////////////////////////////////////////////////////////////////////////////
michael@0 353 //// StorageBaseStatementInternal
michael@0 354
michael@0 355 // proxy to StorageBaseStatementInternal using its define helper.
michael@0 356 MIXIN_IMPL_STORAGEBASESTATEMENTINTERNAL(
michael@0 357 AsyncStatement,
michael@0 358 if (mFinalized) return NS_ERROR_UNEXPECTED;)
michael@0 359
michael@0 360 NS_IMETHODIMP
michael@0 361 AsyncStatement::Finalize()
michael@0 362 {
michael@0 363 if (mFinalized)
michael@0 364 return NS_OK;
michael@0 365
michael@0 366 mFinalized = true;
michael@0 367
michael@0 368 PR_LOG(gStorageLog, PR_LOG_NOTICE, ("Finalizing statement '%s'",
michael@0 369 mSQLString.get()));
michael@0 370
michael@0 371 asyncFinalize();
michael@0 372 cleanupJSHelpers();
michael@0 373
michael@0 374 return NS_OK;
michael@0 375 }
michael@0 376
michael@0 377 NS_IMETHODIMP
michael@0 378 AsyncStatement::BindParameters(mozIStorageBindingParamsArray *aParameters)
michael@0 379 {
michael@0 380 if (mFinalized)
michael@0 381 return NS_ERROR_UNEXPECTED;
michael@0 382
michael@0 383 BindingParamsArray *array = static_cast<BindingParamsArray *>(aParameters);
michael@0 384 if (array->getOwner() != this)
michael@0 385 return NS_ERROR_UNEXPECTED;
michael@0 386
michael@0 387 if (array->length() == 0)
michael@0 388 return NS_ERROR_UNEXPECTED;
michael@0 389
michael@0 390 mParamsArray = array;
michael@0 391 mParamsArray->lock();
michael@0 392
michael@0 393 return NS_OK;
michael@0 394 }
michael@0 395
michael@0 396 NS_IMETHODIMP
michael@0 397 AsyncStatement::GetState(int32_t *_state)
michael@0 398 {
michael@0 399 if (mFinalized)
michael@0 400 *_state = MOZ_STORAGE_STATEMENT_INVALID;
michael@0 401 else
michael@0 402 *_state = MOZ_STORAGE_STATEMENT_READY;
michael@0 403
michael@0 404 return NS_OK;
michael@0 405 }
michael@0 406
michael@0 407 ////////////////////////////////////////////////////////////////////////////////
michael@0 408 //// mozIStorageBindingParams
michael@0 409
michael@0 410 BOILERPLATE_BIND_PROXIES(
michael@0 411 AsyncStatement,
michael@0 412 if (mFinalized) return NS_ERROR_UNEXPECTED;
michael@0 413 )
michael@0 414
michael@0 415 } // namespace storage
michael@0 416 } // namespace mozilla

mercurial