1 /* 2 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Library General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Library General Public License for more details. 13 * 14 * You should have received a copy of the GNU Library General Public License 15 * along with this library; see the file COPYING.LIB. If not, write to 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 * Boston, MA 02110-1301, USA. 18 */ 19 20 #include "config.h" 21 #include "core/css/MediaQueryMatcher.h" 22 23 #include "core/css/MediaList.h" 24 #include "core/css/MediaQueryEvaluator.h" 25 #include "core/css/MediaQueryList.h" 26 #include "core/css/MediaQueryListEvent.h" 27 #include "core/css/MediaQueryListListener.h" 28 #include "core/css/resolver/StyleResolver.h" 29 #include "core/dom/Document.h" 30 #include "core/frame/FrameView.h" 31 #include "core/frame/LocalFrame.h" 32 #include "wtf/Vector.h" 33 34 namespace blink { 35 36 PassRefPtrWillBeRawPtr<MediaQueryMatcher> MediaQueryMatcher::create(Document& document) 37 { 38 return adoptRefWillBeNoop(new MediaQueryMatcher(document)); 39 } 40 41 MediaQueryMatcher::MediaQueryMatcher(Document& document) 42 : m_document(&document) 43 { 44 ASSERT(m_document); 45 } 46 47 DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(MediaQueryMatcher) 48 49 void MediaQueryMatcher::documentDetached() 50 { 51 m_document = nullptr; 52 m_evaluator = nullptr; 53 } 54 55 PassOwnPtr<MediaQueryEvaluator> MediaQueryMatcher::createEvaluator() const 56 { 57 if (!m_document || !m_document->frame()) 58 return nullptr; 59 60 return adoptPtr(new MediaQueryEvaluator(m_document->frame())); 61 } 62 63 bool MediaQueryMatcher::evaluate(const MediaQuerySet* media) 64 { 65 ASSERT(!m_document || m_document->frame() || !m_evaluator); 66 67 if (!media) 68 return false; 69 70 // Cache the evaluator to avoid allocating one per evaluation. 71 if (!m_evaluator) 72 m_evaluator = createEvaluator(); 73 74 if (m_evaluator) 75 return m_evaluator->eval(media); 76 77 return false; 78 } 79 80 PassRefPtrWillBeRawPtr<MediaQueryList> MediaQueryMatcher::matchMedia(const String& query) 81 { 82 if (!m_document) 83 return nullptr; 84 85 RefPtrWillBeRawPtr<MediaQuerySet> media = MediaQuerySet::create(query); 86 // Add warning message to inspector whenever dpi/dpcm values are used for "screen" media. 87 reportMediaQueryWarningIfNeeded(m_document, media.get()); 88 return MediaQueryList::create(m_document, this, media); 89 } 90 91 void MediaQueryMatcher::addMediaQueryList(MediaQueryList* query) 92 { 93 if (!m_document) 94 return; 95 m_mediaLists.add(query); 96 } 97 98 void MediaQueryMatcher::removeMediaQueryList(MediaQueryList* query) 99 { 100 if (!m_document) 101 return; 102 m_mediaLists.remove(query); 103 } 104 105 void MediaQueryMatcher::addViewportListener(PassRefPtrWillBeRawPtr<MediaQueryListListener> listener) 106 { 107 if (!m_document) 108 return; 109 m_viewportListeners.add(listener); 110 } 111 112 void MediaQueryMatcher::removeViewportListener(PassRefPtrWillBeRawPtr<MediaQueryListListener> listener) 113 { 114 if (!m_document) 115 return; 116 m_viewportListeners.remove(listener); 117 } 118 119 void MediaQueryMatcher::mediaFeaturesChanged() 120 { 121 if (!m_document) 122 return; 123 124 WillBeHeapVector<RefPtrWillBeMember<MediaQueryListListener> > listenersToNotify; 125 for (MediaQueryListSet::iterator it = m_mediaLists.begin(); it != m_mediaLists.end(); ++it) { 126 if ((*it)->mediaFeaturesChanged(&listenersToNotify)) { 127 RefPtrWillBeRawPtr<Event> event(MediaQueryListEvent::create(*it)); 128 event->setTarget(*it); 129 m_document->enqueueUniqueAnimationFrameEvent(event); 130 } 131 } 132 m_document->enqueueMediaQueryChangeListeners(listenersToNotify); 133 } 134 135 void MediaQueryMatcher::viewportChanged() 136 { 137 if (!m_document) 138 return; 139 140 WillBeHeapVector<RefPtrWillBeMember<MediaQueryListListener> > listenersToNotify; 141 for (ViewportListenerSet::iterator it = m_viewportListeners.begin(); it != m_viewportListeners.end(); ++it) 142 listenersToNotify.append(*it); 143 144 m_document->enqueueMediaQueryChangeListeners(listenersToNotify); 145 } 146 147 void MediaQueryMatcher::trace(Visitor* visitor) 148 { 149 #if ENABLE(OILPAN) 150 visitor->trace(m_document); 151 visitor->trace(m_mediaLists); 152 visitor->trace(m_viewportListeners); 153 #endif 154 } 155 156 } 157