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
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
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.
32 #include "modules/filesystem/DirectoryReaderSync.h"
34 #include "bindings/core/v8/ExceptionState.h"
35 #include "core/dom/ExceptionCode.h"
36 #include "modules/filesystem/DirectoryEntry.h"
37 #include "modules/filesystem/DirectoryEntrySync.h"
38 #include "modules/filesystem/EntriesCallback.h"
39 #include "modules/filesystem/EntrySync.h"
40 #include "modules/filesystem/ErrorCallback.h"
41 #include "modules/filesystem/FileEntrySync.h"
45 class DirectoryReaderSync::EntriesCallbackHelper final
: public EntriesCallback
{
47 explicit EntriesCallbackHelper(DirectoryReaderSync
* reader
)
52 void handleEvent(const EntryHeapVector
& entries
) override
54 EntrySyncHeapVector syncEntries
;
55 syncEntries
.reserveInitialCapacity(entries
.size());
56 for (size_t i
= 0; i
< entries
.size(); ++i
)
57 syncEntries
.uncheckedAppend(EntrySync::create(entries
[i
].get()));
58 m_reader
->addEntries(syncEntries
);
61 DEFINE_INLINE_VIRTUAL_TRACE()
63 visitor
->trace(m_reader
);
64 EntriesCallback::trace(visitor
);
68 Member
<DirectoryReaderSync
> m_reader
;
71 class DirectoryReaderSync::ErrorCallbackHelper final
: public ErrorCallback
{
73 explicit ErrorCallbackHelper(DirectoryReaderSync
* reader
)
78 void handleEvent(FileError
* error
) override
80 m_reader
->setError(error
->code());
83 DEFINE_INLINE_VIRTUAL_TRACE()
85 visitor
->trace(m_reader
);
86 ErrorCallback::trace(visitor
);
90 Member
<DirectoryReaderSync
> m_reader
;
93 DirectoryReaderSync::DirectoryReaderSync(DOMFileSystemBase
* fileSystem
, const String
& fullPath
)
94 : DirectoryReaderBase(fileSystem
, fullPath
)
96 , m_errorCode(FileError::OK
)
100 DirectoryReaderSync::~DirectoryReaderSync()
104 EntrySyncHeapVector
DirectoryReaderSync::readEntries(ExceptionState
& exceptionState
)
106 if (!m_callbacksId
) {
107 m_callbacksId
= filesystem()->readDirectory(this, m_fullPath
, new EntriesCallbackHelper(this), new ErrorCallbackHelper(this), DOMFileSystemBase::Synchronous
);
110 if (m_errorCode
== FileError::OK
&& m_hasMoreEntries
&& m_entries
.isEmpty())
111 m_fileSystem
->waitForAdditionalResult(m_callbacksId
);
113 if (m_errorCode
!= FileError::OK
) {
114 FileError::throwDOMException(exceptionState
, m_errorCode
);
115 return EntrySyncHeapVector();
118 EntrySyncHeapVector result
;
119 result
.swap(m_entries
);
123 DEFINE_TRACE(DirectoryReaderSync
)
125 visitor
->trace(m_entries
);
126 DirectoryReaderBase::trace(visitor
);