| OLD | NEW |
| (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/DOMFileSystemSync.h" | |
| 32 | |
| 33 #include "bindings/core/v8/ExceptionState.h" | |
| 34 #include "core/fileapi/File.h" | |
| 35 #include "core/fileapi/FileError.h" | |
| 36 #include "modules/filesystem/DOMFilePath.h" | |
| 37 #include "modules/filesystem/DirectoryEntrySync.h" | |
| 38 #include "modules/filesystem/ErrorCallback.h" | |
| 39 #include "modules/filesystem/FileEntrySync.h" | |
| 40 #include "modules/filesystem/FileSystemCallbacks.h" | |
| 41 #include "modules/filesystem/FileWriterBaseCallback.h" | |
| 42 #include "modules/filesystem/FileWriterSync.h" | |
| 43 #include "platform/FileMetadata.h" | |
| 44 #include "public/platform/WebFileSystem.h" | |
| 45 #include "public/platform/WebFileSystemCallbacks.h" | |
| 46 #include "wtf/PtrUtil.h" | |
| 47 #include <memory> | |
| 48 | |
| 49 namespace blink { | |
| 50 | |
| 51 class FileWriterBase; | |
| 52 | |
| 53 DOMFileSystemSync* DOMFileSystemSync::create(DOMFileSystemBase* fileSystem) { | |
| 54 return new DOMFileSystemSync(fileSystem->m_context, fileSystem->name(), | |
| 55 fileSystem->type(), fileSystem->rootURL()); | |
| 56 } | |
| 57 | |
| 58 DOMFileSystemSync::DOMFileSystemSync(ExecutionContext* context, | |
| 59 const String& name, | |
| 60 FileSystemType type, | |
| 61 const KURL& rootURL) | |
| 62 : DOMFileSystemBase(context, name, type, rootURL), | |
| 63 m_rootEntry(DirectoryEntrySync::create(this, DOMFilePath::root)) {} | |
| 64 | |
| 65 DOMFileSystemSync::~DOMFileSystemSync() {} | |
| 66 | |
| 67 void DOMFileSystemSync::reportError(ErrorCallbackBase* errorCallback, | |
| 68 FileError::ErrorCode fileError) { | |
| 69 errorCallback->invoke(fileError); | |
| 70 } | |
| 71 | |
| 72 DirectoryEntrySync* DOMFileSystemSync::root() { | |
| 73 return m_rootEntry.get(); | |
| 74 } | |
| 75 | |
| 76 namespace { | |
| 77 | |
| 78 class CreateFileHelper final : public AsyncFileSystemCallbacks { | |
| 79 public: | |
| 80 class CreateFileResult : public GarbageCollected<CreateFileResult> { | |
| 81 public: | |
| 82 static CreateFileResult* create() { return new CreateFileResult(); } | |
| 83 | |
| 84 bool m_failed; | |
| 85 int m_code; | |
| 86 Member<File> m_file; | |
| 87 | |
| 88 DEFINE_INLINE_TRACE() { visitor->trace(m_file); } | |
| 89 | |
| 90 private: | |
| 91 CreateFileResult() : m_failed(false), m_code(0) {} | |
| 92 }; | |
| 93 | |
| 94 static std::unique_ptr<AsyncFileSystemCallbacks> create( | |
| 95 CreateFileResult* result, | |
| 96 const String& name, | |
| 97 const KURL& url, | |
| 98 FileSystemType type) { | |
| 99 return wrapUnique(static_cast<AsyncFileSystemCallbacks*>( | |
| 100 new CreateFileHelper(result, name, url, type))); | |
| 101 } | |
| 102 | |
| 103 void didFail(int code) override { | |
| 104 m_result->m_failed = true; | |
| 105 m_result->m_code = code; | |
| 106 } | |
| 107 | |
| 108 ~CreateFileHelper() override {} | |
| 109 | |
| 110 void didCreateSnapshotFile(const FileMetadata& metadata, | |
| 111 PassRefPtr<BlobDataHandle> snapshot) override { | |
| 112 // We can't directly use the snapshot blob data handle because the content | |
| 113 // type on it hasn't been set. The |snapshot| param is here to provide a a | |
| 114 // chain of custody thru thread bridging that is held onto until *after* | |
| 115 // we've coined a File with a new handle that has the correct type set on | |
| 116 // it. This allows the blob storage system to track when a temp file can and | |
| 117 // can't be safely deleted. | |
| 118 | |
| 119 m_result->m_file = | |
| 120 DOMFileSystemBase::createFile(metadata, m_url, m_type, m_name); | |
| 121 } | |
| 122 | |
| 123 bool shouldBlockUntilCompletion() const override { return true; } | |
| 124 | |
| 125 private: | |
| 126 CreateFileHelper(CreateFileResult* result, | |
| 127 const String& name, | |
| 128 const KURL& url, | |
| 129 FileSystemType type) | |
| 130 : m_result(result), m_name(name), m_url(url), m_type(type) {} | |
| 131 | |
| 132 Persistent<CreateFileResult> m_result; | |
| 133 String m_name; | |
| 134 KURL m_url; | |
| 135 FileSystemType m_type; | |
| 136 }; | |
| 137 | |
| 138 } // namespace | |
| 139 | |
| 140 File* DOMFileSystemSync::createFile(const FileEntrySync* fileEntry, | |
| 141 ExceptionState& exceptionState) { | |
| 142 KURL fileSystemURL = createFileSystemURL(fileEntry); | |
| 143 CreateFileHelper::CreateFileResult* result( | |
| 144 CreateFileHelper::CreateFileResult::create()); | |
| 145 fileSystem()->createSnapshotFileAndReadMetadata( | |
| 146 fileSystemURL, CreateFileHelper::create(result, fileEntry->name(), | |
| 147 fileSystemURL, type())); | |
| 148 if (result->m_failed) { | |
| 149 exceptionState.throwDOMException( | |
| 150 result->m_code, "Could not create '" + fileEntry->name() + "'."); | |
| 151 return nullptr; | |
| 152 } | |
| 153 return result->m_file.get(); | |
| 154 } | |
| 155 | |
| 156 namespace { | |
| 157 | |
| 158 class ReceiveFileWriterCallback final : public FileWriterBaseCallback { | |
| 159 public: | |
| 160 static ReceiveFileWriterCallback* create() { | |
| 161 return new ReceiveFileWriterCallback(); | |
| 162 } | |
| 163 | |
| 164 void handleEvent(FileWriterBase*) override {} | |
| 165 | |
| 166 private: | |
| 167 ReceiveFileWriterCallback() {} | |
| 168 }; | |
| 169 | |
| 170 class LocalErrorCallback final : public ErrorCallbackBase { | |
| 171 public: | |
| 172 static LocalErrorCallback* create(FileError::ErrorCode& errorCode) { | |
| 173 return new LocalErrorCallback(errorCode); | |
| 174 } | |
| 175 | |
| 176 void invoke(FileError::ErrorCode error) override { | |
| 177 DCHECK_NE(error, FileError::kOK); | |
| 178 m_errorCode = error; | |
| 179 } | |
| 180 | |
| 181 private: | |
| 182 explicit LocalErrorCallback(FileError::ErrorCode& errorCode) | |
| 183 : m_errorCode(errorCode) {} | |
| 184 | |
| 185 FileError::ErrorCode& m_errorCode; | |
| 186 }; | |
| 187 | |
| 188 } // namespace | |
| 189 | |
| 190 FileWriterSync* DOMFileSystemSync::createWriter( | |
| 191 const FileEntrySync* fileEntry, | |
| 192 ExceptionState& exceptionState) { | |
| 193 ASSERT(fileEntry); | |
| 194 | |
| 195 FileWriterSync* fileWriter = FileWriterSync::create(); | |
| 196 ReceiveFileWriterCallback* successCallback = | |
| 197 ReceiveFileWriterCallback::create(); | |
| 198 FileError::ErrorCode errorCode = FileError::kOK; | |
| 199 LocalErrorCallback* errorCallback = LocalErrorCallback::create(errorCode); | |
| 200 | |
| 201 std::unique_ptr<AsyncFileSystemCallbacks> callbacks = | |
| 202 FileWriterBaseCallbacks::create(fileWriter, successCallback, | |
| 203 errorCallback, m_context); | |
| 204 callbacks->setShouldBlockUntilCompletion(true); | |
| 205 | |
| 206 fileSystem()->createFileWriter(createFileSystemURL(fileEntry), fileWriter, | |
| 207 std::move(callbacks)); | |
| 208 if (errorCode != FileError::kOK) { | |
| 209 FileError::throwDOMException(exceptionState, errorCode); | |
| 210 return 0; | |
| 211 } | |
| 212 return fileWriter; | |
| 213 } | |
| 214 | |
| 215 DEFINE_TRACE(DOMFileSystemSync) { | |
| 216 DOMFileSystemBase::trace(visitor); | |
| 217 visitor->trace(m_rootEntry); | |
| 218 } | |
| 219 | |
| 220 } // namespace blink | |
| OLD | NEW |