Bug 982112 - Make accesscheck code deal with WebIDL bindings. r=bz.
authorPeter Van der Beken <peterv@propagandism.org>
Tue, 05 Nov 2013 09:47:59 +0100
changeset 192451 f61df670d9622592abaf690d0fb3c6a88fd46501
parent 192450 ce6a8fa5db7d76a0e0268096128f473f78a53917
child 192452 570a5c5c8e386f29c0873da5f431c0137b0eba0c
push id3624
push userasasaki@mozilla.com
push dateMon, 09 Jun 2014 21:49:01 +0000
treeherdermozilla-beta@b1a5da15899a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs982112
milestone30.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 982112 - Make accesscheck code deal with WebIDL bindings. r=bz.
js/xpconnect/src/XPCJSRuntime.cpp
js/xpconnect/src/nsDOMQS.h
js/xpconnect/src/xpcpublic.h
js/xpconnect/wrappers/AccessCheck.cpp
--- a/js/xpconnect/src/XPCJSRuntime.cpp
+++ b/js/xpconnect/src/XPCJSRuntime.cpp
@@ -583,35 +583,42 @@ GetJunkScopeGlobal()
     // GetJunkScope would ideally never fail, currently it is not yet the case
     // unfortunately...(see Bug 874158)
     if (!junkScope)
         return nullptr;
     return GetNativeForGlobal(junkScope);
 }
 
 nsGlobalWindow*
