2 * Copyright (C) 2013 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 WorkerStorageQuotaCallbacksBridge_h
32 #define WorkerStorageQuotaCallbacksBridge_h
34 #if ENABLE(QUOTA) && ENABLE(WORKERS)
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>
47 class WorkerLoaderProxy;
52 class MainThreadStorageQuotaCallbacks;
53 class WebCommonWorkerClient;
54 class WebStorageQuotaCallbacksImpl;
55 class WorkerStorageQuotaContextObserver;
57 // Used to post a queryUsageAndQuota request to the main thread and get called back for the request.
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.
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> {
73 ~WorkerStorageQuotaCallbacksBridge();
77 static PassRefPtr<WorkerStorageQuotaCallbacksBridge> create(WebCore::WorkerLoaderProxy* workerLoaderProxy, WebCore::ScriptExecutionContext* workerContext, WebStorageQuotaCallbacksImpl* callbacks)
79 return adoptRef(new WorkerStorageQuotaCallbacksBridge(workerLoaderProxy, workerContext, callbacks));
82 // Entry method to post QueryUsageAndQuota task to main thread.
83 void postQueryUsageAndQuotaToMainThread(WebCommonWorkerClient*, WebStorageQuotaType, const String& mode);
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);
90 WorkerStorageQuotaCallbacksBridge(WebCore::WorkerLoaderProxy*, WebCore::ScriptExecutionContext*, WebStorageQuotaCallbacksImpl*);
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);
97 friend class MainThreadStorageQuotaCallbacks;
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);
103 void dispatchTaskToMainThread(PassOwnPtr<WebCore::ScriptExecutionContext::Task>);
104 void mayPostTaskToWorker(PassOwnPtr<WebCore::ScriptExecutionContext::Task>, const String& mode);
106 void cleanUpAfterCallback();
108 Mutex m_loaderProxyMutex;
109 WebCore::WorkerLoaderProxy* m_workerLoaderProxy;
111 WebCore::ScriptExecutionContext* m_workerContext;
113 // Must be deleted on the WorkerContext thread.
114 WorkerStorageQuotaContextObserver* m_workerContextObserver;
116 // This is self-destructed and must be fired on the worker thread.
117 WebStorageQuotaCallbacksImpl* m_callbacksOnWorkerThread;
120 } // namespace WebCore
124 #endif // WorkerStorageQuotaCallbacksBridge_h