Bug 1839526 [wpt PR 40658] - Update wpt metadata, a=testonly
[gecko.git] / layout / style / MediaQueryList.h
blob0fab987f9188d60597e1c0ea9c563936998dfe6d
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 /* implements DOM interface for querying and observing media queries */
9 #ifndef mozilla_dom_MediaQueryList_h
10 #define mozilla_dom_MediaQueryList_h
12 #include "nsISupports.h"
13 #include "nsCycleCollectionParticipant.h"
14 #include "nsCOMPtr.h"
15 #include "nsTArray.h"
16 #include "mozilla/LinkedList.h"
17 #include "mozilla/Attributes.h"
18 #include "nsWrapperCache.h"
19 #include "mozilla/DOMEventTargetHelper.h"
20 #include "mozilla/dom/MediaQueryListBinding.h"
22 namespace mozilla::dom {
24 class MediaList;
26 class MediaQueryList final : public DOMEventTargetHelper,
27 public LinkedListElement<MediaQueryList> {
28 public:
29 // The caller who constructs is responsible for calling Evaluate
30 // before calling any other methods.
31 MediaQueryList(Document* aDocument, const nsACString& aMediaQueryList,
32 CallerType);
34 private:
35 ~MediaQueryList();
37 public:
38 NS_DECL_ISUPPORTS_INHERITED
39 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MediaQueryList, DOMEventTargetHelper)
41 nsISupports* GetParentObject() const;
43 // Returns whether we need to notify of the change event using
44 // FireChangeEvent().
45 [[nodiscard]] bool MediaFeatureValuesChanged();
46 void FireChangeEvent();
48 JSObject* WrapObject(JSContext* aCx,
49 JS::Handle<JSObject*> aGivenProto) override;
51 // WebIDL methods
52 void GetMedia(nsACString& aMedia) const;
53 bool Matches();
54 void AddListener(EventListener* aListener, ErrorResult& aRv);
55 void RemoveListener(EventListener* aListener, ErrorResult& aRv);
57 using DOMEventTargetHelper::EventListenerAdded;
58 void EventListenerAdded(nsAtom* aType) override;
60 IMPL_EVENT_HANDLER(change)
62 bool HasListeners() const;
64 void Disconnect();
66 size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
68 private:
69 void LastRelease() final {
70 auto* listElement = static_cast<LinkedListElement<MediaQueryList>*>(this);
71 if (listElement->isInList()) {
72 listElement->remove();
76 void RecomputeMatches();
78 // We only need a pointer to the document to support lazy
79 // reevaluation following dynamic changes. However, this lazy
80 // reevaluation is perhaps somewhat important, since some usage
81 // patterns may involve the creation of large numbers of
82 // MediaQueryList objects which almost immediately become garbage
83 // (after a single call to the .matches getter).
85 // This pointer does make us a little more dependent on cycle
86 // collection.
88 // We have a non-null mDocument for our entire lifetime except
89 // after cycle collection unlinking. Having a non-null mDocument
90 // is equivalent to being in that document's mDOMMediaQueryLists
91 // linked list.
92 RefPtr<Document> mDocument;
93 const RefPtr<const MediaList> mMediaList;
94 bool mMatches = false;
95 bool mMatchesValid = false;
96 // Whether our MediaList depends on our viewport size. Our medialist is
97 // immutable, so we can just compute this once and carry on with our lives.
98 const bool mViewportDependent;
101 } // namespace mozilla::dom
103 #endif /* !defined(mozilla_dom_MediaQueryList_h) */