| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 #include "modules/serviceworkers/ServiceWorkerClient.h" | 6 #include "modules/serviceworkers/ServiceWorkerClient.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 9 #include "bindings/core/v8/ExceptionState.h" | 9 #include "bindings/core/v8/ExceptionState.h" |
| 10 #include "bindings/core/v8/ScriptPromiseResolver.h" | 10 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 11 #include "bindings/core/v8/SerializedScriptValue.h" | 11 #include "bindings/core/v8/SerializedScriptValue.h" |
| 12 #include "modules/serviceworkers/ServiceWorkerError.h" | 12 #include "modules/serviceworkers/ServiceWorkerError.h" |
| 13 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" | 13 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" |
| 14 #include "public/platform/WebString.h" | 14 #include "public/platform/WebString.h" |
| 15 #include "wtf/RefPtr.h" | 15 #include "wtf/RefPtr.h" |
| 16 | 16 |
| 17 namespace blink { | 17 namespace blink { |
| 18 | 18 |
| 19 ServiceWorkerClient* ServiceWorkerClient::create(unsigned id) | 19 ServiceWorkerClient* ServiceWorkerClient::create(const WebServiceWorkerClientInf
o& info) |
| 20 { | 20 { |
| 21 return new ServiceWorkerClient(id); | 21 return new ServiceWorkerClient(info); |
| 22 } | 22 } |
| 23 | 23 |
| 24 ServiceWorkerClient::ServiceWorkerClient(unsigned id) | 24 ServiceWorkerClient::ServiceWorkerClient(const WebServiceWorkerClientInfo& info) |
| 25 : m_id(id) | 25 : m_id(info.clientID) |
| 26 , m_visibilityState(info.visibilityState) |
| 27 , m_focused(info.focused) |
| 28 , m_url(info.url.string()) |
| 29 , m_frameType(info.frameType) |
| 26 { | 30 { |
| 27 } | 31 } |
| 28 | 32 |
| 33 ServiceWorkerClient::~ServiceWorkerClient() |
| 34 { |
| 35 } |
| 36 |
| 37 String ServiceWorkerClient::frameType() const |
| 38 { |
| 39 DEFINE_STATIC_LOCAL(const String, auxiliary, ("auxiliary")); |
| 40 DEFINE_STATIC_LOCAL(const String, nested, ("nested")); |
| 41 DEFINE_STATIC_LOCAL(const String, none, ("none")); |
| 42 DEFINE_STATIC_LOCAL(const String, topLevel, ("top-level")); |
| 43 |
| 44 switch (m_frameType) { |
| 45 case WebURLRequest::FrameTypeAuxiliary: |
| 46 return auxiliary; |
| 47 case WebURLRequest::FrameTypeNested: |
| 48 return nested; |
| 49 case WebURLRequest::FrameTypeNone: |
| 50 return none; |
| 51 case WebURLRequest::FrameTypeTopLevel: |
| 52 return topLevel; |
| 53 } |
| 54 |
| 55 ASSERT_NOT_REACHED(); |
| 56 return String(); |
| 57 } |
| 58 |
| 29 void ServiceWorkerClient::postMessage(ExecutionContext* context, PassRefPtr<Seri
alizedScriptValue> message, const MessagePortArray* ports, ExceptionState& excep
tionState) | 59 void ServiceWorkerClient::postMessage(ExecutionContext* context, PassRefPtr<Seri
alizedScriptValue> message, const MessagePortArray* ports, ExceptionState& excep
tionState) |
| 30 { | 60 { |
| 31 // 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. |
| 32 OwnPtr<MessagePortChannelArray> channels = MessagePort::disentanglePorts(por
ts, exceptionState); | 62 OwnPtr<MessagePortChannelArray> channels = MessagePort::disentanglePorts(por
ts, exceptionState); |
| 33 if (exceptionState.hadException()) | 63 if (exceptionState.hadException()) |
| 34 return; | 64 return; |
| 35 | 65 |
| 36 WebString messageString = message->toWireString(); | 66 WebString messageString = message->toWireString(); |
| 37 OwnPtr<WebMessagePortChannelArray> webChannels = MessagePort::toWebMessagePo
rtChannelArray(channels.release()); | 67 OwnPtr<WebMessagePortChannelArray> webChannels = MessagePort::toWebMessagePo
rtChannelArray(channels.release()); |
| 38 ServiceWorkerGlobalScopeClient::from(context)->postMessageToClient(m_id, mes
sageString, webChannels.release()); | 68 ServiceWorkerGlobalScopeClient::from(context)->postMessageToClient(m_id, mes
sageString, webChannels.release()); |
| 39 } | 69 } |
| 40 | 70 |
| 41 ScriptPromise ServiceWorkerClient::focus(ScriptState* scriptState) | 71 ScriptPromise ServiceWorkerClient::focus(ScriptState* scriptState) |
| 42 { | 72 { |
| 43 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); | 73 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); |
| 44 ScriptPromise promise = resolver->promise(); | 74 ScriptPromise promise = resolver->promise(); |
| 45 | 75 |
| 46 // FIXME: there should be a check for whether the caller is allowed to focus | 76 // FIXME: there should be a check for whether the caller is allowed to focus |
| 47 // the client. The promise should succeed with |false| if the call is not | 77 // the client. The promise should succeed with |false| if the call is not |
| 48 // allowed. https://crbug.com/437149 | 78 // allowed. https://crbug.com/437149 |
| 49 | 79 |
| 50 ServiceWorkerGlobalScopeClient::from(scriptState->executionContext())->focus
(m_id, new CallbackPromiseAdapter<bool, ServiceWorkerError>(resolver)); | 80 ServiceWorkerGlobalScopeClient::from(scriptState->executionContext())->focus
(m_id, new CallbackPromiseAdapter<bool, ServiceWorkerError>(resolver)); |
| 51 return promise; | 81 return promise; |
| 52 } | 82 } |
| 53 | 83 |
| 54 } // namespace blink | 84 } // namespace blink |
| OLD | NEW |