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 https://mozilla.org/MPL/2.0/. */
7 #include "ChromeObserver.h"
9 #include "nsIBaseWindow.h"
10 #include "nsIWidget.h"
13 #include "nsContentUtils.h"
15 #include "nsPresContext.h"
16 #include "mozilla/dom/Document.h"
17 #include "mozilla/dom/DocumentInlines.h"
18 #include "mozilla/dom/Element.h"
19 #include "mozilla/dom/MutationEventBinding.h"
20 #include "nsXULElement.h"
22 namespace mozilla::dom
{
24 NS_IMPL_ISUPPORTS(ChromeObserver
, nsIMutationObserver
)
26 ChromeObserver::ChromeObserver(Document
* aDocument
)
27 : nsStubMutationObserver(), mDocument(aDocument
) {}
29 ChromeObserver::~ChromeObserver() = default;
31 void ChromeObserver::Init() {
32 mDocument
->AddMutationObserver(this);
33 Element
* rootElement
= mDocument
->GetRootElement();
37 nsAutoScriptBlocker scriptBlocker
;
38 uint32_t attributeCount
= rootElement
->GetAttrCount();
39 for (uint32_t i
= 0; i
< attributeCount
; i
++) {
40 BorrowedAttrInfo info
= rootElement
->GetAttrInfoAt(i
);
41 const nsAttrName
* name
= info
.mName
;
42 if (name
->LocalName() == nsGkAtoms::chromemargin
) {
43 // Some linux windows managers have an issue when the chrome margin is
44 // applied while the browser is loading (bug 1598848). For now, skip
45 // applying this attribute when initializing.
48 AttributeChanged(rootElement
, name
->NamespaceID(), name
->LocalName(),
49 MutationEvent_Binding::ADDITION
, nullptr);
53 nsIWidget
* ChromeObserver::GetWindowWidget() {
54 // only top level chrome documents can set the titlebar color
55 if (mDocument
&& mDocument
->IsRootDisplayDocument()) {
56 nsCOMPtr
<nsISupports
> container
= mDocument
->GetContainer();
57 nsCOMPtr
<nsIBaseWindow
> baseWindow
= do_QueryInterface(container
);
59 nsCOMPtr
<nsIWidget
> mainWidget
;
60 baseWindow
->GetMainWidget(getter_AddRefs(mainWidget
));
67 class SetDrawInTitleBarEvent
: public Runnable
{
69 SetDrawInTitleBarEvent(nsIWidget
* aWidget
, bool aState
)
70 : mozilla::Runnable("SetDrawInTitleBarEvent"),
74 NS_IMETHOD
Run() override
{
76 "You shouldn't call this runnable with a null widget!");
78 mWidget
->SetDrawsInTitlebar(mState
);
83 nsCOMPtr
<nsIWidget
> mWidget
;
87 void ChromeObserver::SetDrawsInTitlebar(bool aState
) {
88 nsIWidget
* mainWidget
= GetWindowWidget();
90 nsContentUtils::AddScriptRunner(
91 new SetDrawInTitleBarEvent(mainWidget
, aState
));
95 void ChromeObserver::SetDrawsTitle(bool aState
) {
96 nsIWidget
* mainWidget
= GetWindowWidget();
98 // We can do this synchronously because SetDrawsTitle doesn't have any
99 // synchronous effects apart from a harmless invalidation.
100 mainWidget
->SetDrawsTitle(aState
);
104 class MarginSetter
: public Runnable
{
106 explicit MarginSetter(nsIWidget
* aWidget
)
107 : mozilla::Runnable("MarginSetter"),
109 mMargin(-1, -1, -1, -1) {}
110 MarginSetter(nsIWidget
* aWidget
, const LayoutDeviceIntMargin
& aMargin
)
111 : mozilla::Runnable("MarginSetter"), mWidget(aWidget
), mMargin(aMargin
) {}
113 NS_IMETHOD
Run() override
{
114 // SetNonClientMargins can dispatch native events, hence doing
115 // it off a script runner.
116 mWidget
->SetNonClientMargins(mMargin
);
121 nsCOMPtr
<nsIWidget
> mWidget
;
122 LayoutDeviceIntMargin mMargin
;
125 void ChromeObserver::SetChromeMargins(const nsAttrValue
* aValue
) {
128 nsIWidget
* mainWidget
= GetWindowWidget();
129 if (!mainWidget
) return;
131 // top, right, bottom, left - see nsAttrValue
133 bool gotMargins
= false;
135 if (aValue
->Type() == nsAttrValue::eIntMarginValue
) {
136 gotMargins
= aValue
->GetIntMarginValue(margins
);
139 aValue
->ToString(tmp
);
140 gotMargins
= nsContentUtils::ParseIntMarginValue(tmp
, margins
);
143 nsContentUtils::AddScriptRunner(new MarginSetter(
144 mainWidget
, LayoutDeviceIntMargin::FromUnknownMargin(margins
)));
148 void ChromeObserver::AttributeChanged(dom::Element
* aElement
,
149 int32_t aNamespaceID
, nsAtom
* aName
,
151 const nsAttrValue
* aOldValue
) {
152 // We only care about changes to the root element.
153 if (!mDocument
|| aElement
!= mDocument
->GetRootElement()) {
157 const nsAttrValue
* value
= aElement
->GetParsedAttr(aName
, aNamespaceID
);
159 // Hide chrome if needed
160 if (aName
== nsGkAtoms::hidechrome
) {
161 HideWindowChrome(value
->Equals(u
"true"_ns
, eCaseMatters
));
162 } else if (aName
== nsGkAtoms::chromemargin
) {
163 SetChromeMargins(value
);
165 // title and drawintitlebar are settable on
166 // any root node (windows, dialogs, etc)
167 else if (aName
== nsGkAtoms::title
) {
168 mDocument
->NotifyPossibleTitleChange(false);
169 } else if (aName
== nsGkAtoms::drawintitlebar
) {
170 SetDrawsInTitlebar(value
->Equals(u
"true"_ns
, eCaseMatters
));
171 } else if (aName
== nsGkAtoms::drawtitle
) {
172 SetDrawsTitle(value
->Equals(u
"true"_ns
, eCaseMatters
));
173 } else if (aName
== nsGkAtoms::localedir
) {
174 // if the localedir changed on the root element, reset the document
176 mDocument
->ResetDocumentDirection();
177 } else if (aName
== nsGkAtoms::lwtheme
) {
178 // if the lwtheme changed, make sure to reset the document lwtheme
180 mDocument
->ResetDocumentLWTheme();
183 if (aName
== nsGkAtoms::hidechrome
) {
184 HideWindowChrome(false);
185 } else if (aName
== nsGkAtoms::chromemargin
) {
186 ResetChromeMargins();
187 } else if (aName
== nsGkAtoms::localedir
) {
188 // if the localedir changed on the root element, reset the document
190 mDocument
->ResetDocumentDirection();
191 } else if (aName
== nsGkAtoms::lwtheme
) {
192 // if the lwtheme changed, make sure to restyle appropriately
193 mDocument
->ResetDocumentLWTheme();
194 } else if (aName
== nsGkAtoms::drawintitlebar
) {
195 SetDrawsInTitlebar(false);
196 } else if (aName
== nsGkAtoms::drawtitle
) {
197 SetDrawsTitle(false);
202 void ChromeObserver::NodeWillBeDestroyed(const nsINode
* aNode
) {
206 void ChromeObserver::ResetChromeMargins() {
207 nsIWidget
* mainWidget
= GetWindowWidget();
208 if (!mainWidget
) return;
210 nsContentUtils::AddScriptRunner(new MarginSetter(mainWidget
));
213 nsresult
ChromeObserver::HideWindowChrome(bool aShouldHide
) {
214 // only top level chrome documents can hide the window chrome
215 if (!mDocument
->IsRootDisplayDocument()) return NS_OK
;
217 nsPresContext
* presContext
= mDocument
->GetPresContext();
219 if (presContext
&& presContext
->IsChrome()) {
220 nsIFrame
* frame
= mDocument
->GetDocumentElement()->GetPrimaryFrame();
223 nsView
* view
= frame
->GetClosestView();
226 nsIWidget
* w
= view
->GetWidget();
228 w
->HideWindowChrome(aShouldHide
);
236 } // namespace mozilla::dom