1 | /* |
2 | * Copyright (C) 2006-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 | * |
8 | * 1. Redistributions of source code must retain the above copyright |
9 | * notice, this list of conditions and the following disclaimer. |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
13 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
14 | * its contributors may be used to endorse or promote products derived |
15 | * from this software without specific prior written permission. |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
18 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
20 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
24 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | * |
28 | */ |
29 | |
30 | #ifndef FileChooser_h |
31 | #define FileChooser_h |
32 | |
33 | #include <wtf/RefCounted.h> |
34 | #include <wtf/Vector.h> |
35 | #include <wtf/text/WTFString.h> |
36 | |
37 | namespace WebCore { |
38 | |
39 | enum MediaCaptureType { |
40 | MediaCaptureTypeNone, |
41 | MediaCaptureTypeUser, |
42 | MediaCaptureTypeEnvironment |
43 | }; |
44 | |
45 | class FileChooser; |
46 | class Icon; |
47 | |
48 | struct FileChooserFileInfo { |
49 | FileChooserFileInfo(const String& path, const String& displayName = String()) |
50 | : path(path) |
51 | , displayName(displayName) |
52 | { |
53 | } |
54 | |
55 | FileChooserFileInfo isolatedCopy() const |
56 | { |
57 | return { path.isolatedCopy(), displayName.isolatedCopy() }; |
58 | } |
59 | |
60 | const String path; |
61 | const String displayName; |
62 | }; |
63 | |
64 | struct FileChooserSettings { |
65 | bool allowsDirectories { false }; |
66 | bool allowsMultipleFiles { false }; |
67 | Vector<String> acceptMIMETypes; |
68 | Vector<String> acceptFileExtensions; |
69 | Vector<String> selectedFiles; |
70 | #if ENABLE(MEDIA_CAPTURE) |
71 | MediaCaptureType mediaCaptureType { MediaCaptureTypeNone }; |
72 | #endif |
73 | }; |
74 | |
75 | class FileChooserClient { |
76 | public: |
77 | virtual ~FileChooserClient() = default; |
78 | |
79 | virtual void filesChosen(const Vector<FileChooserFileInfo>&, const String& displayString = { }, Icon* = nullptr) = 0; |
80 | }; |
81 | |
82 | class FileChooser : public RefCounted<FileChooser> { |
83 | public: |
84 | static Ref<FileChooser> create(FileChooserClient*, const FileChooserSettings&); |
85 | WEBCORE_EXPORT ~FileChooser(); |
86 | |
87 | void invalidate(); |
88 | |
89 | WEBCORE_EXPORT void chooseFile(const String& path); |
90 | WEBCORE_EXPORT void chooseFiles(const Vector<String>& paths); |
91 | #if PLATFORM(IOS_FAMILY) |
92 | // FIXME: This function is almost identical to FileChooser::chooseFiles(). We should merge this |
93 | // function with FileChooser::chooseFiles() and hence remove the PLATFORM(IOS_FAMILY)-guard. |
94 | WEBCORE_EXPORT void chooseMediaFiles(const Vector<String>& paths, const String& displayString, Icon*); |
95 | #endif |
96 | |
97 | // FIXME: We should probably just pass file paths that could be virtual paths with proper display names rather than passing structs. |
98 | void chooseFiles(const Vector<FileChooserFileInfo>& files); |
99 | |
100 | const FileChooserSettings& settings() const { return m_settings; } |
101 | |
102 | private: |
103 | FileChooser(FileChooserClient*, const FileChooserSettings&); |
104 | |
105 | FileChooserClient* m_client { nullptr }; |
106 | FileChooserSettings m_settings; |
107 | }; |
108 | |
109 | } // namespace WebCore |
110 | |
111 | #endif // FileChooser_h |
112 | |