1 /* 2 * Copyright (C) 2011 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 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 14 * its contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include "config.h" 30 #include "IDBFactoryBackendProxy.h" 31 32 #include "public/platform/WebIDBDatabase.h" 33 #include "public/platform/WebIDBDatabaseError.h" 34 #include "public/platform/WebIDBFactory.h" 35 #include "public/platform/WebVector.h" 36 #include "IDBDatabaseBackendProxy.h" 37 #include "WebFrameImpl.h" 38 #include "WebIDBCallbacksImpl.h" 39 #include "WebIDBDatabaseCallbacksImpl.h" 40 #include "WebKit.h" 41 #include "WebPermissionClient.h" 42 #include "WebSecurityOrigin.h" 43 #include "WebViewImpl.h" 44 #include "WebWorkerBase.h" 45 #include "WebWorkerClientImpl.h" 46 #include "WorkerAllowMainThreadBridgeBase.h" 47 #include "bindings/v8/WorkerScriptController.h" 48 #include "core/dom/CrossThreadTask.h" 49 #include "core/dom/DOMError.h" 50 #include "core/dom/ExceptionCode.h" 51 #include "core/dom/ScriptExecutionContext.h" 52 #include "core/workers/WorkerGlobalScope.h" 53 #include "core/workers/WorkerLoaderProxy.h" 54 #include "core/workers/WorkerThread.h" 55 #include "modules/indexeddb/IDBDatabaseCallbacks.h" 56 #include "weborigin/SecurityOrigin.h" 57 58 59 using namespace WebCore; 60 61 namespace WebKit { 62 63 PassRefPtr<IDBFactoryBackendInterface> IDBFactoryBackendProxy::create() 64 { 65 return adoptRef(new IDBFactoryBackendProxy()); 66 } 67 68 IDBFactoryBackendProxy::IDBFactoryBackendProxy() 69 { 70 m_webIDBFactory = WebKit::Platform::current()->idbFactory(); 71 } 72 73 IDBFactoryBackendProxy::~IDBFactoryBackendProxy() 74 { 75 } 76 77 static const char allowIndexedDBMode[] = "allowIndexedDBMode"; 78 79 class AllowIndexedDBMainThreadBridge : public WorkerAllowMainThreadBridgeBase { 80 public: 81 static PassRefPtr<AllowIndexedDBMainThreadBridge> create(WorkerGlobalScope* workerGlobalScope, WebWorkerBase* webWorkerBase, const String& mode, const String& name) 82 { 83 return adoptRef(new AllowIndexedDBMainThreadBridge(workerGlobalScope, webWorkerBase, mode, name)); 84 } 85 86 private: 87 class AllowIDBParams : public AllowParams 88 { 89 public: 90 AllowIDBParams(const String& mode, const String& name) 91 : AllowParams(mode) 92 , m_name(name.isolatedCopy()) 93 { 94 } 95 String m_name; 96 }; 97 98 AllowIndexedDBMainThreadBridge(WorkerGlobalScope* workerGlobalScope, WebWorkerBase* webWorkerBase, const String& mode, const String& name) 99 : WorkerAllowMainThreadBridgeBase(workerGlobalScope, webWorkerBase) 100 { 101 postTaskToMainThread(adoptPtr(new AllowIDBParams(mode, name))); 102 } 103 104 virtual bool allowOnMainThread(WebCommonWorkerClient* commonClient, AllowParams* params) 105 { 106 ASSERT(isMainThread()); 107 AllowIDBParams* allowIDBParams = static_cast<AllowIDBParams*>(params); 108 return commonClient->allowIndexedDB(allowIDBParams->m_name); 109 } 110 }; 111 112 bool IDBFactoryBackendProxy::allowIndexedDB(ScriptExecutionContext* context, const String& name, const WebSecurityOrigin& origin, PassRefPtr<IDBCallbacks> callbacks) 113 { 114 bool allowed; 115 ASSERT_WITH_SECURITY_IMPLICATION(context->isDocument() || context->isWorkerGlobalScope()); 116 if (context->isDocument()) { 117 Document* document = toDocument(context); 118 WebFrameImpl* webFrame = WebFrameImpl::fromFrame(document->frame()); 119 WebViewImpl* webView = webFrame->viewImpl(); 120 // FIXME: webView->permissionClient() returns 0 in test_shell and content_shell http://crbug.com/137269 121 allowed = !webView->permissionClient() || webView->permissionClient()->allowIndexedDB(webFrame, name, origin); 122 } else { 123 WorkerGlobalScope* workerGlobalScope = toWorkerGlobalScope(context); 124 WebWorkerBase* webWorkerBase = static_cast<WebWorkerBase*>(workerGlobalScope->thread()->workerLoaderProxy().toWebWorkerBase()); 125 WorkerRunLoop& runLoop = workerGlobalScope->thread()->runLoop(); 126 127 String mode = allowIndexedDBMode; 128 mode.append(String::number(runLoop.createUniqueId())); 129 RefPtr<AllowIndexedDBMainThreadBridge> bridge = AllowIndexedDBMainThreadBridge::create(workerGlobalScope, webWorkerBase, mode, name); 130 131 // Either the bridge returns, or the queue gets terminated. 132 if (runLoop.runInMode(workerGlobalScope, mode) == MessageQueueTerminated) { 133 bridge->cancel(); 134 return false; 135 } 136 allowed = bridge->result(); 137 } 138 139 if (!allowed) 140 callbacks->onError(WebIDBDatabaseError(UnknownError, "The user denied permission to access the database.")); 141 142 return allowed; 143 } 144 145 void IDBFactoryBackendProxy::getDatabaseNames(PassRefPtr<IDBCallbacks> prpCallbacks, const String& databaseIdentifier, ScriptExecutionContext* context) 146 { 147 RefPtr<IDBCallbacks> callbacks(prpCallbacks); 148 WebSecurityOrigin origin(context->securityOrigin()); 149 if (!allowIndexedDB(context, "Database Listing", origin, callbacks)) 150 return; 151 152 m_webIDBFactory->getDatabaseNames(new WebIDBCallbacksImpl(callbacks), databaseIdentifier); 153 } 154 155 void IDBFactoryBackendProxy::open(const String& name, int64_t version, int64_t transactionId, PassRefPtr<IDBCallbacks> prpCallbacks, PassRefPtr<IDBDatabaseCallbacks> prpDatabaseCallbacks, const String& databaseIdentifier, ScriptExecutionContext* context) 156 { 157 RefPtr<IDBCallbacks> callbacks(prpCallbacks); 158 RefPtr<IDBDatabaseCallbacks> databaseCallbacks(prpDatabaseCallbacks); 159 WebSecurityOrigin origin(context->securityOrigin()); 160 if (!allowIndexedDB(context, name, origin, callbacks)) 161 return; 162 163 m_webIDBFactory->open(name, version, transactionId, new WebIDBCallbacksImpl(callbacks), new WebIDBDatabaseCallbacksImpl(databaseCallbacks), databaseIdentifier); 164 } 165 166 void IDBFactoryBackendProxy::deleteDatabase(const String& name, PassRefPtr<IDBCallbacks> prpCallbacks, const String& databaseIdentifier, ScriptExecutionContext* context) 167 { 168 RefPtr<IDBCallbacks> callbacks(prpCallbacks); 169 WebSecurityOrigin origin(context->securityOrigin()); 170 if (!allowIndexedDB(context, name, origin, callbacks)) 171 return; 172 173 m_webIDBFactory->deleteDatabase(name, new WebIDBCallbacksImpl(callbacks), databaseIdentifier); 174 } 175 176 } // namespace WebKit 177