+WindowOrNull(JSObject *aObj)
+{
+    MOZ_ASSERT(aObj);
+
+    // This will always return null until we have Window on WebIDL bindings,
+    // at which point it will do the right thing.
+    if (!IS_WN_CLASS(js::GetObjectClass(aObj))) {
+        nsGlobalWindow* win = nullptr;
+        UNWRAP_OBJECT(Window, aObj, win);
+        return win;
+    }
+
+    nsISupports* supports = XPCWrappedNative::Get(aObj)->GetIdentityObject();
+    nsCOMPtr<nsPIDOMWindow> piWin = do_QueryInterface(supports);
+    if (!piWin)
+        return nullptr;
+    return static_cast<nsGlobalWindow*>(piWin.get());
+}
+
+nsGlobalWindow*
 WindowGlobalOrNull(JSObject *aObj)
 {
     MOZ_ASSERT(aObj);
     JSObject *glob = js::GetGlobalForObjectCrossCompartment(aObj);
-    MOZ_ASSERT(glob);
-
-    // This will always return null until we have Window on WebIDL bindings,
-    // at which point it will do the right thing.
-    if (!IS_WN_CLASS(js::GetObjectClass(glob))) {
-        nsGlobalWindow* win = nullptr;
-        UNWRAP_OBJECT(Window, glob, win);
-        return win;
-    }
-
-    nsISupports* supports = XPCWrappedNative::Get(glob)->GetIdentityObject();
-    nsCOMPtr<nsPIDOMWindow> piWin = do_QueryInterface(supports);
-    if (!piWin)
-        return nullptr;
-    return static_cast<nsGlobalWindow*>(piWin.get());
+
+    return WindowOrNull(glob);
 }
 
 }
 
 static void
 CompartmentDestroyedCallback(JSFreeOp *fop, JSCompartment *compartment)
 {
     // NB - This callback may be called in JS_DestroyRuntime, which happens
--- a/js/xpconnect/src/nsDOMQS.h
+++ b/js/xpconnect/src/nsDOMQS.h
@@ -55,16 +55,17 @@ NEW_BINDING(mozilla::dom::Element, Eleme
 NEW_BINDING(nsGenericHTMLElement, HTMLElement);
 NEW_BINDING(nsIDocument, Document);
 NEW_BINDING(nsDocument, Document);
 NEW_BINDING(nsHTMLDocument, HTMLDocument);
 NEW_BINDING(nsSVGElement, SVGElement);
 NEW_BINDING(mozilla::dom::Event, Event);
 NEW_BINDING(mozilla::dom::UIEvent, UIEvent);
 NEW_BINDING(mozilla::dom::MouseEvent, MouseEvent);
+NEW_BINDING(nsGlobalWindow, Window);
 
 #define DEFINE_UNWRAP_CAST(_interface, _base, _bit)                           \
 template <>                                                                   \
 MOZ_ALWAYS_INLINE bool                                                        \
 xpc_qsUnwrapThis<_interface>(JSContext *cx,                                   \
                              JS::HandleObject obj,                            \
                              _interface **ppThis,                             \
                              nsISupports **pThisRef,                          \
--- a/js/xpconnect/src/xpcpublic.h
+++ b/js/xpconnect/src/xpcpublic.h
@@ -420,16 +420,23 @@ GetJunkScope();
 /**
  * Returns the native global of the junk scope. See comment of GetJunkScope
  * about the conditions of using it.
  */
 nsIGlobalObject *
 GetJunkScopeGlobal();
 
 /**
+ * If |aObj| is a window, returns the associated nsGlobalWindow.
+ * Otherwise, returns null.
+ */
+nsGlobalWindow*
+WindowOrNull(JSObject *aObj);
+
+/**
  * If |aObj| has a window for a global, returns the associated nsGlobalWindow.
  * Otherwise, returns null.
  */
 nsGlobalWindow*
 WindowGlobalOrNull(JSObject *aObj);
 
 // Error reporter used when there is no associated DOM window on to which to
 // report errors and warnings.
--- a/js/xpconnect/wrappers/AccessCheck.cpp
+++ b/js/xpconnect/wrappers/AccessCheck.cpp
@@ -3,25 +3,26 @@
  *
  * 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 "AccessCheck.h"
 
 #include "nsJSPrincipals.h"
-#include "nsIDOMWindow.h"
-#include "nsIDOMWindowCollection.h"
+#include "nsGlobalWindow.h"
 
 #include "XPCWrapper.h"
 #include "XrayWrapper.h"
 
 #include "jsfriendapi.h"
 #include "mozilla/dom/BindingUtils.h"
 #include "mozilla/dom/WindowBinding.h"
+#include "nsIDOMWindowCollection.h"
+#include "nsJSUtils.h"
 
 using namespace mozilla;
 using namespace JS;
 using namespace js;
 
 namespace xpc {
 
 nsIPrincipal *
@@ -144,40 +145,32 @@ IsPermitted(const char *name, JSFlatStri
 static bool
 IsFrameId(JSContext *cx, JSObject *objArg, jsid idArg)
 {
     RootedObject obj(cx, objArg);
     RootedId id(cx, idArg);
 
     obj = JS_ObjectToInnerObject(cx, obj);
     MOZ_ASSERT(!js::IsWrapper(obj));
-    XPCWrappedNative *wn = IS_WN_REFLECTOR(obj) ? XPCWrappedNative::Get(obj)
-                                                : nullptr;
-    if (!wn) {
-        return false;
-    }
-
-    nsCOMPtr<nsIDOMWindow> domwin(do_QueryWrappedNative(wn));
-    if (!domwin) {
+    nsGlobalWindow* win = WindowOrNull(obj);
+    if (!win) {
         return false;
     }
 
     nsCOMPtr<nsIDOMWindowCollection> col;
-    domwin->GetFrames(getter_AddRefs(col));
+    win->GetFrames(getter_AddRefs(col));
     if (!col) {
         return false;
     }
 
+    nsCOMPtr<nsIDOMWindow> domwin;
     if (JSID_IS_INT(id)) {
         col->Item(JSID_TO_INT(id), getter_AddRefs(domwin));
     } else if (JSID_IS_STRING(id)) {
-        nsAutoString str(JS_GetInternedStringChars(JSID_TO_STRING(id)));
-        col->NamedItem(str, getter_AddRefs(domwin));
-    } else {
-        return false;
+        col->NamedItem(nsDependentJSString(id), getter_AddRefs(domwin));
     }
 
     return domwin != nullptr;
 }
 
 static bool
 IsWindow(const char *name)
 {