Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / filesystem / DirectoryReader.cpp
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 "config.h"
32 #include "modules/filesystem/DirectoryReader.h"
33
34 #include "core/fileapi/FileError.h"
35 #include "modules/filesystem/EntriesCallback.h"
36 #include "modules/filesystem/Entry.h"
37 #include "modules/filesystem/ErrorCallback.h"
38
39 namespace blink {
40
41 class DirectoryReader::EntriesCallbackHelper final : public EntriesCallback {
42 public:
43     explicit EntriesCallbackHelper(DirectoryReader* reader)
44         : m_reader(reader)
45     {
46     }
47
48     virtual void handleEvent(const EntryHeapVector& entries) override
49     {
50         m_reader->addEntries(entries);
51     }
52
53     virtual void trace(Visitor* visitor) override
54     {
55         visitor->trace(m_reader);
56         EntriesCallback::trace(visitor);
57     }
58
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;
62 };
63
64 class DirectoryReader::ErrorCallbackHelper final : public ErrorCallback {
65 public:
66     explicit ErrorCallbackHelper(DirectoryReader* reader)
67         : m_reader(reader)
68     {
69     }
70
71     virtual void handleEvent(FileError* error) override
72     {
73         m_reader->onError(error);
74     }
75
76     virtual void trace(Visitor* visitor) override
77     {
78         visitor->trace(m_reader);
79         ErrorCallback::trace(visitor);
80     }
81
82 private:
83     Member<DirectoryReader> m_reader;
84 };
85
86 DirectoryReader::DirectoryReader(DOMFileSystemBase* fileSystem, const String& fullPath)
87     : DirectoryReaderBase(fileSystem, fullPath)
88     , m_isReading(false)
89 {
90 }
91
92 DirectoryReader::~DirectoryReader()
93 {
94 }
95
96 void DirectoryReader::readEntries(EntriesCallback* entriesCallback, ErrorCallback* errorCallback)
97 {
98     if (!m_isReading) {
99         m_isReading = true;
100         filesystem()->readDirectory(this, m_fullPath, new EntriesCallbackHelper(this), new ErrorCallbackHelper(this));
101     }
102
103     if (m_error) {
104         filesystem()->scheduleCallback(errorCallback, m_error);
105         return;
106     }
107
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;
112     }
113
114     if (!m_hasMoreEntries || !m_entries.isEmpty()) {
115         filesystem()->scheduleCallback(entriesCallback, m_entries);
116         m_entries.clear();
117         return;
118     }
119
120     m_entriesCallback = entriesCallback;
121     m_errorCallback = errorCallback;
122 }
123
124 void DirectoryReader::addEntries(const EntryHeapVector& entries)
125 {
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);
133     }
134 }
135
136 void DirectoryReader::onError(FileError* error)
137 {
138     m_error = error;
139     m_entriesCallback = nullptr;
140     if (m_errorCallback) {
141         ErrorCallback* errorCallback = m_errorCallback.release();
142         errorCallback->handleEvent(error);
143     }
144 }
145
146 void DirectoryReader::trace(Visitor* visitor)
147 {
148     visitor->trace(m_entries);
149     visitor->trace(m_error);
150     visitor->trace(m_entriesCallback);
151     visitor->trace(m_errorCallback);
152     DirectoryReaderBase::trace(visitor);
153 }
154
155 }