| 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/ServiceWorkerClients.h" | 6 #include "modules/serviceworkers/ServiceWorkerClients.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 9 #include "bindings/core/v8/ScriptPromiseResolver.h" | 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 10 #include "core/dom/ExceptionCode.h" | 10 #include "core/dom/ExceptionCode.h" |
| 11 #include "core/workers/WorkerGlobalScope.h" |
| 12 #include "core/workers/WorkerLocation.h" |
| 11 #include "modules/serviceworkers/ServiceWorkerError.h" | 13 #include "modules/serviceworkers/ServiceWorkerError.h" |
| 12 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" | 14 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" |
| 13 #include "modules/serviceworkers/ServiceWorkerWindowClient.h" | 15 #include "modules/serviceworkers/ServiceWorkerWindowClient.h" |
| 14 #include "public/platform/WebServiceWorkerClientsInfo.h" | 16 #include "public/platform/WebServiceWorkerClientsInfo.h" |
| 15 #include "wtf/RefPtr.h" | 17 #include "wtf/RefPtr.h" |
| 16 #include "wtf/Vector.h" | 18 #include "wtf/Vector.h" |
| 17 | 19 |
| 18 namespace blink { | 20 namespace blink { |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 36 delete webClientsRaw; | 38 delete webClientsRaw; |
| 37 } | 39 } |
| 38 | 40 |
| 39 private: | 41 private: |
| 40 WTF_MAKE_NONCOPYABLE(ClientArray); | 42 WTF_MAKE_NONCOPYABLE(ClientArray); |
| 41 ClientArray() = delete; | 43 ClientArray() = delete; |
| 42 }; | 44 }; |
| 43 | 45 |
| 44 } // namespace | 46 } // namespace |
| 45 | 47 |
| 48 class ServiceWorkerClients::OpenWindowCallbacks : public WebServiceWorkerOpenWin
dowCallbacks { |
| 49 WTF_MAKE_NONCOPYABLE(OpenWindowCallbacks); |
| 50 public: |
| 51 explicit OpenWindowCallbacks(PassRefPtrWillBeRawPtr<ScriptPromiseResolver> r
esolver) |
| 52 : m_resolver(resolver) { } |
| 53 virtual ~OpenWindowCallbacks() { } |
| 54 |
| 55 void onSuccess(int clientId, const WebURL& url) override |
| 56 { |
| 57 WebServiceWorkerClientInfo info; |
| 58 // FIXME: Payload should include state snapshot of visibility, focused |
| 59 info.clientID = clientId; |
| 60 info.frameType = WebURLRequest::FrameTypeTopLevel; |
| 61 info.url = url; |
| 62 m_resolver->resolve(ServiceWorkerClient::create(info)); |
| 63 m_resolver.clear(); |
| 64 } |
| 65 |
| 66 void onError() override |
| 67 { |
| 68 // FIXME: Route back a useful error message of some sort. |
| 69 m_resolver->reject(DOMException::create(SecurityError, "Failed to open t
he requested URL.")); |
| 70 m_resolver.clear(); |
| 71 } |
| 72 |
| 73 private: |
| 74 RefPtrWillBePersistent<ScriptPromiseResolver> m_resolver; |
| 75 }; |
| 76 |
| 46 ServiceWorkerClients* ServiceWorkerClients::create() | 77 ServiceWorkerClients* ServiceWorkerClients::create() |
| 47 { | 78 { |
| 48 return new ServiceWorkerClients(); | 79 return new ServiceWorkerClients(); |
| 49 } | 80 } |
| 50 | 81 |
| 51 ServiceWorkerClients::ServiceWorkerClients() | 82 ServiceWorkerClients::ServiceWorkerClients() |
| 52 { | 83 { |
| 53 } | 84 } |
| 54 | 85 |
| 55 ScriptPromise ServiceWorkerClients::getAll(ScriptState* scriptState, const Clien
tQueryOptions& options) | 86 ScriptPromise ServiceWorkerClients::getAll(ScriptState* scriptState, const Clien
tQueryOptions& options) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 66 if (options.type() != "window") { | 97 if (options.type() != "window") { |
| 67 // FIXME: Currently we only support WindowClients. | 98 // FIXME: Currently we only support WindowClients. |
| 68 resolver->reject(DOMException::create(NotSupportedError, "type parameter
of getAll is not supported.")); | 99 resolver->reject(DOMException::create(NotSupportedError, "type parameter
of getAll is not supported.")); |
| 69 return promise; | 100 return promise; |
| 70 } | 101 } |
| 71 | 102 |
| 72 ServiceWorkerGlobalScopeClient::from(scriptState->executionContext())->getCl
ients(new CallbackPromiseAdapter<ClientArray, ServiceWorkerError>(resolver)); | 103 ServiceWorkerGlobalScopeClient::from(scriptState->executionContext())->getCl
ients(new CallbackPromiseAdapter<ClientArray, ServiceWorkerError>(resolver)); |
| 73 return promise; | 104 return promise; |
| 74 } | 105 } |
| 75 | 106 |
| 107 ScriptPromise ServiceWorkerClients::openWindow(ScriptState* scriptState, const S
tring& url) |
| 108 { |
| 109 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
| 110 ExecutionContext* context = scriptState->executionContext(); |
| 111 ScriptPromise promise = resolver->promise(); |
| 112 |
| 113 KURL parsedURL = KURL(toWorkerGlobalScope(context)->location()->url(), url); |
| 114 if (!parsedURL.isValid()) { |
| 115 resolver->reject(DOMException::create(SyntaxError, "'" + url + "' is not
a valid URL.")); |
| 116 return promise; |
| 117 } |
| 118 |
| 119 // FIXME: Is this (1) correct and (2) sufficient? |
| 120 if (!context->securityOrigin()->canRequest(parsedURL)) { |
| 121 resolver->reject(DOMException::create(SecurityError, "Can only open docu
ments in the worker's origin.")); |
| 122 return promise; |
| 123 } |
| 124 |
| 125 ServiceWorkerGlobalScopeClient::from(context)->openWindow(parsedURL, new Ope
nWindowCallbacks(resolver)); |
| 126 return promise; |
| 127 } |
| 128 |
| 76 } // namespace blink | 129 } // namespace blink |
| OLD | NEW |