mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-09 19:35:51 +00:00
Bug 1508657 - Factor out the code used to parse the argument passed to nsIContentPermissionRequest::Allow(); r=baku
Differential Revision: https://phabricator.services.mozilla.com/D12428
This commit is contained in:
parent
877a1c2411
commit
478325b15f
@ -724,6 +724,53 @@ ContentPermissionRequestBase::RequestDelayedTask(nsIEventTarget* aTarget,
|
|||||||
aTarget->Dispatch(r.forget());
|
aTarget->Dispatch(r.forget());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
nsresult
|
||||||
|
TranslateChoices(JS::HandleValue aChoices,
|
||||||
|
const nsTArray<PermissionRequest>& aPermissionRequests,
|
||||||
|
nsTArray<PermissionChoice>& aTranslatedChoices)
|
||||||
|
{
|
||||||
|
if (aChoices.isNullOrUndefined()) {
|
||||||
|
// No choice is specified.
|
||||||
|
} else if (aChoices.isObject()) {
|
||||||
|
// Iterate through all permission types.
|
||||||
|
for (uint32_t i = 0; i < aPermissionRequests.Length(); ++i) {
|
||||||
|
nsCString type = aPermissionRequests[i].type();
|
||||||
|
|
||||||
|
JS::Rooted<JSObject*> obj(RootingCx(), &aChoices.toObject());
|
||||||
|
obj = CheckedUnwrap(obj);
|
||||||
|
if (!obj) {
|
||||||
|
return NS_ERROR_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
AutoJSAPI jsapi;
|
||||||
|
jsapi.Init();
|
||||||
|
|
||||||
|
JSContext* cx = jsapi.cx();
|
||||||
|
JSAutoRealm ar(cx, obj);
|
||||||
|
|
||||||
|
JS::Rooted<JS::Value> val(cx);
|
||||||
|
|
||||||
|
if (!JS_GetProperty(cx, obj, type.BeginReading(), &val) ||
|
||||||
|
!val.isString()) {
|
||||||
|
// no setting for the permission type, clear exception and skip it
|
||||||
|
jsapi.ClearException();
|
||||||
|
} else {
|
||||||
|
nsAutoJSString choice;
|
||||||
|
if (!choice.init(cx, val)) {
|
||||||
|
jsapi.ClearException();
|
||||||
|
return NS_ERROR_FAILURE;
|
||||||
|
}
|
||||||
|
aTranslatedChoices.AppendElement(PermissionChoice(type, choice));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
MOZ_ASSERT(false, "SelectedChoices should be undefined or an JS object");
|
||||||
|
return NS_ERROR_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NS_OK;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace dom
|
} // namespace dom
|
||||||
} // namespace mozilla
|
} // namespace mozilla
|
||||||
|
|
||||||
@ -783,9 +830,7 @@ nsContentPermissionRequestProxy::nsContentPermissionRequestProxy(ContentPermissi
|
|||||||
NS_ASSERTION(mParent, "null parent");
|
NS_ASSERTION(mParent, "null parent");
|
||||||
}
|
}
|
||||||
|
|
||||||
nsContentPermissionRequestProxy::~nsContentPermissionRequestProxy()
|
nsContentPermissionRequestProxy::~nsContentPermissionRequestProxy() = default;
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
nsresult
|
nsresult
|
||||||
nsContentPermissionRequestProxy::Init(const nsTArray<PermissionRequest>& requests)
|
nsContentPermissionRequestProxy::Init(const nsTArray<PermissionRequest>& requests)
|
||||||
@ -912,43 +957,9 @@ nsContentPermissionRequestProxy::Allow(JS::HandleValue aChoices)
|
|||||||
}
|
}
|
||||||
|
|
||||||
nsTArray<PermissionChoice> choices;
|
nsTArray<PermissionChoice> choices;
|
||||||
if (aChoices.isNullOrUndefined()) {
|
nsresult rv = TranslateChoices(aChoices, mPermissionRequests, choices);
|
||||||
// No choice is specified.
|
if (NS_FAILED(rv)) {
|
||||||
} else if (aChoices.isObject()) {
|
return rv;
|
||||||
// Iterate through all permission types.
|
|
||||||
for (uint32_t i = 0; i < mPermissionRequests.Length(); ++i) {
|
|
||||||
nsCString type = mPermissionRequests[i].type();
|
|
||||||
|
|
||||||
JS::Rooted<JSObject*> obj(RootingCx(), &aChoices.toObject());
|
|
||||||
obj = CheckedUnwrap(obj);
|
|
||||||
if (!obj) {
|
|
||||||
return NS_ERROR_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
AutoJSAPI jsapi;
|
|
||||||
jsapi.Init();
|
|
||||||
|
|
||||||
JSContext* cx = jsapi.cx();
|
|
||||||
JSAutoRealm ar(cx, obj);
|
|
||||||
|
|
||||||
JS::Rooted<JS::Value> val(cx);
|
|
||||||
|
|
||||||
if (!JS_GetProperty(cx, obj, type.BeginReading(), &val) ||
|
|
||||||
!val.isString()) {
|
|
||||||
// no setting for the permission type, clear exception and skip it
|
|
||||||
jsapi.ClearException();
|
|
||||||
} else {
|
|
||||||
nsAutoJSString choice;
|
|
||||||
if (!choice.init(cx, val)) {
|
|
||||||
jsapi.ClearException();
|
|
||||||
return NS_ERROR_FAILURE;
|
|
||||||
}
|
|
||||||
choices.AppendElement(PermissionChoice(type, choice));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
MOZ_ASSERT(false, "SelectedChoices should be undefined or an JS object");
|
|
||||||
return NS_ERROR_FAILURE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Unused << mParent->SendNotifyResult(true, choices);
|
Unused << mParent->SendNotifyResult(true, choices);
|
||||||
|
@ -113,6 +113,11 @@ private:
|
|||||||
RefPtr<VisibilityChangeListener> mListener;
|
RefPtr<VisibilityChangeListener> mListener;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
nsresult
|
||||||
|
TranslateChoices(JS::HandleValue aChoices,
|
||||||
|
const nsTArray<PermissionRequest>& aPermissionRequests,
|
||||||
|
nsTArray<PermissionChoice>& aTranslatedChoices);
|
||||||
|
|
||||||
class ContentPermissionRequestBase : public nsIContentPermissionRequest
|
class ContentPermissionRequestBase : public nsIContentPermissionRequest
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
Loading…
Reference in New Issue
Block a user