1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/events/AsyncEventDispatcher.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,96 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "mozilla/AsyncEventDispatcher.h" 1.10 +#include "mozilla/BasicEvents.h" 1.11 +#include "mozilla/EventDispatcher.h" 1.12 +#include "mozilla/dom/Event.h" // for nsIDOMEvent::InternalDOMEvent() 1.13 +#include "mozilla/dom/EventTarget.h" 1.14 +#include "nsContentUtils.h" 1.15 +#include "nsIDOMEvent.h" 1.16 + 1.17 +namespace mozilla { 1.18 + 1.19 +using namespace dom; 1.20 + 1.21 +/****************************************************************************** 1.22 + * mozilla::AsyncEventDispatcher 1.23 + ******************************************************************************/ 1.24 + 1.25 +AsyncEventDispatcher::AsyncEventDispatcher(nsINode* aEventNode, 1.26 + WidgetEvent& aEvent) 1.27 + : mEventNode(aEventNode) 1.28 + , mDispatchChromeOnly(false) 1.29 +{ 1.30 + MOZ_ASSERT(mEventNode); 1.31 + EventDispatcher::CreateEvent(aEventNode, nullptr, &aEvent, EmptyString(), 1.32 + getter_AddRefs(mEvent)); 1.33 + NS_ASSERTION(mEvent, "Should never fail to create an event"); 1.34 + mEvent->DuplicatePrivateData(); 1.35 + mEvent->SetTrusted(aEvent.mFlags.mIsTrusted); 1.36 +} 1.37 + 1.38 +NS_IMETHODIMP 1.39 +AsyncEventDispatcher::Run() 1.40 +{ 1.41 + if (mEvent) { 1.42 + if (mDispatchChromeOnly) { 1.43 + MOZ_ASSERT(mEvent->InternalDOMEvent()->IsTrusted()); 1.44 + 1.45 + nsCOMPtr<nsIDocument> ownerDoc = mEventNode->OwnerDoc(); 1.46 + nsPIDOMWindow* window = ownerDoc->GetWindow(); 1.47 + if (!window) { 1.48 + return NS_ERROR_INVALID_ARG; 1.49 + } 1.50 + 1.51 + nsCOMPtr<EventTarget> target = window->GetParentTarget(); 1.52 + if (!target) { 1.53 + return NS_ERROR_INVALID_ARG; 1.54 + } 1.55 + EventDispatcher::DispatchDOMEvent(target, nullptr, mEvent, 1.56 + nullptr, nullptr); 1.57 + } else { 1.58 + nsCOMPtr<EventTarget> target = mEventNode.get(); 1.59 + bool defaultActionEnabled; // This is not used because the caller is async 1.60 + target->DispatchEvent(mEvent, &defaultActionEnabled); 1.61 + } 1.62 + } else { 1.63 + nsIDocument* doc = mEventNode->OwnerDoc(); 1.64 + if (mDispatchChromeOnly) { 1.65 + nsContentUtils::DispatchChromeEvent(doc, mEventNode, mEventType, 1.66 + mBubbles, false); 1.67 + } else { 1.68 + nsContentUtils::DispatchTrustedEvent(doc, mEventNode, mEventType, 1.69 + mBubbles, false); 1.70 + } 1.71 + } 1.72 + 1.73 + return NS_OK; 1.74 +} 1.75 + 1.76 +nsresult 1.77 +AsyncEventDispatcher::PostDOMEvent() 1.78 +{ 1.79 + return NS_DispatchToCurrentThread(this); 1.80 +} 1.81 + 1.82 +void 1.83 +AsyncEventDispatcher::RunDOMEventWhenSafe() 1.84 +{ 1.85 + nsContentUtils::AddScriptRunner(this); 1.86 +} 1.87 + 1.88 +/****************************************************************************** 1.89 + * mozilla::LoadBlockingAsyncEventDispatcher 1.90 + ******************************************************************************/ 1.91 + 1.92 +LoadBlockingAsyncEventDispatcher::~LoadBlockingAsyncEventDispatcher() 1.93 +{ 1.94 + if (mBlockedDoc) { 1.95 + mBlockedDoc->UnblockOnload(true); 1.96 + } 1.97 +} 1.98 + 1.99 +} // namespace mozilla