2015-05-11 23:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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 *********************
|
|
|
|
****************************************************************/
|
|
|
|
|
2015-05-11 12:35:14 -07:00
|
|
|
nsAutoWindowStateHelper::nsAutoWindowStateHelper(nsPIDOMWindow* aWindow)
|
|
|
|
: mWindow(aWindow)
|
|
|
|
, mDefaultEnabled(DispatchEventToChrome("DOMWillOpenModalDialog"))
|
2008-04-13 17:53:21 -07:00
|
|
|
{
|
2014-05-15 10:26:23 -07:00
|
|
|
if (mWindow) {
|
|
|
|
mWindow->EnterModalState();
|
2008-04-13 17:53:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoWindowStateHelper::~nsAutoWindowStateHelper()
|
|
|
|
{
|
2014-05-15 10:26:23 -07:00
|
|
|
if (mWindow) {
|
|
|
|
mWindow->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
|
2015-05-11 12:35:14 -07:00
|
|
|
nsAutoWindowStateHelper::DispatchEventToChrome(const char* aEventName)
|
2008-04-13 17:53:21 -07:00
|
|
|
{
|
2014-05-15 10:26:23 -07:00
|
|
|
// XXXbz should we skip dispatching the event if the inner changed?
|
|
|
|
// That is, should we store both the inner and the outer?
|
|
|
|
if (!mWindow) {
|
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-15 10:26:23 -07:00
|
|
|
nsIDocument* doc = mWindow->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;
|
|
|
|
}
|
2015-05-11 12:35:14 -07:00
|
|
|
NS_ENSURE_TRUE(NS_SUCCEEDED(event->InitEvent(
|
|
|
|
NS_ConvertASCIItoUTF16(aEventName), true, true)),
|
|
|
|
false);
|
2013-01-16 13:10:18 -08:00
|
|
|
event->SetTrusted(true);
|
|
|
|
event->GetInternalNSEvent()->mFlags.mOnlyChromeDispatch = true;
|
|
|
|
|
2014-05-15 10:26:23 -07:00
|
|
|
nsCOMPtr<EventTarget> target = do_QueryInterface(mWindow);
|
2013-01-16 13:10:18 -08:00
|
|
|
bool defaultActionEnabled;
|
|
|
|
target->DispatchEvent(event, &defaultActionEnabled);
|
|
|
|
return defaultActionEnabled;
|
|
|
|
}
|