2016-11-09 09:28:24 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
/* base class for representation of media lists */
|
|
|
|
|
|
|
|
#include "mozilla/dom/MediaList.h"
|
|
|
|
|
|
|
|
#include "mozAutoDocUpdate.h"
|
2019-04-04 14:25:31 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2016-11-09 09:28:24 +03:00
|
|
|
#include "mozilla/dom/MediaListBinding.h"
|
2018-04-24 16:41:40 +03:00
|
|
|
#include "mozilla/ServoBindings.h"
|
|
|
|
#include "mozilla/ServoStyleSet.h"
|
2016-11-09 09:28:24 +03:00
|
|
|
#include "mozilla/StyleSheetInlines.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaList)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(MediaList)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(MediaList)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(MediaList)
|
|
|
|
|
|
|
|
JSObject* MediaList::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return MediaList_Binding::Wrap(aCx, this, aGivenProto);
|
2016-11-09 09:28:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void MediaList::SetStyleSheet(StyleSheet* aSheet) {
|
|
|
|
MOZ_ASSERT(aSheet == mStyleSheet || !aSheet || !mStyleSheet,
|
|
|
|
"Multiple style sheets competing for one media list");
|
|
|
|
mStyleSheet = aSheet;
|
|
|
|
}
|
|
|
|
|
2020-10-29 13:15:47 +03:00
|
|
|
nsISupports* MediaList::GetParentObject() const { return mStyleSheet; }
|
|
|
|
|
2016-11-09 09:28:24 +03:00
|
|
|
template <typename Func>
|
2020-02-09 06:40:48 +03:00
|
|
|
void MediaList::DoMediaChange(Func aCallback, ErrorResult& aRv) {
|
2019-04-29 08:34:06 +03:00
|
|
|
if (IsReadOnly()) {
|
2020-02-09 06:40:48 +03:00
|
|
|
return;
|
2019-04-29 08:34:06 +03:00
|
|
|
}
|
|
|
|
|
2016-11-09 09:28:24 +03:00
|
|
|
if (mStyleSheet) {
|
|
|
|
mStyleSheet->WillDirty();
|
|
|
|
}
|
|
|
|
|
2020-02-09 06:40:48 +03:00
|
|
|
aCallback(aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
2016-11-09 09:28:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mStyleSheet) {
|
2017-11-28 03:50:45 +03:00
|
|
|
// FIXME(emilio): We should discern between "owned by a rule" (as in @media)
|
|
|
|
// and "owned by a sheet" (as in <style media>), and then pass something
|
|
|
|
// meaningful here.
|
2020-08-10 21:00:44 +03:00
|
|
|
mStyleSheet->RuleChanged(nullptr, StyleRuleChangeKind::Generic);
|
2016-11-09 09:28:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 16:41:40 +03:00
|
|
|
already_AddRefed<MediaList> MediaList::Clone() {
|
|
|
|
RefPtr<MediaList> clone =
|
|
|
|
new MediaList(Servo_MediaList_DeepClone(mRawList).Consume());
|
|
|
|
return clone.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaList::MediaList() : mRawList(Servo_MediaList_Create().Consume()) {}
|
|
|
|
|
2020-12-17 17:04:35 +03:00
|
|
|
MediaList::MediaList(const nsACString& aMedia, CallerType aCallerType)
|
2018-04-24 16:41:40 +03:00
|
|
|
: MediaList() {
|
|
|
|
SetTextInternal(aMedia, aCallerType);
|
|
|
|
}
|
|
|
|
|
2020-12-17 17:04:35 +03:00
|
|
|
void MediaList::GetText(nsACString& aMediaText) {
|
2018-04-24 16:41:40 +03:00
|
|
|
Servo_MediaList_GetText(mRawList, &aMediaText);
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:09:24 +03:00
|
|
|
/* static */
|
2020-12-17 17:04:35 +03:00
|
|
|
already_AddRefed<MediaList> MediaList::Create(const nsACString& aMedia,
|
2019-02-26 01:09:24 +03:00
|
|
|
CallerType aCallerType) {
|
2018-04-24 16:41:40 +03:00
|
|
|
RefPtr<MediaList> mediaList = new MediaList(aMedia, aCallerType);
|
2018-03-28 18:31:46 +03:00
|
|
|
return mediaList.forget();
|
2017-04-10 03:01:38 +03:00
|
|
|
}
|
|
|
|
|
2020-12-17 17:04:35 +03:00
|
|
|
void MediaList::SetText(const nsACString& aMediaText) {
|
2019-04-29 08:34:06 +03:00
|
|
|
if (IsReadOnly()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-24 16:41:40 +03:00
|
|
|
SetTextInternal(aMediaText, CallerType::NonSystem);
|
|
|
|
}
|
|
|
|
|
2020-12-17 17:04:35 +03:00
|
|
|
void MediaList::GetMediaText(nsACString& aMediaText) { GetText(aMediaText); }
|
2016-11-09 09:28:24 +03:00
|
|
|
|
2020-12-17 17:04:35 +03:00
|
|
|
void MediaList::SetTextInternal(const nsACString& aMediaText,
|
2018-04-24 16:41:40 +03:00
|
|
|
CallerType aCallerType) {
|
2020-12-17 17:04:35 +03:00
|
|
|
Servo_MediaList_SetText(mRawList, &aMediaText, aCallerType);
|
2018-04-24 16:41:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t MediaList::Length() { return Servo_MediaList_GetLength(mRawList); }
|
|
|
|
|
|
|
|
void MediaList::IndexedGetter(uint32_t aIndex, bool& aFound,
|
2020-12-17 17:04:35 +03:00
|
|
|
nsACString& aReturn) {
|
2018-04-24 16:41:40 +03:00
|
|
|
aFound = Servo_MediaList_GetMediumAt(mRawList, aIndex, &aReturn);
|
|
|
|
if (!aFound) {
|
2020-12-17 17:04:35 +03:00
|
|
|
aReturn.SetIsVoid(true);
|
2018-04-24 16:41:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 17:04:35 +03:00
|
|
|
void MediaList::Delete(const nsACString& aOldMedium, ErrorResult& aRv) {
|
2019-04-29 08:34:06 +03:00
|
|
|
MOZ_ASSERT(!IsReadOnly());
|
2020-12-17 17:04:35 +03:00
|
|
|
if (Servo_MediaList_DeleteMedium(mRawList, &aOldMedium)) {
|
2020-02-09 06:40:48 +03:00
|
|
|
return;
|
2018-04-24 16:41:40 +03:00
|
|
|
}
|
2020-02-09 06:40:48 +03:00
|
|
|
aRv.ThrowNotFoundError("Medium not in list");
|
2018-04-24 16:41:40 +03:00
|
|
|
}
|
|
|
|
|
2019-04-04 14:25:31 +03:00
|
|
|
bool MediaList::Matches(const Document& aDocument) const {
|
|
|
|
const RawServoStyleSet* rawSet =
|
|
|
|
aDocument.StyleSetForPresShellOrMediaQueryEvaluation()->RawSet();
|
2018-04-24 16:41:40 +03:00
|
|
|
MOZ_ASSERT(rawSet, "The RawServoStyleSet should be valid!");
|
|
|
|
return Servo_MediaList_Matches(mRawList, rawSet);
|
|
|
|
}
|
|
|
|
|
2020-12-17 17:04:35 +03:00
|
|
|
void MediaList::Append(const nsACString& aNewMedium, ErrorResult& aRv) {
|
2019-04-29 08:34:06 +03:00
|
|
|
MOZ_ASSERT(!IsReadOnly());
|
2018-04-24 16:41:40 +03:00
|
|
|
if (aNewMedium.IsEmpty()) {
|
2020-02-09 06:40:48 +03:00
|
|
|
// XXXbz per spec there should not be an exception here, as far as
|
|
|
|
// I can tell...
|
|
|
|
aRv.ThrowNotFoundError("Empty medium");
|
|
|
|
return;
|
2018-04-24 16:41:40 +03:00
|
|
|
}
|
2020-12-17 17:04:35 +03:00
|
|
|
Servo_MediaList_AppendMedium(mRawList, &aNewMedium);
|
2018-04-24 16:41:40 +03:00
|
|
|
}
|
|
|
|
|
2020-12-17 17:04:35 +03:00
|
|
|
void MediaList::SetMediaText(const nsACString& aMediaText) {
|
2020-02-09 06:40:48 +03:00
|
|
|
DoMediaChange([&](ErrorResult& aRv) { SetText(aMediaText); }, IgnoreErrors());
|
2016-11-09 09:28:24 +03:00
|
|
|
}
|
|
|
|
|
2020-12-17 17:04:35 +03:00
|
|
|
void MediaList::Item(uint32_t aIndex, nsACString& aReturn) {
|
2016-11-09 09:28:24 +03:00
|
|
|
bool dummy;
|
|
|
|
IndexedGetter(aIndex, dummy, aReturn);
|
|
|
|
}
|
|
|
|
|
2020-12-17 17:04:35 +03:00
|
|
|
void MediaList::DeleteMedium(const nsACString& aOldMedium, ErrorResult& aRv) {
|
2020-02-09 06:40:48 +03:00
|
|
|
DoMediaChange([&](ErrorResult& aRv) { Delete(aOldMedium, aRv); }, aRv);
|
2016-11-09 09:28:24 +03:00
|
|
|
}
|
|
|
|
|
2020-12-17 17:04:35 +03:00
|
|
|
void MediaList::AppendMedium(const nsACString& aNewMedium, ErrorResult& aRv) {
|
2020-02-09 06:40:48 +03:00
|
|
|
DoMediaChange([&](ErrorResult& aRv) { Append(aNewMedium, aRv); }, aRv);
|
2016-11-09 09:28:24 +03:00
|
|
|
}
|
|
|
|
|
2018-07-06 04:09:12 +03:00
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(ServoMediaListMallocSizeOf)
|
|
|
|
MOZ_DEFINE_MALLOC_ENCLOSING_SIZE_OF(ServoMediaListMallocEnclosingSizeOf)
|
|
|
|
|
|
|
|
size_t MediaList::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
|
|
|
|
size_t n = 0;
|
|
|
|
n += Servo_MediaList_SizeOfIncludingThis(ServoMediaListMallocSizeOf,
|
|
|
|
ServoMediaListMallocEnclosingSizeOf,
|
|
|
|
mRawList);
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2019-04-29 08:34:06 +03:00
|
|
|
bool MediaList::IsReadOnly() const {
|
|
|
|
return mStyleSheet && mStyleSheet->IsReadOnly();
|
|
|
|
}
|
|
|
|
|
2016-11-09 09:28:24 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|