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 void ChromeObserver::SetDrawsTitle(bool aState
) {
68 nsIWidget
* mainWidget
= GetWindowWidget();
70 // We can do this synchronously because SetDrawsTitle doesn't have any
71 // synchronous effects apart from a harmless invalidation.
72 mainWidget
->SetDrawsTitle(aState
);
76 class MarginSetter
: public Runnable
{
78 explicit MarginSetter(nsIWidget
* aWidget
)
79 : mozilla::Runnable("MarginSetter"),
81 mMargin(-1, -1, -1, -1) {}
82 MarginSetter(nsIWidget
* aWidget
, const LayoutDeviceIntMargin
& aMargin
)
83 : mozilla::Runnable("MarginSetter"), mWidget(aWidget
), mMargin(aMargin
) {}
85 NS_IMETHOD
Run() override
{
86 // SetNonClientMargins can dispatch native events, hence doing
87 // it off a script runner.
88 mWidget
->SetNonClientMargins(mMargin
);
93 nsCOMPtr
<nsIWidget
> mWidget
;
94 LayoutDeviceIntMargin mMargin
;
97 void ChromeObserver::SetChromeMargins(const nsAttrValue
* aValue
) {
100 nsIWidget
* mainWidget
= GetWindowWidget();
101 if (!mainWidget
) return;
103 // top, right, bottom, left - see nsAttrValue
105 aValue
->ToString(tmp
);
107 if (nsContentUtils::ParseIntMarginValue(tmp
, margins
)) {
108 nsContentUtils::AddScriptRunner(new MarginSetter(
109 mainWidget
, LayoutDeviceIntMargin::FromUnknownMargin(margins
)));
113 void ChromeObserver::AttributeChanged(dom::Element
* aElement
,
114 int32_t aNamespaceID
, nsAtom
* aName
,
116 const nsAttrValue
* aOldValue
) {
117 // We only care about changes to the root element.
118 if (!mDocument
|| aElement
!= mDocument
->GetRootElement()) {
122 const nsAttrValue
* value
= aElement
->GetParsedAttr(aName
, aNamespaceID
);
124 // Hide chrome if needed
125 if (aName
== nsGkAtoms::hidechrome
) {
126 HideWindowChrome(value
->Equals(u
"true"_ns
, eCaseMatters
));
127 } else if (aName
== nsGkAtoms::chromemargin
) {
128 SetChromeMargins(value
);
130 // title and drawintitlebar are settable on
131 // any root node (windows, dialogs, etc)
132 else if (aName
== nsGkAtoms::title
) {
133 mDocument
->NotifyPossibleTitleChange(false);
134 } else if (aName
== nsGkAtoms::drawtitle
) {
135 SetDrawsTitle(value
->Equals(u
"true"_ns
, eCaseMatters
));
136 } else if (aName
== nsGkAtoms::localedir
) {
137 // if the localedir changed on the root element, reset the document
139 mDocument
->ResetDocumentDirection();
142 if (aName
== nsGkAtoms::hidechrome
) {
143 HideWindowChrome(false);
144 } else if (aName
== nsGkAtoms::chromemargin
) {
145 ResetChromeMargins();
146 } else if (aName
== nsGkAtoms::localedir
) {
147 // if the localedir changed on the root element, reset the document
149 mDocument
->ResetDocumentDirection();
150 } else if (aName
== nsGkAtoms::drawtitle
) {
151 SetDrawsTitle(false);
156 void ChromeObserver::NodeWillBeDestroyed(nsINode
* aNode
) {
160 void ChromeObserver::ResetChromeMargins() {
161 nsIWidget
* mainWidget
= GetWindowWidget();
162 if (!mainWidget
) return;
164 nsContentUtils::AddScriptRunner(new MarginSetter(mainWidget
));
167 nsresult
ChromeObserver::HideWindowChrome(bool aShouldHide
) {
168 // only top level chrome documents can hide the window chrome
169 if (!mDocument
->IsRootDisplayDocument()) return NS_OK
;
171 nsPresContext
* presContext
= mDocument
->GetPresContext();
173 if (presContext
&& presContext
->IsChrome()) {
174 nsIFrame
* frame
= mDocument
->GetDocumentElement()->GetPrimaryFrame();
177 nsView
* view
= frame
->GetClosestView();
180 nsIWidget
* w
= view
->GetWidget();
182 w
->HideWindowChrome(aShouldHide
);
190 } // namespace mozilla::dom