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 */
|
|
|
|
|
2013-12-16 06:03:19 -08:00
|
|
|
#include "mozilla/dom/MediaQueryList.h"
|
2011-04-21 20:17:31 -07:00
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsIMediaList.h"
|
|
|
|
#include "nsCSSParser.h"
|
2013-12-16 06:03:34 -08:00
|
|
|
#include "nsIDocument.h"
|
2013-12-16 06:03:19 -08:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
MediaQueryList::MediaQueryList(nsPresContext *aPresContext,
|
|
|
|
const nsAString &aMediaQueryList)
|
2011-04-21 20:17:31 -07:00
|
|
|
: 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);
|
|
|
|
|
2013-12-16 06:03:34 -08:00
|
|
|
SetIsDOMBinding();
|
|
|
|
|
2011-04-21 20:17:31 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-12-16 06:03:19 -08:00
|
|
|
MediaQueryList::~MediaQueryList()
|
2011-04-21 20:17:31 -07:00
|
|
|
{
|
|
|
|
if (mPresContext) {
|
|
|
|
PR_REMOVE_LINK(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-16 06:03:19 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(MediaQueryList)
|
2013-08-01 18:29:05 -07:00
|
|
|
|
2013-12-16 06:03:19 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(MediaQueryList)
|
2013-12-16 06:03:34 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPresContext)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mListeners)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCallbacks)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
2011-04-21 20:17:31 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2013-12-16 06:03:19 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(MediaQueryList)
|
2013-12-16 06:03:34 -08:00
|
|
|
if (tmp->mPresContext) {
|
|
|
|
PR_REMOVE_LINK(tmp);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPresContext)
|
|
|
|
}
|
|
|
|
tmp->RemoveAllListeners();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
2011-04-21 20:17:31 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2013-12-16 06:03:34 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(MediaQueryList)
|
|
|
|
|
2013-12-16 06:03:19 -08:00
|
|
|
NS_INTERFACE_MAP_BEGIN(MediaQueryList)
|
2013-12-16 06:03:34 -08:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2011-04-21 20:17:31 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMMediaQueryList)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2013-12-16 06:03:19 -08:00
|
|
|
NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(MediaQueryList)
|
2011-04-21 20:17:31 -07:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2013-12-16 06:03:19 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(MediaQueryList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(MediaQueryList)
|
2011-04-21 20:17:31 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-12-16 06:03:19 -08:00
|
|
|
MediaQueryList::GetMedia(nsAString &aMedia)
|
2011-04-21 20:17:31 -07:00
|
|
|
{
|
|
|
|
mMediaList->GetText(aMedia);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-12-16 06:03:19 -08:00
|
|
|
MediaQueryList::GetMatches(bool *aMatches)
|
2013-12-16 06:03:34 -08:00
|
|
|
{
|
|
|
|
*aMatches = Matches();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MediaQueryList::Matches()
|
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();
|
|
|
|
}
|
|
|
|
|
2013-12-16 06:03:34 -08:00
|
|
|
return mMatches;
|
2011-04-21 20:17:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2013-12-16 06:03:19 -08:00
|
|
|
MediaQueryList::AddListener(nsIDOMMediaQueryListListener *aListener)
|
2011-04-21 20:17:31 -07:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-16 06:03:34 -08:00
|
|
|
void
|
|
|
|
MediaQueryList::AddListener(MediaQueryListListener& aListener)
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mMatchesValid) {
|
|
|
|
NS_ABORT_IF_FALSE(!HasListeners(),
|
|
|
|
"when listeners present, must keep mMatches current");
|
|
|
|
RecomputeMatches();
|
|
|
|
}
|
|
|
|
|
|
|
|
CallbackType callback(&aListener);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mCallbacks.Length(); ++i) {
|
|
|
|
CallbackType thisCallback(mCallbacks[i]);
|
|
|
|
if (callback == thisCallback) {
|
|
|
|
// Already registered
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mCallbacks.AppendElement(&aListener);
|
|
|
|
if (!HasListeners()) {
|
|
|
|
// Append failed; undo the AddRef above.
|
|
|
|
NS_RELEASE_THIS();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-21 20:17:31 -07:00
|
|
|
NS_IMETHODIMP
|
2013-12-16 06:03:19 -08:00
|
|
|
MediaQueryList::RemoveListener(nsIDOMMediaQueryListListener *aListener)
|
2011-04-21 20:17:31 -07:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-16 06:03:34 -08:00
|
|
|
void
|
|
|
|
MediaQueryList::RemoveListener(MediaQueryListListener& aListener)
|
|
|
|
{
|
|
|
|
CallbackType callback(&aListener);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mCallbacks.Length(); ++i) {
|
|
|
|
CallbackType thisCallback(mCallbacks[i]);
|
|
|
|
if (callback == thisCallback) {
|
|
|
|
mCallbacks.RemoveElementAt(i);
|
|
|
|
if (!HasListeners()) {
|
|
|
|
// See NS_ADDREF_THIS() in AddListener.
|
|
|
|
NS_RELEASE_THIS();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-29 20:47:55 -08:00
|
|
|
void
|
2013-12-16 06:03:19 -08:00
|
|
|
MediaQueryList::RemoveAllListeners()
|
2012-02-29 20:47:55 -08:00
|
|
|
{
|
|
|
|
bool hadListeners = HasListeners();
|
|
|
|
mListeners.Clear();
|
2013-12-16 06:03:34 -08:00
|
|
|
mCallbacks.Clear();
|
2012-02-29 20:47:55 -08:00
|
|
|
if (hadListeners) {
|
|
|
|
// See NS_ADDREF_THIS() in AddListener.
|
|
|
|
NS_RELEASE_THIS();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-21 20:17:31 -07:00
|
|
|
void
|
2013-12-16 06:03:19 -08:00
|
|
|
MediaQueryList::RecomputeMatches()
|
2011-04-21 20:17:31 -07:00
|
|
|
{
|
|
|
|
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
|
2013-12-16 06:03:19 -08:00
|
|
|
MediaQueryList::MediumFeaturesChanged(NotifyList &aListenersToNotify)
|
2011-04-21 20:17:31 -07:00
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mMatchesValid = false;
|
2011-04-21 20:17:31 -07:00
|
|
|
|
2013-12-16 06:03:34 -08:00
|
|
|
if (HasListeners()) {
|
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];
|
|
|
|
}
|
|
|
|
}
|
2013-12-16 06:03:34 -08:00
|
|
|
for (uint32_t i = 0, i_end = mCallbacks.Length(); i != i_end; ++i) {
|
|
|
|
HandleChangeData *d = aListenersToNotify.AppendElement();
|
|
|
|
if (d) {
|
|
|
|
d->mql = this;
|
|
|
|
d->callback = mCallbacks[i];
|
|
|
|
}
|
|
|
|
}
|
2011-04-21 20:17:31 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-16 06:03:19 -08:00
|
|
|
|
2013-12-16 06:03:34 -08:00
|
|
|
nsISupports*
|
|
|
|
MediaQueryList::GetParentObject() const
|
|
|
|
{
|
|
|
|
if (!mPresContext) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return mPresContext->Document();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
MediaQueryList::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
|
|
|
{
|
|
|
|
return MediaQueryListBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|
|
|
|
|
2013-12-16 06:03:19 -08:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|