gecko-dev/storage/mozStorageStatementJSHelper...

288 строки
8.4 KiB
C++
Исходник Обычный вид История

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
2012-05-21 15:12:37 +04:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsIXPConnect.h"
#include "mozStorageStatement.h"
#include "mozStorageService.h"
#include "nsMemory.h"
#include "nsString.h"
#include "nsServiceManagerUtils.h"
#include "mozStorageStatementJSHelper.h"
#include "mozStorageStatementRow.h"
#include "mozStorageStatementParams.h"
#include "jsapi.h"
#include "xpc_make_class.h"
namespace mozilla {
namespace storage {
////////////////////////////////////////////////////////////////////////////////
//// Global Functions
static
bool
stepFunc(JSContext *aCtx,
uint32_t,
JS::Value *_vp)
{
nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect());
nsCOMPtr<nsIXPConnectWrappedNative> wrapper;
JSObject *obj = JS_THIS_OBJECT(aCtx, _vp);
if (!obj) {
return false;
}
nsresult rv =
xpc->GetWrappedNativeOfJSObject(aCtx, obj, getter_AddRefs(wrapper));
if (NS_FAILED(rv)) {
::JS_ReportErrorASCII(aCtx, "mozIStorageStatement::step() could not obtain native statement");
return false;
}
#ifdef DEBUG
{
nsCOMPtr<mozIStorageStatement> isStatement(
do_QueryInterface(wrapper->Native())
);
NS_ASSERTION(isStatement, "How is this not a statement?!");
}
#endif
Statement *stmt = static_cast<Statement *>(
static_cast<mozIStorageStatement *>(wrapper->Native())
);
bool hasMore = false;
rv = stmt->ExecuteStep(&hasMore);
if (NS_SUCCEEDED(rv) && !hasMore) {
_vp->setBoolean(false);
(void)stmt->Reset();
return true;
}
if (NS_FAILED(rv)) {
::JS_ReportErrorASCII(aCtx, "mozIStorageStatement::step() returned an error");
return false;
}
_vp->setBoolean(hasMore);
return true;
}
////////////////////////////////////////////////////////////////////////////////
//// StatementJSHelper
nsresult
StatementJSHelper::getRow(Statement *aStatement,
JSContext *aCtx,
JSObject *aScopeObj,
JS::Value *_row)
{
MOZ_ASSERT(NS_IsMainThread());
nsresult rv;
#ifdef DEBUG
int32_t state;
(void)aStatement->GetState(&state);
NS_ASSERTION(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_EXECUTING,
"Invalid state to get the row object - all calls will fail!");
#endif
if (!aStatement->mStatementRowHolder) {
JS::RootedObject scope(aCtx, aScopeObj);
nsCOMPtr<mozIStorageStatementRow> row(new StatementRow(aStatement));
NS_ENSURE_TRUE(row, NS_ERROR_OUT_OF_MEMORY);
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect());
rv = xpc->WrapNativeHolder(
aCtx,
::JS_GetGlobalForObject(aCtx, scope),
row,
NS_GET_IID(mozIStorageStatementRow),
getter_AddRefs(holder)
);
NS_ENSURE_SUCCESS(rv, rv);
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<StatementRowHolder> rowHolder = new StatementRowHolder(holder);
aStatement->mStatementRowHolder =
new nsMainThreadPtrHolder<nsIXPConnectJSObjectHolder>(rowHolder);
}
JS::Rooted<JSObject*> obj(aCtx);
obj = aStatement->mStatementRowHolder->GetJSObject();
NS_ENSURE_STATE(obj);
_row->setObject(*obj);
return NS_OK;
}
nsresult
StatementJSHelper::getParams(Statement *aStatement,
JSContext *aCtx,
JSObject *aScopeObj,
JS::Value *_params)
{
MOZ_ASSERT(NS_IsMainThread());
nsresult rv;
#ifdef DEBUG
int32_t state;
(void)aStatement->GetState(&state);
NS_ASSERTION(state == mozIStorageStatement::MOZ_STORAGE_STATEMENT_READY,
"Invalid state to get the params object - all calls will fail!");
#endif
if (!aStatement->mStatementParamsHolder) {
JS::RootedObject scope(aCtx, aScopeObj);
nsCOMPtr<mozIStorageStatementParams> params =
new StatementParams(aStatement);
NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY);
nsCOMPtr<nsIXPConnectJSObjectHolder> holder;
nsCOMPtr<nsIXPConnect> xpc(Service::getXPConnect());
rv = xpc->WrapNativeHolder(
aCtx,
::JS_GetGlobalForObject(aCtx, scope),
params,
NS_GET_IID(mozIStorageStatementParams),
getter_AddRefs(holder)
);
NS_ENSURE_SUCCESS(rv, rv);
Bug 1207245 - part 6 - rename nsRefPtr<T> to RefPtr<T>; r=ehsan; a=Tomcat The bulk of this commit was generated with a script, executed at the top level of a typical source code checkout. The only non-machine-generated part was modifying MFBT's moz.build to reflect the new naming. CLOSED TREE makes big refactorings like this a piece of cake. # The main substitution. find . -name '*.cpp' -o -name '*.cc' -o -name '*.h' -o -name '*.mm' -o -name '*.idl'| \ xargs perl -p -i -e ' s/nsRefPtr\.h/RefPtr\.h/g; # handle includes s/nsRefPtr ?</RefPtr</g; # handle declarations and variables ' # Handle a special friend declaration in gfx/layers/AtomicRefCountedWithFinalize.h. perl -p -i -e 's/::nsRefPtr;/::RefPtr;/' gfx/layers/AtomicRefCountedWithFinalize.h # Handle nsRefPtr.h itself, a couple places that define constructors # from nsRefPtr, and code generators specially. We do this here, rather # than indiscriminantly s/nsRefPtr/RefPtr/, because that would rename # things like nsRefPtrHashtable. perl -p -i -e 's/nsRefPtr/RefPtr/g' \ mfbt/nsRefPtr.h \ xpcom/glue/nsCOMPtr.h \ xpcom/base/OwningNonNull.h \ ipc/ipdl/ipdl/lower.py \ ipc/ipdl/ipdl/builtin.py \ dom/bindings/Codegen.py \ python/lldbutils/lldbutils/utils.py # In our indiscriminate substitution above, we renamed # nsRefPtrGetterAddRefs, the class behind getter_AddRefs. Fix that up. find . -name '*.cpp' -o -name '*.h' -o -name '*.idl' | \ xargs perl -p -i -e 's/nsRefPtrGetterAddRefs/RefPtrGetterAddRefs/g' if [ -d .git ]; then git mv mfbt/nsRefPtr.h mfbt/RefPtr.h else hg mv mfbt/nsRefPtr.h mfbt/RefPtr.h fi --HG-- rename : mfbt/nsRefPtr.h => mfbt/RefPtr.h
2015-10-18 08:24:48 +03:00
RefPtr<StatementParamsHolder> paramsHolder =
new StatementParamsHolder(holder);
aStatement->mStatementParamsHolder =
new nsMainThreadPtrHolder<nsIXPConnectJSObjectHolder>(paramsHolder);
}
JS::Rooted<JSObject*> obj(aCtx);
obj = aStatement->mStatementParamsHolder->GetJSObject();
NS_ENSURE_STATE(obj);
_params->setObject(*obj);
return NS_OK;
}
NS_IMETHODIMP_(MozExternalRefCountType) StatementJSHelper::AddRef() { return 2; }
NS_IMETHODIMP_(MozExternalRefCountType) StatementJSHelper::Release() { return 1; }
NS_INTERFACE_MAP_BEGIN(StatementJSHelper)
NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable)
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
////////////////////////////////////////////////////////////////////////////////
//// nsIXPCScriptable
Bug 1332172 - Remove XPC_MAP_WANT_*. r=mccr8. nsIXPCScriptable flags handling in xpc_map_end.h is a bit of a mess. - Half the flags relate to whether various functions are defined (PreCreate, GetProperty, etc). These are set using the XPC_MAP_WANT_* macros; for each one xpc_map_end.h inserts the corresponding flag using the preprocessor (see XPC_MAP_CLASSNAME::GetScriptableFlags()). - The other half of the flags relate to other things (IS_GLOBAL_OBJECT, DONT_REFLECT_INTERFACE_NAMES, etc). These are set using the XPC_MAP_FLAGS macro. Having two similar but different mechanisms to set the flags for a class is confusing. (Indeed, until recently we had some classes where a single flag was redundantly specified via both mechanisms.) Note also that the classes done in dom/base/nsIDOMClassInfo.h also specify all the flags in a single value, similar to how XPC_MAP_FLAGS works. This patch removes the XPC_MAP_WANT_* macros. All flags are now set via XPC_MAP_FLAGS. This is a significant simplification to xpc_map_end.h and all the places that use it. The downside of this change is that I had to change the flag constants from class constants (i.e. nsIXPCScriptable::FOO) to macros (i.e. NSIXPCSCRIPTABLE_FOO) because they need to be used in #if statements like this in xpc_map_end.h: #if !((XPC_MAP_FLAGS) & NSIXPCSCRIPTABLE_WANT_PRECREATE) and you can't use a '::'-qualified name inside a #if. I think this downside is outweighed by the simplification described above. Overall the patch removes 80 lines of code. --HG-- extra : rebase_source : 6d5c341d0deba8f1529d81c17bb8819e09620b05
2017-01-23 05:33:58 +03:00
#define XPC_MAP_CLASSNAME StatementJSHelper
#define XPC_MAP_QUOTED_CLASSNAME "StatementJSHelper"
Bug 1332172 - Remove XPC_MAP_WANT_*. r=mccr8. nsIXPCScriptable flags handling in xpc_map_end.h is a bit of a mess. - Half the flags relate to whether various functions are defined (PreCreate, GetProperty, etc). These are set using the XPC_MAP_WANT_* macros; for each one xpc_map_end.h inserts the corresponding flag using the preprocessor (see XPC_MAP_CLASSNAME::GetScriptableFlags()). - The other half of the flags relate to other things (IS_GLOBAL_OBJECT, DONT_REFLECT_INTERFACE_NAMES, etc). These are set using the XPC_MAP_FLAGS macro. Having two similar but different mechanisms to set the flags for a class is confusing. (Indeed, until recently we had some classes where a single flag was redundantly specified via both mechanisms.) Note also that the classes done in dom/base/nsIDOMClassInfo.h also specify all the flags in a single value, similar to how XPC_MAP_FLAGS works. This patch removes the XPC_MAP_WANT_* macros. All flags are now set via XPC_MAP_FLAGS. This is a significant simplification to xpc_map_end.h and all the places that use it. The downside of this change is that I had to change the flag constants from class constants (i.e. nsIXPCScriptable::FOO) to macros (i.e. NSIXPCSCRIPTABLE_FOO) because they need to be used in #if statements like this in xpc_map_end.h: #if !((XPC_MAP_FLAGS) & NSIXPCSCRIPTABLE_WANT_PRECREATE) and you can't use a '::'-qualified name inside a #if. I think this downside is outweighed by the simplification described above. Overall the patch removes 80 lines of code. --HG-- extra : rebase_source : 6d5c341d0deba8f1529d81c17bb8819e09620b05
2017-01-23 05:33:58 +03:00
#define XPC_MAP_FLAGS (XPC_SCRIPTABLE_WANT_GETPROPERTY | \
XPC_SCRIPTABLE_WANT_RESOLVE | \
XPC_SCRIPTABLE_ALLOW_PROP_MODS_DURING_RESOLVE)
#include "xpc_map_end.h"
NS_IMETHODIMP
StatementJSHelper::GetProperty(nsIXPConnectWrappedNative *aWrapper,
JSContext *aCtx,
JSObject *aScopeObj,
2010-07-15 10:19:36 +04:00
jsid aId,
JS::Value *_result,
bool *_retval)
{
2010-07-15 10:19:36 +04:00
if (!JSID_IS_STRING(aId))
return NS_OK;
JS::Rooted<JSObject*> scope(aCtx, aScopeObj);
JS::Rooted<jsid> id(aCtx, aId);
#ifdef DEBUG
{
nsCOMPtr<mozIStorageStatement> isStatement(
do_QueryInterface(aWrapper->Native()));
NS_ASSERTION(isStatement, "How is this not a statement?!");
}
#endif
Statement *stmt = static_cast<Statement *>(
static_cast<mozIStorageStatement *>(aWrapper->Native())
);
JSFlatString *str = JSID_TO_FLAT_STRING(id);
if (::JS_FlatStringEqualsAscii(str, "row"))
return getRow(stmt, aCtx, scope, _result);
if (::JS_FlatStringEqualsAscii(str, "params"))
return getParams(stmt, aCtx, scope, _result);
return NS_OK;
}
NS_IMETHODIMP
StatementJSHelper::Resolve(nsIXPConnectWrappedNative *aWrapper,
JSContext *aCtx, JSObject *aScopeObj,
jsid aId, bool *aResolvedp,
bool *_retval)
{
2010-07-15 10:19:36 +04:00
if (!JSID_IS_STRING(aId))
return NS_OK;
JS::RootedObject scope(aCtx, aScopeObj);
if (::JS_FlatStringEqualsAscii(JSID_TO_FLAT_STRING(aId), "step")) {
*_retval = ::JS_DefineFunction(aCtx, scope, "step", stepFunc,
0, JSPROP_RESOLVING) != nullptr;
*aResolvedp = true;
return NS_OK;
}
return NS_OK;
}
////////////////////////////////////////////////////////////////////////////////
//// StatementJSObjectHolder
NS_IMPL_ISUPPORTS(StatementJSObjectHolder, nsIXPConnectJSObjectHolder);
JSObject*
StatementJSObjectHolder::GetJSObject()
{
return mHolder->GetJSObject();
}
StatementJSObjectHolder::StatementJSObjectHolder(nsIXPConnectJSObjectHolder* aHolder)
: mHolder(aHolder)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mHolder);
}
StatementParamsHolder::~StatementParamsHolder()
{
MOZ_ASSERT(NS_IsMainThread());
// We are considered dead at this point, so any wrappers for row or params
// need to lose their reference to the statement.
nsCOMPtr<nsIXPConnectWrappedNative> wrapper = do_QueryInterface(mHolder);
nsCOMPtr<mozIStorageStatementParams> iObj = do_QueryWrappedNative(wrapper);
StatementParams *obj = static_cast<StatementParams *>(iObj.get());
obj->mStatement = nullptr;
}
StatementRowHolder::~StatementRowHolder()
{
MOZ_ASSERT(NS_IsMainThread());
// We are considered dead at this point, so any wrappers for row or params
// need to lose their reference to the statement.
nsCOMPtr<nsIXPConnectWrappedNative> wrapper = do_QueryInterface(mHolder);
nsCOMPtr<mozIStorageStatementRow> iObj = do_QueryWrappedNative(wrapper);
StatementRow *obj = static_cast<StatementRow *>(iObj.get());
obj->mStatement = nullptr;
}
} // namespace storage
} // namespace mozilla