Bug 1861709 replace AudioCallbackDriver::ThreadRunning() assertions that mean to...
[gecko.git] / layout / style / MediaQueryList.h
blob046fe55dd54aa94922b14f921b8bacdadbf1528a
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 void MediaFeatureValuesChanged();
45 // Returns whether we need to notify of the change by dispatching a change
46 // event.
47 [[nodiscard]] bool EvaluateOnRenderingUpdate();
48 void FireChangeEvent();
50 JSObject* WrapObject(JSContext* aCx,
51 JS::Handle<JSObject*> aGivenProto) override;
53 // WebIDL methods
54 void GetMedia(nsACString& aMedia) const;
55 bool Matches();
56 void AddListener(EventListener* aListener, ErrorResult& aRv);
57 void RemoveListener(EventListener* aListener, ErrorResult& aRv);
59 IMPL_EVENT_HANDLER(change)
61 bool HasListeners() const;
63 void Disconnect();
65 size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
67 private:
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
85 // collection.
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
90 // linked list.
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;
96 // The matches state.
97 // https://drafts.csswg.org/cssom-view/#mediaquerylist-matches-state
98 bool mMatches : 1;
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) */