storage/src/mozStorageAsyncStatementParams.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/storage/src/mozStorageAsyncStatementParams.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,126 @@
     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 "nsMemory.h"
    1.11 +#include "nsString.h"
    1.12 +#include "nsCOMPtr.h"
    1.13 +
    1.14 +#include "jsapi.h"
    1.15 +
    1.16 +#include "mozStoragePrivateHelpers.h"
    1.17 +#include "mozStorageAsyncStatement.h"
    1.18 +#include "mozStorageAsyncStatementParams.h"
    1.19 +#include "mozIStorageStatement.h"
    1.20 +
    1.21 +namespace mozilla {
    1.22 +namespace storage {
    1.23 +
    1.24 +////////////////////////////////////////////////////////////////////////////////
    1.25 +//// AsyncStatementParams
    1.26 +
    1.27 +AsyncStatementParams::AsyncStatementParams(AsyncStatement *aStatement)
    1.28 +: mStatement(aStatement)
    1.29 +{
    1.30 +  NS_ASSERTION(mStatement != nullptr, "mStatement is null");
    1.31 +}
    1.32 +
    1.33 +NS_IMPL_ISUPPORTS(
    1.34 +  AsyncStatementParams
    1.35 +, mozIStorageStatementParams
    1.36 +, nsIXPCScriptable
    1.37 +)
    1.38 +
    1.39 +////////////////////////////////////////////////////////////////////////////////
    1.40 +//// nsIXPCScriptable
    1.41 +
    1.42 +#define XPC_MAP_CLASSNAME AsyncStatementParams
    1.43 +#define XPC_MAP_QUOTED_CLASSNAME "AsyncStatementParams"
    1.44 +#define XPC_MAP_WANT_SETPROPERTY
    1.45 +#define XPC_MAP_WANT_NEWRESOLVE
    1.46 +#define XPC_MAP_FLAGS nsIXPCScriptable::ALLOW_PROP_MODS_DURING_RESOLVE
    1.47 +#include "xpc_map_end.h"
    1.48 +
    1.49 +NS_IMETHODIMP
    1.50 +AsyncStatementParams::SetProperty(
    1.51 +  nsIXPConnectWrappedNative *aWrapper,
    1.52 +  JSContext *aCtx,
    1.53 +  JSObject *aScopeObj,
    1.54 +  jsid aId,
    1.55 +  JS::Value *_vp,
    1.56 +  bool *_retval
    1.57 +)
    1.58 +{
    1.59 +  NS_ENSURE_TRUE(mStatement, NS_ERROR_NOT_INITIALIZED);
    1.60 +
    1.61 +  if (JSID_IS_INT(aId)) {
    1.62 +    int idx = JSID_TO_INT(aId);
    1.63 +
    1.64 +    nsCOMPtr<nsIVariant> variant(convertJSValToVariant(aCtx, *_vp));
    1.65 +    NS_ENSURE_TRUE(variant, NS_ERROR_UNEXPECTED);
    1.66 +    nsresult rv = mStatement->BindByIndex(idx, variant);
    1.67 +    NS_ENSURE_SUCCESS(rv, rv);
    1.68 +  }
    1.69 +  else if (JSID_IS_STRING(aId)) {
    1.70 +    JSString *str = JSID_TO_STRING(aId);
    1.71 +    size_t length;
    1.72 +    const jschar *chars = JS_GetInternedStringCharsAndLength(str, &length);
    1.73 +    NS_ConvertUTF16toUTF8 name(chars, length);
    1.74 +
    1.75 +    nsCOMPtr<nsIVariant> variant(convertJSValToVariant(aCtx, *_vp));
    1.76 +    NS_ENSURE_TRUE(variant, NS_ERROR_UNEXPECTED);
    1.77 +    nsresult rv = mStatement->BindByName(name, variant);
    1.78 +    NS_ENSURE_SUCCESS(rv, rv);
    1.79 +  }
    1.80 +  else {
    1.81 +    return NS_ERROR_INVALID_ARG;
    1.82 +  }
    1.83 +
    1.84 +  *_retval = true;
    1.85 +  return NS_OK;
    1.86 +}
    1.87 +
    1.88 +NS_IMETHODIMP
    1.89 +AsyncStatementParams::NewResolve(
    1.90 +  nsIXPConnectWrappedNative *aWrapper,
    1.91 +  JSContext *aCtx,
    1.92 +  JSObject *aScopeObj,
    1.93 +  jsid aId,
    1.94 +  JSObject **_objp,
    1.95 +  bool *_retval
    1.96 +)
    1.97 +{
    1.98 +  JS::Rooted<JSObject*> scopeObj(aCtx, aScopeObj);
    1.99 +
   1.100 +  NS_ENSURE_TRUE(mStatement, NS_ERROR_NOT_INITIALIZED);
   1.101 +  // We do not throw at any point after this because we want to allow the
   1.102 +  // prototype chain to be checked for the property.
   1.103 +
   1.104 +  bool resolved = false;
   1.105 +  bool ok = true;
   1.106 +  if (JSID_IS_INT(aId)) {
   1.107 +    uint32_t idx = JSID_TO_INT(aId);
   1.108 +    // All indexes are good because we don't know how many parameters there
   1.109 +    // really are.
   1.110 +    ok = ::JS_DefineElement(aCtx, scopeObj, idx, JSVAL_VOID, nullptr,
   1.111 +                            nullptr, 0);
   1.112 +    resolved = true;
   1.113 +  }
   1.114 +  else if (JSID_IS_STRING(aId)) {
   1.115 +    // We are unable to tell if there's a parameter with this name and so
   1.116 +    // we must assume that there is.  This screws the rest of the prototype
   1.117 +    // chain, but people really shouldn't be depending on this anyways.
   1.118 +    ok = ::JS_DefinePropertyById(aCtx, scopeObj, aId, JSVAL_VOID, nullptr,
   1.119 +                                 nullptr, 0);
   1.120 +    resolved = true;
   1.121 +  }
   1.122 +
   1.123 +  *_retval = ok;
   1.124 +  *_objp = resolved && ok ? scopeObj.get() : nullptr;
   1.125 +  return NS_OK;
   1.126 +}
   1.127 +
   1.128 +} // namespace storage
   1.129 +} // namespace mozilla

mercurial