2014-10-03 22:15:25 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2011-04-22 07:17:31 +04:00
|
|
|
/* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
|
2012-05-21 15:12:37 +04: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-22 07:17:31 +04:00
|
|
|
|
|
|
|
/* implements DOM interface for querying and observing media queries */
|
|
|
|
|
2013-12-16 18:03:19 +04:00
|
|
|
#include "mozilla/dom/MediaQueryList.h"
|
2011-04-22 07:17:31 +04:00
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsIMediaList.h"
|
|
|
|
#include "nsCSSParser.h"
|
2013-12-16 18:03:34 +04:00
|
|
|
#include "nsIDocument.h"
|
2013-12-16 18:03:19 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-10-03 22:15:25 +04:00
|
|
|
MediaQueryList::MediaQueryList(nsIDocument *aDocument,
|
2013-12-16 18:03:19 +04:00
|
|
|
const nsAString &aMediaQueryList)
|
2014-10-03 22:15:25 +04:00
|
|
|
: mDocument(aDocument),
|
2011-04-22 07:17:31 +04:00
|
|
|
mMediaList(new nsMediaList),
|
2011-10-17 18:59:28 +04:00
|
|
|
mMatchesValid(false)
|
2011-04-22 07:17:31 +04:00
|
|
|
{
|
|
|
|
PR_INIT_CLIST(this);
|
|
|
|
|
|
|
|
nsCSSParser parser;
|
2012-07-30 18:20:58 +04:00
|
|
|
parser.ParseMediaList(aMediaQueryList, nullptr, 0, mMediaList, false);
|
2011-04-22 07:17:31 +04:00
|
|
|
}
|
|
|
|
|
2013-12-16 18:03:19 +04:00
|
|
|
MediaQueryList::~MediaQueryList()
|
2011-04-22 07:17:31 +04:00
|
|
|
{
|
2014-10-03 22:15:25 +04:00
|
|
|
if (mDocument) {
|
2011-04-22 07:17:31 +04:00
|
|
|
PR_REMOVE_LINK(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-16 18:03:19 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(MediaQueryList)
|
2013-08-02 05:29:05 +04:00
|
|
|
|
2013-12-16 18:03:19 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(MediaQueryList)
|
2014-10-03 22:15:25 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocument)
|
2013-12-16 18:03:34 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCallbacks)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
2011-04-22 07:17:31 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2013-12-16 18:03:19 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(MediaQueryList)
|
2014-10-03 22:15:25 +04:00
|
|
|
if (tmp->mDocument) {
|
2013-12-16 18:03:34 +04:00
|
|
|
PR_REMOVE_LINK(tmp);
|
2014-10-03 22:15:25 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocument)
|
2013-12-16 18:03:34 +04:00
|
|
|
}
|
|
|
|
tmp->RemoveAllListeners();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
2011-04-22 07:17:31 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2013-12-16 18:03:34 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(MediaQueryList)
|
|
|
|
|
2013-12-16 18:03:19 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN(MediaQueryList)
|
2013-12-16 18:03:34 +04:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2011-04-22 07:17:31 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2013-12-16 18:03:19 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(MediaQueryList)
|
2011-04-22 07:17:31 +04:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2013-12-16 18:03:19 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(MediaQueryList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(MediaQueryList)
|
2011-04-22 07:17:31 +04:00
|
|
|
|
2013-12-17 17:40:06 +04:00
|
|
|
void
|
2013-12-16 18:03:19 +04:00
|
|
|
MediaQueryList::GetMedia(nsAString &aMedia)
|
2011-04-22 07:17:31 +04:00
|
|
|
{
|
|
|
|
mMediaList->GetText(aMedia);
|
2013-12-16 18:03:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MediaQueryList::Matches()
|
2011-04-22 07:17:31 +04:00
|
|
|
{
|
|
|
|
if (!mMatchesValid) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!HasListeners(),
|
|
|
|
"when listeners present, must keep mMatches current");
|
2011-04-22 07:17:31 +04:00
|
|
|
RecomputeMatches();
|
|
|
|
}
|
|
|
|
|
2013-12-16 18:03:34 +04:00
|
|
|
return mMatches;
|
2011-04-22 07:17:31 +04:00
|
|
|
}
|
|
|
|
|
2013-12-16 18:03:34 +04: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-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!HasListeners(),
|
|
|
|
"when listeners present, must keep mMatches current");
|
2013-12-16 18:03:34 +04:00
|
|
|
RecomputeMatches();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mCallbacks.Length(); ++i) {
|
2013-12-16 22:06:35 +04:00
|
|
|
if (aListener == *mCallbacks[i]) {
|
2013-12-16 18:03:34 +04:00
|
|
|
// Already registered
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 09:19:12 +03:00
|
|
|
if (!mCallbacks.AppendElement(&aListener, fallible)) {
|
|
|
|
if (!HasListeners()) {
|
|
|
|
// Append failed; undo the AddRef above.
|
|
|
|
NS_RELEASE_THIS();
|
|
|
|
}
|
2013-12-16 18:03:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MediaQueryList::RemoveListener(MediaQueryListListener& aListener)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < mCallbacks.Length(); ++i) {
|
2013-12-16 22:06:35 +04:00
|
|
|
if (aListener == *mCallbacks[i]) {
|
2013-12-16 18:03:34 +04:00
|
|
|
mCallbacks.RemoveElementAt(i);
|
|
|
|
if (!HasListeners()) {
|
|
|
|
// See NS_ADDREF_THIS() in AddListener.
|
|
|
|
NS_RELEASE_THIS();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-01 08:47:55 +04:00
|
|
|
void
|
2013-12-16 18:03:19 +04:00
|
|
|
MediaQueryList::RemoveAllListeners()
|
2012-03-01 08:47:55 +04:00
|
|
|
{
|
|
|
|
bool hadListeners = HasListeners();
|
2013-12-16 18:03:34 +04:00
|
|
|
mCallbacks.Clear();
|
2012-03-01 08:47:55 +04:00
|
|
|
if (hadListeners) {
|
|
|
|
// See NS_ADDREF_THIS() in AddListener.
|
|
|
|
NS_RELEASE_THIS();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-22 07:17:31 +04:00
|
|
|
void
|
2013-12-16 18:03:19 +04:00
|
|
|
MediaQueryList::RecomputeMatches()
|
2011-04-22 07:17:31 +04:00
|
|
|
{
|
2014-10-03 22:15:25 +04:00
|
|
|
if (!mDocument) {
|
2011-04-22 07:17:31 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-03 22:15:25 +04: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 18:59:28 +04:00
|
|
|
mMatchesValid = true;
|
2011-04-22 07:17:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-07-13 09:19:12 +03:00
|
|
|
MediaQueryList::MediumFeaturesChanged(
|
|
|
|
nsTArray<HandleChangeData>& aListenersToNotify)
|
2011-04-22 07:17:31 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
mMatchesValid = false;
|
2011-04-22 07:17:31 +04:00
|
|
|
|
2013-12-16 18:03:34 +04:00
|
|
|
if (HasListeners()) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool oldMatches = mMatches;
|
2011-04-22 07:17:31 +04:00
|
|
|
RecomputeMatches();
|
|
|
|
if (mMatches != oldMatches) {
|
2013-12-16 18:03:34 +04:00
|
|
|
for (uint32_t i = 0, i_end = mCallbacks.Length(); i != i_end; ++i) {
|
2015-05-28 21:07:44 +03:00
|
|
|
HandleChangeData *d = aListenersToNotify.AppendElement(fallible);
|
2013-12-16 18:03:34 +04:00
|
|
|
if (d) {
|
|
|
|
d->mql = this;
|
|
|
|
d->callback = mCallbacks[i];
|
|
|
|
}
|
|
|
|
}
|
2011-04-22 07:17:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-16 18:03:19 +04:00
|
|
|
|
2013-12-16 18:03:34 +04:00
|
|
|
nsISupports*
|
|
|
|
MediaQueryList::GetParentObject() const
|
|
|
|
{
|
2014-10-03 22:15:25 +04:00
|
|
|
return mDocument;
|
2013-12-16 18:03:34 +04: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 17:13:33 +03:00
|
|
|
MediaQueryList::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-12-16 18:03:34 +04: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 17:13:33 +03:00
|
|
|
return MediaQueryListBinding::Wrap(aCx, this, aGivenProto);
|
2013-12-16 18:03:34 +04:00
|
|
|
}
|
|
|
|
|
2013-12-16 18:03:19 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|