WinCairo build fails to link.
[WebKit-https.git] / Source / WebKit / chromium / src / WorkerStorageQuotaCallbacksBridge.h
1 /*
2  * Copyright (C) 2013 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 WorkerStorageQuotaCallbacksBridge_h
32 #define WorkerStorageQuotaCallbacksBridge_h
33
34 #if ENABLE(QUOTA) && ENABLE(WORKERS)
35
36 #include "ScriptExecutionContext.h"
37 #include "StorageArea.h"
38 #include <WebStorageQuotaError.h>
39 #include <WebStorageQuotaType.h>
40 #include <public/WebVector.h>
41 #include <wtf/PassOwnPtr.h>
42 #include <wtf/PassRefPtr.h>
43 #include <wtf/Threading.h>
44 #include <wtf/text/WTFString.h>
45
46 namespace WebCore {
47 class WorkerLoaderProxy;
48 }
49
50 namespace WebKit {
51
52 class MainThreadStorageQuotaCallbacks;
53 class WebCommonWorkerClient;
54 class WebStorageQuotaCallbacksImpl;
55 class WorkerStorageQuotaContextObserver;
56
57 // Used to post a queryUsageAndQuota request to the main thread and get called back for the request.
58 //
59 // Lifetime for this class is maintained by posted "tasks" which ref count it and by MainThreadStorageQuotaCallbacks.
60 // Either a task finishing or the MainThreadStorageQuotaCallbacks being deleted may release the last ref on WorkerStorageQuotaCallbacksBridge.
61 //
62 // A typical flow for queryUsageAndQuota would look like this:
63 // Bridge::postQueryUsageAndQuotaToMainThread() on WorkerThread
64 //  --> Bridge::queryUsageAndQuotaOnMainThread() is called on MainThread
65 //      This makes an IPC with a MainThreadStorageQuotaCallbacks instance
66 //     [actual operation is down in the browser]
67 //  --> MainThreadStorageQuotaCallbacks::didXxx is called on MainThread
68 //  --> Bridge::didXxxOnMainThread is called on MainThread
69 //  --> Bridge::didXxxOnWorkerThread is called on WorkerThread
70 //      This calls the original callbacks (m_callbacksOnWorkerThread).
71 class WorkerStorageQuotaCallbacksBridge : public ThreadSafeRefCounted<WorkerStorageQuotaCallbacksBridge> {
72 public:
73     ~WorkerStorageQuotaCallbacksBridge();
74
75     void stop();
76
77     static PassRefPtr<WorkerStorageQuotaCallbacksBridge> create(WebCore::WorkerLoaderProxy* workerLoaderProxy, WebCore::ScriptExecutionContext* workerContext, WebStorageQuotaCallbacksImpl* callbacks)
78     {
79         return adoptRef(new WorkerStorageQuotaCallbacksBridge(workerLoaderProxy, workerContext, callbacks));
80     }
81
82     // Entry method to post QueryUsageAndQuota task to main thread.
83     void postQueryUsageAndQuotaToMainThread(WebCommonWorkerClient*, WebStorageQuotaType, const String& mode);
84
85     // Callback methods that are called on the main thread.
86     void didFailOnMainThread(WebStorageQuotaError, const String& mode);
87     void didQueryStorageUsageAndQuotaOnMainThread(unsigned long long usageInBytes, unsigned long long quotaInBytes, const String& mode);
88
89 private:
90     WorkerStorageQuotaCallbacksBridge(WebCore::WorkerLoaderProxy*, WebCore::ScriptExecutionContext*, WebStorageQuotaCallbacksImpl*);
91
92     // Method that is called on the main thread.
93     static void runTaskOnMainThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerStorageQuotaCallbacksBridge>, PassOwnPtr<WebCore::ScriptExecutionContext::Task>);
94     static void runTaskOnWorkerThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerStorageQuotaCallbacksBridge>, PassOwnPtr<WebCore::ScriptExecutionContext::Task>);
95     static void queryUsageAndQuotaOnMainThread(WebCore::ScriptExecutionContext*, WebCommonWorkerClient*, WebStorageQuotaType, PassRefPtr<WorkerStorageQuotaCallbacksBridge>, const String& mode);
96
97     friend class MainThreadStorageQuotaCallbacks;
98
99     // Methods that dispatch WebStorageQuotaCallbacks on the worker threads.
100     static void didFailOnWorkerThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerStorageQuotaCallbacksBridge>, WebStorageQuotaError);
101     static void didQueryStorageUsageAndQuotaOnWorkerThread(WebCore::ScriptExecutionContext*, PassRefPtr<WorkerStorageQuotaCallbacksBridge>, unsigned long long usageInBytes, unsigned long long quotaInBytes);
102
103     void dispatchTaskToMainThread(PassOwnPtr<WebCore::ScriptExecutionContext::Task>);
104     void mayPostTaskToWorker(PassOwnPtr<WebCore::ScriptExecutionContext::Task>, const String& mode);
105
106     void cleanUpAfterCallback();
107
108     Mutex m_loaderProxyMutex;
109     WebCore::WorkerLoaderProxy* m_workerLoaderProxy;
110
111     WebCore::ScriptExecutionContext* m_workerContext;
112
113     // Must be deleted on the WorkerContext thread.
114     WorkerStorageQuotaContextObserver* m_workerContextObserver;
115
116     // This is self-destructed and must be fired on the worker thread.
117     WebStorageQuotaCallbacksImpl* m_callbacksOnWorkerThread;
118 };
119
120 } // namespace WebCore
121
122 #endif
123
124 #endif // WorkerStorageQuotaCallbacksBridge_h