1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/events/AsyncEventDispatcher.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,90 @@ 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 +#ifndef mozilla_AsyncEventDispatcher_h_ 1.10 +#define mozilla_AsyncEventDispatcher_h_ 1.11 + 1.12 +#include "mozilla/Attributes.h" 1.13 +#include "nsCOMPtr.h" 1.14 +#include "nsIDocument.h" 1.15 +#include "nsIDOMEvent.h" 1.16 +#include "nsINode.h" 1.17 +#include "nsString.h" 1.18 +#include "nsThreadUtils.h" 1.19 + 1.20 +namespace mozilla { 1.21 + 1.22 +/** 1.23 + * Use nsAsyncDOMEvent to fire a DOM event that requires safe a stable DOM. 1.24 + * For example, you may need to fire an event from within layout, but 1.25 + * want to ensure that the event handler doesn't mutate the DOM at 1.26 + * the wrong time, in order to avoid resulting instability. 1.27 + */ 1.28 + 1.29 +class AsyncEventDispatcher : public nsRunnable 1.30 +{ 1.31 +public: 1.32 + AsyncEventDispatcher(nsINode* aEventNode, const nsAString& aEventType, 1.33 + bool aBubbles, bool aDispatchChromeOnly) 1.34 + : mEventNode(aEventNode) 1.35 + , mEventType(aEventType) 1.36 + , mBubbles(aBubbles) 1.37 + , mDispatchChromeOnly(aDispatchChromeOnly) 1.38 + { 1.39 + } 1.40 + 1.41 + AsyncEventDispatcher(nsINode* aEventNode, nsIDOMEvent* aEvent) 1.42 + : mEventNode(aEventNode) 1.43 + , mEvent(aEvent) 1.44 + , mDispatchChromeOnly(false) 1.45 + { 1.46 + } 1.47 + 1.48 + AsyncEventDispatcher(nsINode* aEventNode, WidgetEvent& aEvent); 1.49 + 1.50 + NS_IMETHOD Run() MOZ_OVERRIDE; 1.51 + nsresult PostDOMEvent(); 1.52 + void RunDOMEventWhenSafe(); 1.53 + 1.54 + nsCOMPtr<nsINode> mEventNode; 1.55 + nsCOMPtr<nsIDOMEvent> mEvent; 1.56 + nsString mEventType; 1.57 + bool mBubbles; 1.58 + bool mDispatchChromeOnly; 1.59 +}; 1.60 + 1.61 +class LoadBlockingAsyncEventDispatcher MOZ_FINAL : public AsyncEventDispatcher 1.62 +{ 1.63 +public: 1.64 + LoadBlockingAsyncEventDispatcher(nsINode* aEventNode, 1.65 + const nsAString& aEventType, 1.66 + bool aBubbles, bool aDispatchChromeOnly) 1.67 + : AsyncEventDispatcher(aEventNode, aEventType, 1.68 + aBubbles, aDispatchChromeOnly) 1.69 + , mBlockedDoc(aEventNode->OwnerDoc()) 1.70 + { 1.71 + if (mBlockedDoc) { 1.72 + mBlockedDoc->BlockOnload(); 1.73 + } 1.74 + } 1.75 + 1.76 + LoadBlockingAsyncEventDispatcher(nsINode* aEventNode, nsIDOMEvent* aEvent) 1.77 + : AsyncEventDispatcher(aEventNode, aEvent) 1.78 + , mBlockedDoc(aEventNode->OwnerDoc()) 1.79 + { 1.80 + if (mBlockedDoc) { 1.81 + mBlockedDoc->BlockOnload(); 1.82 + } 1.83 + } 1.84 + 1.85 + ~LoadBlockingAsyncEventDispatcher(); 1.86 + 1.87 +private: 1.88 + nsCOMPtr<nsIDocument> mBlockedDoc; 1.89 +}; 1.90 + 1.91 +} // namespace mozilla 1.92 + 1.93 +#endif // mozilla_AsyncEventDispatcher_h_