dom/base/BarProps.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/base/BarProps.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,325 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "mozilla/dom/BarProps.h"
    1.10 +#include "mozilla/dom/BarPropBinding.h"
    1.11 +#include "nsContentUtils.h"
    1.12 +#include "nsGlobalWindow.h"
    1.13 +#include "nsIDocShell.h"
    1.14 +#include "nsIScrollable.h"
    1.15 +#include "nsIWebBrowserChrome.h"
    1.16 +
    1.17 +namespace mozilla {
    1.18 +namespace dom {
    1.19 +
    1.20 +//
    1.21 +//  Basic (virtual) BarProp class implementation
    1.22 +//
    1.23 +BarProp::BarProp(nsGlobalWindow* aWindow)
    1.24 +  : mDOMWindow(aWindow)
    1.25 +{
    1.26 +  MOZ_ASSERT(aWindow->IsInnerWindow());
    1.27 +  SetIsDOMBinding();
    1.28 +}
    1.29 +
    1.30 +BarProp::~BarProp()
    1.31 +{
    1.32 +}
    1.33 +
    1.34 +nsPIDOMWindow*
    1.35 +BarProp::GetParentObject() const
    1.36 +{
    1.37 +  return mDOMWindow;
    1.38 +}
    1.39 +
    1.40 +JSObject*
    1.41 +BarProp::WrapObject(JSContext* aCx)
    1.42 +{
    1.43 +  return BarPropBinding::Wrap(aCx, this);
    1.44 +}
    1.45 +
    1.46 +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(BarProp, mDOMWindow)
    1.47 +NS_IMPL_CYCLE_COLLECTING_ADDREF(BarProp)
    1.48 +NS_IMPL_CYCLE_COLLECTING_RELEASE(BarProp)
    1.49 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(BarProp)
    1.50 +  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
    1.51 +  NS_INTERFACE_MAP_ENTRY(nsISupports)
    1.52 +NS_INTERFACE_MAP_END
    1.53 +
    1.54 +bool
    1.55 +BarProp::GetVisibleByFlag(uint32_t aChromeFlag, ErrorResult& aRv)
    1.56 +{
    1.57 +  nsCOMPtr<nsIWebBrowserChrome> browserChrome = GetBrowserChrome();
    1.58 +  NS_ENSURE_TRUE(browserChrome, false);
    1.59 +
    1.60 +  uint32_t chromeFlags;
    1.61 +
    1.62 +  if (NS_FAILED(browserChrome->GetChromeFlags(&chromeFlags))) {
    1.63 +    aRv.Throw(NS_ERROR_FAILURE);
    1.64 +    return false;
    1.65 +  }
    1.66 +
    1.67 +  return (chromeFlags & aChromeFlag);
    1.68 +}
    1.69 +
    1.70 +void
    1.71 +BarProp::SetVisibleByFlag(bool aVisible, uint32_t aChromeFlag,
    1.72 +                          ErrorResult& aRv)
    1.73 +{
    1.74 +  nsCOMPtr<nsIWebBrowserChrome> browserChrome = GetBrowserChrome();
    1.75 +  NS_ENSURE_TRUE_VOID(browserChrome);
    1.76 +
    1.77 +  if (!nsContentUtils::IsCallerChrome()) {
    1.78 +    return;
    1.79 +  }
    1.80 +
    1.81 +  uint32_t chromeFlags;
    1.82 +
    1.83 +  if (NS_FAILED(browserChrome->GetChromeFlags(&chromeFlags))) {
    1.84 +    aRv.Throw(NS_ERROR_FAILURE);
    1.85 +    return;
    1.86 +  }
    1.87 +
    1.88 +  if (aVisible)
    1.89 +    chromeFlags |= aChromeFlag;
    1.90 +  else
    1.91 +    chromeFlags &= ~aChromeFlag;
    1.92 +
    1.93 +  if (NS_FAILED(browserChrome->SetChromeFlags(chromeFlags))) {
    1.94 +    aRv.Throw(NS_ERROR_FAILURE);
    1.95 +  }
    1.96 +}
    1.97 +
    1.98 +already_AddRefed<nsIWebBrowserChrome>
    1.99 +BarProp::GetBrowserChrome()
   1.100 +{
   1.101 +  if (!mDOMWindow) {
   1.102 +    return nullptr;
   1.103 +  }
   1.104 +
   1.105 +  return mDOMWindow->GetWebBrowserChrome();
   1.106 +}
   1.107 +
   1.108 +//
   1.109 +// MenubarProp class implementation
   1.110 +//
   1.111 +
   1.112 +MenubarProp::MenubarProp(nsGlobalWindow *aWindow)
   1.113 +  : BarProp(aWindow)
   1.114 +{
   1.115 +}
   1.116 +
   1.117 +MenubarProp::~MenubarProp()
   1.118 +{
   1.119 +}
   1.120 +
   1.121 +bool
   1.122 +MenubarProp::GetVisible(ErrorResult& aRv)
   1.123 +{
   1.124 +  return BarProp::GetVisibleByFlag(nsIWebBrowserChrome::CHROME_MENUBAR, aRv);
   1.125 +}
   1.126 +
   1.127 +void
   1.128 +MenubarProp::SetVisible(bool aVisible, ErrorResult& aRv)
   1.129 +{
   1.130 +  BarProp::SetVisibleByFlag(aVisible, nsIWebBrowserChrome::CHROME_MENUBAR, aRv);
   1.131 +}
   1.132 +
   1.133 +//
   1.134 +// ToolbarProp class implementation
   1.135 +//
   1.136 +
   1.137 +ToolbarProp::ToolbarProp(nsGlobalWindow *aWindow)
   1.138 +  : BarProp(aWindow)
   1.139 +{
   1.140 +}
   1.141 +
   1.142 +ToolbarProp::~ToolbarProp()
   1.143 +{
   1.144 +}
   1.145 +
   1.146 +bool
   1.147 +ToolbarProp::GetVisible(ErrorResult& aRv)
   1.148 +{
   1.149 +  return BarProp::GetVisibleByFlag(nsIWebBrowserChrome::CHROME_TOOLBAR, aRv);
   1.150 +}
   1.151 +
   1.152 +void
   1.153 +ToolbarProp::SetVisible(bool aVisible, ErrorResult& aRv)
   1.154 +{
   1.155 +  BarProp::SetVisibleByFlag(aVisible, nsIWebBrowserChrome::CHROME_TOOLBAR,
   1.156 +                            aRv);
   1.157 +}
   1.158 +
   1.159 +//
   1.160 +// LocationbarProp class implementation
   1.161 +//
   1.162 +
   1.163 +LocationbarProp::LocationbarProp(nsGlobalWindow *aWindow)
   1.164 +  : BarProp(aWindow)
   1.165 +{
   1.166 +}
   1.167 +
   1.168 +LocationbarProp::~LocationbarProp()
   1.169 +{
   1.170 +}
   1.171 +
   1.172 +bool
   1.173 +LocationbarProp::GetVisible(ErrorResult& aRv)
   1.174 +{
   1.175 +  return BarProp::GetVisibleByFlag(nsIWebBrowserChrome::CHROME_LOCATIONBAR,
   1.176 +                                   aRv);
   1.177 +}
   1.178 +
   1.179 +void
   1.180 +LocationbarProp::SetVisible(bool aVisible, ErrorResult& aRv)
   1.181 +{
   1.182 +  BarProp::SetVisibleByFlag(aVisible, nsIWebBrowserChrome::CHROME_LOCATIONBAR,
   1.183 +                            aRv);
   1.184 +}
   1.185 +
   1.186 +//
   1.187 +// PersonalbarProp class implementation
   1.188 +//
   1.189 +
   1.190 +PersonalbarProp::PersonalbarProp(nsGlobalWindow *aWindow)
   1.191 +  : BarProp(aWindow)
   1.192 +{
   1.193 +}
   1.194 +
   1.195 +PersonalbarProp::~PersonalbarProp()
   1.196 +{
   1.197 +}
   1.198 +
   1.199 +bool
   1.200 +PersonalbarProp::GetVisible(ErrorResult& aRv)
   1.201 +{
   1.202 +  return BarProp::GetVisibleByFlag(nsIWebBrowserChrome::CHROME_PERSONAL_TOOLBAR,
   1.203 +                                   aRv);
   1.204 +}
   1.205 +
   1.206 +void
   1.207 +PersonalbarProp::SetVisible(bool aVisible, ErrorResult& aRv)
   1.208 +{
   1.209 +  BarProp::SetVisibleByFlag(aVisible,
   1.210 +                            nsIWebBrowserChrome::CHROME_PERSONAL_TOOLBAR,
   1.211 +                            aRv);
   1.212 +}
   1.213 +
   1.214 +//
   1.215 +// StatusbarProp class implementation
   1.216 +//
   1.217 +
   1.218 +StatusbarProp::StatusbarProp(nsGlobalWindow *aWindow)
   1.219 +  : BarProp(aWindow)
   1.220 +{
   1.221 +}
   1.222 +
   1.223 +StatusbarProp::~StatusbarProp()
   1.224 +{
   1.225 +}
   1.226 +
   1.227 +bool
   1.228 +StatusbarProp::GetVisible(ErrorResult& aRv)
   1.229 +{
   1.230 +  return BarProp::GetVisibleByFlag(nsIWebBrowserChrome::CHROME_STATUSBAR, aRv);
   1.231 +}
   1.232 +
   1.233 +void
   1.234 +StatusbarProp::SetVisible(bool aVisible, ErrorResult& aRv)
   1.235 +{
   1.236 +  return BarProp::SetVisibleByFlag(aVisible,
   1.237 +                                   nsIWebBrowserChrome::CHROME_STATUSBAR, aRv);
   1.238 +}
   1.239 +
   1.240 +//
   1.241 +// ScrollbarsProp class implementation
   1.242 +//
   1.243 +
   1.244 +ScrollbarsProp::ScrollbarsProp(nsGlobalWindow *aWindow)
   1.245 +: BarProp(aWindow)
   1.246 +{
   1.247 +}
   1.248 +
   1.249 +ScrollbarsProp::~ScrollbarsProp()
   1.250 +{
   1.251 +}
   1.252 +
   1.253 +bool
   1.254 +ScrollbarsProp::GetVisible(ErrorResult& aRv)
   1.255 +{
   1.256 +  if (!mDOMWindow) {
   1.257 +    return true;
   1.258 +  }
   1.259 +
   1.260 +  nsCOMPtr<nsIScrollable> scroller =
   1.261 +    do_QueryInterface(mDOMWindow->GetDocShell());
   1.262 +
   1.263 +  if (!scroller) {
   1.264 +    return true;
   1.265 +  }
   1.266 +
   1.267 +  int32_t prefValue;
   1.268 +  scroller->GetDefaultScrollbarPreferences(
   1.269 +              nsIScrollable::ScrollOrientation_Y, &prefValue);
   1.270 +  if (prefValue != nsIScrollable::Scrollbar_Never) {
   1.271 +    return true;
   1.272 +  }
   1.273 +
   1.274 +  scroller->GetDefaultScrollbarPreferences(
   1.275 +              nsIScrollable::ScrollOrientation_X, &prefValue);
   1.276 +  return prefValue != nsIScrollable::Scrollbar_Never;
   1.277 +}
   1.278 +
   1.279 +void
   1.280 +ScrollbarsProp::SetVisible(bool aVisible, ErrorResult& aRv)
   1.281 +{
   1.282 +  if (!nsContentUtils::IsCallerChrome()) {
   1.283 +    return;
   1.284 +  }
   1.285 +
   1.286 +  /* Scrollbars, unlike the other barprops, implement visibility directly
   1.287 +     rather than handing off to the superclass (and from there to the
   1.288 +     chrome window) because scrollbar visibility uniquely applies only
   1.289 +     to the window making the change (arguably. it does now, anyway.)
   1.290 +     and because embedding apps have no interface for implementing this
   1.291 +     themselves, and therefore the implementation must be internal. */
   1.292 +
   1.293 +  nsCOMPtr<nsIScrollable> scroller =
   1.294 +    do_QueryInterface(mDOMWindow->GetDocShell());
   1.295 +
   1.296 +  if (scroller) {
   1.297 +    int32_t prefValue;
   1.298 +
   1.299 +    if (aVisible) {
   1.300 +      prefValue = nsIScrollable::Scrollbar_Auto;
   1.301 +    } else {
   1.302 +      prefValue = nsIScrollable::Scrollbar_Never;
   1.303 +    }
   1.304 +
   1.305 +    scroller->SetDefaultScrollbarPreferences(
   1.306 +                nsIScrollable::ScrollOrientation_Y, prefValue);
   1.307 +    scroller->SetDefaultScrollbarPreferences(
   1.308 +                nsIScrollable::ScrollOrientation_X, prefValue);
   1.309 +  }
   1.310 +
   1.311 +  /* Notably absent is the part where we notify the chrome window using
   1.312 +     GetBrowserChrome()->SetChromeFlags(). Given the possibility of multiple
   1.313 +     DOM windows (multiple top-level windows, even) within a single
   1.314 +     chrome window, the historical concept of a single "has scrollbars"
   1.315 +     flag in the chrome is inapplicable, and we can't tell at this level
   1.316 +     whether we represent the particular DOM window that makes this decision
   1.317 +     for the chrome.
   1.318 +
   1.319 +     So only this object (and its corresponding DOM window) knows whether
   1.320 +     scrollbars are visible. The corresponding chrome window will need to
   1.321 +     ask (one of) its DOM window(s) when it needs to know about scrollbar
   1.322 +     visibility, rather than caching its own copy of that information.
   1.323 +  */
   1.324 +}
   1.325 +
   1.326 +} // namespace dom
   1.327 +} // namespace mozilla
   1.328 +

mercurial