2014-10-03 11:15:25 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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 {
|
|
|
|
|
2014-10-03 11:15:25 -07:00
|
|
|
MediaQueryList::MediaQueryList(nsIDocument *aDocument,
|
2013-12-16 06:03:19 -08:00
|
|
|
const nsAString &aMediaQueryList)
|
2014-10-03 11:15:25 -07:00
|
|
|
: mDocument(aDocument),
|
2011-04-21 20:17:31 -07:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-12-16 06:03:19 -08:00
|
|
|
MediaQueryList::~MediaQueryList()
|
2011-04-21 20:17:31 -07:00
|
|
|
{
|
2014-10-03 11:15:25 -07:00
|
|
|
if (mDocument) {
|
2011-04-21 20:17:31 -07:00
|
|
|
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)
|
2014-10-03 11:15:25 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocument)
|
2013-12-16 06:03:34 -08:00
|
|
|
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)
|
2014-10-03 11:15:25 -07:00
|
|
|
if (tmp->mDocument) {
|
2013-12-16 06:03:34 -08:00
|
|
|
PR_REMOVE_LINK(tmp);
|
2014-10-03 11:15:25 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocument)
|
2013-12-16 06:03:34 -08:00
|
|
|
}
|
|
|
|
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(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
|
|
|
|
2013-12-17 05:40:06 -08:00
|
|
|
void
|
2013-12-16 06:03:19 -08:00
|
|
|
MediaQueryList::GetMedia(nsAString &aMedia)
|
2011-04-21 20:17:31 -07:00
|
|
|
{
|
|
|
|
mMediaList->GetText(aMedia);
|
2013-12-16 06:03:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MediaQueryList::Matches()
|
2011-04-21 20:17:31 -07:00
|
|
|
{
|
|
|
|
if (!mMatchesValid) {
|
2015-02-09 14:34:50 -08:00
|
|
|
MOZ_ASSERT(!HasListeners(),
|
|
|
|
"when listeners present, must keep mMatches current");
|
2011-04-21 20:17:31 -07:00
|
|
|
RecomputeMatches();
|
|
|
|
}
|
|
|
|
|
2013-12-16 06:03:34 -08:00
|
|
|
return mMatches;
|
2011-04-21 20:17:31 -07:00
|
|
|
}
|
|
|
|
|
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) {
|
2015-02-09 14:34:50 -08:00
|
|
|
MOZ_ASSERT(!HasListeners(),
|
|
|
|
"when listeners present, must keep mMatches current");
|
2013-12-16 06:03:34 -08:00
|
|
|
RecomputeMatches();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mCallbacks.Length(); ++i) {
|
2013-12-16 10:06:35 -08:00
|
|
|
if (aListener == *mCallbacks[i]) {
|
2013-12-16 06:03:34 -08:00
|
|
|
// Already registered
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-12 23:19:12 -07:00
|
|
|
if (!mCallbacks.AppendElement(&aListener, fallible)) {
|
|
|
|
if (!HasListeners()) {
|
|
|
|
// Append failed; undo the AddRef above.
|
|
|
|
NS_RELEASE_THIS();
|
|
|
|
}
|
2013-12-16 06:03:34 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaQueryList::RemoveListener(MediaQueryListListener& aListener)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mCallbacks.Length(); ++i) {
|
2013-12-16 10:06:35 -08:00
|
|
|
if (aListener == *mCallbacks[i]) {
|
2013-12-16 06:03:34 -08:00
|
|
|
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();
|
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
|
|
|
{
|
2014-10-03 11:15:25 -07:00
|
|
|
if (!mDocument) {
|
2011-04-21 20:17:31 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-03 11:15:25 -07:00
|
|
|
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);
|
2011-10-17 07:59:28 -07:00
|
|
|
mMatchesValid = true;
|
2011-04-21 20:17:31 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-07-12 23:19:12 -07:00
|
|
|
MediaQueryList::MediumFeaturesChanged(
|
|
|
|
nsTArray<HandleChangeData>& 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) {
|
2013-12-16 06:03:34 -08:00
|
|
|
for (uint32_t i = 0, i_end = mCallbacks.Length(); i != i_end; ++i) {
|
2015-05-28 11:07:44 -07:00
|
|
|
HandleChangeData *d = aListenersToNotify.AppendElement(fallible);
|
2013-12-16 06:03:34 -08:00
|
|
|
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
|
|
|
|
{
|
2014-10-03 11:15:25 -07:00
|
|
|
return mDocument;
|
2013-12-16 06:03:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 07:13:33 -07:00
|
|
|
MediaQueryList::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-12-16 06:03:34 -08:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 07:13:33 -07:00
|
|
|
return MediaQueryListBinding::Wrap(aCx, this, aGivenProto);
|
2013-12-16 06:03:34 -08:00
|
|
|
}
|
|
|
|
|
2013-12-16 06:03:19 -08:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|