1 /* 2 * Copyright (C) 2011 Apple 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 APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 23 * THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #ifndef TextTrackLoader_h 27 #define TextTrackLoader_h 28 29 #include "core/html/track/WebVTTParser.h" 30 #include "core/loader/cache/ResourceClient.h" 31 #include "core/loader/cache/ResourcePtr.h" 32 #include "core/loader/cache/TextTrackResource.h" 33 #include "core/platform/Timer.h" 34 #include "wtf/OwnPtr.h" 35 36 namespace WebCore { 37 38 class Document; 39 class TextTrackLoader; 40 class ScriptExecutionContext; 41 42 class TextTrackLoaderClient { 43 public: 44 virtual ~TextTrackLoaderClient() { } 45 46 virtual bool shouldLoadCues(TextTrackLoader*) = 0; 47 virtual void newCuesAvailable(TextTrackLoader*) = 0; 48 virtual void cueLoadingStarted(TextTrackLoader*) = 0; 49 virtual void cueLoadingCompleted(TextTrackLoader*, bool loadingFailed) = 0; 50 #if ENABLE(WEBVTT_REGIONS) 51 virtual void newRegionsAvailable(TextTrackLoader*) = 0; 52 #endif 53 }; 54 55 class TextTrackLoader : public ResourceClient, private WebVTTParserClient { 56 WTF_MAKE_NONCOPYABLE(TextTrackLoader); 57 WTF_MAKE_FAST_ALLOCATED; 58 public: 59 static PassOwnPtr<TextTrackLoader> create(TextTrackLoaderClient* client, ScriptExecutionContext* context) 60 { 61 return adoptPtr(new TextTrackLoader(client, context)); 62 } 63 virtual ~TextTrackLoader(); 64 65 bool load(const KURL&, const String& crossOriginMode); 66 void cancelLoad(); 67 68 enum State { Idle, Loading, Finished, Failed }; 69 State loadState() { return m_state; } 70 71 void getNewCues(Vector<RefPtr<TextTrackCue> >& outputCues); 72 #if ENABLE(WEBVTT_REGIONS) 73 void getNewRegions(Vector<RefPtr<TextTrackRegion> >& outputRegions); 74 #endif 75 private: 76 77 // ResourceClient 78 virtual void notifyFinished(Resource*); 79 virtual void deprecatedDidReceiveResource(Resource*); 80 81 // WebVTTParserClient 82 virtual void newCuesParsed(); 83 #if ENABLE(WEBVTT_REGIONS) 84 virtual void newRegionsParsed(); 85 #endif 86 virtual void fileFailedToParse(); 87 88 TextTrackLoader(TextTrackLoaderClient*, ScriptExecutionContext*); 89 90 void processNewCueData(Resource*); 91 void cueLoadTimerFired(Timer<TextTrackLoader>*); 92 void corsPolicyPreventedLoad(); 93 94 TextTrackLoaderClient* m_client; 95 OwnPtr<WebVTTParser> m_cueParser; 96 ResourcePtr<TextTrackResource> m_cachedCueData; 97 ScriptExecutionContext* m_scriptExecutionContext; 98 Timer<TextTrackLoader> m_cueLoadTimer; 99 String m_crossOriginMode; 100 State m_state; 101 unsigned m_parseOffset; 102 bool m_newCuesAvailable; 103 }; 104 105 } // namespace WebCore 106 107 #endif 108