Part 2 of removing PlatformString.h, remove PlatformString.h
[WebKit-https.git] / Source / WebKit / chromium / src / WorkerFileSystemCallbacksBridge.h
1 /*
2  * Copyright (C) 2010, 2012 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #ifndef WorkerFileSystemCallbacksBridge_h
32 #define WorkerFileSystemCallbacksBridge_h
33
34 #if ENABLE(FILE_SYSTEM) && ENABLE(WORKERS)
35
36 #include "ScriptExecutionContext.h"
37 #include "WebFileError.h"
38 #include "platform/WebVector.h"
39 #include <public/WebFileSystem.h>
40 #include <wtf/PassOwnPtr.h>
41 #include <wtf/PassRefPtr.h>
42 #include <wtf/Threading.h>
43 #include <wtf/text/WTFString.h>
44
45 namespace WebCore {
46 class WorkerLoaderProxy;
47 }
48
49 namespace WebKit {
50
51 class AsyncFileSystem;
52 class MainThreadFileSystemCallbacks;
53 class WebCommonWorkerClient;
54 class ThreadableCallbacksBridgeWrapper;
55 class WebFileSystemCallbacks;
56 class WorkerFileSystemContextObserver;
57 struct WebFileInfo;
58 struct WebFileSystemEntry;
59
60 // Used to post a openFileSystem request to the main thread and get called back for the request.
61 //
62 // Lifetime for this class is maintained by posted "tasks" which ref count it and by MainThreadFileSystemCallbacks.
63 // Either a task finishing or the MainThreadFileSystemCallbacks being deleted may release the last ref on WorkerFileSystemCallbacksBridge.
64 //
65 // A typical flow for openFileSystem would look like this:
66 // Bridge::postOpenFileSystemToMainThread() on WorkerThread
67 //  --> Bridge::openFileSystemOnMainThread() is called on MainThread
68 //      This makes an IPC with a MainThreadFileSystemCallbacks instance
69 //     [actual operation is down in the browser]
70 //  --> MainThreadFileSystemCallbacks::didXxx is called on MainThread
71 //  --> Bridge::didXxxOnMainThread is called on MainThread
72 //  --> Bridge::didXxxOnWorkerThread is called on WorkerThread
73 //      This calls the original callbacks (m_callbacksOnWorkerThread).
74 class WorkerFileSystemCallbacksBridge : public ThreadSafeRefCounted<WorkerFileSystemCallbacksBridge> {
75 public:
76     ~WorkerFileSystemCallbacksBridge();
77
78     void stop();
79
80     static PassRefPtr<WorkerFileSystemCallbacksBridge> create(WebCore::WorkerLoaderProxy* workerLoaderProxy, WebCore::ScriptExecutionContext* workerContext, WebFileSystemCallbacks* callbacks)
81     {
82         return adoptRef(new WorkerFileSystemCallbacksBridge(workerLoaderProxy, workerContext, callbacks));
83     }
84
85     // Methods that create an instance and post an initial request task to the main thread. They must be called on the worker thread.
86     void postOpenFileSystemToMainThread(WebCommonWorkerClient*, WebFileSystem::Type, long long size, bool create, const String& mode);
87     void postMoveToMainThread(WebFileSystem*, const WebCore::KURL& srcPath, const WebCore::KURL& destPath, const String& mode);
88     void postCopyToMainThread(WebFileSystem*, const WebCore::KURL& srcPath, const WebCore::KURL& destPath, const String& mode);
89     void postRemoveToMainThread(WebFileSystem*, const WebCore::KURL& path, const String& mode);
90     void postRemoveRecursivelyToMainThread(WebFileSystem*, const WebCore::KURL& path, const String& mode);
91     void postReadMetadataToMainThread(WebFileSystem*, const WebCore::KURL& path, const String& mode);
92     void postCreateFileToMainThread(WebFileSystem*, const WebCore::KURL& path, bool exclusive, const String& mode);
93     void postCreateDirectoryToMainThread(WebFileSystem*, const WebCore::KURL& path, bool exclusive, const String& mode);
94     void postFileExistsToMainThread(WebFileSystem*, const WebCore::KURL& path, const String& mode);
95     void postDirectoryExistsToMainThread(WebFileSystem*, const WebCore::KURL& path, const String& mode);
96     void postReadDirectoryToMainThread(WebFileSystem*, const WebCore::KURL& path, const String& mode);
97     void postCreateSnapshotFileToMainThread(WebFileSystem*, const WebCore::KURL& internalBlobURL, const WebCore::KURL& path, const String& mode);
98
99     // Callback methods that are called on the main thread.
100     void didFailOnMainThread(WebFileError, const String& mode);
101     void didOpenFileSystemOnMainThread(const String& name, const WebCore::KURL& rootURL, const String& mode);
102     void didSucceedOnMainThread(const String& mode);
103     void didReadMetadataOnMainThread(const WebFileInfo&, const String& mode);
104     void didReadDirectoryOnMainThread(const WebVector<WebFileSystemEntry>&, bool hasMore, const String& mode);
105
106 private:
107     WorkerFileSystemCallbacksBridge(WebCore::WorkerLoaderProxy*, WebCore::ScriptExecutionContext*, WebFileSystemCallbacks*);
108
109     // Methods that are to be called on the main thread.
110     static void openFileSystemOnMainThread(WebCore::ScriptExecutionContext*, WebCommonWorkerClient*, WebFileSystem::Type, long long size, bool create, PassRefPtr<WorkerFileSystemCallbacksBridge>, const String& mode);
111     static void moveOnMainThread(WebCore::ScriptExecutionContext*, WebFileSystem*, const WebCore::KURL& srcPath, const WebCore::KURL& destPath, PassRefPtr<WorkerFileSystemCallbacksBridge>, const String& mode);
112     static void copyOnMainThread(WebCore::ScriptExecutionContext*, WebFileSystem*, const WebCore::KURL& srcPath, const WebCore::KURL& destPath, PassRefPtr<WorkerFileSystemCallbacksBridge>, const String& mode);
113     static void removeOnMainThread(WebCore::ScriptExecutionContext*, WebFileSystem*, const WebCore::KURL& path, PassRefPtr<WorkerFileSystemCallbacksBridge>, const String& mode);
114     static void removeRecursivelyOnMainThread(WebCore::ScriptExecutionContext*, WebFileSystem*, const WebCore::KURL& path, PassRefPtr<WorkerFileSystemCallbacksBridge>, const String& mode);
115     static void readMetadataOnMainThread(WebCore::ScriptExecutionContext*, WebFileSystem*, const WebCore::KURL& path, PassRefPtr<WorkerFileSystemCallbacksBridge>, const String& mode);
116     static void createFileOnMainThread(WebCore::ScriptExecutionContext*, WebFileSystem*, const WebCore::KURL& path, bool exclusive, PassRefPtr<WorkerFileSystemCallbacksBridge>, const String& mode);
117     static void createDirectoryOnMainThread(WebCore::ScriptExecutionContext*, WebFileSystem*, const WebCore::KURL& path, bool exclusive, PassRefPtr<WorkerFileSystemCallbacksBridge>, const String& mode);
118     static void fileExistsOnMainThread(WebCore::ScriptExecutionContext*, WebFileSystem*, const WebCore::KURL& path, PassRefPtr<WorkerFileSystemCallbacksBridge>, const String& mode);
119     static void directoryExistsOnMainThread(WebCore::ScriptExecutionContext*, WebFileSystem*, const WebCore::KURL& path, PassRefPtr<WorkerFileSystemCallbacksBridge>, const String& mode);
120     static void readDirectoryOnMainThread(WebCore::ScriptExecutionContext*, WebFileSystem*, const WebCore::KURL& path, PassRefPtr<WorkerFileSystemCallbacksBridge>, const String& mode);
121     static void createSnapshotFileOnMainThread(WebCore::ScriptExecutionContext*, WebFileSystem*, const WebCore::KURL& internalBlobURL, const WebCore::KURL& path, PassRefPtr<WorkerFileSystemCallbacksBridge>, const String& mode);
122
123     friend class MainThreadFileSystemCallbacks;
124
125     // Methods that dispatch WebFileSystemCallbacks on the worker threads.
126     static void didFailOnWorkerThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileSystemCallbacksBridge>, WebFileError);
127     static void didOpenFileSystemOnWorkerThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileSystemCallbacksBridge>, const String& name, const WebCore::KURL& rootPath);
128     static void didSucceedOnWorkerThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileSystemCallbacksBridge>);
129     static void didReadMetadataOnWorkerThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileSystemCallbacksBridge>, const WebFileInfo&);
130     static void didReadDirectoryOnWorkerThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileSystemCallbacksBridge>, const WebVector<WebFileSystemEntry>&, bool hasMore);
131
132     static void runTaskOnMainThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileSystemCallbacksBridge>, PassOwnPtr<WebCore::ScriptExecutionContext::Task>);
133     static void runTaskOnWorkerThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileSystemCallbacksBridge>, PassOwnPtr<WebCore::ScriptExecutionContext::Task>);
134
135     void dispatchTaskToMainThread(PassOwnPtr<WebCore::ScriptExecutionContext::Task>);
136     void mayPostTaskToWorker(PassOwnPtr<WebCore::ScriptExecutionContext::Task>, const String& mode);
137
138     void cleanUpAfterCallback();
139
140     Mutex m_loaderProxyMutex;
141     WebCore::WorkerLoaderProxy* m_workerLoaderProxy;
142
143     WebCore::ScriptExecutionContext* m_workerContext;
144
145     // Must be deleted on the WorkerContext thread.
146     WorkerFileSystemContextObserver* m_workerContextObserver;
147
148     // This is self-destructed and must be fired on the worker thread.
149     WebFileSystemCallbacks* m_callbacksOnWorkerThread;
150 };
151
152 } // namespace WebCore
153
154 #endif
155
156 #endif // WorkerFileSystemCallbacksBridge_h