1/*
2 * Copyright (C) 2015 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 NetworkCacheIOChannel_h
27#define NetworkCacheIOChannel_h
28
29#include "NetworkCacheData.h"
30#include <wtf/Function.h>
31#include <wtf/ThreadSafeRefCounted.h>
32#include <wtf/WorkQueue.h>
33#include <wtf/text/WTFString.h>
34
35#if USE(SOUP)
36#include <wtf/glib/GRefPtr.h>
37#endif
38
39namespace WebKit {
40namespace NetworkCache {
41
42class IOChannel : public ThreadSafeRefCounted<IOChannel> {
43public:
44 enum class Type { Read, Write, Create };
45 static Ref<IOChannel> open(const String& file, Type);
46
47 // Using nullptr as queue submits the result to the main queue.
48 // FIXME: We should add WorkQueue::main() instead.
49 void read(size_t offset, size_t, WorkQueue*, Function<void (Data&, int error)>&&);
50 void write(size_t offset, const Data&, WorkQueue*, Function<void (int error)>&&);
51
52 const String& path() const { return m_path; }
53 Type type() const { return m_type; }
54
55#if !USE(SOUP)
56 bool isOpened() const { return FileSystem::isHandleValid(m_fileDescriptor); }
57#else
58 bool isOpened() const { return true; }
59#endif
60
61 ~IOChannel();
62
63private:
64 IOChannel(const String& filePath, IOChannel::Type);
65
66#if USE(SOUP)
67 void readSyncInThread(size_t offset, size_t, WorkQueue*, Function<void (Data&, int error)>&&);
68#endif
69
70 String m_path;
71 Type m_type;
72
73#if !USE(SOUP)
74 FileSystem::PlatformFileHandle m_fileDescriptor { FileSystem::invalidPlatformFileHandle };
75#endif
76 std::atomic<bool> m_wasDeleted { false }; // Try to narrow down a crash, https://bugs.webkit.org/show_bug.cgi?id=165659
77#if PLATFORM(COCOA)
78 OSObjectPtr<dispatch_io_t> m_dispatchIO;
79#endif
80#if USE(SOUP)
81 GRefPtr<GInputStream> m_inputStream;
82 GRefPtr<GOutputStream> m_outputStream;
83 GRefPtr<GFileIOStream> m_ioStream;
84#endif
85};
86
87}
88}
89
90#endif
91