|
1 /* -*- Mode: C++; tab-width: 2; 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 |
|
6 #include "mozilla/dom/BarProps.h" |
|
7 #include "mozilla/dom/BarPropBinding.h" |
|
8 #include "nsContentUtils.h" |
|
9 #include "nsGlobalWindow.h" |
|
10 #include "nsIDocShell.h" |
|
11 #include "nsIScrollable.h" |
|
12 #include "nsIWebBrowserChrome.h" |
|
13 |
|
14 namespace mozilla { |
|
15 namespace dom { |
|
16 |
|
17 // |
|
18 // Basic (virtual) BarProp class implementation |
|
19 // |
|
20 BarProp::BarProp(nsGlobalWindow* aWindow) |
|
21 : mDOMWindow(aWindow) |
|
22 { |
|
23 MOZ_ASSERT(aWindow->IsInnerWindow()); |
|
24 SetIsDOMBinding(); |
|
25 } |
|
26 |
|
27 BarProp::~BarProp() |
|
28 { |
|
29 } |
|
30 |
|
31 nsPIDOMWindow* |
|
32 BarProp::GetParentObject() const |
|
33 { |
|
34 return mDOMWindow; |
|
35 } |
|
36 |
|
37 JSObject* |
|
38 BarProp::WrapObject(JSContext* aCx) |
|
39 { |
|
40 return BarPropBinding::Wrap(aCx, this); |
|
41 } |
|
42 |
|
43 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(BarProp, mDOMWindow) |
|
44 NS_IMPL_CYCLE_COLLECTING_ADDREF(BarProp) |
|
45 NS_IMPL_CYCLE_COLLECTING_RELEASE(BarProp) |
|
46 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(BarProp) |
|
47 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
|
48 NS_INTERFACE_MAP_ENTRY(nsISupports) |
|
49 NS_INTERFACE_MAP_END |
|
50 |
|
51 bool |
|
52 BarProp::GetVisibleByFlag(uint32_t aChromeFlag, ErrorResult& aRv) |
|
53 { |
|
54 nsCOMPtr<nsIWebBrowserChrome> browserChrome = GetBrowserChrome(); |
|
55 NS_ENSURE_TRUE(browserChrome, false); |
|
56 |
|
57 uint32_t chromeFlags; |
|
58 |
|
59 if (NS_FAILED(browserChrome->GetChromeFlags(&chromeFlags))) { |
|
60 aRv.Throw(NS_ERROR_FAILURE); |
|
61 return false; |
|
62 } |
|
63 |
|
64 return (chromeFlags & aChromeFlag); |
|
65 } |
|
66 |
|
67 void |
|
68 BarProp::SetVisibleByFlag(bool aVisible, uint32_t aChromeFlag, |
|
69 ErrorResult& aRv) |
|
70 { |
|
71 nsCOMPtr<nsIWebBrowserChrome> browserChrome = GetBrowserChrome(); |
|
72 NS_ENSURE_TRUE_VOID(browserChrome); |
|
73 |
|
74 if (!nsContentUtils::IsCallerChrome()) { |
|
75 return; |
|
76 } |
|
77 |
|
78 uint32_t chromeFlags; |
|
79 |
|
80 if (NS_FAILED(browserChrome->GetChromeFlags(&chromeFlags))) { |
|
81 aRv.Throw(NS_ERROR_FAILURE); |
|
82 return; |
|
83 } |
|
84 |
|
85 if (aVisible) |
|
86 chromeFlags |= aChromeFlag; |
|
87 else |
|
88 chromeFlags &= ~aChromeFlag; |
|
89 |
|
90 if (NS_FAILED(browserChrome->SetChromeFlags(chromeFlags))) { |
|
91 aRv.Throw(NS_ERROR_FAILURE); |
|
92 } |
|
93 } |
|
94 |
|
95 already_AddRefed<nsIWebBrowserChrome> |
|
96 BarProp::GetBrowserChrome() |
|
97 { |
|
98 if (!mDOMWindow) { |
|
99 return nullptr; |
|
100 } |
|
101 |
|
102 return mDOMWindow->GetWebBrowserChrome(); |
|
103 } |
|
104 |
|
105 // |
|
106 // MenubarProp class implementation |
|
107 // |
|
108 |
|
109 MenubarProp::MenubarProp(nsGlobalWindow *aWindow) |
|
110 : BarProp(aWindow) |
|
111 { |
|
112 } |
|
113 |
|
114 MenubarProp::~MenubarProp() |
|
115 { |
|
116 } |
|
117 |
|
118 bool |
|
119 MenubarProp::GetVisible(ErrorResult& aRv) |
|
120 { |
|
121 return BarProp::GetVisibleByFlag(nsIWebBrowserChrome::CHROME_MENUBAR, aRv); |
|
122 } |
|
123 |
|
124 void |
|
125 MenubarProp::SetVisible(bool aVisible, ErrorResult& aRv) |
|
126 { |
|
127 BarProp::SetVisibleByFlag(aVisible, nsIWebBrowserChrome::CHROME_MENUBAR, aRv); |
|
128 } |
|
129 |
|
130 // |
|
131 // ToolbarProp class implementation |
|
132 // |
|
133 |
|
134 ToolbarProp::ToolbarProp(nsGlobalWindow *aWindow) |
|
135 : BarProp(aWindow) |
|
136 { |
|
137 } |
|
138 |
|
139 ToolbarProp::~ToolbarProp() |
|
140 { |
|
141 } |
|
142 |
|
143 bool |
|
144 ToolbarProp::GetVisible(ErrorResult& aRv) |
|
145 { |
|
146 return BarProp::GetVisibleByFlag(nsIWebBrowserChrome::CHROME_TOOLBAR, aRv); |
|
147 } |
|
148 |
|
149 void |
|
150 ToolbarProp::SetVisible(bool aVisible, ErrorResult& aRv) |
|
151 { |
|
152 BarProp::SetVisibleByFlag(aVisible, nsIWebBrowserChrome::CHROME_TOOLBAR, |
|
153 aRv); |
|
154 } |
|
155 |
|
156 // |
|
157 // LocationbarProp class implementation |
|
158 // |
|
159 |
|
160 LocationbarProp::LocationbarProp(nsGlobalWindow *aWindow) |
|
161 : BarProp(aWindow) |
|
162 { |
|
163 } |
|
164 |
|
165 LocationbarProp::~LocationbarProp() |
|
166 { |
|
167 } |
|
168 |
|
169 bool |
|
170 LocationbarProp::GetVisible(ErrorResult& aRv) |
|
171 { |
|
172 return BarProp::GetVisibleByFlag(nsIWebBrowserChrome::CHROME_LOCATIONBAR, |
|
173 aRv); |
|
174 } |
|
175 |
|
176 void |
|
177 LocationbarProp::SetVisible(bool aVisible, ErrorResult& aRv) |
|
178 { |
|
179 BarProp::SetVisibleByFlag(aVisible, nsIWebBrowserChrome::CHROME_LOCATIONBAR, |
|
180 aRv); |
|
181 } |
|
182 |
|
183 // |
|
184 // PersonalbarProp class implementation |
|
185 // |
|
186 |
|
187 PersonalbarProp::PersonalbarProp(nsGlobalWindow *aWindow) |
|
188 : BarProp(aWindow) |
|
189 { |
|
190 } |
|
191 |
|
192 PersonalbarProp::~PersonalbarProp() |
|
193 { |
|
194 } |
|
195 |
|
196 bool |
|
197 PersonalbarProp::GetVisible(ErrorResult& aRv) |
|
198 { |
|
199 return BarProp::GetVisibleByFlag(nsIWebBrowserChrome::CHROME_PERSONAL_TOOLBAR, |
|
200 aRv); |
|
201 } |
|
202 |
|
203 void |
|
204 PersonalbarProp::SetVisible(bool aVisible, ErrorResult& aRv) |
|
205 { |
|
206 BarProp::SetVisibleByFlag(aVisible, |
|
207 nsIWebBrowserChrome::CHROME_PERSONAL_TOOLBAR, |
|
208 aRv); |
|
209 } |
|
210 |
|
211 // |
|
212 // StatusbarProp class implementation |
|
213 // |
|
214 |
|
215 StatusbarProp::StatusbarProp(nsGlobalWindow *aWindow) |
|
216 : BarProp(aWindow) |
|
217 { |
|
218 } |
|
219 |
|
220 StatusbarProp::~StatusbarProp() |
|
221 { |
|
222 } |
|
223 |
|
224 bool |
|
225 StatusbarProp::GetVisible(ErrorResult& aRv) |
|
226 { |
|
227 return BarProp::GetVisibleByFlag(nsIWebBrowserChrome::CHROME_STATUSBAR, aRv); |
|
228 } |
|
229 |
|
230 void |
|
231 StatusbarProp::SetVisible(bool aVisible, ErrorResult& aRv) |
|
232 { |
|
233 return BarProp::SetVisibleByFlag(aVisible, |
|
234 nsIWebBrowserChrome::CHROME_STATUSBAR, aRv); |
|
235 } |
|
236 |
|
237 // |
|
238 // ScrollbarsProp class implementation |
|
239 // |
|
240 |
|
241 ScrollbarsProp::ScrollbarsProp(nsGlobalWindow *aWindow) |
|
242 : BarProp(aWindow) |
|
243 { |
|
244 } |
|
245 |
|
246 ScrollbarsProp::~ScrollbarsProp() |
|
247 { |
|
248 } |
|
249 |
|
250 bool |
|
251 ScrollbarsProp::GetVisible(ErrorResult& aRv) |
|
252 { |
|
253 if (!mDOMWindow) { |
|
254 return true; |
|
255 } |
|
256 |
|
257 nsCOMPtr<nsIScrollable> scroller = |
|
258 do_QueryInterface(mDOMWindow->GetDocShell()); |
|
259 |
|
260 if (!scroller) { |
|
261 return true; |
|
262 } |
|
263 |
|
264 int32_t prefValue; |
|
265 scroller->GetDefaultScrollbarPreferences( |
|
266 nsIScrollable::ScrollOrientation_Y, &prefValue); |
|
267 if (prefValue != nsIScrollable::Scrollbar_Never) { |
|
268 return true; |
|
269 } |
|
270 |
|
271 scroller->GetDefaultScrollbarPreferences( |
|
272 nsIScrollable::ScrollOrientation_X, &prefValue); |
|
273 return prefValue != nsIScrollable::Scrollbar_Never; |
|
274 } |
|
275 |
|
276 void |
|
277 ScrollbarsProp::SetVisible(bool aVisible, ErrorResult& aRv) |
|
278 { |
|
279 if (!nsContentUtils::IsCallerChrome()) { |
|
280 return; |
|
281 } |
|
282 |
|
283 /* Scrollbars, unlike the other barprops, implement visibility directly |
|
284 rather than handing off to the superclass (and from there to the |
|
285 chrome window) because scrollbar visibility uniquely applies only |
|
286 to the window making the change (arguably. it does now, anyway.) |
|
287 and because embedding apps have no interface for implementing this |
|
288 themselves, and therefore the implementation must be internal. */ |
|
289 |
|
290 nsCOMPtr<nsIScrollable> scroller = |
|
291 do_QueryInterface(mDOMWindow->GetDocShell()); |
|
292 |
|
293 if (scroller) { |
|
294 int32_t prefValue; |
|
295 |
|
296 if (aVisible) { |
|
297 prefValue = nsIScrollable::Scrollbar_Auto; |
|
298 } else { |
|
299 prefValue = nsIScrollable::Scrollbar_Never; |
|
300 } |
|
301 |
|
302 scroller->SetDefaultScrollbarPreferences( |
|
303 nsIScrollable::ScrollOrientation_Y, prefValue); |
|
304 scroller->SetDefaultScrollbarPreferences( |
|
305 nsIScrollable::ScrollOrientation_X, prefValue); |
|
306 } |
|
307 |
|
308 /* Notably absent is the part where we notify the chrome window using |
|
309 GetBrowserChrome()->SetChromeFlags(). Given the possibility of multiple |
|
310 DOM windows (multiple top-level windows, even) within a single |
|
311 chrome window, the historical concept of a single "has scrollbars" |
|
312 flag in the chrome is inapplicable, and we can't tell at this level |
|
313 whether we represent the particular DOM window that makes this decision |
|
314 for the chrome. |
|
315 |
|
316 So only this object (and its corresponding DOM window) knows whether |
|
317 scrollbars are visible. The corresponding chrome window will need to |
|
318 ask (one of) its DOM window(s) when it needs to know about scrollbar |
|
319 visibility, rather than caching its own copy of that information. |
|
320 */ |
|
321 } |
|
322 |
|
323 } // namespace dom |
|
324 } // namespace mozilla |
|
325 |