Bug 1147215 - Add a helper to get the current window with a null check. r=smaug, a=sledru
authorBobby Holley <bobbyholley@gmail.com>
Wed, 25 Mar 2015 11:36:58 -0700
changeset 258155 7049015f9ce37e4957231fbc33f3651baf734efb
parent 258154 d7ec6454a5c947026b16a88300d14786aeebe3b5
child 258156 615a00a095521ada40fc19ed92986c2cb8a6ce70
push id4610
push userjlund@mozilla.com
push dateMon, 30 Mar 2015 18:32:55 +0000
treeherdermozilla-beta@4df54044d9ef [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug, sledru
bugs1147215
milestone38.0a2
Bug 1147215 - Add a helper to get the current window with a null check. r=smaug, a=sledru
dom/base/ScriptSettings.cpp
js/xpconnect/src/XPCJSRuntime.cpp
js/xpconnect/src/xpcpublic.h
--- a/dom/base/ScriptSettings.cpp
+++ b/dom/base/ScriptSettings.cpp
@@ -478,17 +478,17 @@ AutoJSAPI::InitWithLegacyErrorReporting(
 //
 // Eventually, SpiderMonkey will have a special-purpose callback for warnings
 // only.
 void
 WarningOnlyErrorReporter(JSContext* aCx, const char* aMessage, JSErrorReport* aRep)
 {
   MOZ_ASSERT(JSREPORT_IS_WARNING(aRep->flags));
   nsRefPtr<xpc::ErrorReport> xpcReport = new xpc::ErrorReport();
-  nsPIDOMWindow* win = xpc::WindowGlobalOrNull(JS::CurrentGlobalOrNull(aCx));
+  nsPIDOMWindow* win = xpc::CurrentWindowOrNull(aCx);
   xpcReport->Init(aRep, aMessage, nsContentUtils::IsCallerChrome(),
                   win ? win->WindowID() : 0);
   xpcReport->LogToConsole();
 }
 
 void
 AutoJSAPI::TakeOwnershipOfErrorReporting()
 {
--- a/js/xpconnect/src/XPCJSRuntime.cpp
+++ b/js/xpconnect/src/XPCJSRuntime.cpp
@@ -589,16 +589,23 @@ AddonWindowOrNull(JSObject* aObj)
     MOZ_RELEASE_ASSERT(js::IsCrossCompartmentWrapper(proto) ||
                        xpc::IsSandboxPrototypeProxy(proto));
     JSObject* mainGlobal = js::UncheckedUnwrap(proto, /* stopAtOuter = */ false);
     MOZ_RELEASE_ASSERT(JS_IsGlobalObject(mainGlobal));
 
     return WindowOrNull(mainGlobal);
 }
 
+nsGlobalWindow*
+CurrentWindowOrNull(JSContext* cx)
+{
+    JSObject* glob = JS::CurrentGlobalOrNull(cx);
+    return glob ? WindowOrNull(glob) : nullptr;
+}
+
 }
 
 static void
 CompartmentDestroyedCallback(JSFreeOp* fop, JSCompartment* compartment)
 {
     // NB - This callback may be called in JS_DestroyRuntime, which happens
     // after the XPCJSRuntime has been torn down.
 
--- a/js/xpconnect/src/xpcpublic.h
+++ b/js/xpconnect/src/xpcpublic.h
@@ -464,16 +464,23 @@ WindowGlobalOrNull(JSObject* aObj);
 /**
  * If |aObj| is in an addon scope and that addon scope is associated with a
  * live DOM Window, returns the associated nsGlobalWindow. Otherwise, returns
  * null.
  */
 nsGlobalWindow*
 AddonWindowOrNull(JSObject* aObj);
 
+/**
+ * If |cx| is in a compartment whose global is a window, returns the associated
+ * nsGlobalWindow. Otherwise, returns null.
+ */
+nsGlobalWindow*
+CurrentWindowOrNull(JSContext* cx);
+
 // Error reporter used when there is no associated DOM window on to which to
 // report errors and warnings.
 //
 // Note - This is temporarily implemented in nsJSEnvironment.cpp.
 void
 SystemErrorReporter(JSContext* cx, const char* message, JSErrorReport* rep);
 
 void