1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/layout/style/MediaQueryList.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,95 @@ 1.4 +/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* implements DOM interface for querying and observing media queries */ 1.10 + 1.11 +#ifndef mozilla_dom_MediaQueryList_h 1.12 +#define mozilla_dom_MediaQueryList_h 1.13 + 1.14 +#include "nsISupports.h" 1.15 +#include "nsCycleCollectionParticipant.h" 1.16 +#include "nsAutoPtr.h" 1.17 +#include "nsTArray.h" 1.18 +#include "prclist.h" 1.19 +#include "mozilla/Attributes.h" 1.20 +#include "nsWrapperCache.h" 1.21 +#include "mozilla/dom/MediaQueryListBinding.h" 1.22 + 1.23 +class nsPresContext; 1.24 +class nsMediaList; 1.25 + 1.26 +namespace mozilla { 1.27 +namespace dom { 1.28 + 1.29 +class MediaQueryList MOZ_FINAL : public nsISupports, 1.30 + public nsWrapperCache, 1.31 + public PRCList 1.32 +{ 1.33 +public: 1.34 + // The caller who constructs is responsible for calling Evaluate 1.35 + // before calling any other methods. 1.36 + MediaQueryList(nsPresContext *aPresContext, 1.37 + const nsAString &aMediaQueryList); 1.38 +private: 1.39 + ~MediaQueryList(); 1.40 + 1.41 +public: 1.42 + NS_DECL_CYCLE_COLLECTING_ISUPPORTS 1.43 + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(MediaQueryList) 1.44 + 1.45 + nsISupports* GetParentObject() const; 1.46 + 1.47 + struct HandleChangeData { 1.48 + nsRefPtr<MediaQueryList> mql; 1.49 + nsRefPtr<mozilla::dom::MediaQueryListListener> callback; 1.50 + }; 1.51 + 1.52 + typedef FallibleTArray< nsRefPtr<mozilla::dom::MediaQueryListListener> > CallbackList; 1.53 + typedef FallibleTArray<HandleChangeData> NotifyList; 1.54 + 1.55 + // Appends listeners that need notification to aListenersToNotify 1.56 + void MediumFeaturesChanged(NotifyList &aListenersToNotify); 1.57 + 1.58 + bool HasListeners() const { return !mCallbacks.IsEmpty(); } 1.59 + 1.60 + void RemoveAllListeners(); 1.61 + 1.62 + JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; 1.63 + 1.64 + // WebIDL methods 1.65 + void GetMedia(nsAString& aMedia); 1.66 + bool Matches(); 1.67 + void AddListener(mozilla::dom::MediaQueryListListener& aListener); 1.68 + void RemoveListener(mozilla::dom::MediaQueryListListener& aListener); 1.69 + 1.70 +private: 1.71 + void RecomputeMatches(); 1.72 + 1.73 + // We only need a pointer to the pres context to support lazy 1.74 + // reevaluation following dynamic changes. However, this lazy 1.75 + // reevaluation is perhaps somewhat important, since some usage 1.76 + // patterns may involve the creation of large numbers of 1.77 + // MediaQueryList objects which almost immediately become garbage 1.78 + // (after a single call to the .matches getter). 1.79 + // 1.80 + // This pointer does make us a little more dependent on cycle 1.81 + // collection. 1.82 + // 1.83 + // We have a non-null mPresContext for our entire lifetime except 1.84 + // after cycle collection unlinking. Having a non-null mPresContext 1.85 + // is equivalent to being in that pres context's mDOMMediaQueryLists 1.86 + // linked list. 1.87 + nsRefPtr<nsPresContext> mPresContext; 1.88 + 1.89 + nsRefPtr<nsMediaList> mMediaList; 1.90 + bool mMatches; 1.91 + bool mMatchesValid; 1.92 + CallbackList mCallbacks; 1.93 +}; 1.94 + 1.95 +} // namespace dom 1.96 +} // namespace mozilla 1.97 + 1.98 +#endif /* !defined(mozilla_dom_MediaQueryList_h) */