Bug 1769952 - Fix running raptor on a Win10-64 VM r=sparky
[gecko.git] / dom / xul / ChromeObserver.cpp
blobd5edd4fa4afef3da617feeefe23627b075f5e9cc
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"
11 #include "nsIFrame.h"
13 #include "nsContentUtils.h"
14 #include "nsView.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();
34 if (!rootElement) {
35 return;
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.
46 continue;
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);
58 if (baseWindow) {
59 nsCOMPtr<nsIWidget> mainWidget;
60 baseWindow->GetMainWidget(getter_AddRefs(mainWidget));
61 return mainWidget;
64 return nullptr;
67 class SetDrawInTitleBarEvent : public Runnable {
68 public:
69 SetDrawInTitleBarEvent(nsIWidget* aWidget, bool aState)
70 : mozilla::Runnable("SetDrawInTitleBarEvent"),
71 mWidget(aWidget),
72 mState(aState) {}
74 NS_IMETHOD Run() override {
75 NS_ASSERTION(mWidget,
76 "You shouldn't call this runnable with a null widget!");
78 mWidget->SetDrawsInTitlebar(mState);
79 return NS_OK;
82 private:
83 nsCOMPtr<nsIWidget> mWidget;
84 bool mState;
87 void ChromeObserver::SetDrawsInTitlebar(bool aState) {
88 nsIWidget* mainWidget = GetWindowWidget();
89 if (mainWidget) {
90 nsContentUtils::AddScriptRunner(
91 new SetDrawInTitleBarEvent(mainWidget, aState));
95 void ChromeObserver::SetDrawsTitle(bool aState) {
96 nsIWidget* mainWidget = GetWindowWidget();
97 if (mainWidget) {
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 {
105 public:
106 explicit MarginSetter(nsIWidget* aWidget)
107 : mozilla::Runnable("MarginSetter"),
108 mWidget(aWidget),
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);
117 return NS_OK;
120 private:
121 nsCOMPtr<nsIWidget> mWidget;
122 LayoutDeviceIntMargin mMargin;
125 void ChromeObserver::SetChromeMargins(const nsAttrValue* aValue) {
126 if (!aValue) return;
128 nsIWidget* mainWidget = GetWindowWidget();
129 if (!mainWidget) return;
131 // top, right, bottom, left - see nsAttrValue
132 nsIntMargin margins;
133 bool gotMargins = false;
135 if (aValue->Type() == nsAttrValue::eIntMarginValue) {
136 gotMargins = aValue->GetIntMarginValue(margins);
137 } else {
138 nsAutoString tmp;
139 aValue->ToString(tmp);
140 gotMargins = nsContentUtils::ParseIntMarginValue(tmp, margins);
142 if (gotMargins) {
143 nsContentUtils::AddScriptRunner(new MarginSetter(
144 mainWidget, LayoutDeviceIntMargin::FromUnknownMargin(margins)));
148 void ChromeObserver::AttributeChanged(dom::Element* aElement,
149 int32_t aNamespaceID, nsAtom* aName,
150 int32_t aModType,
151 const nsAttrValue* aOldValue) {
152 // We only care about changes to the root element.
153 if (!mDocument || aElement != mDocument->GetRootElement()) {
154 return;
157 const nsAttrValue* value = aElement->GetParsedAttr(aName, aNamespaceID);
158 if (value) {
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
175 // direction
176 mDocument->ResetDocumentDirection();
177 } else if (aName == nsGkAtoms::lwtheme) {
178 // if the lwtheme changed, make sure to reset the document lwtheme
179 // cache
180 mDocument->ResetDocumentLWTheme();
182 } else {
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
189 // direction
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) {
203 mDocument = nullptr;
206 void ChromeObserver::ResetChromeMargins() {
207 nsIWidget* mainWidget = GetWindowWidget();
208 if (!mainWidget) return;
209 // See nsIWidget
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();
222 if (frame) {
223 nsView* view = frame->GetClosestView();
225 if (view) {
226 nsIWidget* w = view->GetWidget();
227 NS_ENSURE_STATE(w);
228 w->HideWindowChrome(aShouldHide);
233 return NS_OK;
236 } // namespace mozilla::dom