2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 #ifndef WorkerFileWriterCallbacksBridge_h
32 #define WorkerFileWriterCallbacksBridge_h
34 #if ENABLE(FILE_SYSTEM) && ENABLE(WORKERS)
37 #include "WebFileError.h"
38 #include "WebFileWriterClient.h"
39 #include "WorkerContext.h"
40 #include <wtf/PassOwnPtr.h>
41 #include <wtf/PassRefPtr.h>
42 #include <wtf/ThreadSafeRefCounted.h>
45 class AsyncFileWriterClient;
47 class WorkerLoaderProxy;
59 class WebFileWriterClient;
63 // This class is used as a mechanism to bridge calls between threads.
64 // Calls to a WebFileWriter must happen on the main thread, but they come from
65 // the context thread. The responses through the WebFileWriterClient interface
66 // start on the main thread, but must be sent via the worker context thread.
68 // A typical flow for write would look like this:
69 // Bridge::postWriteToMainThread() on WorkerThread
70 // --> Bridge::writeOnMainThread() is called on MainThread
71 // --> WebFileWriter::write()
72 // This makes an IPC; the actual operation is down in the browser.
73 // --> Bridge::didWrite is called on MainThread
74 // --> Bridge::didWriteOnWorkerThread is called on WorkerThread
75 // This calls the original client (m_clientOnWorkerThread).
77 // The bridge object is refcounted, so that it doesn't get deleted while there
78 // are cross-thread calls in flight. Each CrossThreadTask carries a reference
79 // to the bridge, which guarantees that the bridge will still be valid when the
80 // task is executed. In order to shut down the bridge, the WebFileWriterClient
81 // should call postShutdownToMainThread before dropping its reference to the
82 // bridge. This ensures that the WebFileWriter will be cleared on the main
83 // thread and that no further calls to the WebFileWriterClient will be made.
84 class WorkerFileWriterCallbacksBridge : public ThreadSafeRefCounted<WorkerFileWriterCallbacksBridge>, public WebCore::WorkerContext::Observer, public WebFileWriterClient {
86 ~WorkerFileWriterCallbacksBridge();
88 // WorkerContext::Observer method.
89 virtual void notifyStop();
91 static PassRefPtr<WorkerFileWriterCallbacksBridge> create(const WebCore::KURL& path, WebCore::WorkerLoaderProxy* proxy, WebCore::ScriptExecutionContext* workerContext, WebCore::AsyncFileWriterClient* client)
93 return adoptRef(new WorkerFileWriterCallbacksBridge(path, proxy, workerContext, client));
96 // Methods that create an instance and post an initial request task to the main thread. They must be called on the worker thread.
97 void postWriteToMainThread(long long position, const WebCore::KURL& data);
98 void postTruncateToMainThread(long long length);
99 void postAbortToMainThread();
101 // The owning WorkerAsyncFileWriterChromium should call this method before dropping its last reference to the bridge, on the context thread.
102 // The actual deletion of the WorkerFileWriterCallbacksBridge may happen on either the main or context thread, depending on where the last reference goes away; that's safe as long as this is called first.
103 void postShutdownToMainThread(PassRefPtr<WorkerFileWriterCallbacksBridge>);
105 // Callback methods that are called on the main thread.
106 // These are the implementation of WebKit::WebFileWriterClient.
107 void didWrite(long long bytes, bool complete);
108 void didFail(WebFileError);
111 // Call this on the context thread to wait for the current operation to complete.
112 bool waitForOperationToComplete();
115 WorkerFileWriterCallbacksBridge(const WebCore::KURL& path, WebCore::WorkerLoaderProxy*, WebCore::ScriptExecutionContext*, WebCore::AsyncFileWriterClient*);
117 void postInitToMainThread(const WebCore::KURL& path);
119 // Methods that are to be called on the main thread.
120 static void writeOnMainThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileWriterCallbacksBridge>, long long position, const WebCore::KURL& data);
121 static void truncateOnMainThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileWriterCallbacksBridge>, long long length);
122 static void abortOnMainThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileWriterCallbacksBridge>);
123 static void initOnMainThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileWriterCallbacksBridge>, const WebCore::KURL& path);
124 static void shutdownOnMainThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileWriterCallbacksBridge>);
126 // Methods that dispatch to AsyncFileWriterClient on the worker threads.
127 static void didWriteOnWorkerThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileWriterCallbacksBridge>, long long length, bool complete);
128 static void didFailOnWorkerThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileWriterCallbacksBridge>, WebFileError);
129 static void didTruncateOnWorkerThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileWriterCallbacksBridge>);
131 // Called on the main thread to run the supplied task.
132 static void runTaskOnMainThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileWriterCallbacksBridge>, PassOwnPtr<WebCore::ScriptExecutionContext::Task>);
133 // Called on the worker thread to run the supplied task.
134 static void runTaskOnWorkerThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileWriterCallbacksBridge>, PassOwnPtr<WebCore::ScriptExecutionContext::Task>);
136 // Called on the worker thread to dispatch to the main thread.
137 void dispatchTaskToMainThread(PassOwnPtr<WebCore::ScriptExecutionContext::Task>);
138 // Called on the main thread to dispatch to the worker thread.
139 void dispatchTaskToWorkerThread(PassOwnPtr<WebCore::ScriptExecutionContext::Task>);
141 // Used from the main thread to post tasks to the context thread.
142 WebCore::WorkerLoaderProxy* m_proxy;
144 // Used on the context thread, only to check that we're running on the context thread.
145 WebCore::ScriptExecutionContext* m_workerContext;
147 // Created and destroyed from the main thread.
148 OwnPtr<WebKit::WebFileWriter> m_writer;
150 // Used on the context thread to call back into the client.
151 WebCore::AsyncFileWriterClient* m_clientOnWorkerThread;
153 // Used to indicate that shutdown has started on the main thread, and hence the writer has been deleted.
154 bool m_writerDeleted;
156 // Used by waitForOperationToComplete.
157 bool m_operationInProgress;
159 // Used by postTaskForModeToWorkerContext and runInMode.
163 } // namespace WebCore
167 #endif // WorkerFileWriterCallbacksBridge_h