1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/embedding/components/windowwatcher/src/nsAutoWindowStateHelper.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,71 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsAutoWindowStateHelper.h" 1.10 + 1.11 +#include "mozilla/dom/Event.h" 1.12 +#include "nsIDocument.h" 1.13 +#include "nsIDOMEvent.h" 1.14 +#include "nsIDOMWindow.h" 1.15 +#include "nsPIDOMWindow.h" 1.16 +#include "nsString.h" 1.17 + 1.18 +using namespace mozilla; 1.19 +using namespace mozilla::dom; 1.20 + 1.21 +/**************************************************************** 1.22 + ****************** nsAutoWindowStateHelper ********************* 1.23 + ****************************************************************/ 1.24 + 1.25 +nsAutoWindowStateHelper::nsAutoWindowStateHelper(nsPIDOMWindow *aWindow) 1.26 + : mWindow(aWindow), 1.27 + mDefaultEnabled(DispatchEventToChrome("DOMWillOpenModalDialog")) 1.28 +{ 1.29 + if (mWindow) { 1.30 + mWindow->EnterModalState(); 1.31 + } 1.32 +} 1.33 + 1.34 +nsAutoWindowStateHelper::~nsAutoWindowStateHelper() 1.35 +{ 1.36 + if (mWindow) { 1.37 + mWindow->LeaveModalState(); 1.38 + } 1.39 + 1.40 + if (mDefaultEnabled) { 1.41 + DispatchEventToChrome("DOMModalDialogClosed"); 1.42 + } 1.43 +} 1.44 + 1.45 +bool 1.46 +nsAutoWindowStateHelper::DispatchEventToChrome(const char *aEventName) 1.47 +{ 1.48 + // XXXbz should we skip dispatching the event if the inner changed? 1.49 + // That is, should we store both the inner and the outer? 1.50 + if (!mWindow) { 1.51 + return true; 1.52 + } 1.53 + 1.54 + // The functions of nsContentUtils do not provide the required behavior, 1.55 + // so the following is inlined. 1.56 + nsIDocument* doc = mWindow->GetExtantDoc(); 1.57 + if (!doc) { 1.58 + return true; 1.59 + } 1.60 + 1.61 + ErrorResult rv; 1.62 + nsRefPtr<Event> event = doc->CreateEvent(NS_LITERAL_STRING("Events"), rv); 1.63 + if (rv.Failed()) { 1.64 + return false; 1.65 + } 1.66 + NS_ENSURE_TRUE(NS_SUCCEEDED(event->InitEvent(NS_ConvertASCIItoUTF16(aEventName), true, true)), false); 1.67 + event->SetTrusted(true); 1.68 + event->GetInternalNSEvent()->mFlags.mOnlyChromeDispatch = true; 1.69 + 1.70 + nsCOMPtr<EventTarget> target = do_QueryInterface(mWindow); 1.71 + bool defaultActionEnabled; 1.72 + target->DispatchEvent(event, &defaultActionEnabled); 1.73 + return defaultActionEnabled; 1.74 +}