• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/DOMFileSystem.h"
33 
34 #include "core/fileapi/File.h"
35 #include "modules/filesystem/DOMFilePath.h"
36 #include "modules/filesystem/DirectoryEntry.h"
37 #include "modules/filesystem/ErrorCallback.h"
38 #include "modules/filesystem/FileCallback.h"
39 #include "modules/filesystem/FileEntry.h"
40 #include "modules/filesystem/FileSystemCallbacks.h"
41 #include "modules/filesystem/FileWriter.h"
42 #include "modules/filesystem/FileWriterBaseCallback.h"
43 #include "modules/filesystem/FileWriterCallback.h"
44 #include "modules/filesystem/MetadataCallback.h"
45 #include "platform/FileMetadata.h"
46 #include "platform/weborigin/DatabaseIdentifier.h"
47 #include "platform/weborigin/SecurityOrigin.h"
48 #include "public/platform/WebFileSystem.h"
49 #include "public/platform/WebFileSystemCallbacks.h"
50 #include "wtf/OwnPtr.h"
51 #include "wtf/text/StringBuilder.h"
52 #include "wtf/text/WTFString.h"
53 
54 namespace WebCore {
55 
56 // static
create(ExecutionContext * context,const String & name,FileSystemType type,const KURL & rootURL)57 DOMFileSystem* DOMFileSystem::create(ExecutionContext* context, const String& name, FileSystemType type, const KURL& rootURL)
58 {
59     DOMFileSystem* fileSystem(new DOMFileSystem(context, name, type, rootURL));
60     fileSystem->suspendIfNeeded();
61     return fileSystem;
62 }
63 
createIsolatedFileSystem(ExecutionContext * context,const String & filesystemId)64 DOMFileSystem* DOMFileSystem::createIsolatedFileSystem(ExecutionContext* context, const String& filesystemId)
65 {
66     if (filesystemId.isEmpty())
67         return 0;
68 
69     StringBuilder filesystemName;
70     filesystemName.append(createDatabaseIdentifierFromSecurityOrigin(context->securityOrigin()));
71     filesystemName.append(":Isolated_");
72     filesystemName.append(filesystemId);
73 
74     // The rootURL created here is going to be attached to each filesystem request and
75     // is to be validated each time the request is being handled.
76     StringBuilder rootURL;
77     rootURL.append("filesystem:");
78     rootURL.append(context->securityOrigin()->toString());
79     rootURL.append("/");
80     rootURL.append(isolatedPathPrefix);
81     rootURL.append("/");
82     rootURL.append(filesystemId);
83     rootURL.append("/");
84 
85     return DOMFileSystem::create(context, filesystemName.toString(), FileSystemTypeIsolated, KURL(ParsedURLString, rootURL.toString()));
86 }
87 
DOMFileSystem(ExecutionContext * context,const String & name,FileSystemType type,const KURL & rootURL)88 DOMFileSystem::DOMFileSystem(ExecutionContext* context, const String& name, FileSystemType type, const KURL& rootURL)
89     : DOMFileSystemBase(context, name, type, rootURL)
90     , ActiveDOMObject(context)
91     , m_numberOfPendingCallbacks(0)
92 {
93     ScriptWrappable::init(this);
94 }
95 
root()96 DirectoryEntry* DOMFileSystem::root()
97 {
98     return DirectoryEntry::create(this, DOMFilePath::root);
99 }
100 
addPendingCallbacks()101 void DOMFileSystem::addPendingCallbacks()
102 {
103     ++m_numberOfPendingCallbacks;
104 }
105 
removePendingCallbacks()106 void DOMFileSystem::removePendingCallbacks()
107 {
108     ASSERT(m_numberOfPendingCallbacks > 0);
109     --m_numberOfPendingCallbacks;
110 }
111 
hasPendingActivity() const112 bool DOMFileSystem::hasPendingActivity() const
113 {
114     ASSERT(m_numberOfPendingCallbacks >= 0);
115     return m_numberOfPendingCallbacks;
116 }
117 
reportError(PassOwnPtr<ErrorCallback> errorCallback,PassRefPtrWillBeRawPtr<FileError> fileError)118 void DOMFileSystem::reportError(PassOwnPtr<ErrorCallback> errorCallback, PassRefPtrWillBeRawPtr<FileError> fileError)
119 {
120     scheduleCallback(errorCallback, fileError);
121 }
122 
123 namespace {
124 
125 class ConvertToFileWriterCallback : public FileWriterBaseCallback {
126 public:
create(PassOwnPtr<FileWriterCallback> callback)127     static PassOwnPtr<ConvertToFileWriterCallback> create(PassOwnPtr<FileWriterCallback> callback)
128     {
129         return adoptPtr(new ConvertToFileWriterCallback(callback));
130     }
131 
handleEvent(FileWriterBase * fileWriterBase)132     void handleEvent(FileWriterBase* fileWriterBase)
133     {
134         m_callback->handleEvent(static_cast<FileWriter*>(fileWriterBase));
135     }
136 private:
ConvertToFileWriterCallback(PassOwnPtr<FileWriterCallback> callback)137     ConvertToFileWriterCallback(PassOwnPtr<FileWriterCallback> callback)
138         : m_callback(callback)
139     {
140     }
141     OwnPtr<FileWriterCallback> m_callback;
142 };
143 
144 }
145 
createWriter(const FileEntry * fileEntry,PassOwnPtr<FileWriterCallback> successCallback,PassOwnPtr<ErrorCallback> errorCallback)146 void DOMFileSystem::createWriter(const FileEntry* fileEntry, PassOwnPtr<FileWriterCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback)
147 {
148     ASSERT(fileEntry);
149 
150     FileWriter* fileWriter = FileWriter::create(executionContext());
151     OwnPtr<FileWriterBaseCallback> conversionCallback = ConvertToFileWriterCallback::create(successCallback);
152     OwnPtr<AsyncFileSystemCallbacks> callbacks = FileWriterBaseCallbacks::create(fileWriter, conversionCallback.release(), errorCallback, m_context);
153     fileSystem()->createFileWriter(createFileSystemURL(fileEntry), fileWriter, callbacks.release());
154 }
155 
156 namespace {
157 
158 class SnapshotFileCallback : public FileSystemCallbacksBase {
159 public:
create(DOMFileSystem * filesystem,const String & name,const KURL & url,PassOwnPtr<FileCallback> successCallback,PassOwnPtr<ErrorCallback> errorCallback,ExecutionContext * context)160     static PassOwnPtr<AsyncFileSystemCallbacks> create(DOMFileSystem* filesystem, const String& name, const KURL& url, PassOwnPtr<FileCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, ExecutionContext* context)
161     {
162         return adoptPtr(static_cast<AsyncFileSystemCallbacks*>(new SnapshotFileCallback(filesystem, name, url, successCallback, errorCallback, context)));
163     }
164 
didCreateSnapshotFile(const FileMetadata & metadata,PassRefPtr<BlobDataHandle> snapshot)165     virtual void didCreateSnapshotFile(const FileMetadata& metadata, PassRefPtr<BlobDataHandle> snapshot)
166     {
167         if (!m_successCallback)
168             return;
169 
170         // We can't directly use the snapshot blob data handle because the content type on it hasn't been set.
171         // The |snapshot| param is here to provide a a chain of custody thru thread bridging that is held onto until
172         // *after* we've coined a File with a new handle that has the correct type set on it. This allows the
173         // blob storage system to track when a temp file can and can't be safely deleted.
174 
175         // For regular filesystem types (temporary or persistent), we should not cache file metadata as it could change File semantics.
176         // For other filesystem types (which could be platform-specific ones), there's a chance that the files are on remote filesystem. If the port has returned metadata just pass it to File constructor (so we may cache the metadata).
177         // FIXME: We should use the snapshot metadata for all files.
178         // https://www.w3.org/Bugs/Public/show_bug.cgi?id=17746
179         if (m_fileSystem->type() == FileSystemTypeTemporary || m_fileSystem->type() == FileSystemTypePersistent) {
180             m_successCallback->handleEvent(File::createWithName(metadata.platformPath, m_name).get());
181         } else if (!metadata.platformPath.isEmpty()) {
182             // If the platformPath in the returned metadata is given, we create a File object for the path.
183             m_successCallback->handleEvent(File::createForFileSystemFile(m_name, metadata).get());
184         } else {
185             // Otherwise create a File from the FileSystem URL.
186             m_successCallback->handleEvent(File::createForFileSystemFile(m_url, metadata).get());
187         }
188 
189         m_successCallback.release();
190     }
191 
192 private:
SnapshotFileCallback(DOMFileSystem * filesystem,const String & name,const KURL & url,PassOwnPtr<FileCallback> successCallback,PassOwnPtr<ErrorCallback> errorCallback,ExecutionContext * context)193     SnapshotFileCallback(DOMFileSystem* filesystem, const String& name, const KURL& url, PassOwnPtr<FileCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback, ExecutionContext* context)
194         : FileSystemCallbacksBase(errorCallback, filesystem, context)
195         , m_name(name)
196         , m_url(url)
197         , m_successCallback(successCallback)
198     {
199     }
200 
201     String m_name;
202     KURL m_url;
203     OwnPtr<FileCallback> m_successCallback;
204 };
205 
206 } // namespace
207 
createFile(const FileEntry * fileEntry,PassOwnPtr<FileCallback> successCallback,PassOwnPtr<ErrorCallback> errorCallback)208 void DOMFileSystem::createFile(const FileEntry* fileEntry, PassOwnPtr<FileCallback> successCallback, PassOwnPtr<ErrorCallback> errorCallback)
209 {
210     KURL fileSystemURL = createFileSystemURL(fileEntry);
211     fileSystem()->createSnapshotFileAndReadMetadata(fileSystemURL, SnapshotFileCallback::create(this, fileEntry->name(), fileSystemURL, successCallback, errorCallback, m_context));
212 }
213 
214 } // namespace WebCore
215