storage/src/mozStorageAsyncStatementJSHelper.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/storage/src/mozStorageAsyncStatementJSHelper.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,116 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "nsIXPConnect.h"
    1.11 +#include "mozStorageAsyncStatement.h"
    1.12 +#include "mozStorageService.h"
    1.13 +
    1.14 +#include "nsMemory.h"
    1.15 +#include "nsString.h"
    1.16 +#include "nsServiceManagerUtils.h"
    1.17 +
    1.18 +#include "mozStorageAsyncStatementJSHelper.h"
    1.19 +
    1.20 +#include "mozStorageAsyncStatementParams.h"
    1.21 +
    1.22 +#include "jsapi.h"
    1.23 +
    1.24 +namespace mozilla {
    1.25 +namespace storage {
    1.26 +
    1.27 +////////////////////////////////////////////////////////////////////////////////
    1.28 +//// AsyncStatementJSHelper
    1.29 +
    1.30 +nsresult
    1.31 +AsyncStatementJSHelper::getParams(AsyncStatement *aStatement,
    1.32 +                                  JSContext *aCtx,
    1.33 +                                  JSObject *aScopeObj,
    1.34 +                                  jsval *_params)
    1.35 +{
    1.36 +  nsresult rv;
    1.37 +
    1.38 +#ifdef DEBUG
    1.39 +  int32_t state;
    1.40 +  (void)aStatement->GetState(&state);
    1.41 +  NS_ASSERTION(state == mozIStorageAsyncStatement::MOZ_STORAGE_STATEMENT_READY,
    1.42 +               "Invalid state to get the params object - all calls will fail!");
    1.43 +#endif
    1.44 +
    1.45 +  if (!aStatement->mStatementParamsHolder) {
    1.46 +    nsCOMPtr<mozIStorageStatementParams> params =
    1.47 +      new AsyncStatementParams(aStatement);
    1.48 +    NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY);
    1.49 +
    1.50 +    JS::RootedObject scope(aCtx, aScopeObj);
    1.51 +    nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect());
    1.52 +    rv = xpc->WrapNative(
    1.53 +      aCtx,
    1.54 +      ::JS_GetGlobalForObject(aCtx, scope),
    1.55 +      params,
    1.56 +      NS_GET_IID(mozIStorageStatementParams),
    1.57 +      getter_AddRefs(aStatement->mStatementParamsHolder)
    1.58 +    );
    1.59 +    NS_ENSURE_SUCCESS(rv, rv);
    1.60 +  }
    1.61 +
    1.62 +  JS::Rooted<JSObject*> obj(aCtx);
    1.63 +  obj = aStatement->mStatementParamsHolder->GetJSObject();
    1.64 +  NS_ENSURE_STATE(obj);
    1.65 +
    1.66 +  *_params = OBJECT_TO_JSVAL(obj);
    1.67 +  return NS_OK;
    1.68 +}
    1.69 +
    1.70 +NS_IMETHODIMP_(MozExternalRefCountType) AsyncStatementJSHelper::AddRef() { return 2; }
    1.71 +NS_IMETHODIMP_(MozExternalRefCountType) AsyncStatementJSHelper::Release() { return 1; }
    1.72 +NS_INTERFACE_MAP_BEGIN(AsyncStatementJSHelper)
    1.73 +  NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable)
    1.74 +  NS_INTERFACE_MAP_ENTRY(nsISupports)
    1.75 +NS_INTERFACE_MAP_END
    1.76 +
    1.77 +////////////////////////////////////////////////////////////////////////////////
    1.78 +//// nsIXPCScriptable
    1.79 +
    1.80 +#define XPC_MAP_CLASSNAME AsyncStatementJSHelper
    1.81 +#define XPC_MAP_QUOTED_CLASSNAME "AsyncStatementJSHelper"
    1.82 +#define XPC_MAP_WANT_GETPROPERTY
    1.83 +#define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE
    1.84 +#include "xpc_map_end.h"
    1.85 +
    1.86 +NS_IMETHODIMP
    1.87 +AsyncStatementJSHelper::GetProperty(nsIXPConnectWrappedNative *aWrapper,
    1.88 +                                    JSContext *aCtx,
    1.89 +                                    JSObject *aScopeObj,
    1.90 +                                    jsid aId,
    1.91 +                                    jsval *_result,
    1.92 +                                    bool *_retval)
    1.93 +{
    1.94 +  if (!JSID_IS_STRING(aId))
    1.95 +    return NS_OK;
    1.96 +
    1.97 +  // Cast to async via mozI* since direct from nsISupports is ambiguous.
    1.98 +  JS::RootedObject scope(aCtx, aScopeObj);
    1.99 +  JS::RootedId id(aCtx, aId);
   1.100 +  mozIStorageAsyncStatement *iAsyncStmt =
   1.101 +    static_cast<mozIStorageAsyncStatement *>(aWrapper->Native());
   1.102 +  AsyncStatement *stmt = static_cast<AsyncStatement *>(iAsyncStmt);
   1.103 +
   1.104 +#ifdef DEBUG
   1.105 +  {
   1.106 +    nsISupports *supp = aWrapper->Native();
   1.107 +    nsCOMPtr<mozIStorageAsyncStatement> isStatement(do_QueryInterface(supp));
   1.108 +    NS_ASSERTION(isStatement, "How is this not an async statement?!");
   1.109 +  }
   1.110 +#endif
   1.111 +
   1.112 +  if (::JS_FlatStringEqualsAscii(JSID_TO_FLAT_STRING(id), "params"))
   1.113 +    return getParams(stmt, aCtx, scope, _result);
   1.114 +
   1.115 +  return NS_OK;
   1.116 +}
   1.117 +
   1.118 +} // namespace storage
   1.119 +} // namespace mozilla

mercurial