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