nsAutoWindowStateHelper.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsAutoWindowStateHelper.h"
  6. #include "mozilla/dom/Event.h"
  7. #include "nsIDocument.h"
  8. #include "nsIDOMEvent.h"
  9. #include "nsIDOMWindow.h"
  10. #include "nsPIDOMWindow.h"
  11. #include "nsString.h"
  12. using namespace mozilla;
  13. using namespace mozilla::dom;
  14. /****************************************************************
  15. ****************** nsAutoWindowStateHelper *********************
  16. ****************************************************************/
  17. nsAutoWindowStateHelper::nsAutoWindowStateHelper(nsPIDOMWindowOuter* aWindow)
  18. : mWindow(aWindow)
  19. , mDefaultEnabled(DispatchEventToChrome("DOMWillOpenModalDialog"))
  20. {
  21. if (mWindow) {
  22. mWindow->EnterModalState();
  23. }
  24. }
  25. nsAutoWindowStateHelper::~nsAutoWindowStateHelper()
  26. {
  27. if (mWindow) {
  28. mWindow->LeaveModalState();
  29. }
  30. if (mDefaultEnabled) {
  31. DispatchEventToChrome("DOMModalDialogClosed");
  32. }
  33. }
  34. bool
  35. nsAutoWindowStateHelper::DispatchEventToChrome(const char* aEventName)
  36. {
  37. // XXXbz should we skip dispatching the event if the inner changed?
  38. // That is, should we store both the inner and the outer?
  39. if (!mWindow) {
  40. return true;
  41. }
  42. // The functions of nsContentUtils do not provide the required behavior,
  43. // so the following is inlined.
  44. nsIDocument* doc = mWindow->GetExtantDoc();
  45. if (!doc) {
  46. return true;
  47. }
  48. ErrorResult rv;
  49. RefPtr<Event> event = doc->CreateEvent(NS_LITERAL_STRING("Events"), rv);
  50. if (rv.Failed()) {
  51. rv.SuppressException();
  52. return false;
  53. }
  54. event->InitEvent(NS_ConvertASCIItoUTF16(aEventName), true, true);
  55. event->SetTrusted(true);
  56. event->WidgetEventPtr()->mFlags.mOnlyChromeDispatch = true;
  57. nsCOMPtr<EventTarget> target = do_QueryInterface(mWindow);
  58. bool defaultActionEnabled;
  59. target->DispatchEvent(event, &defaultActionEnabled);
  60. return defaultActionEnabled;
  61. }