|
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 #ifndef mozilla_AsyncEventDispatcher_h_ |
|
7 #define mozilla_AsyncEventDispatcher_h_ |
|
8 |
|
9 #include "mozilla/Attributes.h" |
|
10 #include "nsCOMPtr.h" |
|
11 #include "nsIDocument.h" |
|
12 #include "nsIDOMEvent.h" |
|
13 #include "nsINode.h" |
|
14 #include "nsString.h" |
|
15 #include "nsThreadUtils.h" |
|
16 |
|
17 namespace mozilla { |
|
18 |
|
19 /** |
|
20 * Use nsAsyncDOMEvent to fire a DOM event that requires safe a stable DOM. |
|
21 * For example, you may need to fire an event from within layout, but |
|
22 * want to ensure that the event handler doesn't mutate the DOM at |
|
23 * the wrong time, in order to avoid resulting instability. |
|
24 */ |
|
25 |
|
26 class AsyncEventDispatcher : public nsRunnable |
|
27 { |
|
28 public: |
|
29 AsyncEventDispatcher(nsINode* aEventNode, const nsAString& aEventType, |
|
30 bool aBubbles, bool aDispatchChromeOnly) |
|
31 : mEventNode(aEventNode) |
|
32 , mEventType(aEventType) |
|
33 , mBubbles(aBubbles) |
|
34 , mDispatchChromeOnly(aDispatchChromeOnly) |
|
35 { |
|
36 } |
|
37 |
|
38 AsyncEventDispatcher(nsINode* aEventNode, nsIDOMEvent* aEvent) |
|
39 : mEventNode(aEventNode) |
|
40 , mEvent(aEvent) |
|
41 , mDispatchChromeOnly(false) |
|
42 { |
|
43 } |
|
44 |
|
45 AsyncEventDispatcher(nsINode* aEventNode, WidgetEvent& aEvent); |
|
46 |
|
47 NS_IMETHOD Run() MOZ_OVERRIDE; |
|
48 nsresult PostDOMEvent(); |
|
49 void RunDOMEventWhenSafe(); |
|
50 |
|
51 nsCOMPtr<nsINode> mEventNode; |
|
52 nsCOMPtr<nsIDOMEvent> mEvent; |
|
53 nsString mEventType; |
|
54 bool mBubbles; |
|
55 bool mDispatchChromeOnly; |
|
56 }; |
|
57 |
|
58 class LoadBlockingAsyncEventDispatcher MOZ_FINAL : public AsyncEventDispatcher |
|
59 { |
|
60 public: |
|
61 LoadBlockingAsyncEventDispatcher(nsINode* aEventNode, |
|
62 const nsAString& aEventType, |
|
63 bool aBubbles, bool aDispatchChromeOnly) |
|
64 : AsyncEventDispatcher(aEventNode, aEventType, |
|
65 aBubbles, aDispatchChromeOnly) |
|
66 , mBlockedDoc(aEventNode->OwnerDoc()) |
|
67 { |
|
68 if (mBlockedDoc) { |
|
69 mBlockedDoc->BlockOnload(); |
|
70 } |
|
71 } |
|
72 |
|
73 LoadBlockingAsyncEventDispatcher(nsINode* aEventNode, nsIDOMEvent* aEvent) |
|
74 : AsyncEventDispatcher(aEventNode, aEvent) |
|
75 , mBlockedDoc(aEventNode->OwnerDoc()) |
|
76 { |
|
77 if (mBlockedDoc) { |
|
78 mBlockedDoc->BlockOnload(); |
|
79 } |
|
80 } |
|
81 |
|
82 ~LoadBlockingAsyncEventDispatcher(); |
|
83 |
|
84 private: |
|
85 nsCOMPtr<nsIDocument> mBlockedDoc; |
|
86 }; |
|
87 |
|
88 } // namespace mozilla |
|
89 |
|
90 #endif // mozilla_AsyncEventDispatcher_h_ |