storage/src/mozStorageAsyncStatementJSHelper.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

     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 #include "nsIXPConnect.h"
     8 #include "mozStorageAsyncStatement.h"
     9 #include "mozStorageService.h"
    11 #include "nsMemory.h"
    12 #include "nsString.h"
    13 #include "nsServiceManagerUtils.h"
    15 #include "mozStorageAsyncStatementJSHelper.h"
    17 #include "mozStorageAsyncStatementParams.h"
    19 #include "jsapi.h"
    21 namespace mozilla {
    22 namespace storage {
    24 ////////////////////////////////////////////////////////////////////////////////
    25 //// AsyncStatementJSHelper
    27 nsresult
    28 AsyncStatementJSHelper::getParams(AsyncStatement *aStatement,
    29                                   JSContext *aCtx,
    30                                   JSObject *aScopeObj,
    31                                   jsval *_params)
    32 {
    33   nsresult rv;
    35 #ifdef DEBUG
    36   int32_t state;
    37   (void)aStatement->GetState(&state);
    38   NS_ASSERTION(state == mozIStorageAsyncStatement::MOZ_STORAGE_STATEMENT_READY,
    39                "Invalid state to get the params object - all calls will fail!");
    40 #endif
    42   if (!aStatement->mStatementParamsHolder) {
    43     nsCOMPtr<mozIStorageStatementParams> params =
    44       new AsyncStatementParams(aStatement);
    45     NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY);
    47     JS::RootedObject scope(aCtx, aScopeObj);
    48     nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect());
    49     rv = xpc->WrapNative(
    50       aCtx,
    51       ::JS_GetGlobalForObject(aCtx, scope),
    52       params,
    53       NS_GET_IID(mozIStorageStatementParams),
    54       getter_AddRefs(aStatement->mStatementParamsHolder)
    55     );
    56     NS_ENSURE_SUCCESS(rv, rv);
    57   }
    59   JS::Rooted<JSObject*> obj(aCtx);
    60   obj = aStatement->mStatementParamsHolder->GetJSObject();
    61   NS_ENSURE_STATE(obj);
    63   *_params = OBJECT_TO_JSVAL(obj);
    64   return NS_OK;
    65 }
    67 NS_IMETHODIMP_(MozExternalRefCountType) AsyncStatementJSHelper::AddRef() { return 2; }
    68 NS_IMETHODIMP_(MozExternalRefCountType) AsyncStatementJSHelper::Release() { return 1; }
    69 NS_INTERFACE_MAP_BEGIN(AsyncStatementJSHelper)
    70   NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable)
    71   NS_INTERFACE_MAP_ENTRY(nsISupports)
    72 NS_INTERFACE_MAP_END
    74 ////////////////////////////////////////////////////////////////////////////////
    75 //// nsIXPCScriptable
    77 #define XPC_MAP_CLASSNAME AsyncStatementJSHelper
    78 #define XPC_MAP_QUOTED_CLASSNAME "AsyncStatementJSHelper"
    79 #define XPC_MAP_WANT_GETPROPERTY
    80 #define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE
    81 #include "xpc_map_end.h"
    83 NS_IMETHODIMP
    84 AsyncStatementJSHelper::GetProperty(nsIXPConnectWrappedNative *aWrapper,
    85                                     JSContext *aCtx,
    86                                     JSObject *aScopeObj,
    87                                     jsid aId,
    88                                     jsval *_result,
    89                                     bool *_retval)
    90 {
    91   if (!JSID_IS_STRING(aId))
    92     return NS_OK;
    94   // Cast to async via mozI* since direct from nsISupports is ambiguous.
    95   JS::RootedObject scope(aCtx, aScopeObj);
    96   JS::RootedId id(aCtx, aId);
    97   mozIStorageAsyncStatement *iAsyncStmt =
    98     static_cast<mozIStorageAsyncStatement *>(aWrapper->Native());
    99   AsyncStatement *stmt = static_cast<AsyncStatement *>(iAsyncStmt);
   101 #ifdef DEBUG
   102   {
   103     nsISupports *supp = aWrapper->Native();
   104     nsCOMPtr<mozIStorageAsyncStatement> isStatement(do_QueryInterface(supp));
   105     NS_ASSERTION(isStatement, "How is this not an async statement?!");
   106   }
   107 #endif
   109   if (::JS_FlatStringEqualsAscii(JSID_TO_FLAT_STRING(id), "params"))
   110     return getParams(stmt, aCtx, scope, _result);
   112   return NS_OK;
   113 }
   115 } // namespace storage
   116 } // namespace mozilla

mercurial