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