| 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 | #include "config.h" |
| 27 | #include "FileListCreator.h" |
| 28 | |
| 29 | #include "FileChooser.h" |
| 30 | #include "FileList.h" |
| 31 | #include <wtf/CrossThreadCopier.h> |
| 32 | #include <wtf/FileMetadata.h> |
| 33 | #include <wtf/FileSystem.h> |
| 34 | |
| 35 | namespace WebCore { |
| 36 | |
| 37 | FileListCreator::~FileListCreator() |
| 38 | { |
| 39 | ASSERT(!m_completionHander); |
| 40 | } |
| 41 | |
| 42 | static void appendDirectoryFiles(const String& directory, const String& relativePath, Vector<Ref<File>>& fileObjects) |
| 43 | { |
| 44 | for (auto& childPath : FileSystem::listDirectory(directory, "*" )) { |
| 45 | auto metadata = FileSystem::fileMetadata(childPath); |
| 46 | if (!metadata) |
| 47 | continue; |
| 48 | |
| 49 | if (metadata.value().isHidden) |
| 50 | continue; |
| 51 | |
| 52 | String childRelativePath = relativePath + "/" + FileSystem::pathGetFileName(childPath); |
| 53 | if (metadata.value().type == FileMetadata::Type::Directory) |
| 54 | appendDirectoryFiles(childPath, childRelativePath, fileObjects); |
| 55 | else if (metadata.value().type == FileMetadata::Type::File) |
| 56 | fileObjects.append(File::createWithRelativePath(childPath, childRelativePath)); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | FileListCreator::FileListCreator(const Vector<FileChooserFileInfo>& paths, ShouldResolveDirectories shouldResolveDirectories, CompletionHandler&& completionHandler) |
| 61 | { |
| 62 | if (shouldResolveDirectories == ShouldResolveDirectories::No) |
| 63 | completionHandler(createFileList<ShouldResolveDirectories::No>(paths)); |
| 64 | else { |
| 65 | // Resolve directories on a background thread to avoid blocking the main thread. |
| 66 | m_completionHander = WTFMove(completionHandler); |
| 67 | m_workQueue = WorkQueue::create("FileListCreator Work Queue" ); |
| 68 | m_workQueue->dispatch([this, protectedThis = makeRef(*this), paths = crossThreadCopy(paths)]() mutable { |
| 69 | auto fileList = createFileList<ShouldResolveDirectories::Yes>(paths); |
| 70 | callOnMainThread([this, protectedThis = WTFMove(protectedThis), fileList = WTFMove(fileList)]() mutable { |
| 71 | if (auto completionHander = WTFMove(m_completionHander)) |
| 72 | completionHander(WTFMove(fileList)); |
| 73 | }); |
| 74 | }); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | template<FileListCreator::ShouldResolveDirectories shouldResolveDirectories> |
| 79 | Ref<FileList> FileListCreator::createFileList(const Vector<FileChooserFileInfo>& paths) |
| 80 | { |
| 81 | Vector<Ref<File>> fileObjects; |
| 82 | for (auto& info : paths) { |
| 83 | if (shouldResolveDirectories == ShouldResolveDirectories::Yes && FileSystem::fileIsDirectory(info.path, FileSystem::ShouldFollowSymbolicLinks::No)) |
| 84 | appendDirectoryFiles(info.path, FileSystem::pathGetFileName(info.path), fileObjects); |
| 85 | else |
| 86 | fileObjects.append(File::createWithName(info.path, info.displayName)); |
| 87 | } |
| 88 | return FileList::create(WTFMove(fileObjects)); |
| 89 | } |
| 90 | |
| 91 | void FileListCreator::cancel() |
| 92 | { |
| 93 | m_completionHander = nullptr; |
| 94 | m_workQueue = nullptr; |
| 95 | } |
| 96 | |
| 97 | } // namespace WebCore |
| 98 | |