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;
|
2012-07-30 07:20:58 -07:00
|
|
|
parser.ParseMediaList(aMediaQueryList, nullptr, 0, mMediaList, false);
|
2011-04-21 20:17:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMMediaQueryList::~nsDOMMediaQueryList()
|
|
|
|
{
|
|
|
|
if (mPresContext) {
|
|
|
|
PR_REMOVE_LINK(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsDOMMediaQueryList)
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPresContext)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mListeners)
|
2011-04-21 20:17:31 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsDOMMediaQueryList)
|
|
|
|
if (tmp->mPresContext) {
|
|
|
|
PR_REMOVE_LINK(tmp);
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPresContext)
|
2011-04-21 20:17:31 -07:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mMatches = mMediaList->Matches(mPresContext, nullptr);
|
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) {
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0, i_end = mListeners.Length(); i != i_end; ++i) {
|
2011-04-21 20:17:31 -07:00
|
|
|
HandleChangeData *d = aListenersToNotify.AppendElement();
|
|
|
|
if (d) {
|
|
|
|
d->mql = this;
|
|
|
|
d->listener = mListeners[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|