Bug 611553 - Make DOMWillOpenModalDialog a chrome-only event. r=dolske r=smaug a=akeybl
authorFrank Yan <fyan@mozilla.com>
Wed, 16 Jan 2013 13:10:18 -0800
changeset 123891 46d904f2f2525a38e4c7f8859407cdc56024292f
parent 123890 df5345414bb18ad5ad5d36e55cb7c861631f1c19
child 123892 c7115618ac6ee8dc86f275620a76210639a8f214
push id3307
push userfyan@mozilla.com
push dateTue, 05 Feb 2013 21:13:42 +0000
treeherdermozilla-aurora@c7115618ac6e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdolske, smaug, akeybl
bugs611553
milestone20.0a2
Bug 611553 - Make DOMWillOpenModalDialog a chrome-only event. r=dolske r=smaug a=akeybl
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;