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#include <wtf/Optional.h>
29#include <wtf/Vector.h>
30#include <wtf/text/WTFString.h>
31
32namespace WebCore {
33
34enum class PasteboardItemPresentationStyle {
35 Unspecified,
36 Inline,
37 Attachment
38};
39
40struct PasteboardItemInfo {
41 Vector<String> pathsForFileUpload;
42 Vector<String> contentTypesForFileUpload;
43 Vector<String> contentTypesByFidelity;
44 String suggestedFileName;
45 bool isNonTextType { false };
46 bool containsFileURLAndFileUploadContent { false };
47 PasteboardItemPresentationStyle preferredPresentationStyle { PasteboardItemPresentationStyle::Unspecified };
48
49 String pathForContentType(const String& type) const
50 {
51 ASSERT(pathsForFileUpload.size() == contentTypesForFileUpload.size());
52 auto index = contentTypesForFileUpload.find(type);
53 if (index == notFound)
54 return { };
55
56 return pathsForFileUpload[index];
57 }
58
59 // The preferredPresentationStyle flag is platform API used by drag or copy sources to explicitly indicate
60 // that the data being written to the item provider should be treated as an attachment; unfortunately, not
61 // all clients attempt to set this flag, so we additionally take having a suggested filename as a strong
62 // indicator that the item should be treated as an attachment or file.
63 bool canBeTreatedAsAttachmentOrFile() const
64 {
65 switch (preferredPresentationStyle) {
66 case PasteboardItemPresentationStyle::Inline:
67 return false;
68 case PasteboardItemPresentationStyle::Attachment:
69 return true;
70 case PasteboardItemPresentationStyle::Unspecified:
71 return !suggestedFileName.isEmpty();
72 }
73 ASSERT_NOT_REACHED();
74 return false;
75 }
76
77 String contentTypeForHighestFidelityItem() const
78 {
79 if (contentTypesForFileUpload.isEmpty())
80 return { };
81
82 return contentTypesForFileUpload.first();
83 }
84
85 String pathForHighestFidelityItem() const
86 {
87 if (pathsForFileUpload.isEmpty())
88 return { };
89
90 return pathsForFileUpload.first();
91 }
92
93 template<class Encoder> void encode(Encoder&) const;
94 template<class Decoder> static Optional<PasteboardItemInfo> decode(Decoder&);
95};
96
97template<class Encoder>
98void PasteboardItemInfo::encode(Encoder& encoder) const
99{
100 encoder << pathsForFileUpload << contentTypesForFileUpload << contentTypesByFidelity << suggestedFileName << isNonTextType << containsFileURLAndFileUploadContent;
101 encoder.encodeEnum(preferredPresentationStyle);
102}
103
104template<class Decoder>
105Optional<PasteboardItemInfo> PasteboardItemInfo::decode(Decoder& decoder)
106{
107 PasteboardItemInfo result;
108 if (!decoder.decode(result.pathsForFileUpload))
109 return WTF::nullopt;
110
111 if (!decoder.decode(result.contentTypesForFileUpload))
112 return WTF::nullopt;
113
114 if (!decoder.decode(result.contentTypesByFidelity))
115 return WTF::nullopt;
116
117 if (!decoder.decode(result.suggestedFileName))
118 return WTF::nullopt;
119
120 if (!decoder.decode(result.isNonTextType))
121 return WTF::nullopt;
122
123 if (!decoder.decode(result.containsFileURLAndFileUploadContent))
124 return WTF::nullopt;
125
126 if (!decoder.decodeEnum(result.preferredPresentationStyle))
127 return WTF::nullopt;
128
129 return WTFMove(result);
130}
131
132}
133
134namespace WTF {
135
136template<> struct EnumTraits<WebCore::PasteboardItemPresentationStyle> {
137 using values = EnumValues<
138 WebCore::PasteboardItemPresentationStyle,
139 WebCore::PasteboardItemPresentationStyle::Unspecified,
140 WebCore::PasteboardItemPresentationStyle::Inline,
141 WebCore::PasteboardItemPresentationStyle::Attachment
142 >;
143};
144
145} // namespace WTF
146