Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : /* base class for representation of media lists */
8 :
9 : #include "mozilla/dom/MediaList.h"
10 :
11 : #include "mozAutoDocUpdate.h"
12 : #include "mozilla/dom/MediaListBinding.h"
13 : #include "mozilla/ServoMediaList.h"
14 : #include "mozilla/StyleSheetInlines.h"
15 : #include "nsCSSParser.h"
16 : #include "nsMediaList.h"
17 :
18 : namespace mozilla {
19 : namespace dom {
20 :
21 54 : NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaList)
22 0 : NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
23 0 : NS_INTERFACE_MAP_ENTRY(nsIDOMMediaList)
24 0 : NS_INTERFACE_MAP_ENTRY(nsISupports)
25 0 : NS_INTERFACE_MAP_END
26 :
27 114 : NS_IMPL_CYCLE_COLLECTING_ADDREF(MediaList)
28 57 : NS_IMPL_CYCLE_COLLECTING_RELEASE(MediaList)
29 :
30 0 : NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(MediaList)
31 :
32 : JSObject*
33 0 : MediaList::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
34 : {
35 0 : return MediaListBinding::Wrap(aCx, this, aGivenProto);
36 : }
37 :
38 : void
39 48 : MediaList::SetStyleSheet(StyleSheet* aSheet)
40 : {
41 48 : MOZ_ASSERT(aSheet == mStyleSheet || !aSheet || !mStyleSheet,
42 : "Multiple style sheets competing for one media list");
43 48 : mStyleSheet = aSheet;
44 48 : }
45 :
46 : template<typename Func>
47 : nsresult
48 0 : MediaList::DoMediaChange(Func aCallback)
49 : {
50 0 : nsCOMPtr<nsIDocument> doc;
51 0 : if (mStyleSheet) {
52 0 : doc = mStyleSheet->GetAssociatedDocument();
53 : }
54 0 : mozAutoDocUpdate updateBatch(doc, UPDATE_STYLE, true);
55 0 : if (mStyleSheet) {
56 0 : mStyleSheet->WillDirty();
57 : }
58 :
59 0 : nsresult rv = aCallback();
60 0 : if (NS_FAILED(rv)) {
61 0 : return rv;
62 : }
63 :
64 0 : if (mStyleSheet) {
65 0 : mStyleSheet->DidDirty();
66 : }
67 : /* XXXldb Pass something meaningful? */
68 0 : if (doc) {
69 0 : doc->StyleRuleChanged(mStyleSheet, nullptr);
70 : }
71 0 : return rv;
72 : }
73 :
74 : /* static */ already_AddRefed<MediaList>
75 3 : MediaList::Create(StyleBackendType aBackendType, const nsAString& aMedia)
76 : {
77 3 : if (aBackendType == StyleBackendType::Servo) {
78 0 : RefPtr<ServoMediaList> mediaList = new ServoMediaList(aMedia);
79 0 : return mediaList.forget();
80 : }
81 :
82 6 : nsCSSParser parser;
83 6 : RefPtr<nsMediaList> mediaList = new nsMediaList();
84 3 : parser.ParseMediaList(aMedia, nullptr, 0, mediaList);
85 3 : return mediaList.forget();
86 : }
87 :
88 : NS_IMETHODIMP
89 0 : MediaList::GetMediaText(nsAString& aMediaText)
90 : {
91 0 : GetText(aMediaText);
92 0 : return NS_OK;
93 : }
94 :
95 : NS_IMETHODIMP
96 0 : MediaList::SetMediaText(const nsAString& aMediaText)
97 : {
98 0 : return DoMediaChange([&]() {
99 0 : SetText(aMediaText);
100 0 : return NS_OK;
101 0 : });
102 : }
103 :
104 : NS_IMETHODIMP
105 0 : MediaList::GetLength(uint32_t* aLength)
106 : {
107 0 : NS_ENSURE_ARG_POINTER(aLength);
108 :
109 0 : *aLength = Length();
110 0 : return NS_OK;
111 : }
112 :
113 : NS_IMETHODIMP
114 0 : MediaList::Item(uint32_t aIndex, nsAString& aReturn)
115 : {
116 : bool dummy;
117 0 : IndexedGetter(aIndex, dummy, aReturn);
118 0 : return NS_OK;
119 : }
120 :
121 : NS_IMETHODIMP
122 0 : MediaList::DeleteMedium(const nsAString& aOldMedium)
123 : {
124 0 : return DoMediaChange([&]() { return Delete(aOldMedium); });
125 : }
126 :
127 : NS_IMETHODIMP
128 0 : MediaList::AppendMedium(const nsAString& aNewMedium)
129 : {
130 0 : return DoMediaChange([&]() { return Append(aNewMedium); });
131 : }
132 :
133 : } // namespace dom
134 : } // namespace mozilla
|