2 * Copyright (C) 2011 Samsung Electronics
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
22 #include "AsyncFileWriterTizen.h"
24 #include "NotImplemented.h"
26 #if ENABLE(TIZEN_FILE_SYSTEM)
28 #include "AsyncFileSystemTizen.h"
29 #include "AsyncFileWriterClientTizen.h"
31 #include "BlobRegistryImpl.h"
32 #include "CrossThreadTask.h"
33 #include "FileSystem.h"
35 #include <wtf/text/CString.h>
39 AsyncFileWriterTizen::AsyncFileWriterTizen(AsyncFileWriterClient* client, const String& path, AsyncFileSystemTaskControllerTizen* taskController)
42 , m_taskController(taskController)
46 static void writeAsync(ScriptExecutionContext* context, PassOwnPtr<AsyncFileWriterClientTizen> helperClient, const String& path, long long position, Blob* data)
49 PlatformFileHandle handle;
50 handle = openFile(path, OpenForWrite);
52 if (!isHandleValid(handle)) {
53 helperClient->didFail(FileError::NOT_FOUND_ERR);
58 if (!AsyncFileSystemTizen::checkQuota(context, path, errorCode)) {
60 helperClient->didFail(static_cast<FileError::ErrorCode>(errorCode));
64 seekFile(handle, position, SeekFromBeginning);
65 RefPtr<BlobStorageData> blobStorage = static_cast<BlobRegistryImpl&>(blobRegistry()).getBlobDataFromURL(data->url());
67 for (size_t i = 0; i < blobStorage->items().size(); i++) {
68 const BlobDataItem& blobItem = blobStorage->items()[i];
69 if (blobItem.type == BlobDataItem::Data) {
71 bytesWritten = writeToFile(handle, blobItem.data->data(), blobItem.data->length());
73 if (bytesWritten != blobItem.data->length()) {
74 helperClient->didFail(FileError::SECURITY_ERR);
76 helperClient->didWrite(bytesWritten, (i + 1 == blobStorage->items().size()) ? true : false);
77 } else if (blobItem.type == BlobDataItem::File) {
80 if (copyFile(blobItem.path, path))
81 helperClient->didWrite(blobItem.length, (i + 1 == blobStorage->items().size()) ? true : false);
83 helperClient->didFail(FileError::SECURITY_ERR);
84 } else if (blobItem.type == BlobDataItem::Blob) {
85 LOG_ERROR("[writeAsync] data item is blob");
94 void AsyncFileWriterTizen::write(long long position, Blob* data)
96 String mode = m_taskController->uniqueMode();
97 m_taskController->postTaskToMainThread(createCallbackTask(&writeAsync, AsyncFileWriterClientTizen::create(m_taskController.get(), m_client, mode), m_path, position, AllowCrossThreadAccess(data)));
100 static void truncateAsync(ScriptExecutionContext*, PassOwnPtr<AsyncFileWriterClientTizen> helperClient, const String& path, long long length)
102 if (!fileExists(path)) {
103 helperClient->didFail(FileError::NOT_FOUND_ERR);
107 PlatformFileHandle handle;
108 handle = openFile(path, OpenForWrite);
110 if (!isHandleValid(handle))
113 if (truncateFile(handle, length))
114 helperClient->didTruncate();
116 helperClient->didFail(FileError::SECURITY_ERR);
121 void AsyncFileWriterTizen::truncate(long long length)
123 String mode = m_taskController->uniqueMode();
124 m_taskController->postTaskToMainThread(createCallbackTask(&truncateAsync, AsyncFileWriterClientTizen::create(m_taskController.get(), m_client, mode), m_path, length));
127 void AsyncFileWriterTizen::abort()
131 bool AsyncFileWriterTizen::waitForOperationToComplete()
133 if (m_taskController->synchronousType() == SynchronousFileSystem)
134 return m_taskController->waitForTaskToComplete();
139 } // namespace WebCore
141 #endif // ENABLE(FILE_SYSTEM)