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 "WebSharedWorkerImpl.h" 33 34 #include "GenericWorkerTask.h" 35 #include "KURL.h" 36 #include "MessageEvent.h" 37 #include "MessagePortChannel.h" 38 #include "PlatformMessagePortChannel.h" 39 #include "ScriptExecutionContext.h" 40 #include "SharedWorkerContext.h" 41 #include "SharedWorkerThread.h" 42 43 #include "WebMessagePortChannel.h" 44 #include "WebString.h" 45 #include "WebURL.h" 46 47 using namespace WebCore; 48 49 namespace WebKit { 50 51 #if ENABLE(SHARED_WORKERS) 52 53 WebSharedWorkerImpl::WebSharedWorkerImpl(WebCommonWorkerClient* client) 54 : m_client(client) 55 { 56 } 57 58 WebSharedWorkerImpl::~WebSharedWorkerImpl() 59 { 60 } 61 62 bool WebSharedWorkerImpl::isStarted() 63 { 64 // Should not ever be called from the worker thread (this API is only called on WebSharedWorkerProxy on the renderer thread). 65 ASSERT_NOT_REACHED(); 66 return workerThread(); 67 } 68 69 void WebSharedWorkerImpl::connect(WebMessagePortChannel* webChannel, ConnectListener* listener) 70 { 71 // Convert the WebMessagePortChanel to a WebCore::MessagePortChannel. 72 RefPtr<PlatformMessagePortChannel> platform_channel = 73 PlatformMessagePortChannel::create(webChannel); 74 webChannel->setClient(platform_channel.get()); 75 OwnPtr<MessagePortChannel> channel = 76 MessagePortChannel::create(platform_channel); 77 78 workerThread()->runLoop().postTask( 79 createCallbackTask(&connectTask, this, channel.release())); 80 if (listener) 81 listener->connected(); 82 } 83 84 void WebSharedWorkerImpl::connectTask(ScriptExecutionContext* context, WebSharedWorkerImpl* worker, PassOwnPtr<MessagePortChannel> channel) 85 { 86 // Wrap the passed-in channel in a MessagePort, and send it off via a connect event. 87 RefPtr<MessagePort> port = MessagePort::create(*context); 88 port->entangle(channel.release()); 89 ASSERT(context->isWorkerContext()); 90 WorkerContext* workerContext = static_cast<WorkerContext*>(context); 91 ASSERT(workerContext->isSharedWorkerContext()); 92 workerContext->toSharedWorkerContext()->dispatchEvent(createConnectEvent(port)); 93 } 94 95 void WebSharedWorkerImpl::startWorkerContext(const WebURL& url, const WebString& name, const WebString& userAgent, const WebString& sourceCode) 96 { 97 initializeLoader(url); 98 setWorkerThread(SharedWorkerThread::create(name, url, userAgent, sourceCode, *this, *this)); 99 workerThread()->start(); 100 } 101 102 void WebSharedWorkerImpl::terminateWorkerContext() 103 { 104 stopWorkerThread(); 105 } 106 107 void WebSharedWorkerImpl::clientDestroyed() 108 { 109 m_client = 0; 110 } 111 112 WebWorkerClient* WebSharedWorkerImpl::client() 113 { 114 // We should never be asked for a WebWorkerClient (only dedicated workers have an associated WebWorkerClient). 115 // It should not be possible for SharedWorkerContext to generate an API call outside those supported by WebCommonWorkerClient. 116 ASSERT_NOT_REACHED(); 117 return 0; 118 } 119 120 WebSharedWorker* WebSharedWorker::create(WebCommonWorkerClient* client) 121 { 122 return new WebSharedWorkerImpl(client); 123 } 124 125 #endif // ENABLE(SHARED_WORKERS) 126 127 } // namespace WebKit 128