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 "FileSystemDirectoryReader.h" |
28 | |
29 | #include "DOMException.h" |
30 | #include "DOMFileSystem.h" |
31 | #include "ErrorCallback.h" |
32 | #include "FileSystemDirectoryEntry.h" |
33 | #include "FileSystemEntriesCallback.h" |
34 | #include "ScriptExecutionContext.h" |
35 | #include <wtf/IsoMallocInlines.h> |
36 | #include <wtf/MainThread.h> |
37 | |
38 | namespace WebCore { |
39 | |
40 | WTF_MAKE_ISO_ALLOCATED_IMPL(FileSystemDirectoryReader); |
41 | |
42 | FileSystemDirectoryReader::FileSystemDirectoryReader(ScriptExecutionContext& context, FileSystemDirectoryEntry& directory) |
43 | : ActiveDOMObject(&context) |
44 | , m_directory(directory) |
45 | { |
46 | suspendIfNeeded(); |
47 | } |
48 | |
49 | FileSystemDirectoryReader::~FileSystemDirectoryReader() = default; |
50 | |
51 | const char* FileSystemDirectoryReader::activeDOMObjectName() const |
52 | { |
53 | return "FileSystemDirectoryReader" ; |
54 | } |
55 | |
56 | bool FileSystemDirectoryReader::canSuspendForDocumentSuspension() const |
57 | { |
58 | return !hasPendingActivity(); |
59 | } |
60 | |
61 | // https://wicg.github.io/entries-api/#dom-filesystemdirectoryentry-readentries |
62 | void FileSystemDirectoryReader::readEntries(ScriptExecutionContext& context, Ref<FileSystemEntriesCallback>&& successCallback, RefPtr<ErrorCallback>&& errorCallback) |
63 | { |
64 | if (m_isReading) { |
65 | if (errorCallback) |
66 | errorCallback->scheduleCallback(context, DOMException::create(Exception { InvalidStateError, "Directory reader is already reading"_s })); |
67 | return; |
68 | } |
69 | |
70 | if (m_error) { |
71 | if (errorCallback) |
72 | errorCallback->scheduleCallback(context, DOMException::create(*m_error)); |
73 | return; |
74 | } |
75 | |
76 | if (m_isDone) { |
77 | successCallback->scheduleCallback(context, { }); |
78 | return; |
79 | } |
80 | |
81 | m_isReading = true; |
82 | auto pendingActivity = makePendingActivity(*this); |
83 | callOnMainThread([this, context = makeRef(context), successCallback = WTFMove(successCallback), errorCallback = WTFMove(errorCallback), pendingActivity = WTFMove(pendingActivity)]() mutable { |
84 | m_isReading = false; |
85 | m_directory->filesystem().listDirectory(context, m_directory, [this, successCallback = WTFMove(successCallback), errorCallback = WTFMove(errorCallback), pendingActivity = WTFMove(pendingActivity)](ExceptionOr<Vector<Ref<FileSystemEntry>>>&& result) { |
86 | if (result.hasException()) { |
87 | m_error = result.releaseException(); |
88 | if (errorCallback) |
89 | errorCallback->handleEvent(DOMException::create(*m_error)); |
90 | return; |
91 | } |
92 | m_isDone = true; |
93 | successCallback->handleEvent(result.releaseReturnValue()); |
94 | }); |
95 | }); |
96 | } |
97 | |
98 | } // namespace WebCore |
99 | |