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