Home | History | Annotate | Download | only in web
      1 /*
      2  * Copyright (C) 2010 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 WorkerFileWriterCallbacksBridge_h
     32 #define WorkerFileWriterCallbacksBridge_h
     33 
     34 #include "WebFileWriterClient.h"
     35 #include "core/workers/WorkerGlobalScope.h"
     36 #include "public/platform/WebFileError.h"
     37 #include "wtf/PassOwnPtr.h"
     38 #include "wtf/PassRefPtr.h"
     39 #include "wtf/ThreadSafeRefCounted.h"
     40 
     41 namespace WebCore {
     42     class AsyncFileWriterClient;
     43     class KURL;
     44     class WorkerLoaderProxy;
     45 }
     46 
     47 namespace WTF {
     48     class String;
     49 }
     50 using WTF::String;
     51 
     52 namespace WebKit {
     53 
     54 class WebFileSystem;
     55 class WebFileWriter;
     56 class WebFileWriterClient;
     57 class WebURL;
     58 class WebWorkerBase;
     59 
     60 // This class is used as a mechanism to bridge calls between threads.
     61 // Calls to a WebFileWriter must happen on the main thread, but they come from
     62 // the context thread. The responses through the WebFileWriterClient interface
     63 // start on the main thread, but must be sent via the worker context thread.
     64 //
     65 // A typical flow for write would look like this:
     66 // Bridge::postWriteToMainThread() on WorkerThread
     67 //  --> Bridge::writeOnMainThread() is called on MainThread
     68 //  --> WebFileWriter::write()
     69 //      This makes an IPC; the actual operation is down in the browser.
     70 //  --> Bridge::didWrite is called on MainThread
     71 //  --> Bridge::didWriteOnWorkerThread is called on WorkerThread
     72 //      This calls the original client (m_clientOnWorkerThread).
     73 //
     74 // The bridge object is refcounted, so that it doesn't get deleted while there
     75 // are cross-thread calls in flight. Each CrossThreadTask carries a reference
     76 // to the bridge, which guarantees that the bridge will still be valid when the
     77 // task is executed. In order to shut down the bridge, the WebFileWriterClient
     78 // should call postShutdownToMainThread before dropping its reference to the
     79 // bridge. This ensures that the WebFileWriter will be cleared on the main
     80 // thread and that no further calls to the WebFileWriterClient will be made.
     81 class WorkerFileWriterCallbacksBridge : public ThreadSafeRefCounted<WorkerFileWriterCallbacksBridge>, public WebCore::WorkerGlobalScope::Observer, public WebFileWriterClient {
     82 public:
     83     ~WorkerFileWriterCallbacksBridge();
     84 
     85     // WorkerGlobalScope::Observer method.
     86     virtual void notifyStop();
     87 
     88     static PassRefPtr<WorkerFileWriterCallbacksBridge> create(const WebCore::KURL& path, WebCore::WorkerLoaderProxy* proxy, WebCore::ScriptExecutionContext* workerGlobalScope, WebCore::AsyncFileWriterClient* client)
     89     {
     90         return adoptRef(new WorkerFileWriterCallbacksBridge(path, proxy, workerGlobalScope, client));
     91     }
     92 
     93     // Methods that create an instance and post an initial request task to the main thread. They must be called on the worker thread.
     94     void postWriteToMainThread(long long position, const WebCore::KURL& data);
     95     void postTruncateToMainThread(long long length);
     96     void postAbortToMainThread();
     97 
     98     // The owning WorkerAsyncFileWriterChromium should call this method before dropping its last reference to the bridge, on the context thread.
     99     // 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.
    100     void postShutdownToMainThread(PassRefPtr<WorkerFileWriterCallbacksBridge>);
    101 
    102     // Callback methods that are called on the main thread.
    103     // These are the implementation of WebKit::WebFileWriterClient.
    104     void didWrite(long long bytes, bool complete);
    105     void didFail(WebFileError);
    106     void didTruncate();
    107 
    108     // Call this on the context thread to wait for the current operation to complete.
    109     bool waitForOperationToComplete();
    110 
    111 private:
    112     WorkerFileWriterCallbacksBridge(const WebCore::KURL& path, WebCore::WorkerLoaderProxy*, WebCore::ScriptExecutionContext*, WebCore::AsyncFileWriterClient*);
    113 
    114     void postInitToMainThread(const WebCore::KURL& path);
    115 
    116     // Methods that are to be called on the main thread.
    117     static void writeOnMainThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileWriterCallbacksBridge>, long long position, const WebCore::KURL& data);
    118     static void truncateOnMainThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileWriterCallbacksBridge>, long long length);
    119     static void abortOnMainThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileWriterCallbacksBridge>);
    120     static void initOnMainThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileWriterCallbacksBridge>, const WebCore::KURL& path);
    121     static void shutdownOnMainThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileWriterCallbacksBridge>);
    122 
    123     // Methods that dispatch to AsyncFileWriterClient on the worker threads.
    124     static void didWriteOnWorkerThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileWriterCallbacksBridge>, long long length, bool complete);
    125     static void didFailOnWorkerThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileWriterCallbacksBridge>, WebFileError);
    126     static void didTruncateOnWorkerThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileWriterCallbacksBridge>);
    127 
    128     // Called on the main thread to run the supplied task.
    129     static void runTaskOnMainThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileWriterCallbacksBridge>, PassOwnPtr<WebCore::ScriptExecutionContext::Task>);
    130     // Called on the worker thread to run the supplied task.
    131     static void runTaskOnWorkerThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerFileWriterCallbacksBridge>, PassOwnPtr<WebCore::ScriptExecutionContext::Task>);
    132 
    133     // Called on the worker thread to dispatch to the main thread.
    134     void dispatchTaskToMainThread(PassOwnPtr<WebCore::ScriptExecutionContext::Task>);
    135     // Called on the main thread to dispatch to the worker thread.
    136     void dispatchTaskToWorkerThread(PassOwnPtr<WebCore::ScriptExecutionContext::Task>);
    137 
    138     // Used from the main thread to post tasks to the context thread.
    139     WebCore::WorkerLoaderProxy* m_proxy;
    140 
    141     // Mutex for proxy.
    142     Mutex m_loaderProxyMutex;
    143 
    144     // Used on the context thread, only to check that we're running on the context thread.
    145     WebCore::ScriptExecutionContext* m_workerGlobalScope;
    146 
    147     // Created and destroyed from the main thread.
    148     OwnPtr<WebKit::WebFileWriter> m_writer;
    149 
    150     // Used on the context thread to call back into the client.
    151     WebCore::AsyncFileWriterClient* m_clientOnWorkerThread;
    152 
    153     // Used to indicate that shutdown has started on the main thread, and hence the writer has been deleted.
    154     bool m_writerDeleted;
    155 
    156     // Used by waitForOperationToComplete.
    157     bool m_operationInProgress;
    158 
    159     // Used by postTaskForModeToWorkerGlobalScope and runInMode.
    160     String m_mode;
    161 };
    162 
    163 } // namespace WebCore
    164 
    165 #endif // WorkerFileWriterCallbacksBridge_h
    166