1/*
2 * Copyright (C) 2010 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 NetscapePluginStream_h
27#define NetscapePluginStream_h
28
29#if ENABLE(NETSCAPE_PLUGIN_API)
30
31#include <WebCore/FileSystem.h>
32#include <WebCore/RunLoop.h>
33#include <WebCore/npruntime_internal.h>
34#include <wtf/Forward.h>
35#include <wtf/PassRefPtr.h>
36#include <wtf/RefCounted.h>
37#include <wtf/RefPtr.h>
38#include <wtf/text/CString.h>
39
40namespace WebCore {
41class KURL;
42}
43
44namespace WebKit {
45
46class NetscapePlugin;
47
48class NetscapePluginStream : public RefCounted<NetscapePluginStream> {
49public:
50    static PassRefPtr<NetscapePluginStream> create(PassRefPtr<NetscapePlugin> plugin, uint64_t streamID, const String& requestURLString, bool sendNotification, void* notificationData)
51    {
52        return adoptRef(new NetscapePluginStream(plugin, streamID, requestURLString, sendNotification, notificationData));
53    }
54    ~NetscapePluginStream();
55
56    uint64_t streamID() const { return m_streamID; }
57    const NPStream* npStream() const { return &m_npStream; }
58
59    void didReceiveResponse(const WebCore::KURL& responseURL, uint32_t streamLength,
60                            uint32_t lastModifiedTime, const String& mimeType, const String& headers);
61    void didReceiveData(const char* bytes, int length);
62    void didFinishLoading();
63    void didFail(bool wasCancelled);
64
65    void sendJavaScriptStream(const String& result);
66
67    void stop(NPReason);
68    NPError destroy(NPReason);
69
70private:
71    NetscapePluginStream(PassRefPtr<NetscapePlugin>, uint64_t streamID, const String& requestURLString, bool sendNotification, void* notificationData);
72
73    bool start(const String& responseURLString, uint32_t streamLength,
74               uint32_t lastModifiedTime, const String& mimeType, const String& headers);
75
76    void cancel();
77    void notifyAndDestroyStream(NPReason);
78
79    void deliverData(const char* bytes, int length);
80    void deliverDataToPlugin();
81    void deliverDataToFile(const char* bytes, int length);
82
83    RefPtr<NetscapePlugin> m_plugin;
84    uint64_t m_streamID;
85
86    String m_requestURLString;
87    bool m_sendNotification;
88    void* m_notificationData;
89
90    NPStream m_npStream;
91    uint16_t m_transferMode;
92    int32_t m_offset;
93
94    String m_filePath;
95    WebCore::PlatformFileHandle m_fileHandle;
96
97    // Whether NPP_NewStream has successfully been called.
98    bool m_isStarted;
99
100#if !ASSERT_DISABLED
101    bool m_urlNotifyHasBeenCalled;
102#endif
103
104    CString m_responseURL;
105    CString m_mimeType;
106    CString m_headers;
107
108    WebCore::RunLoop::Timer<NetscapePluginStream> m_deliveryDataTimer;
109    OwnPtr< Vector<uint8_t>> m_deliveryData;
110    bool m_stopStreamWhenDoneDelivering;
111};
112
113} // namespace WebKit
114
115#endif // ENABLE(NETSCAPE_PLUGIN_API)
116
117#endif // NetscapePluginStream_h
118