1 /* 2 * Copyright (C) 2011 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 #ifndef VTTParser_h 32 #define VTTParser_h 33 34 #include "HTMLNames.h" 35 #include "RuntimeEnabledFeatures.h" 36 #include "core/dom/DocumentFragment.h" 37 #include "core/fetch/TextResourceDecoder.h" 38 #include "core/html/track/vtt/BufferedLineReader.h" 39 #include "core/html/track/vtt/VTTCue.h" 40 #include "core/html/track/vtt/VTTRegion.h" 41 #include "core/html/track/vtt/VTTTokenizer.h" 42 #include "wtf/PassOwnPtr.h" 43 #include "wtf/text/StringBuilder.h" 44 45 namespace WebCore { 46 47 using namespace HTMLNames; 48 49 class Document; 50 51 class VTTParserClient { 52 public: 53 virtual ~VTTParserClient() { } 54 55 virtual void newCuesParsed() = 0; 56 virtual void newRegionsParsed() = 0; 57 virtual void fileFailedToParse() = 0; 58 }; 59 60 class VTTParser FINAL { 61 public: 62 enum ParseState { 63 Initial, 64 Header, 65 Id, 66 TimingsAndSettings, 67 CueText, 68 BadCue 69 }; 70 71 static PassOwnPtr<VTTParser> create(VTTParserClient* client, Document& document) 72 { 73 return adoptPtr(new VTTParser(client, document)); 74 } 75 76 static inline bool isRecognizedTag(const AtomicString& tagName) 77 { 78 return tagName == iTag 79 || tagName == bTag 80 || tagName == uTag 81 || tagName == rubyTag 82 || tagName == rtTag; 83 } 84 85 static inline bool isASpace(char c) 86 { 87 // WebVTT space characters are U+0020 SPACE, U+0009 CHARACTER TABULATION (tab), U+000A LINE FEED (LF), U+000C FORM FEED (FF), and U+000D CARRIAGE RETURN (CR). 88 return c == ' ' || c == '\t' || c == '\n' || c == '\f' || c == '\r'; 89 } 90 static inline bool isValidSettingDelimiter(char c) 91 { 92 // ... a WebVTT cue consists of zero or more of the following components, in any order, separated from each other by one or more 93 // U+0020 SPACE characters or U+0009 CHARACTER TABULATION (tab) characters. 94 return c == ' ' || c == '\t'; 95 } 96 static unsigned collectDigitsToInt(const String& input, unsigned* position, int& number); 97 static String collectWord(const String&, unsigned*); 98 static bool collectTimeStamp(const String&, unsigned*, double& timeStamp); 99 100 // Useful functions for parsing percentage settings. 101 static bool parseFloatPercentageValue(const String&, float&); 102 static bool parseFloatPercentageValuePair(const String&, char, FloatPoint&); 103 104 // Create the DocumentFragment representation of the WebVTT cue text. 105 static PassRefPtr<DocumentFragment> createDocumentFragmentFromCueText(Document&, const String&); 106 107 // Input data to the parser to parse. 108 void parseBytes(const char* data, unsigned length); 109 void flush(); 110 111 // Transfers ownership of last parsed cues to caller. 112 void getNewCues(Vector<RefPtr<VTTCue> >&); 113 void getNewRegions(Vector<RefPtr<VTTRegion> >&); 114 115 private: 116 VTTParser(VTTParserClient*, Document&); 117 118 Document* m_document; 119 ParseState m_state; 120 121 void parse(); 122 void flushPendingCue(); 123 bool hasRequiredFileIdentifier(const String& line); 124 ParseState collectCueId(const String&); 125 ParseState collectTimingsAndSettings(const String&); 126 ParseState collectCueText(const String&); 127 ParseState recoverCue(const String&); 128 ParseState ignoreBadCue(const String&); 129 130 void createNewCue(); 131 void resetCueValues(); 132 133 void collectMetadataHeader(const String&); 134 void createNewRegion(const String& headerValue); 135 136 static void skipWhiteSpace(const String&, unsigned*); 137 138 BufferedLineReader m_lineReader; 139 OwnPtr<TextResourceDecoder> m_decoder; 140 String m_currentId; 141 double m_currentStartTime; 142 double m_currentEndTime; 143 StringBuilder m_currentContent; 144 String m_currentSettings; 145 146 VTTParserClient* m_client; 147 148 Vector<RefPtr<VTTCue> > m_cuelist; 149 150 Vector<RefPtr<VTTRegion> > m_regionList; 151 }; 152 153 } // namespace WebCore 154 155 #endif 156