1 : /* vim: set shiftwidth=2 tabstop=8 autoindent cindent expandtab: */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is nsDOMMediaQueryList.
16 : *
17 : * The Initial Developer of the Original Code is the Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2011
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * L. David Baron <dbaron@dbaron.org>, Mozilla Corporation (original author)
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : /* implements DOM interface for querying and observing media queries */
39 :
40 : #include "nsDOMMediaQueryList.h"
41 : #include "nsPresContext.h"
42 : #include "nsIMediaList.h"
43 : #include "nsCSSParser.h"
44 : #include "nsDOMClassInfoID.h" // DOMCI_DATA
45 :
46 0 : nsDOMMediaQueryList::nsDOMMediaQueryList(nsPresContext *aPresContext,
47 : const nsAString &aMediaQueryList)
48 : : mPresContext(aPresContext),
49 0 : mMediaList(new nsMediaList),
50 0 : mMatchesValid(false)
51 : {
52 0 : PR_INIT_CLIST(this);
53 :
54 0 : nsCSSParser parser;
55 0 : parser.ParseMediaList(aMediaQueryList, nsnull, 0, mMediaList, false);
56 0 : }
57 :
58 0 : nsDOMMediaQueryList::~nsDOMMediaQueryList()
59 : {
60 0 : if (mPresContext) {
61 0 : PR_REMOVE_LINK(this);
62 : }
63 0 : }
64 :
65 1464 : NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMMediaQueryList)
66 :
67 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsDOMMediaQueryList)
68 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mPresContext)
69 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSTARRAY_OF_NSCOMPTR(mListeners)
70 0 : NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
71 :
72 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsDOMMediaQueryList)
73 0 : if (tmp->mPresContext) {
74 0 : PR_REMOVE_LINK(tmp);
75 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mPresContext)
76 : }
77 0 : tmp->RemoveAllListeners();
78 0 : NS_IMPL_CYCLE_COLLECTION_UNLINK_END
79 :
80 : DOMCI_DATA(MediaQueryList, nsDOMMediaQueryList)
81 :
82 0 : NS_INTERFACE_MAP_BEGIN(nsDOMMediaQueryList)
83 0 : NS_INTERFACE_MAP_ENTRY(nsIDOMMediaQueryList)
84 0 : NS_INTERFACE_MAP_ENTRY(nsISupports)
85 0 : NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(nsDOMMediaQueryList)
86 0 : NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MediaQueryList)
87 0 : NS_INTERFACE_MAP_END
88 :
89 0 : NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMMediaQueryList)
90 0 : NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMMediaQueryList)
91 :
92 : NS_IMETHODIMP
93 0 : nsDOMMediaQueryList::GetMedia(nsAString &aMedia)
94 : {
95 0 : mMediaList->GetText(aMedia);
96 0 : return NS_OK;
97 : }
98 :
99 : NS_IMETHODIMP
100 0 : nsDOMMediaQueryList::GetMatches(bool *aMatches)
101 : {
102 0 : if (!mMatchesValid) {
103 0 : NS_ABORT_IF_FALSE(!HasListeners(),
104 : "when listeners present, must keep mMatches current");
105 0 : RecomputeMatches();
106 : }
107 :
108 0 : *aMatches = mMatches;
109 0 : return NS_OK;
110 : }
111 :
112 : NS_IMETHODIMP
113 0 : nsDOMMediaQueryList::AddListener(nsIDOMMediaQueryListListener *aListener)
114 : {
115 0 : if (!aListener) {
116 0 : return NS_OK;
117 : }
118 :
119 0 : if (!HasListeners()) {
120 : // When we have listeners, the pres context owns a reference to
121 : // this. This is a cyclic reference that can only be broken by
122 : // cycle collection.
123 0 : NS_ADDREF_THIS();
124 : }
125 :
126 0 : if (!mMatchesValid) {
127 0 : NS_ABORT_IF_FALSE(!HasListeners(),
128 : "when listeners present, must keep mMatches current");
129 0 : RecomputeMatches();
130 : }
131 :
132 0 : if (!mListeners.Contains(aListener)) {
133 0 : mListeners.AppendElement(aListener);
134 0 : if (!HasListeners()) {
135 : // Append failed; undo the AddRef above.
136 0 : NS_RELEASE_THIS();
137 : }
138 : }
139 0 : return NS_OK;
140 : }
141 :
142 : NS_IMETHODIMP
143 0 : nsDOMMediaQueryList::RemoveListener(nsIDOMMediaQueryListListener *aListener)
144 : {
145 0 : bool removed = mListeners.RemoveElement(aListener);
146 0 : NS_ABORT_IF_FALSE(!mListeners.Contains(aListener),
147 : "duplicate occurrence of listeners");
148 :
149 0 : if (removed && !HasListeners()) {
150 : // See NS_ADDREF_THIS() in AddListener.
151 0 : NS_RELEASE_THIS();
152 : }
153 :
154 0 : return NS_OK;
155 : }
156 :
157 : void
158 0 : nsDOMMediaQueryList::RemoveAllListeners()
159 : {
160 0 : bool hadListeners = HasListeners();
161 0 : mListeners.Clear();
162 0 : if (hadListeners) {
163 : // See NS_ADDREF_THIS() in AddListener.
164 0 : NS_RELEASE_THIS();
165 : }
166 0 : }
167 :
168 : void
169 0 : nsDOMMediaQueryList::RecomputeMatches()
170 : {
171 0 : if (!mPresContext) {
172 0 : return;
173 : }
174 :
175 0 : mMatches = mMediaList->Matches(mPresContext, nsnull);
176 0 : mMatchesValid = true;
177 : }
178 :
179 : void
180 0 : nsDOMMediaQueryList::MediumFeaturesChanged(NotifyList &aListenersToNotify)
181 : {
182 0 : mMatchesValid = false;
183 :
184 0 : if (mListeners.Length()) {
185 0 : bool oldMatches = mMatches;
186 0 : RecomputeMatches();
187 0 : if (mMatches != oldMatches) {
188 0 : for (PRUint32 i = 0, i_end = mListeners.Length(); i != i_end; ++i) {
189 0 : HandleChangeData *d = aListenersToNotify.AppendElement();
190 0 : if (d) {
191 0 : d->mql = this;
192 0 : d->listener = mListeners[i];
193 : }
194 : }
195 : }
196 : }
197 4392 : }
|