OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 20 matching lines...) Expand all Loading... |
31 #include "config.h" | 31 #include "config.h" |
32 #include "modules/filesystem/DirectoryReader.h" | 32 #include "modules/filesystem/DirectoryReader.h" |
33 | 33 |
34 #include "core/fileapi/FileError.h" | 34 #include "core/fileapi/FileError.h" |
35 #include "modules/filesystem/EntriesCallback.h" | 35 #include "modules/filesystem/EntriesCallback.h" |
36 #include "modules/filesystem/Entry.h" | 36 #include "modules/filesystem/Entry.h" |
37 #include "modules/filesystem/ErrorCallback.h" | 37 #include "modules/filesystem/ErrorCallback.h" |
38 | 38 |
39 namespace WebCore { | 39 namespace WebCore { |
40 | 40 |
| 41 class DirectoryReader::EntriesCallbackHelper : public EntriesCallback { |
| 42 public: |
| 43 EntriesCallbackHelper(PassRefPtr<DirectoryReader> reader) |
| 44 : m_reader(reader) |
| 45 { |
| 46 } |
| 47 |
| 48 virtual void handleEvent(const Vector<RefPtr<Entry> >& entries) OVERRIDE |
| 49 { |
| 50 m_reader->addEntries(entries); |
| 51 } |
| 52 |
| 53 private: |
| 54 // FIXME: This RefPtr keeps the reader alive until all of the readDirectory
results are received. crbug.com/350285 |
| 55 RefPtr<DirectoryReader> m_reader; |
| 56 }; |
| 57 |
| 58 class DirectoryReader::ErrorCallbackHelper : public ErrorCallback { |
| 59 public: |
| 60 ErrorCallbackHelper(PassRefPtr<DirectoryReader> reader) |
| 61 : m_reader(reader) |
| 62 { |
| 63 } |
| 64 |
| 65 virtual void handleEvent(FileError* error) OVERRIDE |
| 66 { |
| 67 m_reader->onError(error); |
| 68 } |
| 69 |
| 70 private: |
| 71 RefPtr<DirectoryReader> m_reader; |
| 72 }; |
| 73 |
41 DirectoryReader::DirectoryReader(PassRefPtr<DOMFileSystemBase> fileSystem, const
String& fullPath) | 74 DirectoryReader::DirectoryReader(PassRefPtr<DOMFileSystemBase> fileSystem, const
String& fullPath) |
42 : DirectoryReaderBase(fileSystem, fullPath) | 75 : DirectoryReaderBase(fileSystem, fullPath) |
| 76 , m_isReading(false) |
43 { | 77 { |
44 ScriptWrappable::init(this); | 78 ScriptWrappable::init(this); |
45 } | 79 } |
46 | 80 |
47 void DirectoryReader::readEntries(PassOwnPtr<EntriesCallback> entriesCallback, P
assOwnPtr<ErrorCallback> errorCallback) | 81 void DirectoryReader::readEntries(PassOwnPtr<EntriesCallback> entriesCallback, P
assOwnPtr<ErrorCallback> errorCallback) |
48 { | 82 { |
49 if (!m_hasMoreEntries) { | 83 if (!m_isReading) { |
50 filesystem()->scheduleCallback(entriesCallback, EntryVector()); | 84 m_isReading = true; |
| 85 filesystem()->readDirectory(this, m_fullPath, adoptPtr(new EntriesCallba
ckHelper(this)), adoptPtr(new ErrorCallbackHelper(this))); |
| 86 } |
| 87 |
| 88 if (m_error) { |
| 89 filesystem()->scheduleCallback(errorCallback, m_error.get()); |
51 return; | 90 return; |
52 } | 91 } |
53 filesystem()->readDirectory(this, m_fullPath, entriesCallback, errorCallback
); | 92 |
| 93 if (m_entriesCallback) { |
| 94 // Non-null m_entriesCallback means multiple readEntries() calls are mad
e concurrently. We don't allow doing it. |
| 95 filesystem()->scheduleCallback(errorCallback, FileError::create(FileErro
r::INVALID_STATE_ERR)); |
| 96 return; |
| 97 } |
| 98 |
| 99 if (!m_hasMoreEntries || !m_entries.isEmpty()) { |
| 100 filesystem()->scheduleCallback(entriesCallback, m_entries); |
| 101 m_entries.clear(); |
| 102 return; |
| 103 } |
| 104 |
| 105 m_entriesCallback = entriesCallback; |
| 106 m_errorCallback = errorCallback; |
| 107 } |
| 108 |
| 109 void DirectoryReader::addEntries(const Vector<RefPtr<Entry> >& entries) |
| 110 { |
| 111 m_entries.appendVector(entries); |
| 112 if (m_entriesCallback) { |
| 113 OwnPtr<EntriesCallback> entriesCallback = m_entriesCallback.release(); |
| 114 Vector<RefPtr<Entry> > entries; |
| 115 entries.swap(m_entries); |
| 116 entriesCallback->handleEvent(entries); |
| 117 } |
| 118 } |
| 119 |
| 120 void DirectoryReader::onError(FileError* error) |
| 121 { |
| 122 m_error = error; |
| 123 if (m_errorCallback) |
| 124 m_errorCallback->handleEvent(error); |
54 } | 125 } |
55 | 126 |
56 } | 127 } |
OLD | NEW |