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
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 COMPUTER, 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 APPLE COMPUTER, 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#include "core/loader/TextTrackLoader.h"
29
30#include "core/FetchInitiatorTypeNames.h"
31#include "core/dom/Document.h"
32#include "core/fetch/CrossOriginAccessControl.h"
33#include "core/fetch/FetchRequest.h"
34#include "core/fetch/ResourceFetcher.h"
35#include "core/inspector/ConsoleMessage.h"
36#include "platform/Logging.h"
37#include "platform/SharedBuffer.h"
38#include "platform/weborigin/SecurityOrigin.h"
39
40namespace blink {
41
42TextTrackLoader::TextTrackLoader(TextTrackLoaderClient& client, Document& document)
43    : m_client(client)
44    , m_document(document)
45    , m_cueLoadTimer(this, &TextTrackLoader::cueLoadTimerFired)
46    , m_state(Idle)
47    , m_newCuesAvailable(false)
48{
49}
50
51TextTrackLoader::~TextTrackLoader()
52{
53}
54
55void TextTrackLoader::cueLoadTimerFired(Timer<TextTrackLoader>* timer)
56{
57    ASSERT_UNUSED(timer, timer == &m_cueLoadTimer);
58
59    if (m_newCuesAvailable) {
60        m_newCuesAvailable = false;
61        m_client.newCuesAvailable(this);
62    }
63
64    if (m_state >= Finished)
65        m_client.cueLoadingCompleted(this, m_state == Failed);
66}
67
68void TextTrackLoader::cancelLoad()
69{
70    clearResource();
71}
72
73void TextTrackLoader::dataReceived(Resource* resource, const char* data, int length)
74{
75    ASSERT(this->resource() == resource);
76
77    if (m_state == Failed)
78        return;
79
80    if (!m_cueParser)
81        m_cueParser = VTTParser::create(this, document());
82
83    m_cueParser->parseBytes(data, length);
84}
85
86void TextTrackLoader::corsPolicyPreventedLoad(SecurityOrigin* securityOrigin, const KURL& url)
87{
88    String consoleMessage("Text track from origin '" + SecurityOrigin::create(url)->toString() + "' has been blocked from loading: Not at same origin as the document, and parent of track element does not have a 'crossorigin' attribute. Origin '" + securityOrigin->toString() + "' is therefore not allowed access.");
89    document().addConsoleMessage(ConsoleMessage::create(SecurityMessageSource, ErrorMessageLevel, consoleMessage));
90    m_state = Failed;
91}
92
93void TextTrackLoader::notifyFinished(Resource* resource)
94{
95    ASSERT(this->resource() == resource);
96    if (m_state != Failed)
97        m_state = resource->errorOccurred() ? Failed : Finished;
98
99    if (m_state == Finished && m_cueParser)
100        m_cueParser->flush();
101
102    if (!m_cueLoadTimer.isActive())
103        m_cueLoadTimer.startOneShot(0, FROM_HERE);
104
105    cancelLoad();
106}
107
108bool TextTrackLoader::load(const KURL& url, const AtomicString& crossOriginMode)
109{
110    cancelLoad();
111
112    FetchRequest cueRequest(ResourceRequest(document().completeURL(url)), FetchInitiatorTypeNames::texttrack);
113
114    if (!crossOriginMode.isNull()) {
115        cueRequest.setCrossOriginAccessControl(document().securityOrigin(), crossOriginMode);
116    } else if (!document().securityOrigin()->canRequest(url)) {
117        // Text track elements without 'crossorigin' set on the parent are "No CORS"; report error if not same-origin.
118        corsPolicyPreventedLoad(document().securityOrigin(), url);
119        return false;
120    }
121
122    ResourceFetcher* fetcher = document().fetcher();
123    setResource(fetcher->fetchTextTrack(cueRequest));
124    return resource();
125}
126
127void TextTrackLoader::newCuesParsed()
128{
129    if (m_cueLoadTimer.isActive())
130        return;
131
132    m_newCuesAvailable = true;
133    m_cueLoadTimer.startOneShot(0, FROM_HERE);
134}
135
136void TextTrackLoader::newRegionsParsed()
137{
138    m_client.newRegionsAvailable(this);
139}
140
141void TextTrackLoader::fileFailedToParse()
142{
143    WTF_LOG(Media, "TextTrackLoader::fileFailedToParse");
144
145    m_state = Failed;
146
147    if (!m_cueLoadTimer.isActive())
148        m_cueLoadTimer.startOneShot(0, FROM_HERE);
149
150    cancelLoad();
151}
152
153void TextTrackLoader::getNewCues(WillBeHeapVector<RefPtrWillBeMember<VTTCue> >& outputCues)
154{
155    ASSERT(m_cueParser);
156    if (m_cueParser)
157        m_cueParser->getNewCues(outputCues);
158}
159
160void TextTrackLoader::getNewRegions(WillBeHeapVector<RefPtrWillBeMember<VTTRegion> >& outputRegions)
161{
162    ASSERT(m_cueParser);
163    if (m_cueParser)
164        m_cueParser->getNewRegions(outputRegions);
165}
166
167void TextTrackLoader::trace(Visitor* visitor)
168{
169    visitor->trace(m_cueParser);
170    visitor->trace(m_document);
171}
172
173}
174