Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: third_party/WebKit/Source/modules/filesystem/DOMFileSystem.cpp

Issue 2297043002: Web expose FileSystemFileEntry, FileSystemDirectoryEntry and friends (Closed)
Patch Set: Rebased Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "modules/filesystem/DOMFileSystem.h"
32
33 #include "core/fileapi/BlobCallback.h"
34 #include "modules/filesystem/DOMFilePath.h"
35 #include "modules/filesystem/DirectoryEntry.h"
36 #include "modules/filesystem/FileEntry.h"
37 #include "modules/filesystem/FileSystemCallbacks.h"
38 #include "modules/filesystem/FileWriter.h"
39 #include "modules/filesystem/FileWriterBaseCallback.h"
40 #include "modules/filesystem/FileWriterCallback.h"
41 #include "modules/filesystem/MetadataCallback.h"
42 #include "platform/FileMetadata.h"
43 #include "platform/weborigin/SecurityOrigin.h"
44 #include "public/platform/Platform.h"
45 #include "public/platform/WebFileSystem.h"
46 #include "public/platform/WebFileSystemCallbacks.h"
47 #include "public/platform/WebSecurityOrigin.h"
48 #include "wtf/text/StringBuilder.h"
49 #include "wtf/text/WTFString.h"
50 #include <memory>
51
52 namespace blink {
53
54 // static
55 DOMFileSystem* DOMFileSystem::create(ExecutionContext* context,
56 const String& name,
57 FileSystemType type,
58 const KURL& rootURL) {
59 DOMFileSystem* fileSystem(new DOMFileSystem(context, name, type, rootURL));
60 fileSystem->suspendIfNeeded();
61 return fileSystem;
62 }
63
64 DOMFileSystem* DOMFileSystem::createIsolatedFileSystem(
65 ExecutionContext* context,
66 const String& filesystemId) {
67 if (filesystemId.isEmpty())
68 return 0;
69
70 StringBuilder filesystemName;
71 filesystemName.append(Platform::current()->fileSystemCreateOriginIdentifier(
72 WebSecurityOrigin(context->getSecurityOrigin())));
73 filesystemName.append(":Isolated_");
74 filesystemName.append(filesystemId);
75
76 // The rootURL created here is going to be attached to each filesystem request
77 // and is to be validated each time the request is being handled.
78 StringBuilder rootURL;
79 rootURL.append("filesystem:");
80 rootURL.append(context->getSecurityOrigin()->toString());
81 rootURL.append('/');
82 rootURL.append(isolatedPathPrefix);
83 rootURL.append('/');
84 rootURL.append(filesystemId);
85 rootURL.append('/');
86
87 return DOMFileSystem::create(context, filesystemName.toString(),
88 FileSystemTypeIsolated,
89 KURL(ParsedURLString, rootURL.toString()));
90 }
91
92 DOMFileSystem::DOMFileSystem(ExecutionContext* context,
93 const String& name,
94 FileSystemType type,
95 const KURL& rootURL)
96 : DOMFileSystemBase(context, name, type, rootURL),
97 ActiveScriptWrappable(this),
98 ActiveDOMObject(context),
99 m_numberOfPendingCallbacks(0),
100 m_rootEntry(DirectoryEntry::create(this, DOMFilePath::root)) {}
101
102 DirectoryEntry* DOMFileSystem::root() const {
103 return m_rootEntry.get();
104 }
105
106 void DOMFileSystem::addPendingCallbacks() {
107 ++m_numberOfPendingCallbacks;
108 }
109
110 void DOMFileSystem::removePendingCallbacks() {
111 ASSERT(m_numberOfPendingCallbacks > 0);
112 --m_numberOfPendingCallbacks;
113 }
114
115 bool DOMFileSystem::hasPendingActivity() const {
116 ASSERT(m_numberOfPendingCallbacks >= 0);
117 return m_numberOfPendingCallbacks;
118 }
119
120 void DOMFileSystem::reportError(ErrorCallbackBase* errorCallback,
121 FileError::ErrorCode fileError) {
122 reportError(getExecutionContext(), errorCallback, fileError);
123 }
124
125 void DOMFileSystem::reportError(ExecutionContext* executionContext,
126 ErrorCallbackBase* errorCallback,
127 FileError::ErrorCode fileError) {
128 if (errorCallback)
129 scheduleCallback(
130 executionContext,
131 createSameThreadTask(&ErrorCallbackBase::invoke,
132 wrapPersistent(errorCallback), fileError));
133 }
134
135 namespace {
136
137 class ConvertToFileWriterCallback : public FileWriterBaseCallback {
138 public:
139 static ConvertToFileWriterCallback* create(FileWriterCallback* callback) {
140 return new ConvertToFileWriterCallback(callback);
141 }
142
143 DEFINE_INLINE_TRACE() {
144 visitor->trace(m_callback);
145 FileWriterBaseCallback::trace(visitor);
146 }
147
148 void handleEvent(FileWriterBase* fileWriterBase) {
149 m_callback->handleEvent(static_cast<FileWriter*>(fileWriterBase));
150 }
151
152 private:
153 explicit ConvertToFileWriterCallback(FileWriterCallback* callback)
154 : m_callback(callback) {}
155 Member<FileWriterCallback> m_callback;
156 };
157
158 } // namespace
159
160 void DOMFileSystem::createWriter(const FileEntry* fileEntry,
161 FileWriterCallback* successCallback,
162 ErrorCallbackBase* errorCallback) {
163 ASSERT(fileEntry);
164
165 if (!fileSystem()) {
166 reportError(errorCallback, FileError::kAbortErr);
167 return;
168 }
169
170 FileWriter* fileWriter = FileWriter::create(getExecutionContext());
171 FileWriterBaseCallback* conversionCallback =
172 ConvertToFileWriterCallback::create(successCallback);
173 std::unique_ptr<AsyncFileSystemCallbacks> callbacks =
174 FileWriterBaseCallbacks::create(fileWriter, conversionCallback,
175 errorCallback, m_context);
176 fileSystem()->createFileWriter(createFileSystemURL(fileEntry), fileWriter,
177 std::move(callbacks));
178 }
179
180 void DOMFileSystem::createFile(const FileEntry* fileEntry,
181 BlobCallback* successCallback,
182 ErrorCallbackBase* errorCallback) {
183 KURL fileSystemURL = createFileSystemURL(fileEntry);
184 if (!fileSystem()) {
185 reportError(errorCallback, FileError::kAbortErr);
186 return;
187 }
188
189 fileSystem()->createSnapshotFileAndReadMetadata(
190 fileSystemURL,
191 SnapshotFileCallback::create(this, fileEntry->name(), fileSystemURL,
192 successCallback, errorCallback, m_context));
193 }
194
195 DEFINE_TRACE(DOMFileSystem) {
196 DOMFileSystemBase::trace(visitor);
197 ActiveDOMObject::trace(visitor);
198 visitor->trace(m_rootEntry);
199 }
200
201 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698