michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsAutoWindowStateHelper.h" michael@0: michael@0: #include "mozilla/dom/Event.h" michael@0: #include "nsIDocument.h" michael@0: #include "nsIDOMEvent.h" michael@0: #include "nsIDOMWindow.h" michael@0: #include "nsPIDOMWindow.h" michael@0: #include "nsString.h" michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::dom; michael@0: michael@0: /**************************************************************** michael@0: ****************** nsAutoWindowStateHelper ********************* michael@0: ****************************************************************/ michael@0: michael@0: nsAutoWindowStateHelper::nsAutoWindowStateHelper(nsPIDOMWindow *aWindow) michael@0: : mWindow(aWindow), michael@0: mDefaultEnabled(DispatchEventToChrome("DOMWillOpenModalDialog")) michael@0: { michael@0: if (mWindow) { michael@0: mWindow->EnterModalState(); michael@0: } michael@0: } michael@0: michael@0: nsAutoWindowStateHelper::~nsAutoWindowStateHelper() michael@0: { michael@0: if (mWindow) { michael@0: mWindow->LeaveModalState(); michael@0: } michael@0: michael@0: if (mDefaultEnabled) { michael@0: DispatchEventToChrome("DOMModalDialogClosed"); michael@0: } michael@0: } michael@0: michael@0: bool michael@0: nsAutoWindowStateHelper::DispatchEventToChrome(const char *aEventName) michael@0: { michael@0: // XXXbz should we skip dispatching the event if the inner changed? michael@0: // That is, should we store both the inner and the outer? michael@0: if (!mWindow) { michael@0: return true; michael@0: } michael@0: michael@0: // The functions of nsContentUtils do not provide the required behavior, michael@0: // so the following is inlined. michael@0: nsIDocument* doc = mWindow->GetExtantDoc(); michael@0: if (!doc) { michael@0: return true; michael@0: } michael@0: michael@0: ErrorResult rv; michael@0: nsRefPtr event = doc->CreateEvent(NS_LITERAL_STRING("Events"), rv); michael@0: if (rv.Failed()) { michael@0: return false; michael@0: } michael@0: NS_ENSURE_TRUE(NS_SUCCEEDED(event->InitEvent(NS_ConvertASCIItoUTF16(aEventName), true, true)), false); michael@0: event->SetTrusted(true); michael@0: event->GetInternalNSEvent()->mFlags.mOnlyChromeDispatch = true; michael@0: michael@0: nsCOMPtr target = do_QueryInterface(mWindow); michael@0: bool defaultActionEnabled; michael@0: target->DispatchEvent(event, &defaultActionEnabled); michael@0: return defaultActionEnabled; michael@0: }