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> |
44 | 45 |
45 namespace blink { | 46 namespace blink { |
46 | 47 |
47 const AtomicString& ServiceWorker::interfaceName() const | 48 const AtomicString& ServiceWorker::interfaceName() const |
48 { | 49 { |
49 return EventTargetNames::ServiceWorker; | 50 return EventTargetNames::ServiceWorker; |
50 } | 51 } |
51 | 52 |
52 void ServiceWorker::postMessage(ExecutionContext* context, PassRefPtr<Serialized
ScriptValue> message, const MessagePortArray& ports, ExceptionState& exceptionSt
ate) | 53 void ServiceWorker::postMessage(ExecutionContext* context, PassRefPtr<Serialized
ScriptValue> message, const MessagePortArray& ports, ExceptionState& exceptionSt
ate) |
53 { | 54 { |
54 ServiceWorkerContainerClient* client = ServiceWorkerContainerClient::from(ge
tExecutionContext()); | 55 ServiceWorkerContainerClient* client = ServiceWorkerContainerClient::from(ge
tExecutionContext()); |
55 if (!client || !client->provider()) { | 56 if (!client || !client->provider()) { |
56 exceptionState.throwDOMException(InvalidStateError, "Failed to post a me
ssage: No associated provider is available."); | 57 exceptionState.throwDOMException(InvalidStateError, "Failed to post a me
ssage: No associated provider is available."); |
57 return; | 58 return; |
58 } | 59 } |
59 | 60 |
60 // Disentangle the port in preparation for sending it to the remote context. | 61 // Disentangle the port in preparation for sending it to the remote context. |
61 OwnPtr<MessagePortChannelArray> channels = MessagePort::disentanglePorts(con
text, ports, exceptionState); | 62 std::unique_ptr<MessagePortChannelArray> channels = MessagePort::disentangle
Ports(context, ports, exceptionState); |
62 if (exceptionState.hadException()) | 63 if (exceptionState.hadException()) |
63 return; | 64 return; |
64 if (m_handle->serviceWorker()->state() == WebServiceWorkerStateRedundant) { | 65 if (m_handle->serviceWorker()->state() == WebServiceWorkerStateRedundant) { |
65 exceptionState.throwDOMException(InvalidStateError, "ServiceWorker is in
redundant state."); | 66 exceptionState.throwDOMException(InvalidStateError, "ServiceWorker is in
redundant state."); |
66 return; | 67 return; |
67 } | 68 } |
68 | 69 |
69 if (message->containsTransferableArrayBuffer()) | 70 if (message->containsTransferableArrayBuffer()) |
70 context->addConsoleMessage(ConsoleMessage::create(JSMessageSource, Warni
ngMessageLevel, "ServiceWorker cannot send an ArrayBuffer as a transferable obje
ct yet. See http://crbug.com/511119")); | 71 context->addConsoleMessage(ConsoleMessage::create(JSMessageSource, Warni
ngMessageLevel, "ServiceWorker cannot send an ArrayBuffer as a transferable obje
ct yet. See http://crbug.com/511119")); |
71 | 72 |
72 WebString messageString = message->toWireString(); | 73 WebString messageString = message->toWireString(); |
73 OwnPtr<WebMessagePortChannelArray> webChannels = MessagePort::toWebMessagePo
rtChannelArray(std::move(channels)); | 74 std::unique_ptr<WebMessagePortChannelArray> webChannels = MessagePort::toWeb
MessagePortChannelArray(std::move(channels)); |
74 m_handle->serviceWorker()->postMessage(client->provider(), messageString, We
bSecurityOrigin(getExecutionContext()->getSecurityOrigin()), webChannels.leakPtr
()); | 75 m_handle->serviceWorker()->postMessage(client->provider(), messageString, We
bSecurityOrigin(getExecutionContext()->getSecurityOrigin()), webChannels.release
()); |
75 } | 76 } |
76 | 77 |
77 void ServiceWorker::internalsTerminate() | 78 void ServiceWorker::internalsTerminate() |
78 { | 79 { |
79 m_handle->serviceWorker()->terminate(); | 80 m_handle->serviceWorker()->terminate(); |
80 } | 81 } |
81 | 82 |
82 void ServiceWorker::dispatchStateChangeEvent() | 83 void ServiceWorker::dispatchStateChangeEvent() |
83 { | 84 { |
84 this->dispatchEvent(Event::create(EventTypeNames::statechange)); | 85 this->dispatchEvent(Event::create(EventTypeNames::statechange)); |
(...skipping 20 matching lines...) Expand all Loading... |
105 case WebServiceWorkerStateActivated: | 106 case WebServiceWorkerStateActivated: |
106 return "activated"; | 107 return "activated"; |
107 case WebServiceWorkerStateRedundant: | 108 case WebServiceWorkerStateRedundant: |
108 return "redundant"; | 109 return "redundant"; |
109 default: | 110 default: |
110 ASSERT_NOT_REACHED(); | 111 ASSERT_NOT_REACHED(); |
111 return nullAtom; | 112 return nullAtom; |
112 } | 113 } |
113 } | 114 } |
114 | 115 |
115 ServiceWorker* ServiceWorker::from(ExecutionContext* executionContext, PassOwnPt
r<WebServiceWorker::Handle> handle) | 116 ServiceWorker* ServiceWorker::from(ExecutionContext* executionContext, std::uniq
ue_ptr<WebServiceWorker::Handle> handle) |
116 { | 117 { |
117 return getOrCreate(executionContext, std::move(handle)); | 118 return getOrCreate(executionContext, std::move(handle)); |
118 } | 119 } |
119 | 120 |
120 bool ServiceWorker::hasPendingActivity() const | 121 bool ServiceWorker::hasPendingActivity() const |
121 { | 122 { |
122 if (m_wasStopped) | 123 if (m_wasStopped) |
123 return false; | 124 return false; |
124 return m_handle->serviceWorker()->state() != WebServiceWorkerStateRedundant; | 125 return m_handle->serviceWorker()->state() != WebServiceWorkerStateRedundant; |
125 } | 126 } |
126 | 127 |
127 void ServiceWorker::stop() | 128 void ServiceWorker::stop() |
128 { | 129 { |
129 m_wasStopped = true; | 130 m_wasStopped = true; |
130 } | 131 } |
131 | 132 |
132 ServiceWorker* ServiceWorker::getOrCreate(ExecutionContext* executionContext, Pa
ssOwnPtr<WebServiceWorker::Handle> handle) | 133 ServiceWorker* ServiceWorker::getOrCreate(ExecutionContext* executionContext, st
d::unique_ptr<WebServiceWorker::Handle> handle) |
133 { | 134 { |
134 if (!handle) | 135 if (!handle) |
135 return nullptr; | 136 return nullptr; |
136 | 137 |
137 ServiceWorker* existingWorker = static_cast<ServiceWorker*>(handle->serviceW
orker()->proxy()); | 138 ServiceWorker* existingWorker = static_cast<ServiceWorker*>(handle->serviceW
orker()->proxy()); |
138 if (existingWorker) { | 139 if (existingWorker) { |
139 ASSERT(existingWorker->getExecutionContext() == executionContext); | 140 ASSERT(existingWorker->getExecutionContext() == executionContext); |
140 return existingWorker; | 141 return existingWorker; |
141 } | 142 } |
142 | 143 |
143 ServiceWorker* newWorker = new ServiceWorker(executionContext, std::move(han
dle)); | 144 ServiceWorker* newWorker = new ServiceWorker(executionContext, std::move(han
dle)); |
144 newWorker->suspendIfNeeded(); | 145 newWorker->suspendIfNeeded(); |
145 return newWorker; | 146 return newWorker; |
146 } | 147 } |
147 | 148 |
148 ServiceWorker::ServiceWorker(ExecutionContext* executionContext, PassOwnPtr<WebS
erviceWorker::Handle> handle) | 149 ServiceWorker::ServiceWorker(ExecutionContext* executionContext, std::unique_ptr
<WebServiceWorker::Handle> handle) |
149 : AbstractWorker(executionContext) | 150 : AbstractWorker(executionContext) |
150 , ActiveScriptWrappable(this) | 151 , ActiveScriptWrappable(this) |
151 , m_handle(std::move(handle)) | 152 , m_handle(std::move(handle)) |
152 , m_wasStopped(false) | 153 , m_wasStopped(false) |
153 { | 154 { |
154 ASSERT(m_handle); | 155 ASSERT(m_handle); |
155 m_handle->serviceWorker()->setProxy(this); | 156 m_handle->serviceWorker()->setProxy(this); |
156 } | 157 } |
157 | 158 |
158 ServiceWorker::~ServiceWorker() | 159 ServiceWorker::~ServiceWorker() |
159 { | 160 { |
160 } | 161 } |
161 | 162 |
162 DEFINE_TRACE(ServiceWorker) | 163 DEFINE_TRACE(ServiceWorker) |
163 { | 164 { |
164 AbstractWorker::trace(visitor); | 165 AbstractWorker::trace(visitor); |
165 } | 166 } |
166 | 167 |
167 } // namespace blink | 168 } // namespace blink |
OLD | NEW |