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 void MediaFeatureValuesChanged();
45 // Returns whether we need to notify of the change by dispatching a change
47 [[nodiscard
]] bool EvaluateOnRenderingUpdate();
48 void FireChangeEvent();
50 JSObject
* WrapObject(JSContext
* aCx
,
51 JS::Handle
<JSObject
*> aGivenProto
) override
;
54 void GetMedia(nsACString
& aMedia
) const;
56 void AddListener(EventListener
* aListener
, ErrorResult
& aRv
);
57 void RemoveListener(EventListener
* aListener
, ErrorResult
& aRv
);
59 IMPL_EVENT_HANDLER(change
)
61 bool HasListeners() const;
65 size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf
) const;
68 void LastRelease() final
{
69 auto* listElement
= static_cast<LinkedListElement
<MediaQueryList
>*>(this);
70 if (listElement
->isInList()) {
71 listElement
->remove();
75 void RecomputeMatches();
77 // We only need a pointer to the document to support lazy
78 // reevaluation following dynamic changes. However, this lazy
79 // reevaluation is perhaps somewhat important, since some usage
80 // patterns may involve the creation of large numbers of
81 // MediaQueryList objects which almost immediately become garbage
82 // (after a single call to the .matches getter).
84 // This pointer does make us a little more dependent on cycle
87 // We have a non-null mDocument for our entire lifetime except
88 // after cycle collection unlinking. Having a non-null mDocument
89 // is equivalent to being in that document's mDOMMediaQueryLists
91 RefPtr
<Document
> mDocument
;
92 const RefPtr
<const MediaList
> mMediaList
;
93 // Whether our MediaList depends on our viewport size. Our medialist is
94 // immutable, so we can just compute this once and carry on with our lives.
95 const bool mViewportDependent
: 1;
97 // https://drafts.csswg.org/cssom-view/#mediaquerylist-matches-state
99 // The value of the matches state on creation, or on the last rendering
100 // update, in order to implement:
101 // https://drafts.csswg.org/cssom-view/#evaluate-media-queries-and-report-changes
102 bool mMatchesOnRenderingUpdate
: 1;
105 } // namespace mozilla::dom
107 #endif /* !defined(mozilla_dom_MediaQueryList_h) */