1/*
2 * Copyright (C) 2006 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 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30#include "NetscapePlugInStreamLoader.h"
31
32#include "DocumentLoader.h"
33#include "FrameLoader.h"
34#include "FrameLoaderClient.h"
35#include <wtf/Ref.h>
36
37namespace WebCore {
38
39NetscapePlugInStreamLoader::NetscapePlugInStreamLoader(Frame* frame, NetscapePlugInStreamLoaderClient* client)
40    : ResourceLoader(frame, ResourceLoaderOptions(SendCallbacks, SniffContent, DoNotBufferData, AllowStoredCredentials, AskClientForAllCredentials, SkipSecurityCheck, UseDefaultOriginRestrictionsForType))
41    , m_client(client)
42{
43}
44
45NetscapePlugInStreamLoader::~NetscapePlugInStreamLoader()
46{
47}
48
49PassRefPtr<NetscapePlugInStreamLoader> NetscapePlugInStreamLoader::create(Frame* frame, NetscapePlugInStreamLoaderClient* client, const ResourceRequest& request)
50{
51    RefPtr<NetscapePlugInStreamLoader> loader(adoptRef(new NetscapePlugInStreamLoader(frame, client)));
52    if (!loader->init(request))
53        return 0;
54
55    loader->documentLoader()->addPlugInStreamLoader(loader.get());
56    return loader.release();
57}
58
59bool NetscapePlugInStreamLoader::isDone() const
60{
61    return !m_client;
62}
63
64void NetscapePlugInStreamLoader::releaseResources()
65{
66    m_client = 0;
67    ResourceLoader::releaseResources();
68}
69
70void NetscapePlugInStreamLoader::didReceiveResponse(const ResourceResponse& response)
71{
72    Ref<NetscapePlugInStreamLoader> protect(*this);
73
74    m_client->didReceiveResponse(this, response);
75
76    // Don't continue if the stream is cancelled
77    if (!m_client)
78        return;
79
80    ResourceLoader::didReceiveResponse(response);
81
82    // Don't continue if the stream is cancelled
83    if (!m_client)
84        return;
85
86    if (!response.isHTTP())
87        return;
88
89    if (m_client->wantsAllStreams())
90        return;
91
92    // Status code can be null when serving from a Web archive.
93    if (response.httpStatusCode() && (response.httpStatusCode() < 100 || response.httpStatusCode() >= 400))
94        cancel(frameLoader()->client().fileDoesNotExistError(response));
95}
96
97void NetscapePlugInStreamLoader::didReceiveData(const char* data, unsigned length, long long encodedDataLength, DataPayloadType dataPayloadType)
98{
99    didReceiveDataOrBuffer(data, length, 0, encodedDataLength, dataPayloadType);
100}
101
102void NetscapePlugInStreamLoader::didReceiveBuffer(PassRefPtr<SharedBuffer> buffer, long long encodedDataLength, DataPayloadType dataPayloadType)
103{
104    didReceiveDataOrBuffer(0, 0, buffer, encodedDataLength, dataPayloadType);
105}
106
107void NetscapePlugInStreamLoader::didReceiveDataOrBuffer(const char* data, int length, PassRefPtr<SharedBuffer> buffer, long long encodedDataLength, DataPayloadType dataPayloadType)
108{
109    Ref<NetscapePlugInStreamLoader> protect(*this);
110
111    m_client->didReceiveData(this, buffer ? buffer->data() : data, buffer ? buffer->size() : length);
112
113    ResourceLoader::didReceiveDataOrBuffer(data, length, buffer, encodedDataLength, dataPayloadType);
114}
115
116void NetscapePlugInStreamLoader::didFinishLoading(double finishTime)
117{
118    Ref<NetscapePlugInStreamLoader> protect(*this);
119
120    m_documentLoader->removePlugInStreamLoader(this);
121    m_client->didFinishLoading(this);
122    ResourceLoader::didFinishLoading(finishTime);
123}
124
125void NetscapePlugInStreamLoader::didFail(const ResourceError& error)
126{
127    Ref<NetscapePlugInStreamLoader> protect(*this);
128
129    m_documentLoader->removePlugInStreamLoader(this);
130    m_client->didFail(this, error);
131    ResourceLoader::didFail(error);
132}
133
134void NetscapePlugInStreamLoader::willCancel(const ResourceError& error)
135{
136    m_client->didFail(this, error);
137}
138
139void NetscapePlugInStreamLoader::didCancel(const ResourceError&)
140{
141    // We need to remove the stream loader after the call to didFail, since didFail can
142    // spawn a new run loop and if the loader has been removed it won't be deferred when
143    // the document loader is asked to defer loading.
144    m_documentLoader->removePlugInStreamLoader(this);
145}
146
147}
148