Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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/. */
6 #include "mozilla/AsyncEventDispatcher.h"
7 #include "mozilla/BasicEvents.h"
8 #include "mozilla/EventDispatcher.h"
9 #include "mozilla/dom/Event.h" // for nsIDOMEvent::InternalDOMEvent()
10 #include "mozilla/dom/EventTarget.h"
11 #include "nsContentUtils.h"
12 #include "nsIDOMEvent.h"
14 namespace mozilla {
16 using namespace dom;
18 /******************************************************************************
19 * mozilla::AsyncEventDispatcher
20 ******************************************************************************/
22 AsyncEventDispatcher::AsyncEventDispatcher(nsINode* aEventNode,
23 WidgetEvent& aEvent)
24 : mEventNode(aEventNode)
25 , mDispatchChromeOnly(false)
26 {
27 MOZ_ASSERT(mEventNode);
28 EventDispatcher::CreateEvent(aEventNode, nullptr, &aEvent, EmptyString(),
29 getter_AddRefs(mEvent));
30 NS_ASSERTION(mEvent, "Should never fail to create an event");
31 mEvent->DuplicatePrivateData();
32 mEvent->SetTrusted(aEvent.mFlags.mIsTrusted);
33 }
35 NS_IMETHODIMP
36 AsyncEventDispatcher::Run()
37 {
38 if (mEvent) {
39 if (mDispatchChromeOnly) {
40 MOZ_ASSERT(mEvent->InternalDOMEvent()->IsTrusted());
42 nsCOMPtr<nsIDocument> ownerDoc = mEventNode->OwnerDoc();
43 nsPIDOMWindow* window = ownerDoc->GetWindow();
44 if (!window) {
45 return NS_ERROR_INVALID_ARG;
46 }
48 nsCOMPtr<EventTarget> target = window->GetParentTarget();
49 if (!target) {
50 return NS_ERROR_INVALID_ARG;
51 }
52 EventDispatcher::DispatchDOMEvent(target, nullptr, mEvent,
53 nullptr, nullptr);
54 } else {
55 nsCOMPtr<EventTarget> target = mEventNode.get();
56 bool defaultActionEnabled; // This is not used because the caller is async
57 target->DispatchEvent(mEvent, &defaultActionEnabled);
58 }
59 } else {
60 nsIDocument* doc = mEventNode->OwnerDoc();
61 if (mDispatchChromeOnly) {
62 nsContentUtils::DispatchChromeEvent(doc, mEventNode, mEventType,
63 mBubbles, false);
64 } else {
65 nsContentUtils::DispatchTrustedEvent(doc, mEventNode, mEventType,
66 mBubbles, false);
67 }
68 }
70 return NS_OK;
71 }
73 nsresult
74 AsyncEventDispatcher::PostDOMEvent()
75 {
76 return NS_DispatchToCurrentThread(this);
77 }
79 void
80 AsyncEventDispatcher::RunDOMEventWhenSafe()
81 {
82 nsContentUtils::AddScriptRunner(this);
83 }
85 /******************************************************************************
86 * mozilla::LoadBlockingAsyncEventDispatcher
87 ******************************************************************************/
89 LoadBlockingAsyncEventDispatcher::~LoadBlockingAsyncEventDispatcher()
90 {
91 if (mBlockedDoc) {
92 mBlockedDoc->UnblockOnload(true);
93 }
94 }
96 } // namespace mozilla