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