Bug 611553 - Make DOMWillOpenModalDialog a chrome-only event. r=dolske r=smaug
authorFrank Yan <fyan@mozilla.com>
Wed, 16 Jan 2013 13:10:18 -0800
changeset 119068 64eb6292b965b3cd08ae4a982b2ee7648e2b87d0
parent 119067 f719315ea4121c192a8c9f5c0f65fc114472e351
child 119069 9229b7917f472fba11f25ee272d628202aeac9e0
push id24189
push useremorley@mozilla.com
push dateThu, 17 Jan 2013 10:42:06 +0000
treeherdermozilla-central@712eca11a04e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdolske, smaug
bugs611553
milestone21.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 611553 - Make DOMWillOpenModalDialog a chrome-only event. r=dolske r=smaug
embedding/components/windowwatcher/src/nsAutoWindowStateHelper.cpp
embedding/components/windowwatcher/src/nsAutoWindowStateHelper.h
toolkit/components/prompts/src/nsPrompter.js
--- a/embedding/components/windowwatcher/src/nsAutoWindowStateHelper.cpp
+++ b/embedding/components/windowwatcher/src/nsAutoWindowStateHelper.cpp
@@ -5,24 +5,25 @@
 
 #include "nsAutoWindowStateHelper.h"
 
 #include "nsIDOMWindow.h"
 #include "nsPIDOMWindow.h"
 #include "nsIDOMEventTarget.h"
 #include "nsIDOMEvent.h"
 #include "nsString.h"
+#include "nsGUIEvent.h"
 
 /****************************************************************
  ****************** nsAutoWindowStateHelper *********************
  ****************************************************************/
 
 nsAutoWindowStateHelper::nsAutoWindowStateHelper(nsIDOMWindow *aWindow)
   : mWindow(aWindow),
-    mDefaultEnabled(DispatchCustomEvent("DOMWillOpenModalDialog"))
+    mDefaultEnabled(DispatchEventToChrome("DOMWillOpenModalDialog"))
 {
   nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(aWindow));
 
   if (window) {
     mCallerWindow = window->EnterModalState();
   }
 }
 
@@ -30,23 +31,38 @@ nsAutoWindowStateHelper::~nsAutoWindowSt
 {
   nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(mWindow));
 
   if (window) {
     window->LeaveModalState(mCallerWindow);
   }
 
   if (mDefaultEnabled) {
-    DispatchCustomEvent("DOMModalDialogClosed");
+    DispatchEventToChrome("DOMModalDialogClosed");
   }
 }
 
 bool
-nsAutoWindowStateHelper::DispatchCustomEvent(const char *aEventName)
+nsAutoWindowStateHelper::DispatchEventToChrome(const char *aEventName)
 {
   nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(mWindow);
   if (!window) {
     return true;
   }
 
-  return window->DispatchCustomEvent(aEventName);
+  // The functions of nsContentUtils do not provide the required behavior,
+  // so the following is inlined.
+  nsIDOMDocument* doc = window->GetExtantDocument();
+  if (!doc) {
+    return true;
+  }
+
+  nsCOMPtr<nsIDOMEvent> event;
+  doc->CreateEvent(NS_LITERAL_STRING("Events"), getter_AddRefs(event));
+  NS_ENSURE_TRUE(NS_SUCCEEDED(event->InitEvent(NS_ConvertASCIItoUTF16(aEventName), true, true)), false);
+  event->SetTrusted(true);
+  event->GetInternalNSEvent()->mFlags.mOnlyChromeDispatch = true;
+
+  nsCOMPtr<nsIDOMEventTarget> target(do_QueryInterface(window));
+  bool defaultActionEnabled;
+  target->DispatchEvent(event, &defaultActionEnabled);
+  return defaultActionEnabled;
 }
-
--- a/embedding/components/windowwatcher/src/nsAutoWindowStateHelper.h
+++ b/embedding/components/windowwatcher/src/nsAutoWindowStateHelper.h
@@ -22,17 +22,17 @@ public:
   ~nsAutoWindowStateHelper();
 
   bool DefaultEnabled()
   {
     return mDefaultEnabled;
   }
 
 protected:
-  bool DispatchCustomEvent(const char *aEventName);
+  bool DispatchEventToChrome(const char *aEventName);
 
   nsIDOMWindow *mWindow;
   nsCOMPtr<nsIDOMWindow> mCallerWindow;
   bool mDefaultEnabled;
 };
 
 
 #endif
--- a/toolkit/components/prompts/src/nsPrompter.js
+++ b/toolkit/components/prompts/src/nsPrompter.js
@@ -167,22 +167,22 @@ let PromptUtils = {
             flags >>= 8;
         }
 
         return [buttonLabels[0], buttonLabels[1], buttonLabels[2], defaultButtonNum, isDelayEnabled];
     },
 
     // Fire a dialog open/close event. Used by tabbrowser to focus the
     // tab which is triggering a prompt.
-    //
-    // Bug 611553 - should make these notifications instead of events.
     fireDialogEvent : function (domWin, eventName) {
         let event = domWin.document.createEvent("Events");
         event.initEvent(eventName, true, true);
-        domWin.dispatchEvent(event);
+        let winUtils = domWin.QueryInterface(Ci.nsIInterfaceRequestor)
+                             .getInterface(Ci.nsIDOMWindowUtils);
+        winUtils.dispatchEventToChromeOnly(domWin, event);
     },
 
     getAuthInfo : function (authInfo) {
         let username, password;
 
         let flags = authInfo.flags;
         if (flags & Ci.nsIAuthInformation.NEED_DOMAIN && authInfo.domain)
             username = authInfo.domain + "\\" + authInfo.username;