2925e1fed12e5bfd1f697d0693653a97c902fd1a
[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     RefPtr<BlobStorageData> blobStorage = static_cast<BlobRegistryImpl&>(blobRegistry()).getBlobDataFromURL(data->url());
65     if (blobStorage) {
66         for (size_t i = 0; i < blobStorage->items().size(); i++) {
67             const BlobDataItem& blobItem = blobStorage->items()[i];
68             if (blobItem.type == BlobDataItem::Data) {
69                 bytesWritten = 0;
70                 bytesWritten = writeToFile(handle, blobItem.data->data(), blobItem.data->length());
71
72                 if (bytesWritten < 0)
73                     helperClient->didFail(FileError::SECURITY_ERR);
74                 else
75                     helperClient->didWrite(bytesWritten, (i + 1 == blobStorage->items().size()) ? true : false);
76             } else if (blobItem.type == BlobDataItem::File) {
77                 closeFile(handle);
78
79                 if (copyFile(blobItem.path, path))
80                     helperClient->didWrite(blobItem.length, (i + 1 == blobStorage->items().size()) ? true : false);
81                 else
82                     helperClient->didFail(FileError::SECURITY_ERR);
83             } else if (blobItem.type == BlobDataItem::Blob) {
84                 LOG_ERROR("[writeAsync] data item is blob");
85                 notImplemented();
86             }
87         }
88     }
89
90     closeFile(handle);
91 }
92
93 void AsyncFileWriterTizen::write(long long position, Blob* data)
94 {
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)));
97 }
98
99 static void truncateAsync(ScriptExecutionContext*, PassOwnPtr<AsyncFileWriterClientTizen> helperClient, const String& path, long long length)
100 {
101     if (!fileExists(path)) {
102         helperClient->didFail(FileError::NOT_FOUND_ERR);
103         return;
104     }
105
106     PlatformFileHandle handle;
107     handle = openFile(path, OpenForWrite);
108
109     if (!isHandleValid(handle))
110         return;
111
112     if (truncateFile(handle, length))
113         helperClient->didTruncate();
114     else
115         helperClient->didFail(FileError::SECURITY_ERR);
116
117     closeFile(handle);
118 }
119
120 void AsyncFileWriterTizen::truncate(long long length)
121 {
122     String mode = m_taskController->uniqueMode();
123     m_taskController->postTaskToMainThread(createCallbackTask(&truncateAsync, AsyncFileWriterClientTizen::create(m_taskController.get(), m_client, mode), m_path, length));
124 }
125
126 void AsyncFileWriterTizen::abort()
127 {
128 }
129
130 bool AsyncFileWriterTizen::waitForOperationToComplete()
131 {
132     if (m_taskController->synchronousType() == SynchronousFileSystem)
133         return m_taskController->waitForTaskToComplete();
134
135     return false;
136 }
137
138 } // namespace WebCore
139
140 #endif // ENABLE(FILE_SYSTEM)
141