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 "web/ServiceWorkerGlobalScopeProxy.h" 33 34 #include "bindings/core/v8/WorkerScriptController.h" 35 #include "core/dom/CrossThreadTask.h" 36 #include "core/dom/Document.h" 37 #include "core/dom/ExecutionContext.h" 38 #include "core/dom/MessagePort.h" 39 #include "core/events/MessageEvent.h" 40 #include "core/inspector/ConsoleMessage.h" 41 #include "core/workers/WorkerGlobalScope.h" 42 #include "modules/push_messaging/PushEvent.h" 43 #include "modules/serviceworkers/ExtendableEvent.h" 44 #include "modules/serviceworkers/FetchEvent.h" 45 #include "modules/serviceworkers/InstallEvent.h" 46 #include "modules/serviceworkers/WaitUntilObserver.h" 47 #include "platform/RuntimeEnabledFeatures.h" 48 #include "public/platform/WebServiceWorkerRequest.h" 49 #include "public/web/WebSerializedScriptValue.h" 50 #include "public/web/WebServiceWorkerContextClient.h" 51 #include "web/WebEmbeddedWorkerImpl.h" 52 #include "wtf/Functional.h" 53 #include "wtf/PassOwnPtr.h" 54 55 namespace blink { 56 57 PassOwnPtr<ServiceWorkerGlobalScopeProxy> ServiceWorkerGlobalScopeProxy::create(WebEmbeddedWorkerImpl& embeddedWorker, Document& document, WebServiceWorkerContextClient& client) 58 { 59 return adoptPtr(new ServiceWorkerGlobalScopeProxy(embeddedWorker, document, client)); 60 } 61 62 ServiceWorkerGlobalScopeProxy::~ServiceWorkerGlobalScopeProxy() 63 { 64 } 65 66 void ServiceWorkerGlobalScopeProxy::dispatchInstallEvent(int eventID) 67 { 68 ASSERT(m_workerGlobalScope); 69 WaitUntilObserver* observer = WaitUntilObserver::create(m_workerGlobalScope, WaitUntilObserver::Install, eventID); 70 observer->willDispatchEvent(); 71 m_workerGlobalScope->dispatchEvent(InstallEvent::create(EventTypeNames::install, EventInit(), observer)); 72 observer->didDispatchEvent(); 73 } 74 75 void ServiceWorkerGlobalScopeProxy::dispatchActivateEvent(int eventID) 76 { 77 ASSERT(m_workerGlobalScope); 78 WaitUntilObserver* observer = WaitUntilObserver::create(m_workerGlobalScope, WaitUntilObserver::Activate, eventID); 79 observer->willDispatchEvent(); 80 m_workerGlobalScope->dispatchEvent(ExtendableEvent::create(EventTypeNames::activate, EventInit(), observer)); 81 observer->didDispatchEvent(); 82 } 83 84 void ServiceWorkerGlobalScopeProxy::dispatchFetchEvent(int eventID, const WebServiceWorkerRequest& webRequest) 85 { 86 ASSERT(m_workerGlobalScope); 87 RespondWithObserver* observer = RespondWithObserver::create(m_workerGlobalScope, eventID); 88 if (!RuntimeEnabledFeatures::serviceWorkerOnFetchEnabled()) { 89 observer->didDispatchEvent(); 90 return; 91 } 92 93 Request* request = Request::create(m_workerGlobalScope, webRequest); 94 RefPtrWillBeRawPtr<FetchEvent> fetchEvent(FetchEvent::create(observer, request)); 95 fetchEvent->setIsReload(webRequest.isReload()); 96 m_workerGlobalScope->dispatchEvent(fetchEvent.release()); 97 observer->didDispatchEvent(); 98 } 99 100 void ServiceWorkerGlobalScopeProxy::dispatchMessageEvent(const WebString& message, const WebMessagePortChannelArray& webChannels) 101 { 102 ASSERT(m_workerGlobalScope); 103 104 OwnPtrWillBeRawPtr<MessagePortArray> ports = MessagePort::toMessagePortArray(m_workerGlobalScope, webChannels); 105 WebSerializedScriptValue value = WebSerializedScriptValue::fromString(message); 106 m_workerGlobalScope->dispatchEvent(MessageEvent::create(ports.release(), value)); 107 } 108 109 void ServiceWorkerGlobalScopeProxy::dispatchPushEvent(int eventID, const WebString& data) 110 { 111 ASSERT(m_workerGlobalScope); 112 m_workerGlobalScope->dispatchEvent(PushEvent::create(EventTypeNames::push, data)); 113 } 114 115 void ServiceWorkerGlobalScopeProxy::dispatchSyncEvent(int eventID) 116 { 117 ASSERT(m_workerGlobalScope); 118 if (RuntimeEnabledFeatures::backgroundSyncEnabled()) 119 m_workerGlobalScope->dispatchEvent(Event::create(EventTypeNames::sync)); 120 ServiceWorkerGlobalScopeClient::from(m_workerGlobalScope)->didHandleSyncEvent(eventID); 121 } 122 123 void ServiceWorkerGlobalScopeProxy::reportException(const String& errorMessage, int lineNumber, int columnNumber, const String& sourceURL) 124 { 125 m_client.reportException(errorMessage, lineNumber, columnNumber, sourceURL); 126 } 127 128 void ServiceWorkerGlobalScopeProxy::reportConsoleMessage(PassRefPtrWillBeRawPtr<ConsoleMessage> consoleMessage) 129 { 130 m_client.reportConsoleMessage(consoleMessage->source(), consoleMessage->level(), consoleMessage->message(), consoleMessage->lineNumber(), consoleMessage->url()); 131 } 132 133 void ServiceWorkerGlobalScopeProxy::postMessageToPageInspector(const String& message) 134 { 135 m_document.postInspectorTask(createCrossThreadTask(&WebEmbeddedWorkerImpl::postMessageToPageInspector, &m_embeddedWorker, message)); 136 } 137 138 void ServiceWorkerGlobalScopeProxy::updateInspectorStateCookie(const String& message) 139 { 140 // The inspector cookie saving/restoring is controlled from the main thread. 141 // This method could be removed once shared workers are moved to the main thread inspection as well. 142 } 143 144 void ServiceWorkerGlobalScopeProxy::workerGlobalScopeStarted(WorkerGlobalScope* workerGlobalScope) 145 { 146 ASSERT(!m_workerGlobalScope); 147 m_workerGlobalScope = workerGlobalScope; 148 m_client.workerContextStarted(this); 149 } 150 151 void ServiceWorkerGlobalScopeProxy::workerGlobalScopeClosed() 152 { 153 m_document.postTask(createCrossThreadTask(&WebEmbeddedWorkerImpl::terminateWorkerContext, &m_embeddedWorker)); 154 } 155 156 void ServiceWorkerGlobalScopeProxy::willDestroyWorkerGlobalScope() 157 { 158 m_workerGlobalScope = 0; 159 m_client.willDestroyWorkerContext(); 160 } 161 162 void ServiceWorkerGlobalScopeProxy::workerThreadTerminated() 163 { 164 m_client.workerContextDestroyed(); 165 } 166 167 ServiceWorkerGlobalScopeProxy::ServiceWorkerGlobalScopeProxy(WebEmbeddedWorkerImpl& embeddedWorker, Document& document, WebServiceWorkerContextClient& client) 168 : m_embeddedWorker(embeddedWorker) 169 , m_document(document) 170 , m_client(client) 171 , m_workerGlobalScope(0) 172 { 173 } 174 175 } // namespace blink 176