dom/base/BarProps.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     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"
    14 namespace mozilla {
    15 namespace dom {
    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 }
    27 BarProp::~BarProp()
    28 {
    29 }
    31 nsPIDOMWindow*
    32 BarProp::GetParentObject() const
    33 {
    34   return mDOMWindow;
    35 }
    37 JSObject*
    38 BarProp::WrapObject(JSContext* aCx)
    39 {
    40   return BarPropBinding::Wrap(aCx, this);
    41 }
    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
    51 bool
    52 BarProp::GetVisibleByFlag(uint32_t aChromeFlag, ErrorResult& aRv)
    53 {
    54   nsCOMPtr<nsIWebBrowserChrome> browserChrome = GetBrowserChrome();
    55   NS_ENSURE_TRUE(browserChrome, false);
    57   uint32_t chromeFlags;
    59   if (NS_FAILED(browserChrome->GetChromeFlags(&chromeFlags))) {
    60     aRv.Throw(NS_ERROR_FAILURE);
    61     return false;
    62   }
    64   return (chromeFlags & aChromeFlag);
    65 }
    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);
    74   if (!nsContentUtils::IsCallerChrome()) {
    75     return;
    76   }
    78   uint32_t chromeFlags;
    80   if (NS_FAILED(browserChrome->GetChromeFlags(&chromeFlags))) {
    81     aRv.Throw(NS_ERROR_FAILURE);
    82     return;
    83   }
    85   if (aVisible)
    86     chromeFlags |= aChromeFlag;
    87   else
    88     chromeFlags &= ~aChromeFlag;
    90   if (NS_FAILED(browserChrome->SetChromeFlags(chromeFlags))) {
    91     aRv.Throw(NS_ERROR_FAILURE);
    92   }
    93 }
    95 already_AddRefed<nsIWebBrowserChrome>
    96 BarProp::GetBrowserChrome()
    97 {
    98   if (!mDOMWindow) {
    99     return nullptr;
   100   }
   102   return mDOMWindow->GetWebBrowserChrome();
   103 }
   105 //
   106 // MenubarProp class implementation
   107 //
   109 MenubarProp::MenubarProp(nsGlobalWindow *aWindow)
   110   : BarProp(aWindow)
   111 {
   112 }
   114 MenubarProp::~MenubarProp()
   115 {
   116 }
   118 bool
   119 MenubarProp::GetVisible(ErrorResult& aRv)
   120 {
   121   return BarProp::GetVisibleByFlag(nsIWebBrowserChrome::CHROME_MENUBAR, aRv);
   122 }
   124 void
   125 MenubarProp::SetVisible(bool aVisible, ErrorResult& aRv)
   126 {
   127   BarProp::SetVisibleByFlag(aVisible, nsIWebBrowserChrome::CHROME_MENUBAR, aRv);
   128 }
   130 //
   131 // ToolbarProp class implementation
   132 //
   134 ToolbarProp::ToolbarProp(nsGlobalWindow *aWindow)
   135   : BarProp(aWindow)
   136 {
   137 }
   139 ToolbarProp::~ToolbarProp()
   140 {
   141 }
   143 bool
   144 ToolbarProp::GetVisible(ErrorResult& aRv)
   145 {
   146   return BarProp::GetVisibleByFlag(nsIWebBrowserChrome::CHROME_TOOLBAR, aRv);
   147 }
   149 void
   150 ToolbarProp::SetVisible(bool aVisible, ErrorResult& aRv)
   151 {
   152   BarProp::SetVisibleByFlag(aVisible, nsIWebBrowserChrome::CHROME_TOOLBAR,
   153                             aRv);
   154 }
   156 //
   157 // LocationbarProp class implementation
   158 //
   160 LocationbarProp::LocationbarProp(nsGlobalWindow *aWindow)
   161   : BarProp(aWindow)
   162 {
   163 }
   165 LocationbarProp::~LocationbarProp()
   166 {
   167 }
   169 bool
   170 LocationbarProp::GetVisible(ErrorResult& aRv)
   171 {
   172   return BarProp::GetVisibleByFlag(nsIWebBrowserChrome::CHROME_LOCATIONBAR,
   173                                    aRv);
   174 }
   176 void
   177 LocationbarProp::SetVisible(bool aVisible, ErrorResult& aRv)
   178 {
   179   BarProp::SetVisibleByFlag(aVisible, nsIWebBrowserChrome::CHROME_LOCATIONBAR,
   180                             aRv);
   181 }
   183 //
   184 // PersonalbarProp class implementation
   185 //
   187 PersonalbarProp::PersonalbarProp(nsGlobalWindow *aWindow)
   188   : BarProp(aWindow)
   189 {
   190 }
   192 PersonalbarProp::~PersonalbarProp()
   193 {
   194 }
   196 bool
   197 PersonalbarProp::GetVisible(ErrorResult& aRv)
   198 {
   199   return BarProp::GetVisibleByFlag(nsIWebBrowserChrome::CHROME_PERSONAL_TOOLBAR,
   200                                    aRv);
   201 }
   203 void
   204 PersonalbarProp::SetVisible(bool aVisible, ErrorResult& aRv)
   205 {
   206   BarProp::SetVisibleByFlag(aVisible,
   207                             nsIWebBrowserChrome::CHROME_PERSONAL_TOOLBAR,
   208                             aRv);
   209 }
   211 //
   212 // StatusbarProp class implementation
   213 //
   215 StatusbarProp::StatusbarProp(nsGlobalWindow *aWindow)
   216   : BarProp(aWindow)
   217 {
   218 }
   220 StatusbarProp::~StatusbarProp()
   221 {
   222 }
   224 bool
   225 StatusbarProp::GetVisible(ErrorResult& aRv)
   226 {
   227   return BarProp::GetVisibleByFlag(nsIWebBrowserChrome::CHROME_STATUSBAR, aRv);
   228 }
   230 void
   231 StatusbarProp::SetVisible(bool aVisible, ErrorResult& aRv)
   232 {
   233   return BarProp::SetVisibleByFlag(aVisible,
   234                                    nsIWebBrowserChrome::CHROME_STATUSBAR, aRv);
   235 }
   237 //
   238 // ScrollbarsProp class implementation
   239 //
   241 ScrollbarsProp::ScrollbarsProp(nsGlobalWindow *aWindow)
   242 : BarProp(aWindow)
   243 {
   244 }
   246 ScrollbarsProp::~ScrollbarsProp()
   247 {
   248 }
   250 bool
   251 ScrollbarsProp::GetVisible(ErrorResult& aRv)
   252 {
   253   if (!mDOMWindow) {
   254     return true;
   255   }
   257   nsCOMPtr<nsIScrollable> scroller =
   258     do_QueryInterface(mDOMWindow->GetDocShell());
   260   if (!scroller) {
   261     return true;
   262   }
   264   int32_t prefValue;
   265   scroller->GetDefaultScrollbarPreferences(
   266               nsIScrollable::ScrollOrientation_Y, &prefValue);
   267   if (prefValue != nsIScrollable::Scrollbar_Never) {
   268     return true;
   269   }
   271   scroller->GetDefaultScrollbarPreferences(
   272               nsIScrollable::ScrollOrientation_X, &prefValue);
   273   return prefValue != nsIScrollable::Scrollbar_Never;
   274 }
   276 void
   277 ScrollbarsProp::SetVisible(bool aVisible, ErrorResult& aRv)
   278 {
   279   if (!nsContentUtils::IsCallerChrome()) {
   280     return;
   281   }
   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. */
   290   nsCOMPtr<nsIScrollable> scroller =
   291     do_QueryInterface(mDOMWindow->GetDocShell());
   293   if (scroller) {
   294     int32_t prefValue;
   296     if (aVisible) {
   297       prefValue = nsIScrollable::Scrollbar_Auto;
   298     } else {
   299       prefValue = nsIScrollable::Scrollbar_Never;
   300     }
   302     scroller->SetDefaultScrollbarPreferences(
   303                 nsIScrollable::ScrollOrientation_Y, prefValue);
   304     scroller->SetDefaultScrollbarPreferences(
   305                 nsIScrollable::ScrollOrientation_X, prefValue);
   306   }
   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.
   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 }
   323 } // namespace dom
   324 } // namespace mozilla

mercurial