nsDocShellTreeOwner.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* -*- Mode: C++; tab-width: 8; 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. #ifndef nsDocShellTreeOwner_h__
  6. #define nsDocShellTreeOwner_h__
  7. // Helper Classes
  8. #include "nsCOMPtr.h"
  9. #include "nsString.h"
  10. // Interfaces Needed
  11. #include "nsIBaseWindow.h"
  12. #include "nsIDocShellTreeOwner.h"
  13. #include "nsIInterfaceRequestor.h"
  14. #include "nsIInterfaceRequestorUtils.h"
  15. #include "nsIWebBrowserChrome.h"
  16. #include "nsIDOMEventListener.h"
  17. #include "nsIEmbeddingSiteWindow.h"
  18. #include "nsIWebProgressListener.h"
  19. #include "nsWeakReference.h"
  20. #include "nsITimer.h"
  21. #include "nsIPrompt.h"
  22. #include "nsIAuthPrompt.h"
  23. #include "nsITooltipListener.h"
  24. #include "nsITooltipTextProvider.h"
  25. #include "nsCTooltipTextProvider.h"
  26. #include "nsIDroppedLinkHandler.h"
  27. #include "nsCommandHandler.h"
  28. namespace mozilla {
  29. namespace dom {
  30. class EventTarget;
  31. } // namespace dom
  32. } // namespace mozilla
  33. class nsWebBrowser;
  34. class ChromeTooltipListener;
  35. class ChromeContextMenuListener;
  36. // {6D10C180-6888-11d4-952B-0020183BF181}
  37. #define NS_ICDOCSHELLTREEOWNER_IID \
  38. { 0x6d10c180, 0x6888, 0x11d4, { 0x95, 0x2b, 0x0, 0x20, 0x18, 0x3b, 0xf1, 0x81 } }
  39. // This is a fake 'hidden' interface that nsDocShellTreeOwner implements.
  40. // Classes such as nsCommandHandler can QI for this interface to be sure that
  41. // they're dealing with a valid nsDocShellTreeOwner and not some other object
  42. // that implements nsIDocShellTreeOwner.
  43. class nsICDocShellTreeOwner : public nsISupports
  44. {
  45. public:
  46. NS_DECLARE_STATIC_IID_ACCESSOR(NS_ICDOCSHELLTREEOWNER_IID)
  47. };
  48. NS_DEFINE_STATIC_IID_ACCESSOR(nsICDocShellTreeOwner, NS_ICDOCSHELLTREEOWNER_IID)
  49. class nsDocShellTreeOwner final : public nsIDocShellTreeOwner,
  50. public nsIBaseWindow,
  51. public nsIInterfaceRequestor,
  52. public nsIWebProgressListener,
  53. public nsIDOMEventListener,
  54. public nsICDocShellTreeOwner,
  55. public nsSupportsWeakReference
  56. {
  57. friend class nsWebBrowser;
  58. friend class nsCommandHandler;
  59. public:
  60. NS_DECL_ISUPPORTS
  61. NS_DECL_NSIBASEWINDOW
  62. NS_DECL_NSIDOCSHELLTREEOWNER
  63. NS_DECL_NSIDOMEVENTLISTENER
  64. NS_DECL_NSIINTERFACEREQUESTOR
  65. NS_DECL_NSIWEBPROGRESSLISTENER
  66. protected:
  67. nsDocShellTreeOwner();
  68. virtual ~nsDocShellTreeOwner();
  69. void WebBrowser(nsWebBrowser* aWebBrowser);
  70. nsWebBrowser* WebBrowser();
  71. NS_IMETHOD SetTreeOwner(nsIDocShellTreeOwner* aTreeOwner);
  72. NS_IMETHOD SetWebBrowserChrome(nsIWebBrowserChrome* aWebBrowserChrome);
  73. NS_IMETHOD AddChromeListeners();
  74. NS_IMETHOD RemoveChromeListeners();
  75. nsresult FindItemWithNameAcrossWindows(
  76. const char16_t* aName,
  77. nsIDocShellTreeItem* aRequestor, nsIDocShellTreeItem* aOriginalRequestor,
  78. nsIDocShellTreeItem** aFoundItem);
  79. void EnsurePrompter();
  80. void EnsureAuthPrompter();
  81. void AddToWatcher();
  82. void RemoveFromWatcher();
  83. void EnsureContentTreeOwner();
  84. // These helper functions return the correct instances of the requested
  85. // interfaces. If the object passed to SetWebBrowserChrome() implements
  86. // nsISupportsWeakReference, then these functions call QueryReferent on
  87. // that object. Otherwise, they return an addrefed pointer. If the
  88. // WebBrowserChrome object doesn't exist, they return nullptr.
  89. already_AddRefed<nsIWebBrowserChrome> GetWebBrowserChrome();
  90. already_AddRefed<nsIEmbeddingSiteWindow> GetOwnerWin();
  91. already_AddRefed<nsIInterfaceRequestor> GetOwnerRequestor();
  92. protected:
  93. // Weak References
  94. nsWebBrowser* mWebBrowser;
  95. nsIDocShellTreeOwner* mTreeOwner;
  96. nsIDocShellTreeItem* mPrimaryContentShell;
  97. nsIWebBrowserChrome* mWebBrowserChrome;
  98. nsIEmbeddingSiteWindow* mOwnerWin;
  99. nsIInterfaceRequestor* mOwnerRequestor;
  100. nsWeakPtr mWebBrowserChromeWeak; // nsIWebBrowserChrome
  101. // the objects that listen for chrome events like context menus and tooltips.
  102. // They are separate objects to avoid circular references between |this|
  103. // and the DOM.
  104. RefPtr<ChromeTooltipListener> mChromeTooltipListener;
  105. RefPtr<ChromeContextMenuListener> mChromeContextMenuListener;
  106. RefPtr<nsDocShellTreeOwner> mContentTreeOwner;
  107. nsCOMPtr<nsIPrompt> mPrompter;
  108. nsCOMPtr<nsIAuthPrompt> mAuthPrompter;
  109. nsCOMPtr<nsITabParent> mPrimaryTabParent;
  110. };
  111. // The class that listens to the chrome events and tells the embedding chrome to
  112. // show tooltips, as appropriate. Handles registering itself with the DOM with
  113. // AddChromeListeners() and removing itself with RemoveChromeListeners().
  114. class ChromeTooltipListener final : public nsIDOMEventListener
  115. {
  116. protected:
  117. virtual ~ChromeTooltipListener();
  118. public:
  119. NS_DECL_ISUPPORTS
  120. ChromeTooltipListener(nsWebBrowser* aInBrowser, nsIWebBrowserChrome* aInChrome);
  121. NS_IMETHOD HandleEvent(nsIDOMEvent* aEvent) override;
  122. NS_IMETHOD MouseMove(nsIDOMEvent* aMouseEvent);
  123. // Add/remove the relevant listeners, based on what interfaces the embedding
  124. // chrome implements.
  125. NS_IMETHOD AddChromeListeners();
  126. NS_IMETHOD RemoveChromeListeners();
  127. private:
  128. // various delays for tooltips
  129. enum
  130. {
  131. kTooltipAutoHideTime = 5000, // ms
  132. kTooltipMouseMoveTolerance = 7 // pixel tolerance for mousemove event
  133. };
  134. NS_IMETHOD AddTooltipListener();
  135. NS_IMETHOD RemoveTooltipListener();
  136. NS_IMETHOD ShowTooltip(int32_t aInXCoords, int32_t aInYCoords,
  137. const nsAString& aInTipText,
  138. const nsAString& aDirText);
  139. NS_IMETHOD HideTooltip();
  140. nsWebBrowser* mWebBrowser;
  141. nsCOMPtr<mozilla::dom::EventTarget> mEventTarget;
  142. nsCOMPtr<nsITooltipTextProvider> mTooltipTextProvider;
  143. // This must be a strong ref in order to make sure we can hide the tooltip if
  144. // the window goes away while we're displaying one. If we don't hold a strong
  145. // ref, the chrome might have been disposed of before we get a chance to tell
  146. // it, and no one would ever tell us of that fact.
  147. nsCOMPtr<nsIWebBrowserChrome> mWebBrowserChrome;
  148. bool mTooltipListenerInstalled;
  149. nsCOMPtr<nsITimer> mTooltipTimer;
  150. static void sTooltipCallback(nsITimer* aTimer, void* aListener);
  151. // Mouse coordinates for last mousemove event we saw
  152. int32_t mMouseClientX;
  153. int32_t mMouseClientY;
  154. // Mouse coordinates for tooltip event
  155. int32_t mMouseScreenX;
  156. int32_t mMouseScreenY;
  157. bool mShowingTooltip;
  158. bool mTooltipShownOnce;
  159. // The node hovered over that fired the timer. This may turn into the node
  160. // that triggered the tooltip, but only if the timer ever gets around to
  161. // firing. This is a strong reference, because the tooltip content can be
  162. // destroyed while we're waiting for the tooltip to pup up, and we need to
  163. // detect that. It's set only when the tooltip timer is created and launched.
  164. // The timer must either fire or be cancelled (or possibly released?), and we
  165. // release this reference in each of those cases. So we don't leak.
  166. nsCOMPtr<nsIDOMNode> mPossibleTooltipNode;
  167. };
  168. // The class that listens to the chrome events and tells the embedding chrome to
  169. // show context menus, as appropriate. Handles registering itself with the DOM
  170. // with AddChromeListeners() and removing itself with RemoveChromeListeners().
  171. class ChromeContextMenuListener : public nsIDOMEventListener
  172. {
  173. protected:
  174. virtual ~ChromeContextMenuListener();
  175. public:
  176. NS_DECL_ISUPPORTS
  177. ChromeContextMenuListener(nsWebBrowser* aInBrowser,
  178. nsIWebBrowserChrome* aInChrome);
  179. // nsIDOMContextMenuListener
  180. NS_IMETHOD HandleEvent(nsIDOMEvent* aEvent) override;
  181. // Add/remove the relevant listeners, based on what interfaces
  182. // the embedding chrome implements.
  183. NS_IMETHOD AddChromeListeners();
  184. NS_IMETHOD RemoveChromeListeners();
  185. private:
  186. NS_IMETHOD AddContextMenuListener();
  187. NS_IMETHOD RemoveContextMenuListener();
  188. bool mContextMenuListenerInstalled;
  189. nsWebBrowser* mWebBrowser;
  190. nsCOMPtr<mozilla::dom::EventTarget> mEventTarget;
  191. nsCOMPtr<nsIWebBrowserChrome> mWebBrowserChrome;
  192. };
  193. #endif /* nsDocShellTreeOwner_h__ */