OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 23 matching lines...) Expand all Loading... |
34 #include "core/dom/ExceptionCode.h" | 34 #include "core/dom/ExceptionCode.h" |
35 #include "core/dom/MessagePort.h" | 35 #include "core/dom/MessagePort.h" |
36 #include "core/events/Event.h" | 36 #include "core/events/Event.h" |
37 #include "core/inspector/ConsoleMessage.h" | 37 #include "core/inspector/ConsoleMessage.h" |
38 #include "modules/EventTargetModules.h" | 38 #include "modules/EventTargetModules.h" |
39 #include "modules/serviceworkers/ServiceWorkerContainerClient.h" | 39 #include "modules/serviceworkers/ServiceWorkerContainerClient.h" |
40 #include "public/platform/WebMessagePortChannel.h" | 40 #include "public/platform/WebMessagePortChannel.h" |
41 #include "public/platform/WebSecurityOrigin.h" | 41 #include "public/platform/WebSecurityOrigin.h" |
42 #include "public/platform/WebString.h" | 42 #include "public/platform/WebString.h" |
43 #include "public/platform/modules/serviceworker/WebServiceWorkerState.h" | 43 #include "public/platform/modules/serviceworker/WebServiceWorkerState.h" |
44 #include <memory> | |
45 | 44 |
46 namespace blink { | 45 namespace blink { |
47 | 46 |
48 const AtomicString& ServiceWorker::interfaceName() const | 47 const AtomicString& ServiceWorker::interfaceName() const |
49 { | 48 { |
50 return EventTargetNames::ServiceWorker; | 49 return EventTargetNames::ServiceWorker; |
51 } | 50 } |
52 | 51 |
53 void ServiceWorker::postMessage(ExecutionContext* context, PassRefPtr<Serialized
ScriptValue> message, const MessagePortArray& ports, ExceptionState& exceptionSt
ate) | 52 void ServiceWorker::postMessage(ExecutionContext* context, PassRefPtr<Serialized
ScriptValue> message, const MessagePortArray& ports, ExceptionState& exceptionSt
ate) |
54 { | 53 { |
55 ServiceWorkerContainerClient* client = ServiceWorkerContainerClient::from(ge
tExecutionContext()); | 54 ServiceWorkerContainerClient* client = ServiceWorkerContainerClient::from(ge
tExecutionContext()); |
56 if (!client || !client->provider()) { | 55 if (!client || !client->provider()) { |
57 exceptionState.throwDOMException(InvalidStateError, "Failed to post a me
ssage: No associated provider is available."); | 56 exceptionState.throwDOMException(InvalidStateError, "Failed to post a me
ssage: No associated provider is available."); |
58 return; | 57 return; |
59 } | 58 } |
60 | 59 |
61 // Disentangle the port in preparation for sending it to the remote context. | 60 // Disentangle the port in preparation for sending it to the remote context. |
62 std::unique_ptr<MessagePortChannelArray> channels = MessagePort::disentangle
Ports(context, ports, exceptionState); | 61 OwnPtr<MessagePortChannelArray> channels = MessagePort::disentanglePorts(con
text, ports, exceptionState); |
63 if (exceptionState.hadException()) | 62 if (exceptionState.hadException()) |
64 return; | 63 return; |
65 if (m_handle->serviceWorker()->state() == WebServiceWorkerStateRedundant) { | 64 if (m_handle->serviceWorker()->state() == WebServiceWorkerStateRedundant) { |
66 exceptionState.throwDOMException(InvalidStateError, "ServiceWorker is in
redundant state."); | 65 exceptionState.throwDOMException(InvalidStateError, "ServiceWorker is in
redundant state."); |
67 return; | 66 return; |
68 } | 67 } |
69 | 68 |
70 if (message->containsTransferableArrayBuffer()) | 69 if (message->containsTransferableArrayBuffer()) |
71 context->addConsoleMessage(ConsoleMessage::create(JSMessageSource, Warni
ngMessageLevel, "ServiceWorker cannot send an ArrayBuffer as a transferable obje
ct yet. See http://crbug.com/511119")); | 70 context->addConsoleMessage(ConsoleMessage::create(JSMessageSource, Warni
ngMessageLevel, "ServiceWorker cannot send an ArrayBuffer as a transferable obje
ct yet. See http://crbug.com/511119")); |
72 | 71 |
73 WebString messageString = message->toWireString(); | 72 WebString messageString = message->toWireString(); |
74 std::unique_ptr<WebMessagePortChannelArray> webChannels = MessagePort::toWeb
MessagePortChannelArray(std::move(channels)); | 73 OwnPtr<WebMessagePortChannelArray> webChannels = MessagePort::toWebMessagePo
rtChannelArray(std::move(channels)); |
75 m_handle->serviceWorker()->postMessage(client->provider(), messageString, We
bSecurityOrigin(getExecutionContext()->getSecurityOrigin()), webChannels.release
()); | 74 m_handle->serviceWorker()->postMessage(client->provider(), messageString, We
bSecurityOrigin(getExecutionContext()->getSecurityOrigin()), webChannels.leakPtr
()); |
76 } | 75 } |
77 | 76 |
78 void ServiceWorker::internalsTerminate() | 77 void ServiceWorker::internalsTerminate() |
79 { | 78 { |
80 m_handle->serviceWorker()->terminate(); | 79 m_handle->serviceWorker()->terminate(); |
81 } | 80 } |
82 | 81 |
83 void ServiceWorker::dispatchStateChangeEvent() | 82 void ServiceWorker::dispatchStateChangeEvent() |
84 { | 83 { |
85 this->dispatchEvent(Event::create(EventTypeNames::statechange)); | 84 this->dispatchEvent(Event::create(EventTypeNames::statechange)); |
(...skipping 20 matching lines...) Expand all Loading... |
106 case WebServiceWorkerStateActivated: | 105 case WebServiceWorkerStateActivated: |
107 return "activated"; | 106 return "activated"; |
108 case WebServiceWorkerStateRedundant: | 107 case WebServiceWorkerStateRedundant: |
109 return "redundant"; | 108 return "redundant"; |
110 default: | 109 default: |
111 ASSERT_NOT_REACHED(); | 110 ASSERT_NOT_REACHED(); |
112 return nullAtom; | 111 return nullAtom; |
113 } | 112 } |
114 } | 113 } |
115 | 114 |
116 ServiceWorker* ServiceWorker::from(ExecutionContext* executionContext, std::uniq
ue_ptr<WebServiceWorker::Handle> handle) | 115 ServiceWorker* ServiceWorker::from(ExecutionContext* executionContext, PassOwnPt
r<WebServiceWorker::Handle> handle) |
117 { | 116 { |
118 return getOrCreate(executionContext, std::move(handle)); | 117 return getOrCreate(executionContext, std::move(handle)); |
119 } | 118 } |
120 | 119 |
121 bool ServiceWorker::hasPendingActivity() const | 120 bool ServiceWorker::hasPendingActivity() const |
122 { | 121 { |
123 if (m_wasStopped) | 122 if (m_wasStopped) |
124 return false; | 123 return false; |
125 return m_handle->serviceWorker()->state() != WebServiceWorkerStateRedundant; | 124 return m_handle->serviceWorker()->state() != WebServiceWorkerStateRedundant; |
126 } | 125 } |
127 | 126 |
128 void ServiceWorker::stop() | 127 void ServiceWorker::stop() |
129 { | 128 { |
130 m_wasStopped = true; | 129 m_wasStopped = true; |
131 } | 130 } |
132 | 131 |
133 ServiceWorker* ServiceWorker::getOrCreate(ExecutionContext* executionContext, st
d::unique_ptr<WebServiceWorker::Handle> handle) | 132 ServiceWorker* ServiceWorker::getOrCreate(ExecutionContext* executionContext, Pa
ssOwnPtr<WebServiceWorker::Handle> handle) |
134 { | 133 { |
135 if (!handle) | 134 if (!handle) |
136 return nullptr; | 135 return nullptr; |
137 | 136 |
138 ServiceWorker* existingWorker = static_cast<ServiceWorker*>(handle->serviceW
orker()->proxy()); | 137 ServiceWorker* existingWorker = static_cast<ServiceWorker*>(handle->serviceW
orker()->proxy()); |
139 if (existingWorker) { | 138 if (existingWorker) { |
140 ASSERT(existingWorker->getExecutionContext() == executionContext); | 139 ASSERT(existingWorker->getExecutionContext() == executionContext); |
141 return existingWorker; | 140 return existingWorker; |
142 } | 141 } |
143 | 142 |
144 ServiceWorker* newWorker = new ServiceWorker(executionContext, std::move(han
dle)); | 143 ServiceWorker* newWorker = new ServiceWorker(executionContext, std::move(han
dle)); |
145 newWorker->suspendIfNeeded(); | 144 newWorker->suspendIfNeeded(); |
146 return newWorker; | 145 return newWorker; |
147 } | 146 } |
148 | 147 |
149 ServiceWorker::ServiceWorker(ExecutionContext* executionContext, std::unique_ptr
<WebServiceWorker::Handle> handle) | 148 ServiceWorker::ServiceWorker(ExecutionContext* executionContext, PassOwnPtr<WebS
erviceWorker::Handle> handle) |
150 : AbstractWorker(executionContext) | 149 : AbstractWorker(executionContext) |
151 , ActiveScriptWrappable(this) | 150 , ActiveScriptWrappable(this) |
152 , m_handle(std::move(handle)) | 151 , m_handle(std::move(handle)) |
153 , m_wasStopped(false) | 152 , m_wasStopped(false) |
154 { | 153 { |
155 ASSERT(m_handle); | 154 ASSERT(m_handle); |
156 m_handle->serviceWorker()->setProxy(this); | 155 m_handle->serviceWorker()->setProxy(this); |
157 } | 156 } |
158 | 157 |
159 ServiceWorker::~ServiceWorker() | 158 ServiceWorker::~ServiceWorker() |
160 { | 159 { |
161 } | 160 } |
162 | 161 |
163 DEFINE_TRACE(ServiceWorker) | 162 DEFINE_TRACE(ServiceWorker) |
164 { | 163 { |
165 AbstractWorker::trace(visitor); | 164 AbstractWorker::trace(visitor); |
166 } | 165 } |
167 | 166 |
168 } // namespace blink | 167 } // namespace blink |
OLD | NEW |