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