Home | History | Annotate | Download | only in web
      1 /*
      2  * Copyright (C) 2009 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 "WebWorkerClientImpl.h"
     33 
     34 #include "bindings/v8/WorkerScriptController.h"
     35 #include "core/dom/CrossThreadTask.h"
     36 #include "core/dom/Document.h"
     37 #include "core/dom/ErrorEvent.h"
     38 #include "core/dom/MessageEvent.h"
     39 #include "core/dom/MessagePort.h"
     40 #include "core/dom/MessagePortChannel.h"
     41 #include "core/dom/ScriptExecutionContext.h"
     42 #include "core/inspector/InspectorInstrumentation.h"
     43 #include "core/inspector/ScriptCallStack.h"
     44 #include "core/loader/FrameLoaderClient.h"
     45 #include "core/page/Frame.h"
     46 #include "core/page/Page.h"
     47 #include "core/page/PageGroup.h"
     48 #include "core/workers/DedicatedWorkerThread.h"
     49 #include "core/workers/Worker.h"
     50 #include "core/workers/WorkerClients.h"
     51 #include "core/workers/WorkerGlobalScope.h"
     52 #include "core/workers/WorkerMessagingProxy.h"
     53 #include "wtf/OwnPtr.h"
     54 #include "wtf/Threading.h"
     55 
     56 #include "FrameLoaderClientImpl.h"
     57 #include "WebFrameClient.h"
     58 #include "WebFrameImpl.h"
     59 #include "WebPermissionClient.h"
     60 #include "WebViewImpl.h"
     61 #include "WorkerFileSystemClient.h"
     62 #include "core/dom/default/chromium/PlatformMessagePortChannelChromium.h"
     63 #include "public/platform/WebFileSystemCallbacks.h"
     64 #include "public/platform/WebMessagePortChannel.h"
     65 #include "public/platform/WebString.h"
     66 #include "public/platform/WebURL.h"
     67 
     68 using namespace WebCore;
     69 
     70 namespace WebKit {
     71 
     72 // Chromium-specific decorator of WorkerMessagingProxy.
     73 
     74 // static
     75 WorkerGlobalScopeProxy* WebWorkerClientImpl::createWorkerGlobalScopeProxy(Worker* worker)
     76 {
     77     if (worker->scriptExecutionContext()->isDocument()) {
     78         Document* document = toDocument(worker->scriptExecutionContext());
     79         WebFrameImpl* webFrame = WebFrameImpl::fromFrame(document->frame());
     80         OwnPtr<WorkerClients> workerClients = WorkerClients::create();
     81         provideLocalFileSystemToWorker(workerClients.get(), WorkerFileSystemClient::create());
     82         WebWorkerClientImpl* proxy = new WebWorkerClientImpl(worker, webFrame, workerClients.release());
     83         return proxy;
     84     }
     85     ASSERT_NOT_REACHED();
     86     return 0;
     87 }
     88 
     89 void WebWorkerClientImpl::terminateWorkerGlobalScope()
     90 {
     91     m_webFrame = 0;
     92     WebCore::WorkerMessagingProxy::terminateWorkerGlobalScope();
     93 }
     94 
     95 WebWorkerBase* WebWorkerClientImpl::toWebWorkerBase()
     96 {
     97     return this;
     98 }
     99 
    100 WebView* WebWorkerClientImpl::view() const
    101 {
    102     if (askedToTerminate())
    103         return 0;
    104     return m_webFrame->view();
    105 }
    106 
    107 bool WebWorkerClientImpl::allowDatabase(WebFrame*, const WebString& name, const WebString& displayName, unsigned long estimatedSize)
    108 {
    109     if (askedToTerminate())
    110         return false;
    111     WebKit::WebViewImpl* webView = m_webFrame->viewImpl();
    112     if (!webView)
    113         return false;
    114     return !webView->permissionClient() || webView->permissionClient()->allowDatabase(m_webFrame, name, displayName, estimatedSize);
    115 }
    116 
    117 bool WebWorkerClientImpl::allowFileSystem()
    118 {
    119     if (askedToTerminate())
    120         return false;
    121     WebKit::WebViewImpl* webView = m_webFrame->viewImpl();
    122     if (!webView)
    123         return false;
    124     return !webView->permissionClient() || webView->permissionClient()->allowFileSystem(m_webFrame);
    125 }
    126 
    127 void WebWorkerClientImpl::openFileSystem(WebFileSystemType type, long long size, bool create,
    128     WebFileSystemCallbacks* callbacks)
    129 {
    130     if (askedToTerminate()) {
    131         callbacks->didFail(WebFileErrorAbort);
    132         return;
    133     }
    134     m_webFrame->client()->openFileSystem(m_webFrame, type, size, create, callbacks);
    135 }
    136 
    137 bool WebWorkerClientImpl::allowIndexedDB(const WebString& name)
    138 {
    139     if (askedToTerminate())
    140         return false;
    141     WebKit::WebViewImpl* webView = m_webFrame->viewImpl();
    142     if (!webView)
    143         return false;
    144     return !webView->permissionClient() || webView->permissionClient()->allowIndexedDB(m_webFrame, name, WebSecurityOrigin());
    145 }
    146 
    147 WebWorkerClientImpl::WebWorkerClientImpl(Worker* worker, WebFrameImpl* webFrame, PassOwnPtr<WorkerClients> workerClients)
    148     : WebCore::WorkerMessagingProxy(worker, workerClients)
    149     , m_webFrame(webFrame)
    150 {
    151 }
    152 
    153 WebWorkerClientImpl::~WebWorkerClientImpl()
    154 {
    155 }
    156 
    157 } // namespace WebKit
    158