OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/background_sync/SyncManager.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 "modules/push_messaging/PushController.h" | 16 #include "modules/background_sync/SyncCallbacks.h" |
17 #include "modules/push_messaging/PushError.h" | 17 #include "modules/background_sync/SyncRegistrationOptions.h" |
18 #include "modules/push_messaging/PushPermissionStatusCallbacks.h" | |
19 #include "modules/push_messaging/PushSubscription.h" | |
20 #include "modules/push_messaging/PushSubscriptionCallbacks.h" | |
21 #include "modules/serviceworkers/ServiceWorkerRegistration.h" | 18 #include "modules/serviceworkers/ServiceWorkerRegistration.h" |
22 #include "public/platform/Platform.h" | 19 #include "public/platform/Platform.h" |
23 #include "public/platform/modules/push_messaging/WebPushClient.h" | 20 #include "public/platform/modules/background_sync/WebSyncProvider.h" |
24 #include "public/platform/modules/push_messaging/WebPushProvider.h" | 21 #include "public/platform/modules/background_sync/WebSyncRegistration.h" |
25 #include "wtf/RefPtr.h" | 22 #include "wtf/RefPtr.h" |
26 | 23 |
| 24 |
27 namespace blink { | 25 namespace blink { |
28 namespace { | 26 namespace { |
29 | 27 |
30 WebPushProvider* pushProvider() | 28 /* This is the minimum period which will be allowed by the Background |
| 29 * Sync manager process. It is recorded here in order to be able to |
| 30 * respond to syncManager.minAllowablePeriod. |
| 31 * The time is expressed in milliseconds, |
| 32 */ |
| 33 unsigned long kMinAllowablePeriod = 12 * 60 * 60 * 1000; |
| 34 |
| 35 WebSyncProvider* backgroundSyncProvider() |
31 { | 36 { |
32 WebPushProvider* webPushProvider = Platform::current()->pushProvider(); | 37 WebSyncProvider* webSyncProvider = Platform::current()->backgroundSyncProvid
er(); |
33 ASSERT(webPushProvider); | 38 ASSERT(webSyncProvider); |
34 return webPushProvider; | 39 return webSyncProvider; |
35 } | 40 } |
36 | 41 |
37 } // namespace | 42 } |
38 | 43 |
39 PushManager::PushManager(ServiceWorkerRegistration* registration) | 44 SyncManager::SyncManager(ServiceWorkerRegistration* registration) |
40 : m_registration(registration) | 45 : m_registration(registration) |
41 { | 46 { |
42 ASSERT(registration); | 47 ASSERT(registration); |
43 } | 48 } |
44 | 49 |
45 ScriptPromise PushManager::subscribe(ScriptState* scriptState) | 50 unsigned long SyncManager::minAllowablePeriod() |
| 51 { |
| 52 return kMinAllowablePeriod; |
| 53 } |
| 54 |
| 55 ScriptPromise SyncManager::registerFunction(blink::ScriptState* scriptState, con
st SyncRegistrationOptions& options) |
46 { | 56 { |
47 if (!m_registration->active()) | 57 if (!m_registration->active()) |
48 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Subscription failed - no active Service Worker")); | 58 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Registration failed - no active Service Worker")); |
49 | 59 |
50 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 60 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
51 ScriptPromise promise = resolver->promise(); | 61 ScriptPromise promise = resolver->promise(); |
52 | 62 |
53 // The document context is the only reasonable context from which to ask the
user for permission | 63 WebSyncRegistration::NetworkType networkType; |
54 // to use the Push API. The embedder should persist the permission so that l
ater calls in | 64 String minRequiredNetwork = options.minRequiredNetwork(); |
55 // different contexts can succeed. | 65 if (minRequiredNetwork == "network-any") { |
56 if (scriptState->executionContext()->isDocument()) { | 66 networkType = WebSyncRegistration::NetworkType::NetworkTypeAny; |
57 Document* document = toDocument(scriptState->executionContext()); | 67 } else if (minRequiredNetwork == "network-non-mobile") { |
58 // FIXME: add test coverage for this condition - https://crbug.com/44043
1 | 68 networkType = WebSyncRegistration::NetworkType::NetworkTypeNonMobile; |
59 if (!document->domWindow() || !document->frame()) | 69 } else if (minRequiredNetwork == "network-offline") { |
60 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti
on::create(InvalidStateError, "Document is detached from window.")); | 70 networkType = WebSyncRegistration::NetworkType::NetworkTypeOffline; |
61 PushController::clientFrom(document->frame()).registerPushMessaging(m_re
gistration->webRegistration(), new PushSubscriptionCallbacks(resolver, m_registr
ation)); | |
62 } else { | 71 } else { |
63 pushProvider()->registerPushMessaging(m_registration->webRegistration(),
new PushSubscriptionCallbacks(resolver, m_registration)); | 72 networkType = WebSyncRegistration::NetworkType::NetworkTypeOnline; |
64 } | 73 } |
| 74 WebSyncRegistration webSyncRegistration = WebSyncRegistration(options.id(),
options.minDelay(), options.maxDelay(), options.minPeriod(), networkType, option
s.allowOnBattery(), options.idleRequired()); |
| 75 backgroundSyncProvider()->registerBackgroundSync(&webSyncRegistration, m_reg
istration->webRegistration(), new SyncRegistrationCallbacks(resolver, m_registra
tion)); |
65 | 76 |
66 return promise; | 77 return promise; |
67 } | 78 } |
68 | 79 |
69 ScriptPromise PushManager::getSubscription(ScriptState* scriptState) | 80 ScriptPromise SyncManager::getRegistration(blink::ScriptState* scriptState, cons
t String& syncRegistrationId) |
70 { | 81 { |
| 82 if (!m_registration->active()) |
| 83 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Operation failed - no active Service Worker")); |
| 84 |
71 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 85 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
72 ScriptPromise promise = resolver->promise(); | 86 ScriptPromise promise = resolver->promise(); |
73 | 87 |
74 pushProvider()->getRegistration(m_registration->webRegistration(), new PushS
ubscriptionCallbacks(resolver, m_registration)); | 88 backgroundSyncProvider()->getRegistration(syncRegistrationId, m_registration
->webRegistration(), new SyncRegistrationCallbacks(resolver, m_registration)); |
| 89 |
75 return promise; | 90 return promise; |
76 } | 91 } |
77 | 92 |
78 ScriptPromise PushManager::hasPermission(ScriptState* scriptState) | 93 ScriptPromise SyncManager::getRegistrations(blink::ScriptState* scriptState) |
79 { | 94 { |
80 if (scriptState->executionContext()->isDocument()) { | 95 if (!m_registration->active()) |
81 Document* document = toDocument(scriptState->executionContext()); | 96 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(AbortError, "Operation failed - no active Service Worker")); |
82 // FIXME: add test coverage for this condition - https://crbug.com/44043
1 | |
83 if (!document->domWindow() || !document->frame()) | |
84 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti
on::create(InvalidStateError, "Document is detached from window.")); | |
85 } | |
86 | 97 |
87 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 98 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
88 ScriptPromise promise = resolver->promise(); | 99 ScriptPromise promise = resolver->promise(); |
89 pushProvider()->getPermissionStatus(m_registration->webRegistration(), new P
ushPermissionStatusCallbacks(resolver)); | 100 |
| 101 backgroundSyncProvider()->getRegistrations(m_registration->webRegistration()
, new SyncGetRegistrationsCallbacks(resolver, m_registration)); |
| 102 |
90 return promise; | 103 return promise; |
91 } | 104 } |
92 | 105 |
93 DEFINE_TRACE(PushManager) | 106 DEFINE_TRACE(SyncManager) |
94 { | 107 { |
95 visitor->trace(m_registration); | 108 visitor->trace(m_registration); |
96 } | 109 } |
97 | 110 |
98 } // namespace blink | 111 } // namespace blink |
OLD | NEW |