Bug 786688 - Part 1: Add chrome only openForPrincipal() and deleteForPrincipal(). r=bent a=bajaj

This commit is contained in:
Jonas Sicking 2012-10-30 07:07:09 +01:00
Родитель 45a13269ac
Коммит d69117a579
5 изменённых файлов: 116 добавлений и 10 удалений

Просмотреть файл

@ -702,6 +702,24 @@ DOMCI_DATA_NO_CLASS(DOMConstructor)
namespace {
class IDBFactorySH : public nsDOMGenericSH
{
protected:
IDBFactorySH(nsDOMClassInfoData* aData) : nsDOMGenericSH(aData)
{ }
virtual ~IDBFactorySH()
{ }
public:
NS_IMETHOD PostCreatePrototype(JSContext * cx, JSObject * proto);
static nsIClassInfo *doCreate(nsDOMClassInfoData *aData)
{
return new IDBFactorySH(aData);
}
};
class IDBEventTargetSH : public nsEventTargetSH
{
protected:
@ -1591,7 +1609,7 @@ static nsDOMClassInfoData sClassInfoData[] = {
NS_DEFINE_CLASSINFO_DATA(DesktopNotificationCenter, nsDOMGenericSH,
DOM_DEFAULT_SCRIPTABLE_FLAGS)
NS_DEFINE_CLASSINFO_DATA(IDBFactory, nsDOMGenericSH,
NS_DEFINE_CLASSINFO_DATA(IDBFactory, IDBFactorySH,
DOM_DEFAULT_SCRIPTABLE_FLAGS)
NS_DEFINE_CLASSINFO_DATA_WITH_NAME(IDBFileHandle, FileHandle, nsEventTargetSH,
EVENTTARGET_SCRIPTABLE_FLAGS)
@ -8002,6 +8020,88 @@ nsDOMMutationObserverSH::PreserveWrapper(nsISupports* aNative)
nsContentUtils::PreserveWrapper(aNative, mutationObserver);
}
// IDBFactory helper
/* static */
template<nsresult (*func)(JSContext *cx, unsigned argc, jsval *vp, bool aDelete), bool aDelete>
JSBool
IDBFNativeShim(JSContext *cx, unsigned argc, jsval *vp)
{
nsresult rv = (*func)(cx, argc, vp, aDelete);
if (NS_FAILED(rv)) {
xpc::Throw(cx, rv);
return false;
}
return true;
}
/* static */ nsresult
IDBFOpenForPrincipal(JSContext *cx, unsigned argc, JS::Value *vp, bool aDelete)
{
// Just to be on the extra-safe side
if (!nsContentUtils::IsCallerChrome()) {
MOZ_NOT_REACHED("Shouldn't be possible to get here");
return NS_ERROR_FAILURE;
}
JSObject* principalJS;
JSString* nameJS;
uint32_t version = 0;
if (!JS_ConvertArguments(cx, argc, JS_ARGV(cx, vp), "oS/u",
&principalJS, &nameJS, &version)) {
return NS_ERROR_FAILURE;
}
if (version < 1 && argc >= 3) {
return NS_ERROR_TYPE_ERR;
}
nsDependentJSString name;
NS_ENSURE_TRUE(name.init(cx, nameJS), NS_ERROR_UNEXPECTED);
nsCOMPtr<nsIPrincipal> principal = do_QueryWrapper(cx, principalJS);
NS_ENSURE_TRUE(principal, NS_ERROR_NO_INTERFACE);
nsCString extendedOrigin;
nsresult rv = principal->GetExtendedOrigin(extendedOrigin);
NS_ENSURE_FALSE(extendedOrigin.IsEmpty(), NS_ERROR_UNEXPECTED);
nsCOMPtr<nsIIDBFactory> factory =
do_QueryWrapper(cx, JS_THIS_OBJECT(cx, vp));
NS_ENSURE_TRUE(factory, NS_ERROR_NO_INTERFACE);
nsRefPtr<indexedDB::IDBOpenDBRequest> request;
rv = static_cast<indexedDB::IDBFactory*>(factory.get())->
OpenCommon(name, version, extendedOrigin, aDelete, cx,
getter_AddRefs(request));
NS_ENSURE_SUCCESS(rv, rv);
return WrapNative(cx, JS_GetGlobalForScopeChain(cx),
static_cast<nsIIDBOpenDBRequest*>(request),
&NS_GET_IID(nsIIDBOpenDBRequest), true, vp);
}
NS_IMETHODIMP
IDBFactorySH::PostCreatePrototype(JSContext * cx, JSObject * proto)
{
// set up our proto first
nsresult rv = nsDOMGenericSH::PostCreatePrototype(cx, proto);
NS_ENSURE_SUCCESS(rv, rv);
if (xpc::AccessCheck::isChrome(js::GetObjectCompartment(proto)) &&
(!JS_DefineFunction(cx, proto, "openForPrincipal",
IDBFNativeShim<IDBFOpenForPrincipal, false>,
3, JSPROP_ENUMERATE) ||
!JS_DefineFunction(cx, proto, "deleteForPrincipal",
IDBFNativeShim<IDBFOpenForPrincipal, true>,
3, JSPROP_ENUMERATE))) {
return NS_ERROR_UNEXPECTED;
}
return rv;
}
// IDBEventTarget helper
NS_IMETHODIMP

Просмотреть файл

@ -273,7 +273,8 @@ protected:
static JSPropertyOp sXrayWrapperPropertyHolderGetPropertyOp;
};
// THIS ONE ISN'T SAFE!! It assumes that the private of the JSObject is
// an nsISupports.
inline
const nsQueryInterface
do_QueryWrappedNative(nsIXPConnectWrappedNative *wrapper, JSObject *obj)
@ -281,6 +282,8 @@ do_QueryWrappedNative(nsIXPConnectWrappedNative *wrapper, JSObject *obj)
return nsQueryInterface(nsDOMClassInfo::GetNative(wrapper, obj));
}
// THIS ONE ISN'T SAFE!! It assumes that the private of the JSObject is
// an nsISupports.
inline
const nsQueryInterfaceWithError
do_QueryWrappedNative(nsIXPConnectWrappedNative *wrapper, JSObject *obj,

Просмотреть файл

@ -499,6 +499,7 @@ DOMCI_DATA(IDBFactory, IDBFactory)
nsresult
IDBFactory::OpenCommon(const nsAString& aName,
int64_t aVersion,
const nsACString& aASCIIOrigin,
bool aDeleting,
JSContext* aCallingCx,
IDBOpenDBRequest** _retval)
@ -529,7 +530,7 @@ IDBFactory::OpenCommon(const nsAString& aName,
if (IndexedDatabaseManager::IsMainProcess()) {
nsRefPtr<OpenDatabaseHelper> openHelper =
new OpenDatabaseHelper(request, aName, mASCIIOrigin, aVersion, aDeleting,
new OpenDatabaseHelper(request, aName, aASCIIOrigin, aVersion, aDeleting,
mContentParent, privilege);
rv = openHelper->Init();
@ -542,13 +543,13 @@ IDBFactory::OpenCommon(const nsAString& aName,
NS_ASSERTION(mgr, "This should never be null!");
rv =
mgr->WaitForOpenAllowed(OriginOrPatternString::FromOrigin(mASCIIOrigin),
mgr->WaitForOpenAllowed(OriginOrPatternString::FromOrigin(aASCIIOrigin),
openHelper->Id(), permissionHelper);
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
}
else if (aDeleting) {
nsCOMPtr<nsIAtom> databaseId =
IndexedDatabaseManager::GetDatabaseId(mASCIIOrigin, aName);
IndexedDatabaseManager::GetDatabaseId(aASCIIOrigin, aName);
NS_ENSURE_TRUE(databaseId, NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR);
IndexedDBDeleteDatabaseRequestChild* actor =
@ -583,7 +584,7 @@ IDBFactory::Open(const nsAString& aName,
}
nsRefPtr<IDBOpenDBRequest> request;
nsresult rv = OpenCommon(aName, aVersion, false, aCx,
nsresult rv = OpenCommon(aName, aVersion, mASCIIOrigin, false, aCx,
getter_AddRefs(request));
NS_ENSURE_SUCCESS(rv, rv);
@ -597,7 +598,7 @@ IDBFactory::DeleteDatabase(const nsAString& aName,
nsIIDBOpenDBRequest** _retval)
{
nsRefPtr<IDBOpenDBRequest> request;
nsresult rv = OpenCommon(aName, 0, true, aCx, getter_AddRefs(request));
nsresult rv = OpenCommon(aName, 0, mASCIIOrigin, true, aCx, getter_AddRefs(request));
NS_ENSURE_SUCCESS(rv, rv);
request.forget(_retval);

Просмотреть файл

@ -92,6 +92,7 @@ public:
nsresult
OpenCommon(const nsAString& aName,
int64_t aVersion,
const nsACString& aASCIIOrigin,
bool aDeleting,
JSContext* aCallingCx,
IDBOpenDBRequest** _retval);

Просмотреть файл

@ -162,8 +162,8 @@ IndexedDBParent::RecvPIndexedDBDatabaseConstructor(
nsRefPtr<IDBOpenDBRequest> request;
nsresult rv =
mFactory->OpenCommon(aName, aVersion, false, nullptr,
getter_AddRefs(request));
mFactory->OpenCommon(aName, aVersion, mFactory->GetASCIIOrigin(), false,
nullptr, getter_AddRefs(request));
NS_ENSURE_SUCCESS(rv, false);
IndexedDBDatabaseParent* actor =
@ -199,7 +199,8 @@ IndexedDBParent::RecvPIndexedDBDeleteDatabaseRequestConstructor(
nsRefPtr<IDBOpenDBRequest> request;
nsresult rv =
mFactory->OpenCommon(aName, 0, true, nullptr, getter_AddRefs(request));
mFactory->OpenCommon(aName, 0, mFactory->GetASCIIOrigin(), true, nullptr,
getter_AddRefs(request));
NS_ENSURE_SUCCESS(rv, false);
rv = actor->SetOpenRequest(request);