Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(585)

Side by Side Diff: third_party/WebKit/Source/modules/filesystem/DirectoryReader.cpp

Issue 2297043002: Web expose FileSystemFileEntry, FileSystemDirectoryEntry and friends (Closed)
Patch Set: Rebased Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2010 Google 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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "modules/filesystem/DirectoryReader.h"
32
33 #include "core/fileapi/FileError.h"
34 #include "modules/filesystem/EntriesCallback.h"
35 #include "modules/filesystem/Entry.h"
36 #include "modules/filesystem/ErrorCallback.h"
37 #include "modules/filesystem/FileSystemCallbacks.h"
38
39 namespace blink {
40
41 class DirectoryReader::EntriesCallbackHelper final : public EntriesCallback {
42 public:
43 explicit EntriesCallbackHelper(DirectoryReader* reader) : m_reader(reader) {}
44
45 void handleEvent(const EntryHeapVector& entries) override {
46 m_reader->addEntries(entries);
47 }
48
49 DEFINE_INLINE_VIRTUAL_TRACE() {
50 visitor->trace(m_reader);
51 EntriesCallback::trace(visitor);
52 }
53
54 private:
55 // FIXME: This Member keeps the reader alive until all of the readDirectory
56 // results are received. crbug.com/350285
57 Member<DirectoryReader> m_reader;
58 };
59
60 class DirectoryReader::ErrorCallbackHelper final : public ErrorCallbackBase {
61 public:
62 explicit ErrorCallbackHelper(DirectoryReader* reader) : m_reader(reader) {}
63
64 void invoke(FileError::ErrorCode error) override { m_reader->onError(error); }
65
66 DEFINE_INLINE_VIRTUAL_TRACE() {
67 visitor->trace(m_reader);
68 ErrorCallbackBase::trace(visitor);
69 }
70
71 private:
72 Member<DirectoryReader> m_reader;
73 };
74
75 DirectoryReader::DirectoryReader(DOMFileSystemBase* fileSystem,
76 const String& fullPath)
77 : DirectoryReaderBase(fileSystem, fullPath), m_isReading(false) {}
78
79 DirectoryReader::~DirectoryReader() {}
80
81 void DirectoryReader::readEntries(EntriesCallback* entriesCallback,
82 ErrorCallback* errorCallback) {
83 if (!m_isReading) {
84 m_isReading = true;
85 filesystem()->readDirectory(this, m_fullPath,
86 new EntriesCallbackHelper(this),
87 new ErrorCallbackHelper(this));
88 }
89
90 if (m_error) {
91 filesystem()->reportError(ScriptErrorCallback::wrap(errorCallback),
92 m_error);
93 return;
94 }
95
96 if (m_entriesCallback) {
97 // Non-null m_entriesCallback means multiple readEntries() calls are made
98 // concurrently. We don't allow doing it.
99 filesystem()->reportError(ScriptErrorCallback::wrap(errorCallback),
100 FileError::kInvalidStateErr);
101 return;
102 }
103
104 if (!m_hasMoreEntries || !m_entries.isEmpty()) {
105 if (entriesCallback)
106 DOMFileSystem::scheduleCallback(
107 filesystem()->getExecutionContext(),
108 createSameThreadTask(&EntriesCallback::handleEvent,
109 wrapPersistent(entriesCallback),
110 PersistentHeapVector<Member<Entry>>(m_entries)));
111 m_entries.clear();
112 return;
113 }
114
115 m_entriesCallback = entriesCallback;
116 m_errorCallback = errorCallback;
117 }
118
119 void DirectoryReader::addEntries(const EntryHeapVector& entries) {
120 m_entries.appendVector(entries);
121 m_errorCallback = nullptr;
122 if (m_entriesCallback) {
123 EntriesCallback* entriesCallback = m_entriesCallback.release();
124 EntryHeapVector entries;
125 entries.swap(m_entries);
126 entriesCallback->handleEvent(entries);
127 }
128 }
129
130 void DirectoryReader::onError(FileError::ErrorCode error) {
131 m_error = error;
132 m_entriesCallback = nullptr;
133 if (m_errorCallback)
134 m_errorCallback->handleEvent(FileError::createDOMException(error));
135 }
136
137 DEFINE_TRACE(DirectoryReader) {
138 visitor->trace(m_entries);
139 visitor->trace(m_entriesCallback);
140 visitor->trace(m_errorCallback);
141 DirectoryReaderBase::trace(visitor);
142 }
143
144 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698