1/*
2 * Copyright (C) 2017 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(ATTACHMENT_ELEMENT)
29
30#include "APIObject.h"
31#include "WKBase.h"
32#include "WebPageProxy.h"
33#include <wtf/RefPtr.h>
34#include <wtf/WeakPtr.h>
35#include <wtf/text/WTFString.h>
36
37OBJC_CLASS NSFileWrapper;
38
39namespace WebCore {
40class SharedBuffer;
41}
42
43namespace WebKit {
44class WebPageProxy;
45}
46
47namespace API {
48
49class Attachment final : public ObjectImpl<Object::Type::Attachment> {
50public:
51 static Ref<Attachment> create(const WTF::String& identifier, WebKit::WebPageProxy&);
52 virtual ~Attachment();
53
54 enum class InsertionState : uint8_t { NotInserted, Inserted };
55
56 const WTF::String& identifier() const { return m_identifier; }
57 void updateAttributes(Function<void(WebKit::CallbackBase::Error)>&&);
58
59 void invalidate();
60 bool isValid() const { return !!m_webPage; }
61
62#if PLATFORM(COCOA)
63 NSFileWrapper *fileWrapper() const;
64 void setFileWrapper(NSFileWrapper *fileWrapper) { m_fileWrapper = fileWrapper; }
65 void setFileWrapperAndUpdateContentType(NSFileWrapper *, NSString *contentType);
66 void setFileWrapperGenerator(Function<RetainPtr<NSFileWrapper>(void)>&&);
67 void invalidateGeneratedFileWrapper();
68 WTF::String utiType() const;
69#endif
70 WTF::String mimeType() const;
71
72 const WTF::String& filePath() const { return m_filePath; }
73 void setFilePath(const WTF::String& filePath) { m_filePath = filePath; }
74 WTF::String fileName() const;
75
76 const WTF::String& contentType() const { return m_contentType; }
77 void setContentType(const WTF::String& contentType) { m_contentType = contentType; }
78
79 InsertionState insertionState() const { return m_insertionState; }
80 void setInsertionState(InsertionState state) { m_insertionState = state; }
81
82 bool isEmpty() const;
83
84 RefPtr<WebCore::SharedBuffer> enclosingImageData() const;
85 Optional<uint64_t> fileSizeForDisplay() const;
86
87 void setHasEnclosingImage(bool hasEnclosingImage) { m_hasEnclosingImage = hasEnclosingImage; }
88 bool hasEnclosingImage() const { return m_hasEnclosingImage; }
89
90 RefPtr<WebCore::SharedBuffer> createSerializedRepresentation() const;
91 void updateFromSerializedRepresentation(Ref<WebCore::SharedBuffer>&&, const WTF::String& contentType);
92
93private:
94 explicit Attachment(const WTF::String& identifier, WebKit::WebPageProxy&);
95
96#if PLATFORM(COCOA)
97 mutable RetainPtr<NSFileWrapper> m_fileWrapper;
98 Function<RetainPtr<NSFileWrapper>(void)> m_fileWrapperGenerator;
99#endif
100 WTF::String m_identifier;
101 WTF::String m_filePath;
102 WTF::String m_contentType;
103 WeakPtr<WebKit::WebPageProxy> m_webPage;
104 InsertionState m_insertionState { InsertionState::NotInserted };
105 bool m_hasEnclosingImage { false };
106};
107
108} // namespace API
109
110#endif // ENABLE(ATTACHMENT_ELEMENT)
111