Make FileWriter writes from current position and change failure condition
[framework/web/webkit-efl.git] / Source / WebCore / platform / efl / tizen / AsyncFileWriterTizen.cpp
1 /*
2  * Copyright (C) 2011 Samsung Electronics
3  *
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.
8  *
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.
13  *
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.
18  *
19  */
20
21 #include "config.h"
22 #include "AsyncFileWriterTizen.h"
23
24 #include "NotImplemented.h"
25
26 #if ENABLE(TIZEN_FILE_SYSTEM)
27
28 #include "AsyncFileSystemTizen.h"
29 #include "AsyncFileWriterClientTizen.h"
30 #include "Blob.h"
31 #include "BlobRegistryImpl.h"
32 #include "CrossThreadTask.h"
33 #include "FileSystem.h"
34 #include "GOwnPtr.h"
35 #include <wtf/text/CString.h>
36
37 namespace WebCore {
38
39 AsyncFileWriterTizen::AsyncFileWriterTizen(AsyncFileWriterClient* client, const String& path, AsyncFileSystemTaskControllerTizen* taskController)
40     : m_client(client)
41     , m_path(path)
42     , m_taskController(taskController)
43 {
44 }
45
46 static void writeAsync(ScriptExecutionContext* context, PassOwnPtr<AsyncFileWriterClientTizen> helperClient, const String& path, long long position, Blob* data)
47 {
48     int bytesWritten;
49     PlatformFileHandle handle;
50     handle = openFile(path, OpenForWrite);
51
52     if (!isHandleValid(handle)) {
53         helperClient->didFail(FileError::NOT_FOUND_ERR);
54         return;
55     }
56
57     int errorCode = 0;
58     if (!AsyncFileSystemTizen::checkQuota(context, path, errorCode)) {
59         closeFile(handle);
60         helperClient->didFail(static_cast<FileError::ErrorCode>(errorCode));
61         return;
62     }
63
64     seekFile(handle, position, SeekFromBeginning);
65     RefPtr<BlobStorageData> blobStorage = static_cast<BlobRegistryImpl&>(blobRegistry()).getBlobDataFromURL(data->url());
66     if (blobStorage) {
67         for (size_t i = 0; i < blobStorage->items().size(); i++) {
68             const BlobDataItem& blobItem = blobStorage->items()[i];
69             if (blobItem.type == BlobDataItem::Data) {
70                 bytesWritten = 0;
71                 bytesWritten = writeToFile(handle, blobItem.data->data(), blobItem.data->length());
72
73                 if (bytesWritten != blobItem.data->length()) {
74                     helperClient->didFail(FileError::SECURITY_ERR);
75                 else
76                     helperClient->didWrite(bytesWritten, (i + 1 == blobStorage->items().size()) ? true : false);
77             } else if (blobItem.type == BlobDataItem::File) {
78                 closeFile(handle);
79
80                 if (copyFile(blobItem.path, path))
81                     helperClient->didWrite(blobItem.length, (i + 1 == blobStorage->items().size()) ? true : false);
82                 else
83                     helperClient->didFail(FileError::SECURITY_ERR);
84             } else if (blobItem.type == BlobDataItem::Blob) {
85                 LOG_ERROR("[writeAsync] data item is blob");
86                 notImplemented();
87             }
88         }
89     }
90
91     closeFile(handle);
92 }
93
94 void AsyncFileWriterTizen::write(long long position, Blob* data)
95 {
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)));
98 }
99
100 static void truncateAsync(ScriptExecutionContext*, PassOwnPtr<AsyncFileWriterClientTizen> helperClient, const String& path, long long length)
101 {
102     if (!fileExists(path)) {
103         helperClient->didFail(FileError::NOT_FOUND_ERR);
104         return;
105     }
106
107     PlatformFileHandle handle;
108     handle = openFile(path, OpenForWrite);
109
110     if (!isHandleValid(handle))
111         return;
112
113     if (truncateFile(handle, length))
114         helperClient->didTruncate();
115     else
116         helperClient->didFail(FileError::SECURITY_ERR);
117
118     closeFile(handle);
119 }
120
121 void AsyncFileWriterTizen::truncate(long long length)
122 {
123     String mode = m_taskController->uniqueMode();
124     m_taskController->postTaskToMainThread(createCallbackTask(&truncateAsync, AsyncFileWriterClientTizen::create(m_taskController.get(), m_client, mode), m_path, length));
125 }
126
127 void AsyncFileWriterTizen::abort()
128 {
129 }
130
131 bool AsyncFileWriterTizen::waitForOperationToComplete()
132 {
133     if (m_taskController->synchronousType() == SynchronousFileSystem)
134         return m_taskController->waitForTaskToComplete();
135
136     return false;
137 }
138
139 } // namespace WebCore
140
141 #endif // ENABLE(FILE_SYSTEM)
142