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