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" | 11 #include "core/workers/WorkerGlobalScope.h" |
12 #include "core/workers/WorkerLocation.h" | 12 #include "core/workers/WorkerLocation.h" |
13 #include "modules/serviceworkers/ServiceWorkerError.h" | 13 #include "modules/serviceworkers/ServiceWorkerError.h" |
14 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" | 14 #include "modules/serviceworkers/ServiceWorkerGlobalScopeClient.h" |
15 #include "modules/serviceworkers/ServiceWorkerWindowClient.h" | 15 #include "modules/serviceworkers/ServiceWorkerWindowClient.h" |
| 16 #include "public/platform/WebServiceWorkerClientQueryOptions.h" |
16 #include "public/platform/WebServiceWorkerClientsInfo.h" | 17 #include "public/platform/WebServiceWorkerClientsInfo.h" |
17 #include "wtf/RefPtr.h" | 18 #include "wtf/RefPtr.h" |
18 #include "wtf/Vector.h" | 19 #include "wtf/Vector.h" |
19 | 20 |
20 namespace blink { | 21 namespace blink { |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 class ClientArray { | 25 class ClientArray { |
25 public: | 26 public: |
26 typedef blink::WebServiceWorkerClientsInfo WebType; | 27 typedef blink::WebServiceWorkerClientsInfo WebType; |
27 static HeapVector<Member<ServiceWorkerClient>> take(ScriptPromiseResolve
r*, WebType* webClientsRaw) | 28 static HeapVector<Member<ServiceWorkerClient>> take(ScriptPromiseResolver*,
WebType* webClientsRaw) |
28 { | 29 { |
29 OwnPtr<WebType> webClients = adoptPtr(webClientsRaw); | 30 OwnPtr<WebType> webClients = adoptPtr(webClientsRaw); |
30 HeapVector<Member<ServiceWorkerClient>> clients; | 31 HeapVector<Member<ServiceWorkerClient>> clients; |
31 for (size_t i = 0; i < webClients->clients.size(); ++i) { | 32 for (size_t i = 0; i < webClients->clients.size(); ++i) { |
32 clients.append(ServiceWorkerWindowClient::create(webClients->cli
ents[i])); | 33 // FIXME: For now we only support getting "window" type clients. |
33 } | 34 ASSERT(webClients->clients[i].clientType == WebServiceWorkerClientTy
peWindow); |
34 return clients; | 35 clients.append(ServiceWorkerWindowClient::create(webClients->clients
[i])); |
35 } | 36 } |
36 static void dispose(WebType* webClientsRaw) | 37 return clients; |
37 { | 38 } |
38 delete webClientsRaw; | 39 static void dispose(WebType* webClientsRaw) |
39 } | 40 { |
| 41 delete webClientsRaw; |
| 42 } |
40 | 43 |
41 private: | 44 private: |
42 WTF_MAKE_NONCOPYABLE(ClientArray); | 45 WTF_MAKE_NONCOPYABLE(ClientArray); |
43 ClientArray() = delete; | 46 ClientArray() = delete; |
44 }; | 47 }; |
| 48 |
| 49 WebServiceWorkerClientType getClientType(const String& type) |
| 50 { |
| 51 if (type == "window") |
| 52 return WebServiceWorkerClientTypeWindow; |
| 53 if (type == "worker") |
| 54 return WebServiceWorkerClientTypeWorker; |
| 55 if (type == "sharedworker") |
| 56 return WebServiceWorkerClientTypeSharedWorker; |
| 57 if (type == "all") |
| 58 return WebServiceWorkerClientTypeAll; |
| 59 ASSERT_NOT_REACHED(); |
| 60 return WebServiceWorkerClientTypeWindow; |
| 61 } |
45 | 62 |
46 } // namespace | 63 } // namespace |
47 | 64 |
48 ServiceWorkerClients* ServiceWorkerClients::create() | 65 ServiceWorkerClients* ServiceWorkerClients::create() |
49 { | 66 { |
50 return new ServiceWorkerClients(); | 67 return new ServiceWorkerClients(); |
51 } | 68 } |
52 | 69 |
53 ServiceWorkerClients::ServiceWorkerClients() | 70 ServiceWorkerClients::ServiceWorkerClients() |
54 { | 71 { |
55 } | 72 } |
56 | 73 |
57 ScriptPromise ServiceWorkerClients::matchAll(ScriptState* scriptState, const Cli
entQueryOptions& options) | 74 ScriptPromise ServiceWorkerClients::matchAll(ScriptState* scriptState, const Cli
entQueryOptions& options) |
58 { | 75 { |
59 ExecutionContext* executionContext = scriptState->executionContext(); | 76 ExecutionContext* executionContext = scriptState->executionContext(); |
60 // FIXME: May be null due to worker termination: http://crbug.com/413518. | 77 // FIXME: May be null due to worker termination: http://crbug.com/413518. |
61 if (!executionContext) | 78 if (!executionContext) |
62 return ScriptPromise(); | 79 return ScriptPromise(); |
63 | 80 |
64 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 81 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
65 ScriptPromise promise = resolver->promise(); | 82 ScriptPromise promise = resolver->promise(); |
66 | 83 |
67 if (options.includeUncontrolled()) { | 84 if (options.includeUncontrolled()) { |
68 // FIXME: Currently we don't support includeUncontrolled=true. | 85 // FIXME: Remove this when query options are supported in the embedder. |
69 resolver->reject(DOMException::create(NotSupportedError, "includeUncontr
olled parameter of getAll is not supported.")); | 86 resolver->reject(DOMException::create(NotSupportedError, "includeUncontr
olled parameter of getAll is not supported.")); |
70 return promise; | 87 return promise; |
71 } | 88 } |
72 | 89 |
73 if (options.type() != "window") { | 90 if (options.type() != "window") { |
74 // FIXME: Currently we only support WindowClients. | 91 // FIXME: Remove this when query options are supported in the embedder. |
75 resolver->reject(DOMException::create(NotSupportedError, "type parameter
of getAll is not supported.")); | 92 resolver->reject(DOMException::create(NotSupportedError, "type parameter
of getAll is not supported.")); |
76 return promise; | 93 return promise; |
77 } | 94 } |
78 | 95 |
79 ServiceWorkerGlobalScopeClient::from(executionContext)->getClients(new Callb
ackPromiseAdapter<ClientArray, ServiceWorkerError>(resolver)); | 96 WebServiceWorkerClientQueryOptions webOptions; |
| 97 webOptions.clientType = getClientType(options.type()); |
| 98 webOptions.includeUncontrolled = options.includeUncontrolled(); |
| 99 ServiceWorkerGlobalScopeClient::from(executionContext)->getClients(webOption
s, new CallbackPromiseAdapter<ClientArray, ServiceWorkerError>(resolver)); |
80 return promise; | 100 return promise; |
81 } | 101 } |
82 | 102 |
83 ScriptPromise ServiceWorkerClients::claim(ScriptState* scriptState) | 103 ScriptPromise ServiceWorkerClients::claim(ScriptState* scriptState) |
84 { | 104 { |
85 ExecutionContext* executionContext = scriptState->executionContext(); | 105 ExecutionContext* executionContext = scriptState->executionContext(); |
86 | 106 |
87 // FIXME: May be null due to worker termination: http://crbug.com/413518. | 107 // FIXME: May be null due to worker termination: http://crbug.com/413518. |
88 if (!executionContext) | 108 if (!executionContext) |
89 return ScriptPromise(); | 109 return ScriptPromise(); |
(...skipping 27 matching lines...) Expand all Loading... |
117 resolver->reject(DOMException::create(InvalidAccessError, "Not allowed t
o open a window.")); | 137 resolver->reject(DOMException::create(InvalidAccessError, "Not allowed t
o open a window.")); |
118 return promise; | 138 return promise; |
119 } | 139 } |
120 context->consumeWindowInteraction(); | 140 context->consumeWindowInteraction(); |
121 | 141 |
122 ServiceWorkerGlobalScopeClient::from(context)->openWindow(parsedUrl, new Cal
lbackPromiseAdapter<ServiceWorkerWindowClient, ServiceWorkerError>(resolver)); | 142 ServiceWorkerGlobalScopeClient::from(context)->openWindow(parsedUrl, new Cal
lbackPromiseAdapter<ServiceWorkerWindowClient, ServiceWorkerError>(resolver)); |
123 return promise; | 143 return promise; |
124 } | 144 } |
125 | 145 |
126 } // namespace blink | 146 } // namespace blink |
OLD | NEW |