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#if ENABLE(VIDEO_TRACK)
30
31#include "CachedResourceClient.h"
32#include "CachedResourceHandle.h"
33#include "Timer.h"
34#include "WebVTTParser.h"
35#include <wtf/OwnPtr.h>
36
37namespace WebCore {
38
39class CachedTextTrack;
40class Document;
41class TextTrackLoader;
42class ScriptExecutionContext;
43
44class TextTrackLoaderClient {
45public:
46    virtual ~TextTrackLoaderClient() { }
47
48    virtual bool shouldLoadCues(TextTrackLoader*) = 0;
49    virtual void newCuesAvailable(TextTrackLoader*) = 0;
50    virtual void cueLoadingStarted(TextTrackLoader*) = 0;
51    virtual void cueLoadingCompleted(TextTrackLoader*, bool loadingFailed) = 0;
52#if ENABLE(WEBVTT_REGIONS)
53    virtual void newRegionsAvailable(TextTrackLoader*) = 0;
54#endif
55};
56
57class TextTrackLoader : public CachedResourceClient, private WebVTTParserClient {
58    WTF_MAKE_NONCOPYABLE(TextTrackLoader);
59    WTF_MAKE_FAST_ALLOCATED;
60public:
61    static PassOwnPtr<TextTrackLoader> create(TextTrackLoaderClient* client, ScriptExecutionContext* context)
62    {
63        return adoptPtr(new TextTrackLoader(client, context));
64    }
65    virtual ~TextTrackLoader();
66
67    bool load(const KURL&, const String& crossOriginMode);
68    void cancelLoad();
69    void getNewCues(Vector<RefPtr<TextTrackCue> >& outputCues);
70#if ENABLE(WEBVTT_REGIONS)
71    void getNewRegions(Vector<RefPtr<TextTrackRegion> >& outputRegions);
72#endif
73private:
74
75    // CachedResourceClient
76    virtual void notifyFinished(CachedResource*);
77    virtual void deprecatedDidReceiveCachedResource(CachedResource*);
78
79    // WebVTTParserClient
80    virtual void newCuesParsed();
81#if ENABLE(WEBVTT_REGIONS)
82    virtual void newRegionsParsed();
83#endif
84    virtual void fileFailedToParse();
85
86    TextTrackLoader(TextTrackLoaderClient*, ScriptExecutionContext*);
87
88    void processNewCueData(CachedResource*);
89    void cueLoadTimerFired(Timer<TextTrackLoader>*);
90    void corsPolicyPreventedLoad();
91
92    enum State { Idle, Loading, Finished, Failed };
93
94    TextTrackLoaderClient* m_client;
95    OwnPtr<WebVTTParser> m_cueParser;
96    CachedResourceHandle<CachedTextTrack> 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#endif
109