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
2302772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) */
25926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
26926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#ifndef BackgroundHTMLInputStream_h
27926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#define BackgroundHTMLInputStream_h
28926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
291e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#include "platform/text/SegmentedString.h"
30e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch#include "wtf/Vector.h"
31e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch#include "wtf/text/WTFString.h"
32926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
33c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
34926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
35926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)typedef size_t HTMLInputCheckpoint;
36926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
37926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)class BackgroundHTMLInputStream {
38926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    WTF_MAKE_NONCOPYABLE(BackgroundHTMLInputStream);
39926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)public:
40926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    BackgroundHTMLInputStream();
41926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
42926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    void append(const String&);
43926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    void close();
44926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
45926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    SegmentedString& current() { return m_current; }
46926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
47926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    // An HTMLInputCheckpoint is valid until the next call to rewindTo, at which
48926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    // point all outstanding checkpoints are invalidated.
49a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    HTMLInputCheckpoint createCheckpoint(size_t tokensExtractedSincePreviousCheckpoint);
50926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    void rewindTo(HTMLInputCheckpoint, const String& unparsedInput);
51926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    void invalidateCheckpointsBefore(HTMLInputCheckpoint);
52926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
53a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    size_t totalCheckpointTokenCount() const { return m_totalCheckpointTokenCount; }
54926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
55926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)private:
56926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    struct Checkpoint {
57a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        Checkpoint(const SegmentedString& i, size_t n, size_t t) : input(i), numberOfSegmentsAlreadyAppended(n), tokensExtractedSincePreviousCheckpoint(t) { }
58926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
59926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        SegmentedString input;
60926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        size_t numberOfSegmentsAlreadyAppended;
61a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        size_t tokensExtractedSincePreviousCheckpoint;
62926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
63197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#if ENABLE(ASSERT)
64926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)        bool isNull() const { return input.isEmpty() && !numberOfSegmentsAlreadyAppended; }
65926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#endif
66a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)        void clear() { input.clear(); numberOfSegmentsAlreadyAppended = 0; tokensExtractedSincePreviousCheckpoint = 0;}
67926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    };
68926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
69926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    SegmentedString m_current;
70926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    Vector<String> m_segments;
71926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    Vector<Checkpoint> m_checkpoints;
72926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
73926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    // Note: These indicies may === vector.size(), in which case there are no valid checkpoints/segments at this time.
74926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    size_t m_firstValidCheckpointIndex;
75926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    size_t m_firstValidSegmentIndex;
76a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    size_t m_totalCheckpointTokenCount;
77a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
78a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    void updateTotalCheckpointTokenCount();
79926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)};
80926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
81926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
82926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
83926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#endif
84