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 15 matching lines...) Expand all Loading... |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "modules/filesystem/DOMFileSystem.h" | 32 #include "modules/filesystem/DOMFileSystem.h" |
33 | 33 |
34 #include "core/dom/ScriptExecutionContext.h" | 34 #include "core/dom/ScriptExecutionContext.h" |
35 #include "core/fileapi/File.h" | 35 #include "core/fileapi/File.h" |
36 #include "core/platform/AsyncFileSystem.h" | |
37 #include "core/platform/FileMetadata.h" | 36 #include "core/platform/FileMetadata.h" |
38 #include "modules/filesystem/DOMFilePath.h" | 37 #include "modules/filesystem/DOMFilePath.h" |
39 #include "modules/filesystem/DirectoryEntry.h" | 38 #include "modules/filesystem/DirectoryEntry.h" |
40 #include "modules/filesystem/ErrorCallback.h" | 39 #include "modules/filesystem/ErrorCallback.h" |
41 #include "modules/filesystem/FileCallback.h" | 40 #include "modules/filesystem/FileCallback.h" |
42 #include "modules/filesystem/FileEntry.h" | 41 #include "modules/filesystem/FileEntry.h" |
43 #include "modules/filesystem/FileSystemCallbacks.h" | 42 #include "modules/filesystem/FileSystemCallbacks.h" |
44 #include "modules/filesystem/FileWriter.h" | 43 #include "modules/filesystem/FileWriter.h" |
45 #include "modules/filesystem/FileWriterBaseCallback.h" | 44 #include "modules/filesystem/FileWriterBaseCallback.h" |
46 #include "modules/filesystem/FileWriterCallback.h" | 45 #include "modules/filesystem/FileWriterCallback.h" |
47 #include "modules/filesystem/MetadataCallback.h" | 46 #include "modules/filesystem/MetadataCallback.h" |
| 47 #include "public/platform/WebFileSystem.h" |
| 48 #include "public/platform/WebFileSystemCallbacks.h" |
48 #include "weborigin/DatabaseIdentifier.h" | 49 #include "weborigin/DatabaseIdentifier.h" |
49 #include "weborigin/SecurityOrigin.h" | 50 #include "weborigin/SecurityOrigin.h" |
50 #include "wtf/OwnPtr.h" | 51 #include "wtf/OwnPtr.h" |
51 #include "wtf/text/StringBuilder.h" | 52 #include "wtf/text/StringBuilder.h" |
52 #include "wtf/text/WTFString.h" | 53 #include "wtf/text/WTFString.h" |
53 | 54 |
54 namespace WebCore { | 55 namespace WebCore { |
55 | 56 |
56 // static | 57 // static |
57 PassRefPtr<DOMFileSystem> DOMFileSystem::create(ScriptExecutionContext* context,
const String& name, FileSystemType type, const KURL& rootURL, PassOwnPtr<AsyncF
ileSystem> asyncFileSystem) | 58 PassRefPtr<DOMFileSystem> DOMFileSystem::create(ScriptExecutionContext* context,
const String& name, FileSystemType type, const KURL& rootURL) |
58 { | 59 { |
59 RefPtr<DOMFileSystem> fileSystem(adoptRef(new DOMFileSystem(context, name, t
ype, rootURL, asyncFileSystem))); | 60 RefPtr<DOMFileSystem> fileSystem(adoptRef(new DOMFileSystem(context, name, t
ype, rootURL))); |
60 fileSystem->suspendIfNeeded(); | 61 fileSystem->suspendIfNeeded(); |
61 return fileSystem.release(); | 62 return fileSystem.release(); |
62 } | 63 } |
63 | 64 |
64 PassRefPtr<DOMFileSystem> DOMFileSystem::createIsolatedFileSystem(ScriptExecutio
nContext* context, const String& filesystemId) | 65 PassRefPtr<DOMFileSystem> DOMFileSystem::createIsolatedFileSystem(ScriptExecutio
nContext* context, const String& filesystemId) |
65 { | 66 { |
66 if (filesystemId.isEmpty()) | 67 if (filesystemId.isEmpty()) |
67 return 0; | 68 return 0; |
68 | 69 |
69 StringBuilder filesystemName; | 70 StringBuilder filesystemName; |
70 filesystemName.append(createDatabaseIdentifierFromSecurityOrigin(context->se
curityOrigin())); | 71 filesystemName.append(createDatabaseIdentifierFromSecurityOrigin(context->se
curityOrigin())); |
71 filesystemName.append(":Isolated_"); | 72 filesystemName.append(":Isolated_"); |
72 filesystemName.append(filesystemId); | 73 filesystemName.append(filesystemId); |
73 | 74 |
74 // The rootURL created here is going to be attached to each filesystem reque
st and | 75 // The rootURL created here is going to be attached to each filesystem reque
st and |
75 // is to be validated each time the request is being handled. | 76 // is to be validated each time the request is being handled. |
76 StringBuilder rootURL; | 77 StringBuilder rootURL; |
77 rootURL.append("filesystem:"); | 78 rootURL.append("filesystem:"); |
78 rootURL.append(context->securityOrigin()->toString()); | 79 rootURL.append(context->securityOrigin()->toString()); |
79 rootURL.append("/"); | 80 rootURL.append("/"); |
80 rootURL.append(isolatedPathPrefix); | 81 rootURL.append(isolatedPathPrefix); |
81 rootURL.append("/"); | 82 rootURL.append("/"); |
82 rootURL.append(filesystemId); | 83 rootURL.append(filesystemId); |
83 rootURL.append("/"); | 84 rootURL.append("/"); |
84 | 85 |
85 return DOMFileSystem::create(context, filesystemName.toString(), FileSystemT
ypeIsolated, KURL(ParsedURLString, rootURL.toString()), AsyncFileSystem::create(
)); | 86 return DOMFileSystem::create(context, filesystemName.toString(), FileSystemT
ypeIsolated, KURL(ParsedURLString, rootURL.toString())); |
86 } | 87 } |
87 | 88 |
88 DOMFileSystem::DOMFileSystem(ScriptExecutionContext* context, const String& name
, FileSystemType type, const KURL& rootURL, PassOwnPtr<AsyncFileSystem> asyncFil
eSystem) | 89 DOMFileSystem::DOMFileSystem(ScriptExecutionContext* context, const String& name
, FileSystemType type, const KURL& rootURL) |
89 : DOMFileSystemBase(context, name, type, rootURL, asyncFileSystem) | 90 : DOMFileSystemBase(context, name, type, rootURL) |
90 , ActiveDOMObject(context) | 91 , ActiveDOMObject(context) |
91 { | 92 { |
92 ScriptWrappable::init(this); | 93 ScriptWrappable::init(this); |
93 } | 94 } |
94 | 95 |
95 PassRefPtr<DirectoryEntry> DOMFileSystem::root() | 96 PassRefPtr<DirectoryEntry> DOMFileSystem::root() |
96 { | 97 { |
97 return DirectoryEntry::create(this, DOMFilePath::root); | 98 return DirectoryEntry::create(this, DOMFilePath::root); |
98 } | 99 } |
99 | 100 |
100 void DOMFileSystem::stop() | 101 void DOMFileSystem::addPendingCallbacks() |
101 { | 102 { |
102 m_asyncFileSystem->stop(); | 103 setPendingActivity(this); |
103 } | 104 } |
104 | 105 |
105 bool DOMFileSystem::hasPendingActivity() const | 106 void DOMFileSystem::removePendingCallbacks() |
106 { | 107 { |
107 return m_asyncFileSystem->hasPendingActivity(); | 108 unsetPendingActivity(this); |
108 } | |
109 | |
110 void DOMFileSystem::contextDestroyed() | |
111 { | |
112 m_asyncFileSystem->stop(); | |
113 ActiveDOMObject::contextDestroyed(); | |
114 } | 109 } |
115 | 110 |
116 namespace { | 111 namespace { |
117 | 112 |
118 class ConvertToFileWriterCallback : public FileWriterBaseCallback { | 113 class ConvertToFileWriterCallback : public FileWriterBaseCallback { |
119 public: | 114 public: |
120 static PassRefPtr<ConvertToFileWriterCallback> create(PassRefPtr<FileWriterC
allback> callback) | 115 static PassRefPtr<ConvertToFileWriterCallback> create(PassRefPtr<FileWriterC
allback> callback) |
121 { | 116 { |
122 return adoptRef(new ConvertToFileWriterCallback(callback)); | 117 return adoptRef(new ConvertToFileWriterCallback(callback)); |
123 } | 118 } |
(...skipping 11 matching lines...) Expand all Loading... |
135 }; | 130 }; |
136 | 131 |
137 } | 132 } |
138 | 133 |
139 void DOMFileSystem::createWriter(const FileEntry* fileEntry, PassRefPtr<FileWrit
erCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) | 134 void DOMFileSystem::createWriter(const FileEntry* fileEntry, PassRefPtr<FileWrit
erCallback> successCallback, PassRefPtr<ErrorCallback> errorCallback) |
140 { | 135 { |
141 ASSERT(fileEntry); | 136 ASSERT(fileEntry); |
142 | 137 |
143 RefPtr<FileWriter> fileWriter = FileWriter::create(scriptExecutionContext())
; | 138 RefPtr<FileWriter> fileWriter = FileWriter::create(scriptExecutionContext())
; |
144 RefPtr<FileWriterBaseCallback> conversionCallback = ConvertToFileWriterCallb
ack::create(successCallback); | 139 RefPtr<FileWriterBaseCallback> conversionCallback = ConvertToFileWriterCallb
ack::create(successCallback); |
145 OwnPtr<FileWriterBaseCallbacks> callbacks = FileWriterBaseCallbacks::create(
fileWriter, conversionCallback, errorCallback); | 140 OwnPtr<AsyncFileSystemCallbacks> callbacks = FileWriterBaseCallbacks::create
(fileWriter, conversionCallback, errorCallback); |
146 m_asyncFileSystem->createWriter(fileWriter.get(), createFileSystemURL(fileEn
try), callbacks.release()); | 141 fileSystem()->createFileWriter(createFileSystemURL(fileEntry), fileWriter.ge
t(), callbacks.release()); |
147 } | 142 } |
148 | 143 |
149 namespace { | 144 namespace { |
150 | 145 |
151 class SnapshotFileCallback : public FileSystemCallbacksBase { | 146 class SnapshotFileCallback : public FileSystemCallbacksBase { |
152 public: | 147 public: |
153 static PassOwnPtr<SnapshotFileCallback> create(PassRefPtr<DOMFileSystem> fil
esystem, const String& name, const KURL& url, PassRefPtr<FileCallback> successCa
llback, PassRefPtr<ErrorCallback> errorCallback) | 148 static PassOwnPtr<AsyncFileSystemCallbacks> create(PassRefPtr<DOMFileSystem>
filesystem, const String& name, const KURL& url, PassRefPtr<FileCallback> succe
ssCallback, PassRefPtr<ErrorCallback> errorCallback) |
154 { | 149 { |
155 return adoptPtr(new SnapshotFileCallback(filesystem, name, url, successC
allback, errorCallback)); | 150 return adoptPtr(static_cast<AsyncFileSystemCallbacks*>(new SnapshotFileC
allback(filesystem, name, url, successCallback, errorCallback))); |
156 } | 151 } |
157 | 152 |
158 virtual void didCreateSnapshotFile(const FileMetadata& metadata, PassRefPtr<
BlobDataHandle> snapshot) | 153 virtual void didCreateSnapshotFile(const FileMetadata& metadata, PassRefPtr<
BlobDataHandle> snapshot) |
159 { | 154 { |
160 ASSERT(!metadata.platformPath.isEmpty()); | 155 ASSERT(!metadata.platformPath.isEmpty()); |
161 if (!m_successCallback) | 156 if (!m_successCallback) |
162 return; | 157 return; |
163 | 158 |
164 // We can't directly use the snapshot blob data handle because the conte
nt type on it hasn't been set. | 159 // We can't directly use the snapshot blob data handle because the conte
nt type on it hasn't been set. |
165 // The |snapshot| param is here to provide a a chain of custody thru thr
ead bridging that is held onto until | 160 // The |snapshot| param is here to provide a a chain of custody thru thr
ead bridging that is held onto until |
166 // *after* we've coined a File with a new handle that has the correct ty
pe set on it. This allows the | 161 // *after* we've coined a File with a new handle that has the correct ty
pe set on it. This allows the |
167 // blob storage system to track when a temp file can and can't be safely
deleted. | 162 // blob storage system to track when a temp file can and can't be safely
deleted. |
168 | 163 |
169 // For regular filesystem types (temporary or persistent), we should not
cache file metadata as it could change File semantics. | 164 // For regular filesystem types (temporary or persistent), we should not
cache file metadata as it could change File semantics. |
170 // For other filesystem types (which could be platform-specific ones), t
here's a chance that the files are on remote filesystem. If the port has returne
d metadata just pass it to File constructor (so we may cache the metadata). | 165 // For other filesystem types (which could be platform-specific ones), t
here's a chance that the files are on remote filesystem. If the port has returne
d metadata just pass it to File constructor (so we may cache the metadata). |
171 // FIXME: We should use the snapshot metadata for all files. | 166 // FIXME: We should use the snapshot metadata for all files. |
172 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=17746 | 167 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=17746 |
173 if (m_filesystem->type() == FileSystemTypeTemporary || m_filesystem->typ
e() == FileSystemTypePersistent) { | 168 if (m_fileSystem->type() == FileSystemTypeTemporary || m_fileSystem->typ
e() == FileSystemTypePersistent) { |
174 m_successCallback->handleEvent(File::createWithName(metadata.platfor
mPath, m_name).get()); | 169 m_successCallback->handleEvent(File::createWithName(metadata.platfor
mPath, m_name).get()); |
175 } else if (!metadata.platformPath.isEmpty()) { | 170 } else if (!metadata.platformPath.isEmpty()) { |
176 // If the platformPath in the returned metadata is given, we create
a File object for the path. | 171 // If the platformPath in the returned metadata is given, we create
a File object for the path. |
177 m_successCallback->handleEvent(File::createForFileSystemFile(m_name,
metadata).get()); | 172 m_successCallback->handleEvent(File::createForFileSystemFile(m_name,
metadata).get()); |
178 } else { | 173 } else { |
179 // Otherwise create a File from the FileSystem URL. | 174 // Otherwise create a File from the FileSystem URL. |
180 m_successCallback->handleEvent(File::createForFileSystemFile(m_url,
metadata).get()); | 175 m_successCallback->handleEvent(File::createForFileSystemFile(m_url,
metadata).get()); |
181 } | 176 } |
182 | 177 |
183 m_successCallback.release(); | 178 m_successCallback.release(); |
184 } | 179 } |
185 | 180 |
186 private: | 181 private: |
187 SnapshotFileCallback(PassRefPtr<DOMFileSystem> filesystem, const String& nam
e, const KURL& url, PassRefPtr<FileCallback> successCallback, PassRefPtr<ErrorC
allback> errorCallback) | 182 SnapshotFileCallback(PassRefPtr<DOMFileSystem> filesystem, const String& nam
e, const KURL& url, PassRefPtr<FileCallback> successCallback, PassRefPtr<ErrorC
allback> errorCallback) |
188 : FileSystemCallbacksBase(errorCallback) | 183 : FileSystemCallbacksBase(errorCallback, filesystem.get()) |
189 , m_filesystem(filesystem) | |
190 , m_name(name) | 184 , m_name(name) |
191 , m_url(url) | 185 , m_url(url) |
192 , m_successCallback(successCallback) | 186 , m_successCallback(successCallback) |
193 { | 187 { |
194 } | 188 } |
195 | 189 |
196 RefPtr<DOMFileSystem> m_filesystem; | |
197 String m_name; | 190 String m_name; |
198 KURL m_url; | 191 KURL m_url; |
199 RefPtr<FileCallback> m_successCallback; | 192 RefPtr<FileCallback> m_successCallback; |
200 }; | 193 }; |
201 | 194 |
202 } // namespace | 195 } // namespace |
203 | 196 |
204 void DOMFileSystem::createFile(const FileEntry* fileEntry, PassRefPtr<FileCallba
ck> successCallback, PassRefPtr<ErrorCallback> errorCallback) | 197 void DOMFileSystem::createFile(const FileEntry* fileEntry, PassRefPtr<FileCallba
ck> successCallback, PassRefPtr<ErrorCallback> errorCallback) |
205 { | 198 { |
206 KURL fileSystemURL = createFileSystemURL(fileEntry); | 199 KURL fileSystemURL = createFileSystemURL(fileEntry); |
207 m_asyncFileSystem->createSnapshotFileAndReadMetadata(fileSystemURL, Snapshot
FileCallback::create(this, fileEntry->name(), fileSystemURL, successCallback, er
rorCallback)); | 200 fileSystem()->createSnapshotFileAndReadMetadata(fileSystemURL, SnapshotFileC
allback::create(this, fileEntry->name(), fileSystemURL, successCallback, errorCa
llback)); |
208 } | 201 } |
209 | 202 |
210 } // namespace WebCore | 203 } // namespace WebCore |
OLD | NEW |