nsDSURIContentListener.h (3169B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef nsDSURIContentListener_h__ 8 #define nsDSURIContentListener_h__ 9 10 #include "nsCOMPtr.h" 11 #include "nsIURIContentListener.h" 12 #include "nsWeakReference.h" 13 #include "nsITimer.h" 14 #include "mozilla/WeakPtr.h" 15 #include "nsDocShell.h" 16 17 class nsIInterfaceRequestor; 18 class nsIWebNavigationInfo; 19 class nsPIDOMWindowOuter; 20 21 // Helper Class to eventually close an already opened window 22 class MaybeCloseWindowHelper final : public nsITimerCallback, public nsINamed { 23 public: 24 NS_DECL_ISUPPORTS 25 NS_DECL_NSITIMERCALLBACK 26 NS_DECL_NSINAMED 27 28 explicit MaybeCloseWindowHelper( 29 mozilla::dom::BrowsingContext* aContentContext); 30 31 /** 32 * Closes the provided window async (if mShouldCloseWindow is true) and 33 * returns a valid browsingContext to be used instead as parent for dialogs or 34 * similar things. 35 * In case mShouldCloseWindow is true, the returned BrowsingContext will be 36 * the window's opener (or original cross-group opener in the case of a 37 * `noopener` popup). 38 */ 39 mozilla::dom::BrowsingContext* MaybeCloseWindow(); 40 41 void SetShouldCloseWindow(bool aShouldCloseWindow); 42 43 protected: 44 ~MaybeCloseWindowHelper(); 45 46 private: 47 already_AddRefed<mozilla::dom::BrowsingContext> ChooseNewBrowsingContext( 48 mozilla::dom::BrowsingContext* aBC); 49 50 /** 51 * The dom window associated to handle content. 52 */ 53 RefPtr<mozilla::dom::BrowsingContext> mBrowsingContext; 54 55 /** 56 * Used to close the window on a timer, to avoid any exceptions that are 57 * thrown if we try to close the window before it's fully loaded. 58 */ 59 RefPtr<mozilla::dom::BrowsingContext> mBCToClose; 60 nsCOMPtr<nsITimer> mTimer; 61 62 /** 63 * This is set based on whether the channel indicates that a new window 64 * was opened, e.g. for a download, or was blocked. If so, then we 65 * close it. 66 */ 67 bool mShouldCloseWindow; 68 }; 69 70 class nsDSURIContentListener final : public nsIURIContentListener, 71 public nsSupportsWeakReference { 72 friend class nsDocShell; 73 74 public: 75 NS_DECL_THREADSAFE_ISUPPORTS 76 NS_DECL_NSIURICONTENTLISTENER 77 78 protected: 79 explicit nsDSURIContentListener(nsDocShell* aDocShell); 80 virtual ~nsDSURIContentListener(); 81 82 void DropDocShellReference() { 83 mDocShell = nullptr; 84 mExistingJPEGRequest = nullptr; 85 mExistingJPEGStreamListener = nullptr; 86 } 87 88 protected: 89 mozilla::MainThreadWeakPtr<nsDocShell> mDocShell; 90 // Hack to handle multipart images without creating a new viewer 91 nsCOMPtr<nsIStreamListener> mExistingJPEGStreamListener; 92 nsCOMPtr<nsIChannel> mExistingJPEGRequest; 93 94 // Store the parent listener in either of these depending on 95 // if supports weak references or not. Proper weak refs are 96 // preferred and encouraged! 97 nsWeakPtr mWeakParentContentListener; 98 nsIURIContentListener* mParentContentListener; 99 }; 100 101 #endif /* nsDSURIContentListener_h__ */