123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
- /* This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
- /* implements DOM interface for querying and observing media queries */
- #include "mozilla/dom/MediaQueryList.h"
- #include "mozilla/dom/MediaQueryListEvent.h"
- #include "mozilla/dom/EventTarget.h"
- #include "mozilla/dom/EventTargetBinding.h"
- #include "nsPresContext.h"
- #include "nsIMediaList.h"
- #include "nsCSSParser.h"
- #include "nsIDocument.h"
- // Fixed event target type
- #define ONCHANGE_STRING NS_LITERAL_STRING("change")
- namespace mozilla {
- namespace dom {
- MediaQueryList::MediaQueryList(nsIDocument *aDocument,
- const nsAString &aMediaQueryList)
- : mDocument(aDocument)
- , mMediaList(new nsMediaList)
- , mMatchesValid(false)
- , mIsKeptAlive(false)
- {
- PR_INIT_CLIST(this);
- nsCSSParser parser;
- parser.ParseMediaList(aMediaQueryList, nullptr, 0, mMediaList, false);
- }
- MediaQueryList::~MediaQueryList()
- {
- if (mDocument) {
- PR_REMOVE_LINK(this);
- }
- }
- NS_IMPL_CYCLE_COLLECTION_CLASS(MediaQueryList)
- NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(MediaQueryList, DOMEventTargetHelper)
- NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocument)
- NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
- NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(MediaQueryList, DOMEventTargetHelper)
- if (tmp->mDocument) {
- PR_REMOVE_LINK(tmp);
- NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocument)
- }
- tmp->Disconnect();
- NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
- NS_IMPL_CYCLE_COLLECTION_UNLINK_END
- NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(MediaQueryList)
- NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
- NS_IMPL_ADDREF_INHERITED(MediaQueryList, DOMEventTargetHelper)
- NS_IMPL_RELEASE_INHERITED(MediaQueryList, DOMEventTargetHelper)
- void
- MediaQueryList::GetMedia(nsAString &aMedia)
- {
- mMediaList->GetText(aMedia);
- }
- bool
- MediaQueryList::Matches()
- {
- if (!mMatchesValid) {
- MOZ_ASSERT(!HasListeners(),
- "when listeners present, must keep mMatches current");
- RecomputeMatches();
- }
- return mMatches;
- }
- void
- MediaQueryList::AddListener(EventListener* aListener, ErrorResult& aRv)
- {
- if (!aListener) {
- return;
- }
- AddEventListenerOptionsOrBoolean options;
- options.SetAsBoolean() = false;
- AddEventListener(ONCHANGE_STRING, aListener, options, Nullable<bool>(), aRv);
- }
- void
- MediaQueryList::AddEventListener(const nsAString& aType,
- EventListener* aCallback,
- const AddEventListenerOptionsOrBoolean& aOptions,
- const dom::Nullable<bool>& aWantsUntrusted,
- ErrorResult& aRv)
- {
- if (!mMatchesValid) {
- MOZ_ASSERT(!HasListeners(),
- "when listeners present, must keep mMatches current");
- RecomputeMatches();
- }
- DOMEventTargetHelper::AddEventListener(aType, aCallback, aOptions,
- aWantsUntrusted, aRv);
- if (aRv.Failed()) {
- return;
- }
- UpdateMustKeepAlive();
- }
- void
- MediaQueryList::RemoveListener(EventListener* aListener, ErrorResult& aRv)
- {
- if (!aListener) {
- return;
- }
- EventListenerOptionsOrBoolean options;
- options.SetAsBoolean() = false;
- RemoveEventListener(ONCHANGE_STRING, aListener, options, aRv);
- }
- void
- MediaQueryList::RemoveEventListener(const nsAString& aType,
- EventListener* aCallback,
- const EventListenerOptionsOrBoolean& aOptions,
- ErrorResult& aRv)
- {
- DOMEventTargetHelper::RemoveEventListener(aType, aCallback, aOptions, aRv);
- if (aRv.Failed()) {
- return;
- }
- UpdateMustKeepAlive();
- }
- EventHandlerNonNull*
- MediaQueryList::GetOnchange()
- {
- if (NS_IsMainThread()) {
- return GetEventHandler(nsGkAtoms::onchange, EmptyString());
- }
- return GetEventHandler(nullptr, ONCHANGE_STRING);
- }
- void
- MediaQueryList::SetOnchange(EventHandlerNonNull* aCallback)
- {
- if (NS_IsMainThread()) {
- SetEventHandler(nsGkAtoms::onchange, EmptyString(), aCallback);
- } else {
- SetEventHandler(nullptr, ONCHANGE_STRING, aCallback);
- }
- UpdateMustKeepAlive();
- }
- void
- MediaQueryList::UpdateMustKeepAlive()
- {
- bool toKeepAlive = HasListeners();
- if (toKeepAlive == mIsKeptAlive) {
- return;
- }
- // When we have listeners, the pres context owns a reference to
- // this. This is a cyclic reference that can only be broken by
- // cycle collection.
- mIsKeptAlive = toKeepAlive;
- if (toKeepAlive) {
- NS_ADDREF_THIS();
- } else {
- NS_RELEASE_THIS();
- }
- }
- bool
- MediaQueryList::HasListeners()
- {
- return HasListenersFor(ONCHANGE_STRING);
- }
- void
- MediaQueryList::Disconnect()
- {
- DisconnectFromOwner();
- if (mIsKeptAlive) {
- mIsKeptAlive = false;
- // See NS_ADDREF_THIS() in AddListener.
- NS_RELEASE_THIS();
- }
- }
- void
- MediaQueryList::RecomputeMatches()
- {
- if (!mDocument) {
- return;
- }
- if (mDocument->GetParentDocument()) {
- // Flush frames on the parent so our prescontext will get
- // recreated as needed.
- mDocument->GetParentDocument()->FlushPendingNotifications(Flush_Frames);
- // That might have killed our document, so recheck that.
- if (!mDocument) {
- return;
- }
- }
- nsIPresShell* shell = mDocument->GetShell();
- if (!shell) {
- // XXXbz What's the right behavior here? Spec doesn't say.
- return;
- }
- nsPresContext* presContext = shell->GetPresContext();
- if (!presContext) {
- // XXXbz What's the right behavior here? Spec doesn't say.
- return;
- }
- mMatches = mMediaList->Matches(presContext, nullptr);
- mMatchesValid = true;
- }
- nsISupports*
- MediaQueryList::GetParentObject() const
- {
- return mDocument;
- }
- JSObject*
- MediaQueryList::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
- {
- return MediaQueryListBinding::Wrap(aCx, this, aGivenProto);
- }
- void
- MediaQueryList::MaybeNotify()
- {
- mMatchesValid = false;
- if (!HasListeners()) {
- return;
- }
- bool oldMatches = mMatches;
- RecomputeMatches();
- // No need to notify the change.
- if (mMatches == oldMatches) {
- return;
- }
- MediaQueryListEventInit init;
- init.mBubbles = false;
- init.mCancelable = false;
- init.mMatches = mMatches;
- mMediaList->GetText(init.mMedia);
- RefPtr<MediaQueryListEvent> event =
- MediaQueryListEvent::Constructor(this, ONCHANGE_STRING, init);
- event->SetTrusted(true);
- bool dummy;
- DispatchEvent(event, &dummy);
- }
- } // namespace dom
- } // namespace mozilla
|