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 http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/UIDirectionManager.h"
8 #include "mozilla/Preferences.h"
9 #include "nsIObserverService.h"
10 #include "nsIWindowMediator.h"
11 #include "nsDocShell.h"
12 #include "nsServiceManagerUtils.h"
13 #include "mozilla/dom/Document.h"
14 #include "mozilla/Services.h"
15 #include "mozilla/SimpleEnumerator.h"
17 namespace mozilla::dom
{
19 NS_IMPL_ISUPPORTS(UIDirectionManager
, nsIObserver
)
23 UIDirectionManager::Observe(nsISupports
* aSubject
, const char* aTopic
,
24 const char16_t
* aData
) {
25 NS_ENSURE_FALSE(strcmp(aTopic
, "intl:app-locales-changed"), NS_ERROR_FAILURE
);
27 // Iterate over all of the windows and notify them of the direction change.
28 nsCOMPtr
<nsIWindowMediator
> windowMediator
=
29 do_GetService(NS_WINDOWMEDIATOR_CONTRACTID
);
30 NS_ENSURE_TRUE(windowMediator
, NS_ERROR_FAILURE
);
32 nsCOMPtr
<nsISimpleEnumerator
> windowEnumerator
;
33 windowMediator
->GetEnumerator(nullptr, getter_AddRefs(windowEnumerator
));
34 NS_ENSURE_TRUE(windowEnumerator
, NS_ERROR_FAILURE
);
36 for (auto& elements
: SimpleEnumerator
<nsISupports
>(windowEnumerator
)) {
37 nsCOMPtr
<nsPIDOMWindowOuter
> window
= do_QueryInterface(elements
);
38 if (window
->Closed()) {
42 RefPtr
<BrowsingContext
> context
= window
->GetBrowsingContext();
43 MOZ_DIAGNOSTIC_ASSERT(context
);
45 if (context
->IsDiscarded()) {
49 context
->PreOrderWalk([](BrowsingContext
* aContext
) {
50 if (dom::Document
* doc
= aContext
->GetDocument()) {
51 doc
->ResetDocumentDirection();
59 void UIDirectionManager::Initialize() {
60 MOZ_ASSERT(!gUIDirectionManager
);
61 MOZ_ASSERT(NS_IsMainThread());
63 RefPtr
<UIDirectionManager
> observer
= new UIDirectionManager();
65 nsCOMPtr
<nsIObserverService
> obs
= mozilla::services::GetObserverService();
66 if (NS_WARN_IF(!obs
)) {
69 obs
->AddObserver(observer
, "intl:app-locales-changed", false);
71 gUIDirectionManager
= observer
;
75 void UIDirectionManager::Shutdown() {
76 MOZ_ASSERT(NS_IsMainThread());
77 if (!gUIDirectionManager
) {
80 RefPtr
<UIDirectionManager
> observer
= gUIDirectionManager
;
81 gUIDirectionManager
= nullptr;
83 nsCOMPtr
<nsIObserverService
> obs
= mozilla::services::GetObserverService();
88 obs
->RemoveObserver(observer
, "intl:app-locales-changed");
91 mozilla::StaticRefPtr
<UIDirectionManager
>
92 UIDirectionManager::gUIDirectionManager
;
94 } // namespace mozilla::dom