2011-04-21 20:17:31 -07:00
|
|
|
/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2011-04-21 20:17:31 -07:00
|
|
|
|
|
|
|
/* implements DOM interface for querying and observing media queries */
|
|
|
|
|
|
|
|
#include "nsDOMMediaQueryList.h"
|
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsIMediaList.h"
|
|
|
|
#include "nsCSSParser.h"
|
2011-08-11 06:29:50 -07:00
|
|
|
#include "nsDOMClassInfoID.h" // DOMCI_DATA
|
2011-04-21 20:17:31 -07:00
|
|
|
|
|
|
|
nsDOMMediaQueryList::nsDOMMediaQueryList(nsPresContext *aPresContext,
|
|
|
|
const nsAString &aMediaQueryList)
|
|
|
|
: mPresContext(aPresContext),
|
|
|
|
mMediaList(new nsMediaList),
|
2011-10-17 07:59:28 -07:00
|
|
|
mMatchesValid(false)
|
2011-04-21 20:17:31 -07:00
|
|
|
{
|
|
|
|
PR_INIT_CLIST(this);
|
|
|
|
|
|
|
|
nsCSSParser parser;
|
2011-10-17 07:59:28 -07:00
|
|
|
parser.ParseMediaList(aMediaQueryList, nsnull, 0, mMediaList, false);
|
2011-04-21 20:17:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMMediaQueryList::~nsDOMMediaQueryList()
|
|
|
|
{
|
|
|
|
if (mPresContext) {
|
|
|
|
PR_REMOVE_LINK(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMMediaQueryList)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsDOMMediaQueryList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mPresContext)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSTARRAY_OF_NSCOMPTR(mListeners)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsDOMMediaQueryList)
|
|
|
|
if (tmp->mPresContext) {
|
|
|
|
PR_REMOVE_LINK(tmp);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mPresContext)
|
|
|
|
}
|
2012-02-29 20:47:55 -08:00
|
|
|
tmp->RemoveAllListeners();
|
2011-04-21 20:17:31 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
DOMCI_DATA(MediaQueryList, nsDOMMediaQueryList)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsDOMMediaQueryList)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMMediaQueryList)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(nsDOMMediaQueryList)
|
|
|
|
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MediaQueryList)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMMediaQueryList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMMediaQueryList)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMMediaQueryList::GetMedia(nsAString &aMedia)
|
|
|
|
{
|
|
|
|
mMediaList->GetText(aMedia);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsDOMMediaQueryList::GetMatches(bool *aMatches)
|
2011-04-21 20:17:31 -07:00
|
|
|
{
|
|
|
|
if (!mMatchesValid) {
|
2012-02-29 20:47:55 -08:00
|
|
|
NS_ABORT_IF_FALSE(!HasListeners(),
|
2011-04-21 20:17:31 -07:00
|
|
|
"when listeners present, must keep mMatches current");
|
|
|
|
RecomputeMatches();
|
|
|
|
}
|
|
|
|
|
|
|
|
*aMatches = mMatches;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMMediaQueryList::AddListener(nsIDOMMediaQueryListListener *aListener)
|
|
|
|
{
|
2012-02-29 20:47:55 -08:00
|
|
|
if (!aListener) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HasListeners()) {
|
|
|
|
// 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.
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
}
|
|
|
|
|
2011-04-21 20:17:31 -07:00
|
|
|
if (!mMatchesValid) {
|
2012-02-29 20:47:55 -08:00
|
|
|
NS_ABORT_IF_FALSE(!HasListeners(),
|
2011-04-21 20:17:31 -07:00
|
|
|
"when listeners present, must keep mMatches current");
|
|
|
|
RecomputeMatches();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mListeners.Contains(aListener)) {
|
|
|
|
mListeners.AppendElement(aListener);
|
2012-02-29 20:47:55 -08:00
|
|
|
if (!HasListeners()) {
|
|
|
|
// Append failed; undo the AddRef above.
|
|
|
|
NS_RELEASE_THIS();
|
|
|
|
}
|
2011-04-21 20:17:31 -07:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsDOMMediaQueryList::RemoveListener(nsIDOMMediaQueryListListener *aListener)
|
|
|
|
{
|
2012-02-29 20:47:55 -08:00
|
|
|
bool removed = mListeners.RemoveElement(aListener);
|
2011-04-21 20:17:31 -07:00
|
|
|
NS_ABORT_IF_FALSE(!mListeners.Contains(aListener),
|
|
|
|
"duplicate occurrence of listeners");
|
2012-02-29 20:47:55 -08:00
|
|
|
|
|
|
|
if (removed && !HasListeners()) {
|
|
|
|
// See NS_ADDREF_THIS() in AddListener.
|
|
|
|
NS_RELEASE_THIS();
|
|
|
|
}
|
|
|
|
|
2011-04-21 20:17:31 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-29 20:47:55 -08:00
|
|
|
void
|
|
|
|
nsDOMMediaQueryList::RemoveAllListeners()
|
|
|
|
{
|
|
|
|
bool hadListeners = HasListeners();
|
|
|
|
mListeners.Clear();
|
|
|
|
if (hadListeners) {
|
|
|
|
// See NS_ADDREF_THIS() in AddListener.
|
|
|
|
NS_RELEASE_THIS();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-21 20:17:31 -07:00
|
|
|
void
|
|
|
|
nsDOMMediaQueryList::RecomputeMatches()
|
|
|
|
{
|
|
|
|
if (!mPresContext) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mMatches = mMediaList->Matches(mPresContext, nsnull);
|
2011-10-17 07:59:28 -07:00
|
|
|
mMatchesValid = true;
|
2011-04-21 20:17:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMMediaQueryList::MediumFeaturesChanged(NotifyList &aListenersToNotify)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mMatchesValid = false;
|
2011-04-21 20:17:31 -07:00
|
|
|
|
|
|
|
if (mListeners.Length()) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool oldMatches = mMatches;
|
2011-04-21 20:17:31 -07:00
|
|
|
RecomputeMatches();
|
|
|
|
if (mMatches != oldMatches) {
|
|
|
|
for (PRUint32 i = 0, i_end = mListeners.Length(); i != i_end; ++i) {
|
|
|
|
HandleChangeData *d = aListenersToNotify.AppendElement();
|
|
|
|
if (d) {
|
|
|
|
d->mql = this;
|
|
|
|
d->listener = mListeners[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|