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"
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
{
26 class MediaQueryList final
: public DOMEventTargetHelper
,
27 public LinkedListElement
<MediaQueryList
> {
29 // The caller who constructs is responsible for calling Evaluate
30 // before calling any other methods.
31 MediaQueryList(Document
* aDocument
, const nsACString
& aMediaQueryList
,
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
45 [[nodiscard
]] bool MediaFeatureValuesChanged();
46 void FireChangeEvent();
48 JSObject
* WrapObject(JSContext
* aCx
,
49 JS::Handle
<JSObject
*> aGivenProto
) override
;
52 void GetMedia(nsACString
& aMedia
) const;
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;
66 size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf
) const;
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
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
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) */