1/*
2 * Copyright (C) 2012, 2013 Research In Motion Limited. All rights reserved.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser 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 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
18 */
19
20#include "config.h"
21#if ENABLE(FILE_SYSTEM) && ENABLE(WORKERS)
22#include "WorkerAsyncFileSystemBlackBerry.h"
23
24#include "AsyncFileSystemCallbacks.h"
25#include "AsyncFileWriterClient.h"
26#include "CrossThreadTask.h"
27#include "WorkerAsyncFileWriterBlackBerry.h"
28#include "WorkerContext.h"
29#include "WorkerLoaderProxy.h"
30#include "WorkerPlatformAsyncFileSystemCallbacks.h"
31#include "WorkerThread.h"
32
33#include <BlackBerryPlatformMessageClient.h>
34#include <wtf/MainThread.h>
35#include <wtf/text/CString.h>
36#include <wtf/text/StringBuilder.h>
37
38using BlackBerry::Platform::WebFileSystem;
39using BlackBerry::Platform::WebFileWriter;
40using BlackBerry::Platform::webKitThreadMessageClient;
41
42namespace WebCore {
43
44WorkerAsyncFileSystemBlackBerry::WorkerAsyncFileSystemBlackBerry(WorkerContext* context, const String& mode, PassOwnPtr<WebFileSystem> platformFileSystem)
45    : AsyncFileSystemBlackBerry(platformFileSystem)
46    , m_context(context)
47    , m_mode(mode)
48{
49}
50
51WorkerAsyncFileSystemBlackBerry::~WorkerAsyncFileSystemBlackBerry()
52{
53}
54
55bool WorkerAsyncFileSystemBlackBerry::waitForOperationToComplete()
56{
57    if (m_context->thread()->runLoop().runInMode(m_context, m_mode) == MessageQueueTerminated)
58        return false;
59    return true;
60}
61
62void WorkerAsyncFileSystemBlackBerry::openFileSystemOnMainThread(ScriptExecutionContext*, const String& basePath, const String& storageIdentifier, FileSystemType type, long long size, bool create, WorkerPlatformAsyncFileSystemCallbacks* callbacks)
63{
64    WebFileSystem::openFileSystem(webKitThreadMessageClient(), basePath, storageIdentifier, static_cast<WebFileSystem::Type>(type), size, create, callbacks, 0);
65}
66
67void WorkerAsyncFileSystemBlackBerry::deleteFileSystemOnMainThread(ScriptExecutionContext*, const String& basePath, const String& storageIdentifier, FileSystemType type, WorkerPlatformAsyncFileSystemCallbacks* callbacks)
68{
69    WebFileSystem::deleteFileSystem(webKitThreadMessageClient(), basePath, storageIdentifier, static_cast<WebFileSystem::Type>(type), callbacks);
70}
71
72void WorkerAsyncFileSystemBlackBerry::moveOnMainThread(ScriptExecutionContext*, WebFileSystem* platformFileSystem, const KURL& sourcePath, const KURL& destinationPath, WorkerPlatformAsyncFileSystemCallbacks* callbacks)
73{
74    platformFileSystem->move(fileSystemURLToPath(sourcePath), fileSystemURLToPath(destinationPath), callbacks);
75}
76
77void WorkerAsyncFileSystemBlackBerry::copyOnMainThread(ScriptExecutionContext*, WebFileSystem* platformFileSystem, const KURL& sourcePath, const KURL& destinationPath, WorkerPlatformAsyncFileSystemCallbacks* callbacks)
78{
79    platformFileSystem->copy(fileSystemURLToPath(sourcePath), fileSystemURLToPath(destinationPath), callbacks);
80}
81
82void WorkerAsyncFileSystemBlackBerry::removeOnMainThread(ScriptExecutionContext*, WebFileSystem* platformFileSystem, const KURL& path, WorkerPlatformAsyncFileSystemCallbacks* callbacks)
83{
84    platformFileSystem->remove(fileSystemURLToPath(path), callbacks);
85}
86
87void WorkerAsyncFileSystemBlackBerry::removeRecursivelyOnMainThread(ScriptExecutionContext*, WebFileSystem* platformFileSystem, const KURL& path, WorkerPlatformAsyncFileSystemCallbacks* callbacks)
88{
89    platformFileSystem->removeRecursively(fileSystemURLToPath(path), callbacks);
90}
91
92void WorkerAsyncFileSystemBlackBerry::readMetadataOnMainThread(ScriptExecutionContext*, WebFileSystem* platformFileSystem, const KURL& path, WorkerPlatformAsyncFileSystemCallbacks* callbacks)
93{
94    platformFileSystem->readMetadata(fileSystemURLToPath(path), callbacks);
95}
96
97void WorkerAsyncFileSystemBlackBerry::createFileOnMainThread(ScriptExecutionContext*, WebFileSystem* platformFileSystem, const KURL& path, bool exclusive, WorkerPlatformAsyncFileSystemCallbacks* callbacks)
98{
99    platformFileSystem->createFile(fileSystemURLToPath(path), exclusive, callbacks);
100}
101
102void WorkerAsyncFileSystemBlackBerry::createDirectoryOnMainThread(ScriptExecutionContext*, WebFileSystem* platformFileSystem, const KURL& path, bool exclusive, WorkerPlatformAsyncFileSystemCallbacks* callbacks)
103{
104    platformFileSystem->createDirectory(fileSystemURLToPath(path), exclusive, callbacks);
105}
106
107void WorkerAsyncFileSystemBlackBerry::fileExistsOnMainThread(ScriptExecutionContext*, WebFileSystem* platformFileSystem, const KURL& path, WorkerPlatformAsyncFileSystemCallbacks* callbacks)
108{
109    platformFileSystem->fileExists(fileSystemURLToPath(path), callbacks);
110}
111
112void WorkerAsyncFileSystemBlackBerry::directoryExistsOnMainThread(ScriptExecutionContext*, WebFileSystem* platformFileSystem, const KURL& path, WorkerPlatformAsyncFileSystemCallbacks* callbacks)
113{
114    platformFileSystem->directoryExists(fileSystemURLToPath(path), callbacks);
115}
116
117void WorkerAsyncFileSystemBlackBerry::readDirectoryOnMainThread(ScriptExecutionContext*, WebFileSystem* platformFileSystem, const KURL& path, WorkerPlatformAsyncFileSystemCallbacks* callbacks)
118{
119    platformFileSystem->readDirectory(fileSystemURLToPath(path), callbacks);
120}
121
122void WorkerAsyncFileSystemBlackBerry::createWriterOnMainThread(ScriptExecutionContext*, WebFileSystem* platformFileSystem, AsyncFileWriterClient*, const KURL& path, WorkerPlatformAsyncFileSystemCallbacks* callbacks)
123{
124    WebFileWriter::createWriter(platformFileSystem, fileSystemURLToPath(path), callbacks);
125}
126
127void WorkerAsyncFileSystemBlackBerry::createSnapshotFileAndReadMetadataOnMainThread(ScriptExecutionContext*, WebFileSystem* platformFileSystem, const KURL& path, WorkerPlatformAsyncFileSystemCallbacks* callbacks)
128{
129    platformFileSystem->createSnapshotFileAndReadMetadata(fileSystemURLToPath(path), callbacks);
130}
131
132void WorkerAsyncFileSystemBlackBerry::openFileSystem(WorkerContext* context, const KURL& rootURL, const String& mode, const String& basePath, const String& storageIdentifier, FileSystemType type, long long size, bool create, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
133{
134    ASSERT(context);
135    postTaskToMainThread(createCallbackTask(&WorkerAsyncFileSystemBlackBerry::openFileSystemOnMainThread, basePath, storageIdentifier, type, size, create, new WorkerPlatformAsyncFileSystemCallbacks(callbacks, context, mode, rootURL)));
136}
137
138void WorkerAsyncFileSystemBlackBerry::deleteFileSystem(WorkerContext* context, const String& mode, const String& basePath, const String& storageIdentifier, FileSystemType type, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
139{
140    ASSERT(context);
141    postTaskToMainThread(createCallbackTask(&WorkerAsyncFileSystemBlackBerry::deleteFileSystemOnMainThread, basePath, storageIdentifier, type, new WorkerPlatformAsyncFileSystemCallbacks(callbacks, context, mode)));
142}
143
144void WorkerAsyncFileSystemBlackBerry::move(const KURL& sourcePath, const KURL& destinationPath, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
145{
146    postTaskToMainThread(createCallbackTask(&WorkerAsyncFileSystemBlackBerry::moveOnMainThread, m_platformFileSystem.get(), sourcePath, destinationPath, new WorkerPlatformAsyncFileSystemCallbacks(callbacks, m_context, m_mode)));
147}
148
149void WorkerAsyncFileSystemBlackBerry::copy(const KURL& sourcePath, const KURL& destinationPath, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
150{
151    postTaskToMainThread(createCallbackTask(&WorkerAsyncFileSystemBlackBerry::copyOnMainThread, m_platformFileSystem.get(), sourcePath, destinationPath, new WorkerPlatformAsyncFileSystemCallbacks(callbacks, m_context, m_mode)));
152}
153
154void WorkerAsyncFileSystemBlackBerry::remove(const KURL& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
155{
156    postTaskToMainThread(createCallbackTask(&WorkerAsyncFileSystemBlackBerry::removeOnMainThread, m_platformFileSystem.get(), path, new WorkerPlatformAsyncFileSystemCallbacks(callbacks, m_context, m_mode)));
157}
158
159void WorkerAsyncFileSystemBlackBerry::removeRecursively(const KURL& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
160{
161    postTaskToMainThread(createCallbackTask(&WorkerAsyncFileSystemBlackBerry::removeRecursivelyOnMainThread, m_platformFileSystem.get(), path, new WorkerPlatformAsyncFileSystemCallbacks(callbacks, m_context, m_mode)));
162}
163
164void WorkerAsyncFileSystemBlackBerry::readMetadata(const KURL& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
165{
166    postTaskToMainThread(createCallbackTask(&WorkerAsyncFileSystemBlackBerry::readMetadataOnMainThread, m_platformFileSystem.get(), path, new WorkerPlatformAsyncFileSystemCallbacks(callbacks, m_context, m_mode)));
167}
168
169void WorkerAsyncFileSystemBlackBerry::createFile(const KURL& path, bool exclusive, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
170{
171    postTaskToMainThread(createCallbackTask(&WorkerAsyncFileSystemBlackBerry::createFileOnMainThread, m_platformFileSystem.get(), path, exclusive, new WorkerPlatformAsyncFileSystemCallbacks(callbacks, m_context, m_mode)));
172}
173
174void WorkerAsyncFileSystemBlackBerry::createDirectory(const KURL& path, bool exclusive, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
175{
176    postTaskToMainThread(createCallbackTask(&WorkerAsyncFileSystemBlackBerry::createDirectoryOnMainThread, m_platformFileSystem.get(), path, exclusive, new WorkerPlatformAsyncFileSystemCallbacks(callbacks, m_context, m_mode)));
177}
178
179void WorkerAsyncFileSystemBlackBerry::fileExists(const KURL& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
180{
181    postTaskToMainThread(createCallbackTask(&WorkerAsyncFileSystemBlackBerry::fileExistsOnMainThread, m_platformFileSystem.get(), path, new WorkerPlatformAsyncFileSystemCallbacks(callbacks, m_context, m_mode)));
182}
183
184void WorkerAsyncFileSystemBlackBerry::directoryExists(const KURL& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
185{
186    postTaskToMainThread(createCallbackTask(&WorkerAsyncFileSystemBlackBerry::directoryExistsOnMainThread, m_platformFileSystem.get(), path, new WorkerPlatformAsyncFileSystemCallbacks(callbacks, m_context, m_mode)));
187}
188
189void WorkerAsyncFileSystemBlackBerry::readDirectory(const KURL& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
190{
191    postTaskToMainThread(createCallbackTask(&WorkerAsyncFileSystemBlackBerry::readDirectoryOnMainThread, m_platformFileSystem.get(), path, new WorkerPlatformAsyncFileSystemCallbacks(callbacks, m_context, m_mode)));
192}
193
194void WorkerAsyncFileSystemBlackBerry::createWriter(AsyncFileWriterClient* client, const KURL& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
195{
196    postTaskToMainThread(createCallbackTask(&WorkerAsyncFileSystemBlackBerry::createWriterOnMainThread, m_platformFileSystem.get(), client, path, new WorkerPlatformAsyncFileSystemCallbacks(callbacks, client, m_context, m_mode)));
197}
198
199void WorkerAsyncFileSystemBlackBerry::createSnapshotFileAndReadMetadata(const KURL& path, PassOwnPtr<AsyncFileSystemCallbacks> callbacks)
200{
201    postTaskToMainThread(createCallbackTask(&WorkerAsyncFileSystemBlackBerry::createSnapshotFileAndReadMetadataOnMainThread, m_platformFileSystem.get(), path, new WorkerPlatformAsyncFileSystemCallbacks(callbacks, m_context, m_mode)));
202}
203
204} // namespace WebCore
205#endif // ENABLE(FILE_SYSTEM) && ENABLE(WORKERS)
206