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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GOOGLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#if ENABLE(THREADED_HTML_PARSER)
29
30#include "BackgroundHTMLInputStream.h"
31
32namespace WebCore {
33
34BackgroundHTMLInputStream::BackgroundHTMLInputStream()
35    : m_firstValidCheckpointIndex(0)
36    , m_firstValidSegmentIndex(0)
37{
38}
39
40void BackgroundHTMLInputStream::append(const String& input)
41{
42    m_current.append(SegmentedString(input));
43    m_segments.append(input);
44}
45
46void BackgroundHTMLInputStream::close()
47{
48    m_current.close();
49}
50
51HTMLInputCheckpoint BackgroundHTMLInputStream::createCheckpoint()
52{
53    HTMLInputCheckpoint checkpoint = m_checkpoints.size();
54    m_checkpoints.append(Checkpoint(m_current, m_segments.size()));
55    return checkpoint;
56}
57
58void BackgroundHTMLInputStream::invalidateCheckpointsBefore(HTMLInputCheckpoint newFirstValidCheckpointIndex)
59{
60    ASSERT(newFirstValidCheckpointIndex < m_checkpoints.size());
61    // There is nothing to do for the first valid checkpoint.
62    if (m_firstValidCheckpointIndex == newFirstValidCheckpointIndex)
63        return;
64
65    ASSERT(newFirstValidCheckpointIndex > m_firstValidCheckpointIndex);
66    const Checkpoint& lastInvalidCheckpoint = m_checkpoints[newFirstValidCheckpointIndex - 1];
67
68    ASSERT(m_firstValidSegmentIndex <= lastInvalidCheckpoint.numberOfSegmentsAlreadyAppended);
69    for (size_t i = m_firstValidSegmentIndex; i < lastInvalidCheckpoint.numberOfSegmentsAlreadyAppended; ++i)
70        m_segments[i] = String();
71    m_firstValidSegmentIndex = lastInvalidCheckpoint.numberOfSegmentsAlreadyAppended;
72
73    for (size_t i = m_firstValidCheckpointIndex; i < newFirstValidCheckpointIndex; ++i)
74        m_checkpoints[i].clear();
75    m_firstValidCheckpointIndex = newFirstValidCheckpointIndex;
76}
77
78void BackgroundHTMLInputStream::rewindTo(HTMLInputCheckpoint checkpointIndex, const String& unparsedInput)
79{
80    ASSERT(checkpointIndex < m_checkpoints.size()); // If this ASSERT fires, checkpointIndex is invalid.
81    const Checkpoint& checkpoint = m_checkpoints[checkpointIndex];
82    ASSERT(!checkpoint.isNull());
83
84    bool isClosed = m_current.isClosed();
85
86    m_current = checkpoint.input;
87
88    for (size_t i = checkpoint.numberOfSegmentsAlreadyAppended; i < m_segments.size(); ++i) {
89        ASSERT(!m_segments[i].isNull());
90        m_current.append(SegmentedString(m_segments[i]));
91    }
92
93    if (!unparsedInput.isEmpty())
94        m_current.prepend(SegmentedString(unparsedInput));
95
96    if (isClosed && !m_current.isClosed())
97        m_current.close();
98
99    ASSERT(m_current.isClosed() == isClosed);
100
101    m_segments.clear();
102    m_checkpoints.clear();
103    m_firstValidCheckpointIndex = 0;
104    m_firstValidSegmentIndex = 0;
105}
106
107}
108
109#endif // ENABLE(THREADED_HTML_PARSER)
110