1a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)/*
2a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * Copyright (C) 2013 Google Inc. All rights reserved.
3a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) *
4a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
5a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * modification, are permitted provided that the following conditions are
6a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * met:
7a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) *
8a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
9a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
10a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
11a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
12a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * in the documentation and/or other materials provided with the
13a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * distribution.
14a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
15a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
16a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * this software without specific prior written permission.
17a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) *
18a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles) */
30a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
31a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "config.h"
32f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include "web/ServiceWorkerGlobalScopeProxy.h"
33a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
34197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "bindings/core/v8/WorkerScriptController.h"
35197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "core/dom/CrossThreadTask.h"
369e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)#include "core/dom/Document.h"
37a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "core/dom/ExecutionContext.h"
3807a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch#include "core/dom/MessagePort.h"
3907a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch#include "core/events/MessageEvent.h"
40e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)#include "core/inspector/ConsoleMessage.h"
4109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "core/workers/WorkerGlobalScope.h"
425d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)#include "modules/push_messaging/PushEvent.h"
437242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci#include "modules/serviceworkers/ExtendableEvent.h"
44d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)#include "modules/serviceworkers/FetchEvent.h"
4509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "modules/serviceworkers/InstallEvent.h"
4609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#include "modules/serviceworkers/WaitUntilObserver.h"
477242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci#include "platform/RuntimeEnabledFeatures.h"
48197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch#include "public/platform/WebServiceWorkerRequest.h"
49f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include "public/web/WebSerializedScriptValue.h"
50f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include "public/web/WebServiceWorkerContextClient.h"
51f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include "web/WebEmbeddedWorkerImpl.h"
52a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "wtf/Functional.h"
53a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)#include "wtf/PassOwnPtr.h"
54a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
55a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)namespace blink {
56a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
579e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)PassOwnPtr<ServiceWorkerGlobalScopeProxy> ServiceWorkerGlobalScopeProxy::create(WebEmbeddedWorkerImpl& embeddedWorker, Document& document, WebServiceWorkerContextClient& client)
58a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles){
599e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    return adoptPtr(new ServiceWorkerGlobalScopeProxy(embeddedWorker, document, client));
60a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)}
61a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
62a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)ServiceWorkerGlobalScopeProxy::~ServiceWorkerGlobalScopeProxy()
63a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles){
64a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)}
65a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
6609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)void ServiceWorkerGlobalScopeProxy::dispatchInstallEvent(int eventID)
6709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
6809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    ASSERT(m_workerGlobalScope);
697242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    WaitUntilObserver* observer = WaitUntilObserver::create(m_workerGlobalScope, WaitUntilObserver::Install, eventID);
7009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    observer->willDispatchEvent();
7109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    m_workerGlobalScope->dispatchEvent(InstallEvent::create(EventTypeNames::install, EventInit(), observer));
7209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    observer->didDispatchEvent();
7309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
7409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
7507a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdochvoid ServiceWorkerGlobalScopeProxy::dispatchActivateEvent(int eventID)
7607a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch{
7707a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    ASSERT(m_workerGlobalScope);
787242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    WaitUntilObserver* observer = WaitUntilObserver::create(m_workerGlobalScope, WaitUntilObserver::Activate, eventID);
7907a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    observer->willDispatchEvent();
807242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    m_workerGlobalScope->dispatchEvent(ExtendableEvent::create(EventTypeNames::activate, EventInit(), observer));
8107a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    observer->didDispatchEvent();
8207a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch}
8307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
84f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)void ServiceWorkerGlobalScopeProxy::dispatchFetchEvent(int eventID, const WebServiceWorkerRequest& webRequest)
85d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles){
86d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    ASSERT(m_workerGlobalScope);
877242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    RespondWithObserver* observer = RespondWithObserver::create(m_workerGlobalScope, eventID);
887242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    if (!RuntimeEnabledFeatures::serviceWorkerOnFetchEnabled()) {
897242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        observer->didDispatchEvent();
907242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        return;
917242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    }
927242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci
937242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    Request* request = Request::create(m_workerGlobalScope, webRequest);
94197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    RefPtrWillBeRawPtr<FetchEvent> fetchEvent(FetchEvent::create(observer, request));
95197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    fetchEvent->setIsReload(webRequest.isReload());
96197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    m_workerGlobalScope->dispatchEvent(fetchEvent.release());
97d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    observer->didDispatchEvent();
98d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)}
99d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
10007a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdochvoid ServiceWorkerGlobalScopeProxy::dispatchMessageEvent(const WebString& message, const WebMessagePortChannelArray& webChannels)
10107a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch{
10207a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    ASSERT(m_workerGlobalScope);
10307a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
104197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    OwnPtrWillBeRawPtr<MessagePortArray> ports = MessagePort::toMessagePortArray(m_workerGlobalScope, webChannels);
10507a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    WebSerializedScriptValue value = WebSerializedScriptValue::fromString(message);
10607a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    m_workerGlobalScope->dispatchEvent(MessageEvent::create(ports.release(), value));
10707a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch}
10807a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
1095d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)void ServiceWorkerGlobalScopeProxy::dispatchPushEvent(int eventID, const WebString& data)
1105d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles){
1115d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    ASSERT(m_workerGlobalScope);
1125d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)    m_workerGlobalScope->dispatchEvent(PushEvent::create(EventTypeNames::push, data));
1135d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)}
1145d92fedcae5e801a8b224de090094f2d9df0b54aTorne (Richard Coles)
11507a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdochvoid ServiceWorkerGlobalScopeProxy::dispatchSyncEvent(int eventID)
11607a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch{
11707a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    ASSERT(m_workerGlobalScope);
1187242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    if (RuntimeEnabledFeatures::backgroundSyncEnabled())
1197242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci        m_workerGlobalScope->dispatchEvent(Event::create(EventTypeNames::sync));
12007a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch    ServiceWorkerGlobalScopeClient::from(m_workerGlobalScope)->didHandleSyncEvent(eventID);
12107a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch}
12207a852d8c1953036774d8f3b65d18dcfea3bb4a2Ben Murdoch
123a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)void ServiceWorkerGlobalScopeProxy::reportException(const String& errorMessage, int lineNumber, int columnNumber, const String& sourceURL)
124a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles){
12509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    m_client.reportException(errorMessage, lineNumber, columnNumber, sourceURL);
126a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)}
127a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
128e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)void ServiceWorkerGlobalScopeProxy::reportConsoleMessage(PassRefPtrWillBeRawPtr<ConsoleMessage> consoleMessage)
129a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles){
130e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    m_client.reportConsoleMessage(consoleMessage->source(), consoleMessage->level(), consoleMessage->message(), consoleMessage->lineNumber(), consoleMessage->url());
131a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)}
132a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
133a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)void ServiceWorkerGlobalScopeProxy::postMessageToPageInspector(const String& message)
134a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles){
1359e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    m_document.postInspectorTask(createCrossThreadTask(&WebEmbeddedWorkerImpl::postMessageToPageInspector, &m_embeddedWorker, message));
136a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)}
137a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
138a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)void ServiceWorkerGlobalScopeProxy::updateInspectorStateCookie(const String& message)
139a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles){
1407242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    // The inspector cookie saving/restoring is controlled from the main thread.
1417242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    // This method could be removed once shared workers are moved to the main thread inspection as well.
142a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)}
143a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
14409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)void ServiceWorkerGlobalScopeProxy::workerGlobalScopeStarted(WorkerGlobalScope* workerGlobalScope)
145a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles){
14609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    ASSERT(!m_workerGlobalScope);
14709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    m_workerGlobalScope = workerGlobalScope;
14809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    m_client.workerContextStarted(this);
149a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)}
150a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
151a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)void ServiceWorkerGlobalScopeProxy::workerGlobalScopeClosed()
152a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles){
1539e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    m_document.postTask(createCrossThreadTask(&WebEmbeddedWorkerImpl::terminateWorkerContext, &m_embeddedWorker));
154a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)}
155a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
15610f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdochvoid ServiceWorkerGlobalScopeProxy::willDestroyWorkerGlobalScope()
157a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles){
15809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    m_workerGlobalScope = 0;
15910f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    m_client.willDestroyWorkerContext();
160a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)}
161a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
162e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)void ServiceWorkerGlobalScopeProxy::workerThreadTerminated()
163323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles){
164323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    m_client.workerContextDestroyed();
165323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)}
166323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)
1679e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)ServiceWorkerGlobalScopeProxy::ServiceWorkerGlobalScopeProxy(WebEmbeddedWorkerImpl& embeddedWorker, Document& document, WebServiceWorkerContextClient& client)
168a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    : m_embeddedWorker(embeddedWorker)
1699e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    , m_document(document)
170a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)    , m_client(client)
17109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    , m_workerGlobalScope(0)
172a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles){
173a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)}
174a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)
175a854de003a23bf3c7f95ec0f8154ada64092ff5cTorne (Richard Coles)} // namespace blink
176