Home | History | Annotate | Download | only in dom
      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/CSSSelectorList.h"
     35 #include "core/css/StylePropertySet.h"
     36 #include "core/css/parser/CSSParser.h"
     37 #include "core/dom/Document.h"
     38 #include "core/dom/ExecutionContext.h"
     39 #include "core/frame/LocalFrame.h"
     40 #include "core/loader/FrameLoaderClient.h"
     41 #include "core/rendering/style/StyleRareNonInheritedData.h"
     42 
     43 namespace blink {
     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, adoptPtrWillBeNoop(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, FROM_HERE);
     73         return;
     74     }
     75     if (document().frame()) {
     76         Vector<String> addedSelectors;
     77         Vector<String> removedSelectors;
     78         copyToVector(m_addedSelectors, addedSelectors);
     79         copyToVector(m_removedSelectors, removedSelectors);
     80         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, FROM_HERE);
    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     CSSParser parser(CSSParserContext(UASheetMode, 0));
    145 
    146     const RefPtrWillBeRawPtr<StylePropertySet> callbackPropertySet = ImmutableStylePropertySet::create(nullptr, 0, UASheetMode);
    147 
    148     CSSSelectorList selectorList;
    149     for (unsigned i = 0; i < selectors.size(); ++i) {
    150         parser.parseSelector(selectors[i], selectorList);
    151         if (!selectorList.isValid())
    152             continue;
    153 
    154         // Only accept Compound Selectors, since they're cheaper to match.
    155         if (!allCompound(selectorList))
    156             continue;
    157 
    158         RefPtrWillBeRawPtr<StyleRule> rule = StyleRule::create();
    159         rule->wrapperAdoptSelectorList(selectorList);
    160         rule->setProperties(callbackPropertySet);
    161         m_watchedCallbackSelectors.append(rule.release());
    162     }
    163     document().changedSelectorWatch();
    164 }
    165 
    166 void CSSSelectorWatch::trace(Visitor* visitor)
    167 {
    168     visitor->trace(m_watchedCallbackSelectors);
    169     visitor->trace(m_document);
    170     DocumentSupplement::trace(visitor);
    171 }
    172 
    173 } // namespace blink
    174