| OLD | NEW |
| (Empty) | |
| 1 /* |
| 2 * Copyright (C) 2013 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 "WebEmbeddedWorkerImpl.h" |
| 33 |
| 34 #include "WebDataSourceImpl.h" |
| 35 #include "WebFrameImpl.h" |
| 36 #include "WebServiceWorkerContextClient.h" |
| 37 #include "WebView.h" |
| 38 #include "WebWorkerPermissionClientProxy.h" |
| 39 #include "WorkerPermissionClient.h" |
| 40 #include "core/dom/Document.h" |
| 41 #include "core/frame/ContentSecurityPolicyResponseHeaders.h" |
| 42 #include "core/loader/FrameLoadRequest.h" |
| 43 #include "core/loader/SubstituteData.h" |
| 44 #include "core/workers/WorkerClients.h" |
| 45 #include "core/workers/WorkerScriptLoader.h" |
| 46 #include "core/workers/WorkerScriptLoaderClient.h" |
| 47 #include "core/workers/WorkerThreadStartupData.h" |
| 48 #include "platform/NotImplemented.h" |
| 49 #include "platform/SharedBuffer.h" |
| 50 #include "wtf/Functional.h" |
| 51 |
| 52 using namespace WebCore; |
| 53 |
| 54 namespace blink { |
| 55 |
| 56 // A thin wrapper for one-off script loading. |
| 57 class WebEmbeddedWorkerImpl::Loader : public WorkerScriptLoaderClient { |
| 58 public: |
| 59 static PassOwnPtr<Loader> create(ExecutionContext* loadingContext, const KUR
L& scriptURL, const Closure& callback) |
| 60 { |
| 61 return adoptPtr(new Loader(loadingContext, scriptURL, callback)); |
| 62 } |
| 63 |
| 64 virtual ~Loader() |
| 65 { |
| 66 m_scriptLoader->setClient(0); |
| 67 } |
| 68 |
| 69 virtual void notifyFinished() OVERRIDE |
| 70 { |
| 71 m_callback(); |
| 72 } |
| 73 |
| 74 void cancel() |
| 75 { |
| 76 m_scriptLoader->cancel(); |
| 77 } |
| 78 |
| 79 bool failed() const { return m_scriptLoader->failed(); } |
| 80 String script() const { return m_scriptLoader->script(); } |
| 81 |
| 82 private: |
| 83 Loader(ExecutionContext* loadingContext, const KURL& scriptURL, const Closur
e& callback) |
| 84 : m_scriptLoader(WorkerScriptLoader::create()) |
| 85 , m_callback(callback) |
| 86 { |
| 87 m_scriptLoader->setTargetType(ResourceRequest::TargetIsServiceWorker); |
| 88 m_scriptLoader->loadAsynchronously( |
| 89 loadingContext, scriptURL, DenyCrossOriginRequests, this); |
| 90 } |
| 91 |
| 92 RefPtr<WorkerScriptLoader> m_scriptLoader; |
| 93 Closure m_callback; |
| 94 }; |
| 95 |
| 96 WebEmbeddedWorker* WebEmbeddedWorker::create( |
| 97 WebServiceWorkerContextClient* client, |
| 98 WebWorkerPermissionClientProxy* permissionClient) |
| 99 { |
| 100 return new WebEmbeddedWorkerImpl(adoptPtr(client), adoptPtr(permissionClient
)); |
| 101 } |
| 102 |
| 103 WebEmbeddedWorkerImpl::WebEmbeddedWorkerImpl( |
| 104 PassOwnPtr<WebServiceWorkerContextClient> client, |
| 105 PassOwnPtr<WebWorkerPermissionClientProxy> permissionClient) |
| 106 : m_workerContextClient(client) |
| 107 , m_permissionClient(permissionClient) |
| 108 , m_askedToTerminate(false) |
| 109 { |
| 110 } |
| 111 |
| 112 WebEmbeddedWorkerImpl::~WebEmbeddedWorkerImpl() |
| 113 { |
| 114 ASSERT(m_webView); |
| 115 |
| 116 // Detach the client before closing the view to avoid getting called back. |
| 117 toWebFrameImpl(m_mainFrame)->setClient(0); |
| 118 |
| 119 m_webView->close(); |
| 120 m_mainFrame->close(); |
| 121 } |
| 122 |
| 123 void WebEmbeddedWorkerImpl::startWorkerContext( |
| 124 const WebEmbeddedWorkerStartData& data) |
| 125 { |
| 126 ASSERT(!m_askedToTerminate); |
| 127 ASSERT(!m_mainScriptLoader); |
| 128 m_workerStartData = data; |
| 129 |
| 130 prepareShadowPageForLoader(); |
| 131 |
| 132 m_mainScriptLoader = Loader::create( |
| 133 m_loadingContext.get(), |
| 134 data.scriptURL, |
| 135 bind(&WebEmbeddedWorkerImpl::onScriptLoaderFinished, this)); |
| 136 } |
| 137 |
| 138 void WebEmbeddedWorkerImpl::terminateWorkerContext() |
| 139 { |
| 140 if (m_askedToTerminate) |
| 141 return; |
| 142 m_askedToTerminate = true; |
| 143 if (m_mainScriptLoader) |
| 144 m_mainScriptLoader->cancel(); |
| 145 if (m_workerThread) |
| 146 m_workerThread->stop(); |
| 147 } |
| 148 |
| 149 void WebEmbeddedWorkerImpl::prepareShadowPageForLoader() |
| 150 { |
| 151 // Create 'shadow page', which is never displayed and is used mainly to |
| 152 // provide a context for loading on the main thread. |
| 153 // |
| 154 // FIXME: This does mostly same as WebSharedWorkerImpl::initializeLoader. |
| 155 // This code, and probably most of the code in this class should be shared |
| 156 // with SharedWorker. |
| 157 ASSERT(!m_webView); |
| 158 m_webView = WebView::create(0); |
| 159 m_mainFrame = WebFrame::create(this); |
| 160 m_webView->setMainFrame(m_mainFrame); |
| 161 |
| 162 WebFrameImpl* webFrame = toWebFrameImpl(m_webView->mainFrame()); |
| 163 |
| 164 // Construct substitute data source for the 'shadow page'. We only need it |
| 165 // to have same origin as the worker so the loading checks work correctly. |
| 166 CString content(""); |
| 167 int length = static_cast<int>(content.length()); |
| 168 RefPtr<SharedBuffer> buffer(SharedBuffer::create(content.data(), length)); |
| 169 webFrame->frame()->loader().load(FrameLoadRequest(0, ResourceRequest(m_worke
rStartData.scriptURL), SubstituteData(buffer, "text/html", "UTF-8", KURL()))); |
| 170 |
| 171 // This document context will be used as 'loading context' for the worker. |
| 172 m_loadingContext = webFrame->frame()->document(); |
| 173 } |
| 174 |
| 175 void WebEmbeddedWorkerImpl::didCreateDataSource(WebFrame*, WebDataSource* ds) |
| 176 { |
| 177 // Tell the loader to load the data into the 'shadow page' synchronously, |
| 178 // so we can grab the resulting Document right after load. |
| 179 static_cast<WebDataSourceImpl*>(ds)->setDeferMainResourceDataLoad(false); |
| 180 } |
| 181 |
| 182 void WebEmbeddedWorkerImpl::onScriptLoaderFinished() |
| 183 { |
| 184 ASSERT(m_mainScriptLoader); |
| 185 |
| 186 if (m_mainScriptLoader->failed() || m_askedToTerminate) { |
| 187 m_workerContextClient->workerContextFailedToStart(); |
| 188 m_mainScriptLoader.clear(); |
| 189 return; |
| 190 } |
| 191 |
| 192 // FIXME: Create WorkerReportingProxy, set up WorkerThreadStartupData, |
| 193 // create ServiceWorkerThread and start it with m_scripLoader->script(). |
| 194 |
| 195 m_mainScriptLoader.clear(); |
| 196 |
| 197 notImplemented(); |
| 198 } |
| 199 |
| 200 } // namespace blink |
| OLD | NEW |