Skip to content

Instantly share code, notes, and snippets.

@ehsan
Created November 20, 2018 14:44
Show Gist options
  • Save ehsan/01b43aae227f11545011b2a86067f69c to your computer and use it in GitHub Desktop.
Save ehsan/01b43aae227f11545011b2a86067f69c to your computer and use it in GitHub Desktop.
commit 18186d1a9ebfeb1dde7c402503979ebfbc5c636c
Author: Ehsan Akhgari <ehsan@mozilla.com>
Date: Tue Nov 20 09:40:42 2018 -0500
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
diff --git a/dom/base/nsContentPermissionHelper.cpp b/dom/base/nsContentPermissionHelper.cpp
index f2d366af6af6e..2288ff00fa769 100644
--- a/dom/base/nsContentPermissionHelper.cpp
+++ b/dom/base/nsContentPermissionHelper.cpp
@@ -724,6 +724,53 @@ ContentPermissionRequestBase::RequestDelayedTask(nsIEventTarget* aTarget,
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 mozilla
@@ -912,43 +959,9 @@ nsContentPermissionRequestProxy::Allow(JS::HandleValue aChoices)
}
nsTArray<PermissionChoice> choices;
- if (aChoices.isNullOrUndefined()) {
- // No choice is specified.
- } else if (aChoices.isObject()) {
- // 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;
+ nsresult rv = TranslateChoices(aChoices, mPermissionRequests, choices);
+ if (NS_FAILED(rv)) {
+ return rv;
}
Unused << mParent->SendNotifyResult(true, choices);
diff --git a/dom/base/nsContentPermissionHelper.h b/dom/base/nsContentPermissionHelper.h
index 39d20a248857c..f8dab043b2ae8 100644
--- a/dom/base/nsContentPermissionHelper.h
+++ b/dom/base/nsContentPermissionHelper.h
@@ -113,6 +113,11 @@ private:
RefPtr<VisibilityChangeListener> mListener;
};
+nsresult
+TranslateChoices(JS::HandleValue aChoices,
+ const nsTArray<PermissionRequest>& aPermissionRequests,
+ nsTArray<PermissionChoice>& aTranslatedChoices);
+
class ContentPermissionRequestBase : public nsIContentPermissionRequest
{
public:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment