|
1 /* -*- Mode: C++; tab-width: 2; 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 |
|
6 #include "nsAutoWindowStateHelper.h" |
|
7 |
|
8 #include "mozilla/dom/Event.h" |
|
9 #include "nsIDocument.h" |
|
10 #include "nsIDOMEvent.h" |
|
11 #include "nsIDOMWindow.h" |
|
12 #include "nsPIDOMWindow.h" |
|
13 #include "nsString.h" |
|
14 |
|
15 using namespace mozilla; |
|
16 using namespace mozilla::dom; |
|
17 |
|
18 /**************************************************************** |
|
19 ****************** nsAutoWindowStateHelper ********************* |
|
20 ****************************************************************/ |
|
21 |
|
22 nsAutoWindowStateHelper::nsAutoWindowStateHelper(nsPIDOMWindow *aWindow) |
|
23 : mWindow(aWindow), |
|
24 mDefaultEnabled(DispatchEventToChrome("DOMWillOpenModalDialog")) |
|
25 { |
|
26 if (mWindow) { |
|
27 mWindow->EnterModalState(); |
|
28 } |
|
29 } |
|
30 |
|
31 nsAutoWindowStateHelper::~nsAutoWindowStateHelper() |
|
32 { |
|
33 if (mWindow) { |
|
34 mWindow->LeaveModalState(); |
|
35 } |
|
36 |
|
37 if (mDefaultEnabled) { |
|
38 DispatchEventToChrome("DOMModalDialogClosed"); |
|
39 } |
|
40 } |
|
41 |
|
42 bool |
|
43 nsAutoWindowStateHelper::DispatchEventToChrome(const char *aEventName) |
|
44 { |
|
45 // XXXbz should we skip dispatching the event if the inner changed? |
|
46 // That is, should we store both the inner and the outer? |
|
47 if (!mWindow) { |
|
48 return true; |
|
49 } |
|
50 |
|
51 // The functions of nsContentUtils do not provide the required behavior, |
|
52 // so the following is inlined. |
|
53 nsIDocument* doc = mWindow->GetExtantDoc(); |
|
54 if (!doc) { |
|
55 return true; |
|
56 } |
|
57 |
|
58 ErrorResult rv; |
|
59 nsRefPtr<Event> event = doc->CreateEvent(NS_LITERAL_STRING("Events"), rv); |
|
60 if (rv.Failed()) { |
|
61 return false; |
|
62 } |
|
63 NS_ENSURE_TRUE(NS_SUCCEEDED(event->InitEvent(NS_ConvertASCIItoUTF16(aEventName), true, true)), false); |
|
64 event->SetTrusted(true); |
|
65 event->GetInternalNSEvent()->mFlags.mOnlyChromeDispatch = true; |
|
66 |
|
67 nsCOMPtr<EventTarget> target = do_QueryInterface(mWindow); |
|
68 bool defaultActionEnabled; |
|
69 target->DispatchEvent(event, &defaultActionEnabled); |
|
70 return defaultActionEnabled; |
|
71 } |