1
2/*
3 * Copyright (C) 2009-2018 Apple Inc. All rights reserved.
4 * Copyright (C) 2009 Google Inc. All rights reserved.
5 * Copyright (C) 2012 Samsung Electronics Ltd. All Rights Reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following disclaimer
15 * in the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Google Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#pragma once
35
36#include "SocketStreamHandle.h"
37
38#if USE(SOUP)
39
40#include <pal/SessionID.h>
41#include <wtf/StreamBuffer.h>
42#include <wtf/UniqueArray.h>
43#include <wtf/glib/GRefPtr.h>
44
45typedef struct _GIOStream GIOStream;
46typedef struct _GObject GObject;
47
48namespace WebCore {
49
50class SocketStreamError;
51class SocketStreamHandleClient;
52class StorageSessionProvider;
53
54class SocketStreamHandleImpl final : public SocketStreamHandle {
55public:
56 static Ref<SocketStreamHandleImpl> create(const URL&, SocketStreamHandleClient&, PAL::SessionID, const String&, SourceApplicationAuditToken&&, const StorageSessionProvider*);
57 virtual ~SocketStreamHandleImpl();
58
59 const URL& url() const { return m_url; }
60
61 void platformSend(const uint8_t* data, size_t length, Function<void(bool)>&&) final;
62 void platformSendHandshake(const uint8_t* data, size_t length, const Optional<CookieRequestHeaderFieldProxy>&, Function<void(bool, bool)>&&) final;
63 void platformClose() final;
64private:
65 SocketStreamHandleImpl(const URL&, SocketStreamHandleClient&, const StorageSessionProvider*);
66
67 size_t bufferedAmount() final;
68 Optional<size_t> platformSendInternal(const uint8_t*, size_t);
69 bool sendPendingData();
70
71 void beginWaitingForSocketWritability();
72 void stopWaitingForSocketWritability();
73
74 static void connectedCallback(GObject*, GAsyncResult*, SocketStreamHandleImpl*);
75 static void readReadyCallback(GInputStream*, GAsyncResult*, SocketStreamHandleImpl*);
76 static gboolean writeReadyCallback(GPollableOutputStream*, SocketStreamHandleImpl*);
77
78 void connected(GRefPtr<GIOStream>&&);
79 void readBytes(gssize);
80 void didFail(SocketStreamError&&);
81 void writeReady();
82
83 RefPtr<const StorageSessionProvider> m_storageSessionProvider;
84
85 GRefPtr<GIOStream> m_stream;
86 GRefPtr<GInputStream> m_inputStream;
87 GRefPtr<GPollableOutputStream> m_outputStream;
88 GRefPtr<GSource> m_writeReadySource;
89 GRefPtr<GCancellable> m_cancellable;
90 UniqueArray<char> m_readBuffer;
91
92 StreamBuffer<uint8_t, 1024 * 1024> m_buffer;
93 static const unsigned maxBufferSize = 100 * 1024 * 1024;
94};
95
96} // namespace WebCore
97
98#endif
99