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 RefPtr<BlobStorageData> blobStorage = static_cast<BlobRegistryImpl&>(blobRegistry()).getBlobDataFromURL(data->url());
66 for (size_t i = 0; i < blobStorage->items().size(); i++) {
67 const BlobDataItem& blobItem = blobStorage->items()[i];
68 if (blobItem.type == BlobDataItem::Data) {
70 bytesWritten = writeToFile(handle, blobItem.data->data(), blobItem.data->length());
73 helperClient->didFail(FileError::SECURITY_ERR);
75 helperClient->didWrite(bytesWritten, (i + 1 == blobStorage->items().size()) ? true : false);
76 } else if (blobItem.type == BlobDataItem::File) {
79 if (copyFile(blobItem.path, path))
80 helperClient->didWrite(blobItem.length, (i + 1 == blobStorage->items().size()) ? true : false);
82 helperClient->didFail(FileError::SECURITY_ERR);
83 } else if (blobItem.type == BlobDataItem::Blob) {
84 LOG_ERROR("[writeAsync] data item is blob");
93 void AsyncFileWriterTizen::write(long long position, Blob* data)
95 String mode = m_taskController->uniqueMode();
96 m_taskController->postTaskToMainThread(createCallbackTask(&writeAsync, AsyncFileWriterClientTizen::create(m_taskController.get(), m_client, mode), m_path, position, AllowCrossThreadAccess(data)));
99 static void truncateAsync(ScriptExecutionContext*, PassOwnPtr<AsyncFileWriterClientTizen> helperClient, const String& path, long long length)
101 if (!fileExists(path)) {
102 helperClient->didFail(FileError::NOT_FOUND_ERR);
106 PlatformFileHandle handle;
107 handle = openFile(path, OpenForWrite);
109 if (!isHandleValid(handle))
112 if (truncateFile(handle, length))
113 helperClient->didTruncate();
115 helperClient->didFail(FileError::SECURITY_ERR);
120 void AsyncFileWriterTizen::truncate(long long length)
122 String mode = m_taskController->uniqueMode();
123 m_taskController->postTaskToMainThread(createCallbackTask(&truncateAsync, AsyncFileWriterClientTizen::create(m_taskController.get(), m_client, mode), m_path, length));
126 void AsyncFileWriterTizen::abort()
130 bool AsyncFileWriterTizen::waitForOperationToComplete()
132 if (m_taskController->synchronousType() == SynchronousFileSystem)
133 return m_taskController->waitForTaskToComplete();
138 } // namespace WebCore
140 #endif // ENABLE(FILE_SYSTEM)