tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebKit / chromium / src / DatabaseObserver.cpp
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 #include "config.h"
32 #include "DatabaseObserver.h"
33
34 #if ENABLE(SQL_DATABASE)
35
36 #include "AbstractDatabase.h"
37 #include "CrossThreadCopier.h"
38 #include "CrossThreadTask.h"
39 #include "Document.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"
54
55 using namespace WebKit;
56
57 namespace {
58
59 #if ENABLE(WORKERS)
60
61 static const char allowDatabaseMode[] = "allowDatabaseMode";
62
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> {
66 public:
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)
68     {
69         return adoptRef(new AllowDatabaseMainThreadBridge(workerLoaderProxy, mode, commonClient, frame, name, displayName, estimatedSize));
70     }
71
72     // These methods are invoked on the worker context.
73     void cancel()
74     {
75         MutexLocker locker(m_mutex);
76         m_workerLoaderProxy = 0;
77     }
78
79     bool result()
80     {
81         return m_result;
82     }
83
84     // This method is invoked on the main thread.
85     void signalCompleted(bool result)
86     {
87         MutexLocker locker(m_mutex);
88         if (m_workerLoaderProxy)
89             m_workerLoaderProxy->postTaskForModeToWorkerContext(
90                 createCallbackTask(&didComplete, WebCore::AllowCrossThreadAccess(this), result), 
91                 m_mode);
92     }
93
94 private:
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)
97         , m_mode(mode)
98     {
99         WebWorkerBase::dispatchTaskToMainThread(
100             createCallbackTask(&allowDatabaseTask, WebCore::AllowCrossThreadAccess(commonClient),
101                                WebCore::AllowCrossThreadAccess(frame),
102                                String(name), String(displayName), estimatedSize,
103                                WebCore::AllowCrossThreadAccess(this)));
104     }
105
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)
107     {
108         if (commonClient)
109             bridge->signalCompleted(commonClient->allowDatabase(frame, name, displayName, estimatedSize));
110         else
111             bridge->signalCompleted(false);
112     }
113
114     static void didComplete(WebCore::ScriptExecutionContext* context, PassRefPtr<AllowDatabaseMainThreadBridge> bridge, bool result)
115     {
116         bridge->m_result = result;
117     }
118
119     bool m_result;
120     Mutex m_mutex;
121     WebCore::WorkerLoaderProxy* m_workerLoaderProxy;
122     WTF::String m_mode;
123 };
124
125 bool allowDatabaseForWorker(NewWebCommonWorkerClient* commonClient, WebFrame* frame, const WebString& name, const WebString& displayName, unsigned long estimatedSize)
126 {
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();
132
133     // Create a unique mode just for this synchronous call.
134     String mode = allowDatabaseMode;
135     mode.append(String::number(runLoop.createUniqueId()));
136
137     RefPtr<AllowDatabaseMainThreadBridge> bridge = AllowDatabaseMainThreadBridge::create(workerLoaderProxy, mode, commonClient, frame, String(name), String(displayName), estimatedSize);
138
139     // Either the bridge returns, or the queue gets terminated.
140     if (runLoop.runInMode(workerContext, mode) == MessageQueueTerminated) {
141         bridge->cancel();
142         return false;
143     }
144
145     return bridge->result();
146 }
147
148 #endif
149
150 }
151
152 namespace WebCore {
153
154 bool DatabaseObserver::canEstablishDatabase(ScriptExecutionContext* scriptExecutionContext, const String& name, const String& displayName, unsigned long estimatedSize)
155 {
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());
161         if (!webFrame)
162             return false;
163         WebViewImpl* webView = webFrame->viewImpl();
164         if (!webView)
165             return false;
166         if (webView->permissionClient())
167             return webView->permissionClient()->allowDatabase(webFrame, name, displayName, estimatedSize);
168     } else {
169 #if ENABLE(WORKERS)
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);
174 #else
175         ASSERT_NOT_REACHED();
176 #endif
177     }
178
179     return true;
180 }
181
182 void DatabaseObserver::databaseOpened(AbstractDatabase* database)
183 {
184     ASSERT(database->scriptExecutionContext()->isContextThread());
185     WebDatabase::observer()->databaseOpened(WebDatabase(database));
186 }
187
188 void DatabaseObserver::databaseModified(AbstractDatabase* database)
189 {
190     ASSERT(database->scriptExecutionContext()->isContextThread());
191     WebDatabase::observer()->databaseModified(WebDatabase(database));
192 }
193
194 void DatabaseObserver::databaseClosed(AbstractDatabase* database)
195 {
196     ASSERT(database->scriptExecutionContext()->isContextThread());
197     WebDatabase::observer()->databaseClosed(WebDatabase(database));
198 }
199
200 void DatabaseObserver::reportOpenDatabaseResult(AbstractDatabase* database, int errorSite, int webSqlErrorCode, int sqliteErrorCode)
201 {
202     WebDatabase::observer()->reportOpenDatabaseResult(WebDatabase(database), errorSite, webSqlErrorCode, sqliteErrorCode);
203 }
204
205 void DatabaseObserver::reportChangeVersionResult(AbstractDatabase* database, int errorSite, int webSqlErrorCode, int sqliteErrorCode)
206 {
207     WebDatabase::observer()->reportChangeVersionResult(WebDatabase(database), errorSite, webSqlErrorCode, sqliteErrorCode);
208 }
209
210 void DatabaseObserver::reportStartTransactionResult(AbstractDatabase* database, int errorSite, int webSqlErrorCode, int sqliteErrorCode)
211 {
212     WebDatabase::observer()->reportStartTransactionResult(WebDatabase(database), errorSite, webSqlErrorCode, sqliteErrorCode);
213 }
214
215 void DatabaseObserver::reportCommitTransactionResult(AbstractDatabase* database, int errorSite, int webSqlErrorCode, int sqliteErrorCode)
216 {
217     WebDatabase::observer()->reportCommitTransactionResult(WebDatabase(database), errorSite, webSqlErrorCode, sqliteErrorCode);
218 }
219
220 void DatabaseObserver::reportExecuteStatementResult(AbstractDatabase* database, int errorSite, int webSqlErrorCode, int sqliteErrorCode)
221 {
222     WebDatabase::observer()->reportExecuteStatementResult(WebDatabase(database), errorSite, webSqlErrorCode, sqliteErrorCode);
223 }
224
225 void DatabaseObserver::reportVacuumDatabaseResult(AbstractDatabase* database, int sqliteErrorCode)
226 {
227     WebDatabase::observer()->reportVacuumDatabaseResult(WebDatabase(database), sqliteErrorCode);
228 }
229
230 } // namespace WebCore
231
232 #endif // ENABLE(SQL_DATABASE)