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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 #include "modules/serviceworkers/ServiceWorkerRegistration.h" | 47 #include "modules/serviceworkers/ServiceWorkerRegistration.h" |
48 #include "platform/RuntimeEnabledFeatures.h" | 48 #include "platform/RuntimeEnabledFeatures.h" |
49 #include "public/platform/WebServiceWorker.h" | 49 #include "public/platform/WebServiceWorker.h" |
50 #include "public/platform/WebServiceWorkerProvider.h" | 50 #include "public/platform/WebServiceWorkerProvider.h" |
51 #include "public/platform/WebServiceWorkerRegistration.h" | 51 #include "public/platform/WebServiceWorkerRegistration.h" |
52 #include "public/platform/WebString.h" | 52 #include "public/platform/WebString.h" |
53 #include "public/platform/WebURL.h" | 53 #include "public/platform/WebURL.h" |
54 | 54 |
55 namespace blink { | 55 namespace blink { |
56 | 56 |
| 57 // This wraps CallbackPromiseAdapter and resolves the promise with undefined |
| 58 // when nullptr is given to onSuccess. |
| 59 class GetRegistrationCallback : public WebServiceWorkerProvider::WebServiceWorke
rGetRegistrationCallbacks { |
| 60 public: |
| 61 explicit GetRegistrationCallback(PassRefPtr<ScriptPromiseResolver> resolver) |
| 62 : m_resolver(resolver) |
| 63 , m_adapter(m_resolver) { } |
| 64 virtual ~GetRegistrationCallback() { } |
| 65 virtual void onSuccess(WebServiceWorkerRegistration* registration) OVERRIDE |
| 66 { |
| 67 if (registration) |
| 68 m_adapter.onSuccess(registration); |
| 69 else if (m_resolver->executionContext() && !m_resolver->executionContext
()->activeDOMObjectsAreStopped()) |
| 70 m_resolver->resolve(); |
| 71 } |
| 72 virtual void onError(WebServiceWorkerError* error) OVERRIDE { m_adapter.onEr
ror(error); } |
| 73 private: |
| 74 RefPtr<ScriptPromiseResolver> m_resolver; |
| 75 CallbackPromiseAdapter<ServiceWorkerRegistration, ServiceWorkerError> m_adap
ter; |
| 76 WTF_MAKE_NONCOPYABLE(GetRegistrationCallback); |
| 77 }; |
| 78 |
57 ServiceWorkerContainer* ServiceWorkerContainer::create(ExecutionContext* executi
onContext) | 79 ServiceWorkerContainer* ServiceWorkerContainer::create(ExecutionContext* executi
onContext) |
58 { | 80 { |
59 return new ServiceWorkerContainer(executionContext); | 81 return new ServiceWorkerContainer(executionContext); |
60 } | 82 } |
61 | 83 |
62 ServiceWorkerContainer::~ServiceWorkerContainer() | 84 ServiceWorkerContainer::~ServiceWorkerContainer() |
63 { | 85 { |
64 ASSERT(!m_provider); | 86 ASSERT(!m_provider); |
65 } | 87 } |
66 | 88 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 KURL patternURL = scriptState->executionContext()->completeURL(pattern); | 178 KURL patternURL = scriptState->executionContext()->completeURL(pattern); |
157 patternURL.removeFragmentIdentifier(); | 179 patternURL.removeFragmentIdentifier(); |
158 if (!pattern.isEmpty() && !documentOrigin->canRequest(patternURL)) { | 180 if (!pattern.isEmpty() && !documentOrigin->canRequest(patternURL)) { |
159 resolver->reject(DOMException::create(SecurityError, "The scope must mat
ch the current origin.")); | 181 resolver->reject(DOMException::create(SecurityError, "The scope must mat
ch the current origin.")); |
160 return promise; | 182 return promise; |
161 } | 183 } |
162 m_provider->unregisterServiceWorker(patternURL, new CallbackPromiseAdapter<B
ooleanValue, ServiceWorkerError>(resolver)); | 184 m_provider->unregisterServiceWorker(patternURL, new CallbackPromiseAdapter<B
ooleanValue, ServiceWorkerError>(resolver)); |
163 return promise; | 185 return promise; |
164 } | 186 } |
165 | 187 |
| 188 ScriptPromise ServiceWorkerContainer::getRegistration(ScriptState* scriptState,
const String& documentURL) |
| 189 { |
| 190 ASSERT(RuntimeEnabledFeatures::serviceWorkerEnabled()); |
| 191 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState); |
| 192 ScriptPromise promise = resolver->promise(); |
| 193 |
| 194 // FIXME: This should use the container's execution context, not |
| 195 // the callers. |
| 196 ExecutionContext* executionContext = scriptState->executionContext(); |
| 197 RefPtr<SecurityOrigin> documentOrigin = executionContext->securityOrigin(); |
| 198 String errorMessage; |
| 199 if (!documentOrigin->canAccessFeatureRequiringSecureOrigin(errorMessage)) { |
| 200 resolver->reject(DOMException::create(NotSupportedError, errorMessage)); |
| 201 return promise; |
| 202 } |
| 203 |
| 204 KURL completedURL = executionContext->completeURL(documentURL); |
| 205 if (!documentOrigin->canRequest(completedURL)) { |
| 206 resolver->reject(DOMException::create(SecurityError, "The documentURL mu
st match the current origin.")); |
| 207 return promise; |
| 208 } |
| 209 m_provider->getRegistration(completedURL, new GetRegistrationCallback(resolv
er)); |
| 210 |
| 211 return promise; |
| 212 } |
| 213 |
166 ServiceWorkerContainer::ReadyProperty* ServiceWorkerContainer::createReadyProper
ty() | 214 ServiceWorkerContainer::ReadyProperty* ServiceWorkerContainer::createReadyProper
ty() |
167 { | 215 { |
168 return new ReadyProperty(executionContext(), this, ReadyProperty::Ready); | 216 return new ReadyProperty(executionContext(), this, ReadyProperty::Ready); |
169 } | 217 } |
170 | 218 |
171 ScriptPromise ServiceWorkerContainer::ready(ScriptState* callerState) | 219 ScriptPromise ServiceWorkerContainer::ready(ScriptState* callerState) |
172 { | 220 { |
173 if (!executionContext()) | 221 if (!executionContext()) |
174 return ScriptPromise(); | 222 return ScriptPromise(); |
175 | 223 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 m_ready = createReadyProperty(); | 295 m_ready = createReadyProperty(); |
248 | 296 |
249 if (ServiceWorkerContainerClient* client = ServiceWorkerContainerClient::fro
m(executionContext)) { | 297 if (ServiceWorkerContainerClient* client = ServiceWorkerContainerClient::fro
m(executionContext)) { |
250 m_provider = client->provider(); | 298 m_provider = client->provider(); |
251 if (m_provider) | 299 if (m_provider) |
252 m_provider->setClient(this); | 300 m_provider->setClient(this); |
253 } | 301 } |
254 } | 302 } |
255 | 303 |
256 } // namespace blink | 304 } // namespace blink |
OLD | NEW |