2011-07-11 06:14:08 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2008-04-13 17:53:21 -07:00
|
|
|
|
|
|
|
#include "nsAutoWindowStateHelper.h"
|
|
|
|
|
2014-03-04 16:37:43 -08:00
|
|
|
#include "mozilla/dom/Event.h"
|
2013-04-23 21:22:37 -07:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDOMEvent.h"
|
2008-04-13 17:53:21 -07:00
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
|
2013-04-23 21:22:37 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2008-04-13 17:53:21 -07:00
|
|
|
/****************************************************************
|
|
|
|
****************** nsAutoWindowStateHelper *********************
|
|
|
|
****************************************************************/
|
|
|
|
|
2014-05-16 09:29:37 -07:00
|
|
|
nsAutoWindowStateHelper::nsAutoWindowStateHelper(nsIDOMWindow *aWindow)
|
2008-04-13 17:53:21 -07:00
|
|
|
: mWindow(aWindow),
|
2013-01-16 13:10:18 -08:00
|
|
|
mDefaultEnabled(DispatchEventToChrome("DOMWillOpenModalDialog"))
|
2008-04-13 17:53:21 -07:00
|
|
|
{
|
2014-05-16 09:29:37 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(aWindow));
|
|
|
|
|
|
|
|
if (window) {
|
|
|
|
window->EnterModalState();
|
2008-04-13 17:53:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoWindowStateHelper::~nsAutoWindowStateHelper()
|
|
|
|
{
|
2014-05-16 09:29:37 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(mWindow));
|
|
|
|
|
|
|
|
if (window) {
|
|
|
|
window->LeaveModalState();
|
2008-04-13 17:53:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mDefaultEnabled) {
|
2013-01-16 13:10:18 -08:00
|
|
|
DispatchEventToChrome("DOMModalDialogClosed");
|
2008-04-13 17:53:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2013-01-16 13:10:18 -08:00
|
|
|
nsAutoWindowStateHelper::DispatchEventToChrome(const char *aEventName)
|
2008-04-13 17:53:21 -07:00
|
|
|
{
|
2014-05-16 09:29:37 -07:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(mWindow);
|
|
|
|
if (!window || (window->IsInnerWindow() && !window->IsCurrentInnerWindow())) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2008-04-13 17:53:21 -07:00
|
|
|
}
|
|
|
|
|
2013-01-16 13:10:18 -08:00
|
|
|
// The functions of nsContentUtils do not provide the required behavior,
|
|
|
|
// so the following is inlined.
|
2014-05-16 09:29:37 -07:00
|
|
|
nsIDocument* doc = window->GetExtantDoc();
|
2013-01-16 13:10:18 -08:00
|
|
|
if (!doc) {
|
|
|
|
return true;
|
|
|
|
}
|
2008-04-13 17:53:21 -07:00
|
|
|
|
2013-04-23 21:22:37 -07:00
|
|
|
ErrorResult rv;
|
2014-03-04 16:37:43 -08:00
|
|
|
nsRefPtr<Event> event = doc->CreateEvent(NS_LITERAL_STRING("Events"), rv);
|
2013-04-23 21:22:37 -07:00
|
|
|
if (rv.Failed()) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-16 13:10:18 -08:00
|
|
|
NS_ENSURE_TRUE(NS_SUCCEEDED(event->InitEvent(NS_ConvertASCIItoUTF16(aEventName), true, true)), false);
|
|
|
|
event->SetTrusted(true);
|
|
|
|
event->GetInternalNSEvent()->mFlags.mOnlyChromeDispatch = true;
|
|
|
|
|
2014-05-16 09:29:37 -07:00
|
|
|
nsCOMPtr<EventTarget> target = do_QueryInterface(window);
|
2013-01-16 13:10:18 -08:00
|
|
|
bool defaultActionEnabled;
|
|
|
|
target->DispatchEvent(event, &defaultActionEnabled);
|
|
|
|
return defaultActionEnabled;
|
|
|
|
}
|