• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/v8/WorkerScriptController.h"
35 #include "core/dom/ExecutionContext.h"
36 #include "core/dom/MessagePort.h"
37 #include "core/events/MessageEvent.h"
38 #include "core/workers/WorkerGlobalScope.h"
39 #include "modules/push_messaging/PushEvent.h"
40 #include "modules/serviceworkers/FetchEvent.h"
41 #include "modules/serviceworkers/InstallEvent.h"
42 #include "modules/serviceworkers/InstallPhaseEvent.h"
43 #include "modules/serviceworkers/WaitUntilObserver.h"
44 #include "platform/NotImplemented.h"
45 #include "public/web/WebSerializedScriptValue.h"
46 #include "public/web/WebServiceWorkerContextClient.h"
47 #include "web/WebEmbeddedWorkerImpl.h"
48 #include "wtf/Functional.h"
49 #include "wtf/PassOwnPtr.h"
50 
51 using namespace WebCore;
52 
53 namespace blink {
54 
create(WebEmbeddedWorkerImpl & embeddedWorker,ExecutionContext & executionContext,WebServiceWorkerContextClient & client)55 PassOwnPtr<ServiceWorkerGlobalScopeProxy> ServiceWorkerGlobalScopeProxy::create(WebEmbeddedWorkerImpl& embeddedWorker, ExecutionContext& executionContext, WebServiceWorkerContextClient& client)
56 {
57     return adoptPtr(new ServiceWorkerGlobalScopeProxy(embeddedWorker, executionContext, client));
58 }
59 
~ServiceWorkerGlobalScopeProxy()60 ServiceWorkerGlobalScopeProxy::~ServiceWorkerGlobalScopeProxy()
61 {
62 }
63 
dispatchInstallEvent(int eventID)64 void ServiceWorkerGlobalScopeProxy::dispatchInstallEvent(int eventID)
65 {
66     ASSERT(m_workerGlobalScope);
67     RefPtr<WaitUntilObserver> observer = WaitUntilObserver::create(m_workerGlobalScope, WaitUntilObserver::Install, eventID);
68     observer->willDispatchEvent();
69     m_workerGlobalScope->dispatchEvent(InstallEvent::create(EventTypeNames::install, EventInit(), observer));
70     observer->didDispatchEvent();
71 }
72 
dispatchActivateEvent(int eventID)73 void ServiceWorkerGlobalScopeProxy::dispatchActivateEvent(int eventID)
74 {
75     ASSERT(m_workerGlobalScope);
76     RefPtr<WaitUntilObserver> observer = WaitUntilObserver::create(m_workerGlobalScope, WaitUntilObserver::Activate, eventID);
77     observer->willDispatchEvent();
78     m_workerGlobalScope->dispatchEvent(InstallPhaseEvent::create(EventTypeNames::activate, EventInit(), observer));
79     observer->didDispatchEvent();
80 }
81 
dispatchFetchEvent(int eventID,const WebServiceWorkerRequest & webRequest)82 void ServiceWorkerGlobalScopeProxy::dispatchFetchEvent(int eventID, const WebServiceWorkerRequest& webRequest)
83 {
84     ASSERT(m_workerGlobalScope);
85     RefPtr<RespondWithObserver> observer = RespondWithObserver::create(m_workerGlobalScope, eventID);
86     RefPtr<Request> request = Request::create(webRequest);
87     m_workerGlobalScope->dispatchEvent(FetchEvent::create(observer, request));
88     observer->didDispatchEvent();
89 }
90 
dispatchMessageEvent(const WebString & message,const WebMessagePortChannelArray & webChannels)91 void ServiceWorkerGlobalScopeProxy::dispatchMessageEvent(const WebString& message, const WebMessagePortChannelArray& webChannels)
92 {
93     ASSERT(m_workerGlobalScope);
94 
95     OwnPtr<MessagePortArray> ports = MessagePort::toMessagePortArray(m_workerGlobalScope, webChannels);
96     WebSerializedScriptValue value = WebSerializedScriptValue::fromString(message);
97     m_workerGlobalScope->dispatchEvent(MessageEvent::create(ports.release(), value));
98 }
99 
dispatchPushEvent(int eventID,const WebString & data)100 void ServiceWorkerGlobalScopeProxy::dispatchPushEvent(int eventID, const WebString& data)
101 {
102     ASSERT(m_workerGlobalScope);
103     m_workerGlobalScope->dispatchEvent(PushEvent::create(EventTypeNames::push, data));
104 }
105 
dispatchSyncEvent(int eventID)106 void ServiceWorkerGlobalScopeProxy::dispatchSyncEvent(int eventID)
107 {
108     ASSERT(m_workerGlobalScope);
109     m_workerGlobalScope->dispatchEvent(Event::create(EventTypeNames::sync));
110     ServiceWorkerGlobalScopeClient::from(m_workerGlobalScope)->didHandleSyncEvent(eventID);
111 }
112 
reportException(const String & errorMessage,int lineNumber,int columnNumber,const String & sourceURL)113 void ServiceWorkerGlobalScopeProxy::reportException(const String& errorMessage, int lineNumber, int columnNumber, const String& sourceURL)
114 {
115     m_client.reportException(errorMessage, lineNumber, columnNumber, sourceURL);
116 }
117 
reportConsoleMessage(MessageSource source,MessageLevel level,const String & message,int lineNumber,const String & sourceURL)118 void ServiceWorkerGlobalScopeProxy::reportConsoleMessage(MessageSource source, MessageLevel level, const String& message, int lineNumber, const String& sourceURL)
119 {
120     m_client.reportConsoleMessage(source, level, message, lineNumber, sourceURL);
121 }
122 
postMessageToPageInspector(const String & message)123 void ServiceWorkerGlobalScopeProxy::postMessageToPageInspector(const String& message)
124 {
125     m_client.dispatchDevToolsMessage(message);
126 }
127 
updateInspectorStateCookie(const String & message)128 void ServiceWorkerGlobalScopeProxy::updateInspectorStateCookie(const String& message)
129 {
130     m_client.saveDevToolsAgentState(message);
131 }
132 
workerGlobalScopeStarted(WorkerGlobalScope * workerGlobalScope)133 void ServiceWorkerGlobalScopeProxy::workerGlobalScopeStarted(WorkerGlobalScope* workerGlobalScope)
134 {
135     ASSERT(!m_workerGlobalScope);
136     m_workerGlobalScope = workerGlobalScope;
137     m_client.workerContextStarted(this);
138 }
139 
workerGlobalScopeClosed()140 void ServiceWorkerGlobalScopeProxy::workerGlobalScopeClosed()
141 {
142     m_executionContext.postTask(bind(&WebEmbeddedWorkerImpl::terminateWorkerContext, &m_embeddedWorker));
143 }
144 
willDestroyWorkerGlobalScope()145 void ServiceWorkerGlobalScopeProxy::willDestroyWorkerGlobalScope()
146 {
147     m_workerGlobalScope = 0;
148     m_client.willDestroyWorkerContext();
149 }
150 
workerGlobalScopeDestroyed()151 void ServiceWorkerGlobalScopeProxy::workerGlobalScopeDestroyed()
152 {
153     m_client.workerContextDestroyed();
154 }
155 
ServiceWorkerGlobalScopeProxy(WebEmbeddedWorkerImpl & embeddedWorker,ExecutionContext & executionContext,WebServiceWorkerContextClient & client)156 ServiceWorkerGlobalScopeProxy::ServiceWorkerGlobalScopeProxy(WebEmbeddedWorkerImpl& embeddedWorker, ExecutionContext& executionContext, WebServiceWorkerContextClient& client)
157     : m_embeddedWorker(embeddedWorker)
158     , m_executionContext(executionContext)
159     , m_client(client)
160     , m_workerGlobalScope(0)
161 {
162 }
163 
164 } // namespace blink
165