1 /* 2 * Copyright (C) 2013 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "config.h" 32 #include "core/dom/CSSSelectorWatch.h" 33 34 #include "core/css/CSSParser.h" 35 #include "core/css/CSSSelectorList.h" 36 #include "core/css/StylePropertySet.h" 37 #include "core/dom/Document.h" 38 #include "core/dom/ExecutionContext.h" 39 #include "core/loader/FrameLoaderClient.h" 40 #include "core/frame/Frame.h" 41 #include "core/rendering/style/StyleRareNonInheritedData.h" 42 43 namespace WebCore { 44 45 // The address of this string is important; its value is just documentation. 46 static const char kSupplementName[] = "CSSSelectorWatch"; 47 48 CSSSelectorWatch::CSSSelectorWatch(Document& document) 49 : m_document(document) 50 , m_callbackSelectorChangeTimer(this, &CSSSelectorWatch::callbackSelectorChangeTimerFired) 51 , m_timerExpirations(0) 52 { 53 } 54 55 CSSSelectorWatch& CSSSelectorWatch::from(Document& document) 56 { 57 CSSSelectorWatch* watch = static_cast<CSSSelectorWatch*>(DocumentSupplement::from(&document, kSupplementName)); 58 if (!watch) { 59 watch = new CSSSelectorWatch(document); 60 DocumentSupplement::provideTo(&document, kSupplementName, adoptPtr(watch)); 61 } 62 return *watch; 63 } 64 65 void CSSSelectorWatch::callbackSelectorChangeTimerFired(Timer<CSSSelectorWatch>*) 66 { 67 // Should be ensured by updateSelectorMatches(): 68 ASSERT(!m_addedSelectors.isEmpty() || !m_removedSelectors.isEmpty()); 69 70 if (m_timerExpirations < 1) { 71 m_timerExpirations++; 72 m_callbackSelectorChangeTimer.startOneShot(0); 73 return; 74 } 75 if (m_document.frame()) { 76 Vector<String> addedSelectors; 77 Vector<String> removedSelectors; 78 copyToVector(m_addedSelectors, addedSelectors); 79 copyToVector(m_removedSelectors, removedSelectors); 80 m_document.frame()->loader().client()->selectorMatchChanged(addedSelectors, removedSelectors); 81 } 82 m_addedSelectors.clear(); 83 m_removedSelectors.clear(); 84 m_timerExpirations = 0; 85 } 86 87 void CSSSelectorWatch::updateSelectorMatches(const Vector<String>& removedSelectors, const Vector<String>& addedSelectors) 88 { 89 bool shouldUpdateTimer = false; 90 91 for (unsigned i = 0; i < removedSelectors.size(); ++i) { 92 const String& selector = removedSelectors[i]; 93 if (!m_matchingCallbackSelectors.remove(selector)) 94 continue; 95 96 // Count reached 0. 97 shouldUpdateTimer = true; 98 if (m_addedSelectors.contains(selector)) 99 m_addedSelectors.remove(selector); 100 else 101 m_removedSelectors.add(selector); 102 } 103 104 for (unsigned i = 0; i < addedSelectors.size(); ++i) { 105 const String& selector = addedSelectors[i]; 106 HashCountedSet<String>::AddResult result = m_matchingCallbackSelectors.add(selector); 107 if (!result.isNewEntry) 108 continue; 109 110 shouldUpdateTimer = true; 111 if (m_removedSelectors.contains(selector)) 112 m_removedSelectors.remove(selector); 113 else 114 m_addedSelectors.add(selector); 115 } 116 117 if (!shouldUpdateTimer) 118 return; 119 120 if (m_removedSelectors.isEmpty() && m_addedSelectors.isEmpty()) { 121 if (m_callbackSelectorChangeTimer.isActive()) { 122 m_timerExpirations = 0; 123 m_callbackSelectorChangeTimer.stop(); 124 } 125 } else { 126 m_timerExpirations = 0; 127 if (!m_callbackSelectorChangeTimer.isActive()) 128 m_callbackSelectorChangeTimer.startOneShot(0); 129 } 130 } 131 132 static bool allCompound(const CSSSelectorList& selectorList) 133 { 134 for (const CSSSelector* selector = selectorList.first(); selector; selector = selectorList.next(selector)) { 135 if (!selector->isCompound()) 136 return false; 137 } 138 return true; 139 } 140 141 void CSSSelectorWatch::watchCSSSelectors(const Vector<String>& selectors) 142 { 143 m_watchedCallbackSelectors.clear(); 144 CSSParserContext context(UASheetMode); 145 CSSParser parser(context); 146 147 const CSSProperty callbackProperty(CSSPropertyInternalCallback, CSSPrimitiveValue::createIdentifier(CSSValueInternalPresence)); 148 const RefPtr<StylePropertySet> callbackPropertySet = ImmutableStylePropertySet::create(&callbackProperty, 1, UASheetMode); 149 150 CSSSelectorList selectorList; 151 for (unsigned i = 0; i < selectors.size(); ++i) { 152 parser.parseSelector(selectors[i], selectorList); 153 if (!selectorList.isValid()) 154 continue; 155 156 // Only accept Compound Selectors, since they're cheaper to match. 157 if (!allCompound(selectorList)) 158 continue; 159 160 RefPtr<StyleRule> rule = StyleRule::create(); 161 rule->wrapperAdoptSelectorList(selectorList); 162 rule->setProperties(callbackPropertySet); 163 m_watchedCallbackSelectors.append(rule.release()); 164 } 165 m_document.changedSelectorWatch(); 166 } 167 168 } // namespace WebCore 169