MediaQueryList.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /* implements DOM interface for querying and observing media queries */
  6. #ifndef mozilla_dom_MediaQueryList_h
  7. #define mozilla_dom_MediaQueryList_h
  8. #include "nsISupports.h"
  9. #include "nsCycleCollectionParticipant.h"
  10. #include "nsCOMPtr.h"
  11. #include "nsTArray.h"
  12. #include "prclist.h"
  13. #include "mozilla/Attributes.h"
  14. #include "nsWrapperCache.h"
  15. #include "mozilla/DOMEventTargetHelper.h"
  16. #include "mozilla/dom/MediaQueryListBinding.h"
  17. class nsIDocument;
  18. class nsMediaList;
  19. namespace mozilla {
  20. namespace dom {
  21. class MediaQueryList final : public DOMEventTargetHelper,
  22. public PRCList
  23. {
  24. public:
  25. // The caller who constructs is responsible for calling Evaluate
  26. // before calling any other methods.
  27. MediaQueryList(nsIDocument *aDocument,
  28. const nsAString &aMediaQueryList);
  29. private:
  30. ~MediaQueryList();
  31. public:
  32. NS_DECL_ISUPPORTS_INHERITED
  33. NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MediaQueryList, DOMEventTargetHelper)
  34. nsISupports* GetParentObject() const;
  35. void MaybeNotify();
  36. JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  37. // WebIDL methods
  38. void GetMedia(nsAString& aMedia);
  39. bool Matches();
  40. void AddListener(EventListener* aListener, ErrorResult& aRv);
  41. void RemoveListener(EventListener* aListener, ErrorResult& aRv);
  42. EventHandlerNonNull* GetOnchange();
  43. void SetOnchange(EventHandlerNonNull* aCallback);
  44. using nsIDOMEventTarget::AddEventListener;
  45. using nsIDOMEventTarget::RemoveEventListener;
  46. virtual void AddEventListener(const nsAString& aType,
  47. EventListener* aCallback,
  48. const AddEventListenerOptionsOrBoolean& aOptions,
  49. const Nullable<bool>& aWantsUntrusted,
  50. ErrorResult& aRv) override;
  51. virtual void RemoveEventListener(const nsAString& aType,
  52. EventListener* aCallback,
  53. const EventListenerOptionsOrBoolean& aOptions,
  54. ErrorResult& aRv) override;
  55. bool HasListeners();
  56. void Disconnect();
  57. private:
  58. void RecomputeMatches();
  59. void UpdateMustKeepAlive();
  60. // We only need a pointer to the document to support lazy
  61. // reevaluation following dynamic changes. However, this lazy
  62. // reevaluation is perhaps somewhat important, since some usage
  63. // patterns may involve the creation of large numbers of
  64. // MediaQueryList objects which almost immediately become garbage
  65. // (after a single call to the .matches getter).
  66. //
  67. // This pointer does make us a little more dependent on cycle
  68. // collection.
  69. //
  70. // We have a non-null mDocument for our entire lifetime except
  71. // after cycle collection unlinking. Having a non-null mDocument
  72. // is equivalent to being in that document's mDOMMediaQueryLists
  73. // linked list.
  74. nsCOMPtr<nsIDocument> mDocument;
  75. RefPtr<nsMediaList> mMediaList;
  76. bool mMatches;
  77. bool mMatchesValid;
  78. bool mIsKeptAlive;
  79. };
  80. } // namespace dom
  81. } // namespace mozilla
  82. #endif /* !defined(mozilla_dom_MediaQueryList_h) */