Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / modules / filesystem / DirectoryReader.cpp
blob47c87fdfc440c9461be87b7986beee7184e352eb
1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
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.
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.
31 #include "config.h"
32 #include "modules/filesystem/DirectoryReader.h"
34 #include "core/fileapi/FileError.h"
35 #include "modules/filesystem/EntriesCallback.h"
36 #include "modules/filesystem/Entry.h"
37 #include "modules/filesystem/ErrorCallback.h"
39 namespace blink {
41 class DirectoryReader::EntriesCallbackHelper final : public EntriesCallback {
42 public:
43 explicit EntriesCallbackHelper(DirectoryReader* reader)
44 : m_reader(reader)
48 void handleEvent(const EntryHeapVector& entries) override
50 m_reader->addEntries(entries);
53 DEFINE_INLINE_VIRTUAL_TRACE()
55 visitor->trace(m_reader);
56 EntriesCallback::trace(visitor);
59 private:
60 // FIXME: This Member keeps the reader alive until all of the readDirectory results are received. crbug.com/350285
61 Member<DirectoryReader> m_reader;
64 class DirectoryReader::ErrorCallbackHelper final : public ErrorCallback {
65 public:
66 explicit ErrorCallbackHelper(DirectoryReader* reader)
67 : m_reader(reader)
71 void handleEvent(FileError* error) override
73 m_reader->onError(error);
76 DEFINE_INLINE_VIRTUAL_TRACE()
78 visitor->trace(m_reader);
79 ErrorCallback::trace(visitor);
82 private:
83 Member<DirectoryReader> m_reader;
86 DirectoryReader::DirectoryReader(DOMFileSystemBase* fileSystem, const String& fullPath)
87 : DirectoryReaderBase(fileSystem, fullPath)
88 , m_isReading(false)
92 DirectoryReader::~DirectoryReader()
96 void DirectoryReader::readEntries(EntriesCallback* entriesCallback, ErrorCallback* errorCallback)
98 if (!m_isReading) {
99 m_isReading = true;
100 filesystem()->readDirectory(this, m_fullPath, new EntriesCallbackHelper(this), new ErrorCallbackHelper(this));
103 if (m_error) {
104 filesystem()->scheduleCallback(errorCallback, m_error);
105 return;
108 if (m_entriesCallback) {
109 // Non-null m_entriesCallback means multiple readEntries() calls are made concurrently. We don't allow doing it.
110 filesystem()->scheduleCallback(errorCallback, FileError::create(FileError::INVALID_STATE_ERR));
111 return;
114 if (!m_hasMoreEntries || !m_entries.isEmpty()) {
115 filesystem()->scheduleCallback(entriesCallback, m_entries);
116 m_entries.clear();
117 return;
120 m_entriesCallback = entriesCallback;
121 m_errorCallback = errorCallback;
124 void DirectoryReader::addEntries(const EntryHeapVector& entries)
126 m_entries.appendVector(entries);
127 m_errorCallback = nullptr;
128 if (m_entriesCallback) {
129 EntriesCallback* entriesCallback = m_entriesCallback.release();
130 EntryHeapVector entries;
131 entries.swap(m_entries);
132 entriesCallback->handleEvent(entries);
136 void DirectoryReader::onError(FileError* error)
138 m_error = error;
139 m_entriesCallback = nullptr;
140 if (m_errorCallback) {
141 ErrorCallback* errorCallback = m_errorCallback.release();
142 errorCallback->handleEvent(error);
146 DEFINE_TRACE(DirectoryReader)
148 visitor->trace(m_entries);
149 visitor->trace(m_error);
150 visitor->trace(m_entriesCallback);
151 visitor->trace(m_errorCallback);
152 DirectoryReaderBase::trace(visitor);