tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

mozStorageAsyncStatementJSHelper.cpp (4681B)


      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/. */
      6 
      7 #include "nsIXPConnect.h"
      8 #include "mozStorageAsyncStatement.h"
      9 #include "mozStorageService.h"
     10 
     11 #include "nsString.h"
     12 #include "nsServiceManagerUtils.h"
     13 
     14 #include "mozStorageAsyncStatementJSHelper.h"
     15 
     16 #include "mozStorageAsyncStatementParams.h"
     17 
     18 #include "jsapi.h"
     19 #include "js/PropertyAndElement.h"  // JS_DefineProperty, JS_DefinePropertyById
     20 
     21 #include "xpc_make_class.h"
     22 
     23 namespace mozilla {
     24 namespace storage {
     25 
     26 ////////////////////////////////////////////////////////////////////////////////
     27 //// AsyncStatementJSHelper
     28 
     29 nsresult AsyncStatementJSHelper::getParams(AsyncStatement* aStatement,
     30                                           JSContext* aCtx, JSObject* aScopeObj,
     31                                           JS::Value* _params) {
     32  MOZ_ASSERT(NS_IsMainThread());
     33 
     34 #ifdef DEBUG
     35  int32_t state;
     36  (void)aStatement->GetState(&state);
     37  NS_ASSERTION(state == mozIStorageAsyncStatement::MOZ_STORAGE_STATEMENT_READY,
     38               "Invalid state to get the params object - all calls will fail!");
     39 #endif
     40 
     41  JS::Rooted<JSObject*> scope(aCtx, aScopeObj);
     42 
     43  if (!aStatement->mStatementParamsHolder) {
     44    dom::GlobalObject global(aCtx, scope);
     45    if (global.Failed()) {
     46      return NS_ERROR_UNEXPECTED;
     47    }
     48 
     49    nsCOMPtr<nsPIDOMWindowInner> window =
     50        do_QueryInterface(global.GetAsSupports());
     51 
     52    RefPtr<AsyncStatementParams> params(
     53        new AsyncStatementParams(window, aStatement));
     54    NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY);
     55 
     56    RefPtr<AsyncStatementParamsHolder> paramsHolder =
     57        new AsyncStatementParamsHolder(params);
     58    NS_ENSURE_TRUE(paramsHolder, NS_ERROR_OUT_OF_MEMORY);
     59 
     60    aStatement->mStatementParamsHolder =
     61        new nsMainThreadPtrHolder<AsyncStatementParamsHolder>(
     62            "Statement::mStatementParamsHolder", paramsHolder);
     63  }
     64 
     65  RefPtr<AsyncStatementParams> params(
     66      aStatement->mStatementParamsHolder->Get());
     67  JSObject* obj = params->WrapObject(aCtx, nullptr);
     68  if (!obj) {
     69    return NS_ERROR_UNEXPECTED;
     70  }
     71 
     72  _params->setObject(*obj);
     73  return NS_OK;
     74 }
     75 
     76 NS_IMETHODIMP_(MozExternalRefCountType) AsyncStatementJSHelper::AddRef() {
     77  return 2;
     78 }
     79 NS_IMETHODIMP_(MozExternalRefCountType) AsyncStatementJSHelper::Release() {
     80  return 1;
     81 }
     82 NS_INTERFACE_MAP_BEGIN(AsyncStatementJSHelper)
     83  NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable)
     84  NS_INTERFACE_MAP_ENTRY(nsISupports)
     85 NS_INTERFACE_MAP_END
     86 
     87 ////////////////////////////////////////////////////////////////////////////////
     88 //// nsIXPCScriptable
     89 
     90 #define XPC_MAP_CLASSNAME AsyncStatementJSHelper
     91 #define XPC_MAP_QUOTED_CLASSNAME "AsyncStatementJSHelper"
     92 #define XPC_MAP_FLAGS \
     93  (XPC_SCRIPTABLE_WANT_RESOLVE | XPC_SCRIPTABLE_ALLOW_PROP_MODS_DURING_RESOLVE)
     94 #include "xpc_map_end.h"
     95 
     96 NS_IMETHODIMP
     97 AsyncStatementJSHelper::Resolve(nsIXPConnectWrappedNative* aWrapper,
     98                                JSContext* aCtx, JSObject* aScopeObj, jsid aId,
     99                                bool* resolvedp, bool* _retval) {
    100  if (!aId.isString()) return NS_OK;
    101 
    102  // Cast to async via mozI* since direct from nsISupports is ambiguous.
    103  JS::Rooted<JSObject*> scope(aCtx, aScopeObj);
    104  JS::Rooted<JS::PropertyKey> id(aCtx, aId);
    105  mozIStorageAsyncStatement* iAsyncStmt =
    106      static_cast<mozIStorageAsyncStatement*>(aWrapper->Native());
    107  AsyncStatement* stmt = static_cast<AsyncStatement*>(iAsyncStmt);
    108 
    109 #ifdef DEBUG
    110  {
    111    nsISupports* supp = aWrapper->Native();
    112    nsCOMPtr<mozIStorageAsyncStatement> isStatement(do_QueryInterface(supp));
    113    NS_ASSERTION(isStatement, "How is this not an async statement?!");
    114  }
    115 #endif
    116 
    117  if (::JS_LinearStringEqualsLiteral(id.toLinearString(), "params")) {
    118    JS::Rooted<JS::Value> val(aCtx);
    119    nsresult rv = getParams(stmt, aCtx, scope, val.address());
    120    NS_ENSURE_SUCCESS(rv, rv);
    121    *_retval = ::JS_DefinePropertyById(aCtx, scope, id, val, JSPROP_RESOLVING);
    122    *resolvedp = true;
    123    return NS_OK;
    124  }
    125 
    126  return NS_OK;
    127 }
    128 
    129 ////////////////////////////////////////////////////////////////////////////////
    130 //// AsyncStatementParamsHolder
    131 
    132 NS_IMPL_ISUPPORTS0(AsyncStatementParamsHolder);
    133 
    134 AsyncStatementParamsHolder::~AsyncStatementParamsHolder() {
    135  MOZ_ASSERT(NS_IsMainThread());
    136  // We are considered dead at this point, so any wrappers for row or params
    137  // need to lose their reference to the statement.
    138  mParams->mStatement = nullptr;
    139 }
    140 
    141 }  // namespace storage
    142 }  // namespace mozilla