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.
32 #include "DatabaseObserver.h"
34 #if ENABLE(SQL_DATABASE)
36 #include "AbstractDatabase.h"
37 #include "CrossThreadCopier.h"
38 #include "CrossThreadTask.h"
40 #include "ScriptExecutionContext.h"
41 #include "WebCommonWorkerClient.h"
42 #include "WebDatabase.h"
43 #include "WebDatabaseObserver.h"
44 #include "WebFrameClient.h"
45 #include "WebFrameImpl.h"
46 #include "WebPermissionClient.h"
47 #include "WebSecurityOrigin.h"
48 #include "WebViewImpl.h"
49 #include "WebWorkerBase.h"
50 #include "WorkerContext.h"
51 #include "WorkerLoaderProxy.h"
52 #include "WorkerScriptController.h"
53 #include "WorkerThread.h"
55 using namespace WebKit;
61 static const char allowDatabaseMode[] = "allowDatabaseMode";
63 // This class is used to route the result of the WebWorkerBase::allowDatabase
64 // call back to the worker context.
65 class AllowDatabaseMainThreadBridge : public ThreadSafeRefCounted<AllowDatabaseMainThreadBridge> {
67 static PassRefPtr<AllowDatabaseMainThreadBridge> create(WebCore::WorkerLoaderProxy* workerLoaderProxy, const WTF::String& mode, NewWebCommonWorkerClient* commonClient, WebFrame* frame, const WTF::String& name, const WTF::String& displayName, unsigned long estimatedSize)
69 return adoptRef(new AllowDatabaseMainThreadBridge(workerLoaderProxy, mode, commonClient, frame, name, displayName, estimatedSize));
72 // These methods are invoked on the worker context.
75 MutexLocker locker(m_mutex);
76 m_workerLoaderProxy = 0;
84 // This method is invoked on the main thread.
85 void signalCompleted(bool result)
87 MutexLocker locker(m_mutex);
88 if (m_workerLoaderProxy)
89 m_workerLoaderProxy->postTaskForModeToWorkerContext(
90 createCallbackTask(&didComplete, WebCore::AllowCrossThreadAccess(this), result),
95 AllowDatabaseMainThreadBridge(WebCore::WorkerLoaderProxy* workerLoaderProxy, const WTF::String& mode, NewWebCommonWorkerClient* commonClient, WebFrame* frame, const WTF::String& name, const WTF::String& displayName, unsigned long estimatedSize)
96 : m_workerLoaderProxy(workerLoaderProxy)
99 WebWorkerBase::dispatchTaskToMainThread(
100 createCallbackTask(&allowDatabaseTask, WebCore::AllowCrossThreadAccess(commonClient),
101 WebCore::AllowCrossThreadAccess(frame),
102 String(name), String(displayName), estimatedSize,
103 WebCore::AllowCrossThreadAccess(this)));
106 static void allowDatabaseTask(WebCore::ScriptExecutionContext* context, NewWebCommonWorkerClient* commonClient, WebFrame* frame, const WTF::String name, const WTF::String displayName, unsigned long estimatedSize, PassRefPtr<AllowDatabaseMainThreadBridge> bridge)
109 bridge->signalCompleted(commonClient->allowDatabase(frame, name, displayName, estimatedSize));
111 bridge->signalCompleted(false);
114 static void didComplete(WebCore::ScriptExecutionContext* context, PassRefPtr<AllowDatabaseMainThreadBridge> bridge, bool result)
116 bridge->m_result = result;
121 WebCore::WorkerLoaderProxy* m_workerLoaderProxy;
125 bool allowDatabaseForWorker(NewWebCommonWorkerClient* commonClient, WebFrame* frame, const WebString& name, const WebString& displayName, unsigned long estimatedSize)
127 WebCore::WorkerScriptController* controller = WebCore::WorkerScriptController::controllerForContext();
128 WebCore::WorkerContext* workerContext = controller->workerContext();
129 WebCore::WorkerThread* workerThread = workerContext->thread();
130 WebCore::WorkerRunLoop& runLoop = workerThread->runLoop();
131 WebCore::WorkerLoaderProxy* workerLoaderProxy = &workerThread->workerLoaderProxy();
133 // Create a unique mode just for this synchronous call.
134 String mode = allowDatabaseMode;
135 mode.append(String::number(runLoop.createUniqueId()));
137 RefPtr<AllowDatabaseMainThreadBridge> bridge = AllowDatabaseMainThreadBridge::create(workerLoaderProxy, mode, commonClient, frame, String(name), String(displayName), estimatedSize);
139 // Either the bridge returns, or the queue gets terminated.
140 if (runLoop.runInMode(workerContext, mode) == MessageQueueTerminated) {
145 return bridge->result();
154 bool DatabaseObserver::canEstablishDatabase(ScriptExecutionContext* scriptExecutionContext, const String& name, const String& displayName, unsigned long estimatedSize)
156 ASSERT(scriptExecutionContext->isContextThread());
157 ASSERT(scriptExecutionContext->isDocument() || scriptExecutionContext->isWorkerContext());
158 if (scriptExecutionContext->isDocument()) {
159 Document* document = static_cast<Document*>(scriptExecutionContext);
160 WebFrameImpl* webFrame = WebFrameImpl::fromFrame(document->frame());
163 WebViewImpl* webView = webFrame->viewImpl();
166 if (webView->permissionClient())
167 return webView->permissionClient()->allowDatabase(webFrame, name, displayName, estimatedSize);
170 WorkerContext* workerContext = static_cast<WorkerContext*>(scriptExecutionContext);
171 WorkerLoaderProxy* workerLoaderProxy = &workerContext->thread()->workerLoaderProxy();
172 NewWebWorkerBase* webWorker = static_cast<NewWebWorkerBase*>(workerLoaderProxy);
173 return allowDatabaseForWorker(webWorker->newCommonClient(), webWorker->view()->mainFrame(), name, displayName, estimatedSize);
175 ASSERT_NOT_REACHED();
182 void DatabaseObserver::databaseOpened(AbstractDatabase* database)
184 ASSERT(database->scriptExecutionContext()->isContextThread());
185 WebDatabase::observer()->databaseOpened(WebDatabase(database));
188 void DatabaseObserver::databaseModified(AbstractDatabase* database)
190 ASSERT(database->scriptExecutionContext()->isContextThread());
191 WebDatabase::observer()->databaseModified(WebDatabase(database));
194 void DatabaseObserver::databaseClosed(AbstractDatabase* database)
196 ASSERT(database->scriptExecutionContext()->isContextThread());
197 WebDatabase::observer()->databaseClosed(WebDatabase(database));
200 void DatabaseObserver::reportOpenDatabaseResult(AbstractDatabase* database, int errorSite, int webSqlErrorCode, int sqliteErrorCode)
202 WebDatabase::observer()->reportOpenDatabaseResult(WebDatabase(database), errorSite, webSqlErrorCode, sqliteErrorCode);
205 void DatabaseObserver::reportChangeVersionResult(AbstractDatabase* database, int errorSite, int webSqlErrorCode, int sqliteErrorCode)
207 WebDatabase::observer()->reportChangeVersionResult(WebDatabase(database), errorSite, webSqlErrorCode, sqliteErrorCode);
210 void DatabaseObserver::reportStartTransactionResult(AbstractDatabase* database, int errorSite, int webSqlErrorCode, int sqliteErrorCode)
212 WebDatabase::observer()->reportStartTransactionResult(WebDatabase(database), errorSite, webSqlErrorCode, sqliteErrorCode);
215 void DatabaseObserver::reportCommitTransactionResult(AbstractDatabase* database, int errorSite, int webSqlErrorCode, int sqliteErrorCode)
217 WebDatabase::observer()->reportCommitTransactionResult(WebDatabase(database), errorSite, webSqlErrorCode, sqliteErrorCode);
220 void DatabaseObserver::reportExecuteStatementResult(AbstractDatabase* database, int errorSite, int webSqlErrorCode, int sqliteErrorCode)
222 WebDatabase::observer()->reportExecuteStatementResult(WebDatabase(database), errorSite, webSqlErrorCode, sqliteErrorCode);
225 void DatabaseObserver::reportVacuumDatabaseResult(AbstractDatabase* database, int sqliteErrorCode)
227 WebDatabase::observer()->reportVacuumDatabaseResult(WebDatabase(database), sqliteErrorCode);
230 } // namespace WebCore
232 #endif // ENABLE(SQL_DATABASE)