1926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)/* 2926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * Copyright (C) 2013 Google, Inc. All Rights Reserved. 3926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * 4926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without 5926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * modification, are permitted provided that the following conditions 6926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * are met: 7926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * 1. Redistributions of source code must retain the above copyright 8926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * notice, this list of conditions and the following disclaimer. 9926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * 2. Redistributions in binary form must reproduce the above copyright 10926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * notice, this list of conditions and the following disclaimer in the 11926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * documentation and/or other materials provided with the distribution. 12926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * 13926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. ``AS IS'' AND ANY 14926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC. OR 17926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) */ 25926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 26926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#include "config.h" 2753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/html/parser/BackgroundHTMLInputStream.h" 28926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 29c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink { 30926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 31926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)BackgroundHTMLInputStream::BackgroundHTMLInputStream() 32926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) : m_firstValidCheckpointIndex(0) 33926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) , m_firstValidSegmentIndex(0) 34a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) , m_totalCheckpointTokenCount(0) 35926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){ 36926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} 37926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 38926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)void BackgroundHTMLInputStream::append(const String& input) 39926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){ 40926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) m_current.append(SegmentedString(input)); 41926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) m_segments.append(input); 42926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} 43926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 44926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)void BackgroundHTMLInputStream::close() 45926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){ 46926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) m_current.close(); 47926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} 48926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 49a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)HTMLInputCheckpoint BackgroundHTMLInputStream::createCheckpoint(size_t tokensExtractedSincePreviousCheckpoint) 50926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){ 51926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) HTMLInputCheckpoint checkpoint = m_checkpoints.size(); 52a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) m_checkpoints.append(Checkpoint(m_current, m_segments.size(), tokensExtractedSincePreviousCheckpoint)); 53a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) m_totalCheckpointTokenCount += tokensExtractedSincePreviousCheckpoint; 54926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) return checkpoint; 55926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} 56926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 57926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)void BackgroundHTMLInputStream::invalidateCheckpointsBefore(HTMLInputCheckpoint newFirstValidCheckpointIndex) 58926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){ 59926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) ASSERT(newFirstValidCheckpointIndex < m_checkpoints.size()); 60926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) // There is nothing to do for the first valid checkpoint. 61926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) if (m_firstValidCheckpointIndex == newFirstValidCheckpointIndex) 62926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) return; 63926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 64926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) ASSERT(newFirstValidCheckpointIndex > m_firstValidCheckpointIndex); 65926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) const Checkpoint& lastInvalidCheckpoint = m_checkpoints[newFirstValidCheckpointIndex - 1]; 66926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 67926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) ASSERT(m_firstValidSegmentIndex <= lastInvalidCheckpoint.numberOfSegmentsAlreadyAppended); 68926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) for (size_t i = m_firstValidSegmentIndex; i < lastInvalidCheckpoint.numberOfSegmentsAlreadyAppended; ++i) 69926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) m_segments[i] = String(); 70926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) m_firstValidSegmentIndex = lastInvalidCheckpoint.numberOfSegmentsAlreadyAppended; 71926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 72926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) for (size_t i = m_firstValidCheckpointIndex; i < newFirstValidCheckpointIndex; ++i) 73926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) m_checkpoints[i].clear(); 74926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) m_firstValidCheckpointIndex = newFirstValidCheckpointIndex; 75a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 76a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) updateTotalCheckpointTokenCount(); 77926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} 78926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 79926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)void BackgroundHTMLInputStream::rewindTo(HTMLInputCheckpoint checkpointIndex, const String& unparsedInput) 80926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles){ 81926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) ASSERT(checkpointIndex < m_checkpoints.size()); // If this ASSERT fires, checkpointIndex is invalid. 82926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) const Checkpoint& checkpoint = m_checkpoints[checkpointIndex]; 83926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) ASSERT(!checkpoint.isNull()); 84926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 85926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) bool isClosed = m_current.isClosed(); 86926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 87926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) m_current = checkpoint.input; 88926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 89926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) for (size_t i = checkpoint.numberOfSegmentsAlreadyAppended; i < m_segments.size(); ++i) { 90926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) ASSERT(!m_segments[i].isNull()); 91926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) m_current.append(SegmentedString(m_segments[i])); 92926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) } 93926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 94926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) if (!unparsedInput.isEmpty()) 95926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) m_current.prepend(SegmentedString(unparsedInput)); 96926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 97926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) if (isClosed && !m_current.isClosed()) 98926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) m_current.close(); 99926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 100926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) ASSERT(m_current.isClosed() == isClosed); 101926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 102926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) m_segments.clear(); 103926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) m_checkpoints.clear(); 104926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) m_firstValidCheckpointIndex = 0; 105926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) m_firstValidSegmentIndex = 0; 106a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 107a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) updateTotalCheckpointTokenCount(); 108a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)} 109a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) 110a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)void BackgroundHTMLInputStream::updateTotalCheckpointTokenCount() 111a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles){ 112a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) m_totalCheckpointTokenCount = 0; 113a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) size_t lastCheckpointIndex = m_checkpoints.size(); 114a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) for (size_t i = 0; i < lastCheckpointIndex; ++i) 115a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) m_totalCheckpointTokenCount += m_checkpoints[i].tokensExtractedSincePreviousCheckpoint; 116926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} 117926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) 118926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)} 119