michael@0: /* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* implements DOM interface for querying and observing media queries */ michael@0: michael@0: #ifndef mozilla_dom_MediaQueryList_h michael@0: #define mozilla_dom_MediaQueryList_h michael@0: michael@0: #include "nsISupports.h" michael@0: #include "nsCycleCollectionParticipant.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsTArray.h" michael@0: #include "prclist.h" michael@0: #include "mozilla/Attributes.h" michael@0: #include "nsWrapperCache.h" michael@0: #include "mozilla/dom/MediaQueryListBinding.h" michael@0: michael@0: class nsPresContext; michael@0: class nsMediaList; michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: class MediaQueryList MOZ_FINAL : public nsISupports, michael@0: public nsWrapperCache, michael@0: public PRCList michael@0: { michael@0: public: michael@0: // The caller who constructs is responsible for calling Evaluate michael@0: // before calling any other methods. michael@0: MediaQueryList(nsPresContext *aPresContext, michael@0: const nsAString &aMediaQueryList); michael@0: private: michael@0: ~MediaQueryList(); michael@0: michael@0: public: michael@0: NS_DECL_CYCLE_COLLECTING_ISUPPORTS michael@0: NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(MediaQueryList) michael@0: michael@0: nsISupports* GetParentObject() const; michael@0: michael@0: struct HandleChangeData { michael@0: nsRefPtr mql; michael@0: nsRefPtr callback; michael@0: }; michael@0: michael@0: typedef FallibleTArray< nsRefPtr > CallbackList; michael@0: typedef FallibleTArray NotifyList; michael@0: michael@0: // Appends listeners that need notification to aListenersToNotify michael@0: void MediumFeaturesChanged(NotifyList &aListenersToNotify); michael@0: michael@0: bool HasListeners() const { return !mCallbacks.IsEmpty(); } michael@0: michael@0: void RemoveAllListeners(); michael@0: michael@0: JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; michael@0: michael@0: // WebIDL methods michael@0: void GetMedia(nsAString& aMedia); michael@0: bool Matches(); michael@0: void AddListener(mozilla::dom::MediaQueryListListener& aListener); michael@0: void RemoveListener(mozilla::dom::MediaQueryListListener& aListener); michael@0: michael@0: private: michael@0: void RecomputeMatches(); michael@0: michael@0: // We only need a pointer to the pres context to support lazy michael@0: // reevaluation following dynamic changes. However, this lazy michael@0: // reevaluation is perhaps somewhat important, since some usage michael@0: // patterns may involve the creation of large numbers of michael@0: // MediaQueryList objects which almost immediately become garbage michael@0: // (after a single call to the .matches getter). michael@0: // michael@0: // This pointer does make us a little more dependent on cycle michael@0: // collection. michael@0: // michael@0: // We have a non-null mPresContext for our entire lifetime except michael@0: // after cycle collection unlinking. Having a non-null mPresContext michael@0: // is equivalent to being in that pres context's mDOMMediaQueryLists michael@0: // linked list. michael@0: nsRefPtr mPresContext; michael@0: michael@0: nsRefPtr mMediaList; michael@0: bool mMatches; michael@0: bool mMatchesValid; michael@0: CallbackList mCallbacks; michael@0: }; michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla michael@0: michael@0: #endif /* !defined(mozilla_dom_MediaQueryList_h) */