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/push_messaging/PushManager.h" | 6 #include "modules/push_messaging/PushManager.h" |
7 | 7 |
8 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
9 #include "bindings/core/v8/ScriptPromise.h" | 9 #include "bindings/core/v8/ScriptPromise.h" |
10 #include "bindings/core/v8/ScriptPromiseResolver.h" | 10 #include "bindings/core/v8/ScriptPromiseResolver.h" |
11 #include "bindings/core/v8/ScriptState.h" | 11 #include "bindings/core/v8/ScriptState.h" |
12 #include "core/dom/DOMException.h" | 12 #include "core/dom/DOMException.h" |
13 #include "core/dom/Document.h" | 13 #include "core/dom/Document.h" |
14 #include "core/dom/ExceptionCode.h" | 14 #include "core/dom/ExceptionCode.h" |
15 #include "core/dom/ExecutionContext.h" | 15 #include "core/dom/ExecutionContext.h" |
16 #include "core/frame/LocalDOMWindow.h" | 16 #include "core/frame/LocalDOMWindow.h" |
Peter Beverloo
2014/12/08 21:40:51
nit: This include seems to be unused.
Michael van Ouwerkerk
2014/12/09 18:47:45
Done.
| |
17 #include "modules/push_messaging/PushController.h" | 17 #include "modules/push_messaging/PushController.h" |
18 #include "modules/push_messaging/PushError.h" | 18 #include "modules/push_messaging/PushError.h" |
19 #include "modules/push_messaging/PushPermissionStatusCallback.h" | 19 #include "modules/push_messaging/PushPermissionStatusCallback.h" |
20 #include "modules/push_messaging/PushRegistration.h" | 20 #include "modules/push_messaging/PushRegistration.h" |
21 #include "modules/serviceworkers/NavigatorServiceWorker.h" | 21 #include "modules/serviceworkers/NavigatorServiceWorker.h" |
Peter Beverloo
2014/12/08 21:40:51
nit: This include seems to be unused.
Michael van Ouwerkerk
2014/12/09 18:47:45
Could have sworn I removed that. Maybe I mishandle
| |
22 #include "modules/serviceworkers/ServiceWorkerContainer.h" | 22 #include "modules/serviceworkers/ServiceWorkerContainer.h" |
Peter Beverloo
2014/12/08 21:40:51
nit: This include seems to be unused.
Michael van Ouwerkerk
2014/12/09 18:47:45
Done.
| |
23 #include "modules/serviceworkers/ServiceWorkerRegistration.h" | |
24 #include "public/platform/Platform.h" | |
23 #include "public/platform/WebPushClient.h" | 25 #include "public/platform/WebPushClient.h" |
26 #include "public/platform/WebPushProvider.h" | |
24 #include "wtf/RefPtr.h" | 27 #include "wtf/RefPtr.h" |
25 | 28 |
26 namespace blink { | 29 namespace blink { |
27 | 30 |
28 PushManager::PushManager() | 31 PushManager::PushManager(ServiceWorkerRegistration* registration) |
32 : m_registration(registration) | |
29 { | 33 { |
Peter Beverloo
2014/12/08 21:40:51
ASSERT(registration)
Michael van Ouwerkerk
2014/12/09 18:47:45
Done.
| |
30 } | 34 } |
31 | 35 |
32 // FIXME: This call should be available from workers which will not have a Docum ent object available. | |
33 // See crbug.com/389194 | |
34 ScriptPromise PushManager::registerPushMessaging(ScriptState* scriptState) | 36 ScriptPromise PushManager::registerPushMessaging(ScriptState* scriptState) |
35 { | 37 { |
36 ASSERT(scriptState->executionContext()->isDocument()); | 38 if (scriptState->executionContext()->isDocument()) { |
39 Document* document = toDocument(scriptState->executionContext()); | |
40 if (!document->domWindow() || !document->frame()) | |
41 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti on::create(InvalidStateError, "Document is detached from window.")); | |
37 | 42 |
38 Document* document = toDocument(scriptState->executionContext()); | 43 WebPushClient* client = PushController::clientFrom(document->frame()); |
39 if (!document->domWindow() || !document->frame()) | 44 ASSERT(client); |
40 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(AbortError, "Document is detached from window.")); | 45 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(s criptState); |
41 | 46 ScriptPromise promise = resolver->promise(); |
Peter Beverloo
2014/12/08 21:40:51
There's quite a bit of shared code here. Can we pe
Michael van Ouwerkerk
2014/12/09 18:47:45
Done.
| |
42 WebServiceWorkerProvider* serviceWorkerProvider = NavigatorServiceWorker::se rviceWorker(*document->domWindow()->navigator())->provider(); | 47 client->registerPushMessaging(m_registration->webRegistration(), new Cal lbackPromiseAdapter<PushRegistration, PushError>(resolver)); |
43 if (!serviceWorkerProvider) | 48 return promise; |
44 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(AbortError, "No Service Worker installed for this document.")); | 49 } |
45 | |
46 WebPushClient* client = PushController::clientFrom(document->frame()); | |
47 ASSERT(client); | |
48 | 50 |
49 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState); | 51 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState); |
50 ScriptPromise promise = resolver->promise(); | 52 ScriptPromise promise = resolver->promise(); |
51 | 53 Platform::current()->pushProvider()->registerPushMessaging(m_registration->w ebRegistration(), new CallbackPromiseAdapter<PushRegistration, PushError>(resolv er)); |
Peter Beverloo
2014/12/08 21:40:51
Can you add a utility method to receive the pushPr
Michael van Ouwerkerk
2014/12/09 18:47:45
Done.
| |
52 client->registerPushMessaging(new CallbackPromiseAdapter<PushRegistration, P ushError>(resolver), serviceWorkerProvider); | |
53 | |
54 return promise; | 54 return promise; |
55 } | 55 } |
56 | 56 |
57 // FIXME: This call should be available from workers which will not have a Docum ent object available. | |
58 // See crbug.com/389194 | |
59 ScriptPromise PushManager::hasPermission(ScriptState* scriptState) | 57 ScriptPromise PushManager::hasPermission(ScriptState* scriptState) |
60 { | 58 { |
61 ASSERT(scriptState->executionContext()->isDocument()); | 59 if (scriptState->executionContext()->isDocument()) { |
62 | 60 Document* document = toDocument(scriptState->executionContext()); |
63 Document* document = toDocument(scriptState->executionContext()); | 61 if (!document->domWindow() || !document->frame()) |
64 if (!document->domWindow() || !document->frame()) | 62 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti on::create(InvalidStateError, "Document is detached from window.")); |
Peter Beverloo
2014/12/08 21:40:51
I'm a bit reluctant to continue having these check
Michael van Ouwerkerk
2014/12/09 18:47:45
As discussed offline, I ran into some trouble port
| |
65 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(InvalidStateError, "Document is detached from window.")); | 63 } |
66 blink::WebPushClient* client = PushController::clientFrom(document->frame()) ; | |
67 ASSERT(client); | |
68 | |
69 // The currently implemented specification does not require a Service Worker to be present for the | |
70 // hasPermission() call to work, but it will become a requirement soon. | |
71 WebServiceWorkerProvider* serviceWorkerProvider = NavigatorServiceWorker::se rviceWorker(*document->domWindow()->navigator())->provider(); | |
72 if (!serviceWorkerProvider) | |
73 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(InvalidStateError, "No Service Worker installed for this document.")); | |
74 | 64 |
75 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState); | 65 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState); |
76 | |
77 ScriptPromise promise = resolver->promise(); | 66 ScriptPromise promise = resolver->promise(); |
78 client->getPermissionStatus(new PushPermissionStatusCallback(resolver), serv iceWorkerProvider); | 67 Platform::current()->pushProvider()->getPermissionStatus(m_registration->web Registration(), new PushPermissionStatusCallback(resolver)); |
79 return promise; | 68 return promise; |
80 } | 69 } |
81 | 70 |
71 void PushManager::trace(Visitor* visitor) | |
72 { | |
73 visitor->trace(m_registration); | |
74 } | |
75 | |
82 } // namespace blink | 76 } // namespace blink |
OLD | NEW |