Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: Source/modules/background_sync/SyncManager.cpp

Issue 963683002: Add IDL and initial Blink API for Background Sync (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Add OWNERS for platform/modules/background_sync Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 unsigned long kMinAllowablePeriod = 60000;
jochen (gone - plz use gerrit) 2015/03/13 14:39:03 don't indent in a namespace what's the unit of ti
jkarlin 2015/03/13 18:51:39 Oddly enough the Blink style guide says that neste
jkarlin 2015/03/15 12:43:10 Ah, I'm wrong. "The contents of other nested name
iclelland 2015/03/16 13:09:40 I'll comment in the code, but all times are in mil
iclelland 2015/03/16 13:09:40 Done.
31 { 29
32 WebPushProvider* webPushProvider = Platform::current()->pushProvider(); 30 WebSyncProvider* backgroundSyncProvider()
33 ASSERT(webPushProvider); 31 {
34 return webPushProvider; 32 WebSyncProvider* webSyncProvider = Platform::current()->backgroundSyncPr ovider();
33 ASSERT(webSyncProvider);
34 return webSyncProvider;
35 }
36
35 } 37 }
36 38
37 } // namespace 39 SyncManager::SyncManager(ServiceWorkerRegistration* registration)
38
39 PushManager::PushManager(ServiceWorkerRegistration* registration)
40 : m_registration(registration) 40 : m_registration(registration)
41 { 41 {
42 ASSERT(registration); 42 ASSERT(registration);
43 } 43 }
44 44
45 ScriptPromise PushManager::subscribe(ScriptState* scriptState) 45 unsigned long SyncManager::minAllowablePeriod()
46 {
47 return kMinAllowablePeriod;
48 }
49
50 ScriptPromise SyncManager::registerFunction(blink::ScriptState* scriptState, con st SyncRegistrationOptions& options)
46 { 51 {
47 if (!m_registration->active()) 52 if (!m_registration->active())
48 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(AbortError, "Subscription failed - no active Service Worker")); 53 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(AbortError, "Registration failed - no active Service Worker"));
49 54
50 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); 55 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
51 ScriptPromise promise = resolver->promise(); 56 ScriptPromise promise = resolver->promise();
52 57
53 // The document context is the only reasonable context from which to ask the user for permission 58 WebSyncRegistration::NetworkType networkType;
54 // to use the Push API. The embedder should persist the permission so that l ater calls in 59 String minRequiredNetwork = options.minRequiredNetwork();
55 // different contexts can succeed. 60 if (minRequiredNetwork == "network-any") {
56 if (scriptState->executionContext()->isDocument()) { 61 networkType = WebSyncRegistration::NetworkType::NetworkTypeAny;
57 Document* document = toDocument(scriptState->executionContext()); 62 } else if (minRequiredNetwork == "network-non-mobile") {
58 // FIXME: add test coverage for this condition - https://crbug.com/44043 1 63 networkType = WebSyncRegistration::NetworkType::NetworkTypeNonMobile;
59 if (!document->domWindow() || !document->frame()) 64 } else if (minRequiredNetwork == "network-offline") {
60 return ScriptPromise::rejectWithDOMException(scriptState, DOMExcepti on::create(InvalidStateError, "Document is detached from window.")); 65 networkType = WebSyncRegistration::NetworkType::NetworkTypeOffline;
61 PushController::clientFrom(document->frame()).registerPushMessaging(m_re gistration->webRegistration(), new PushSubscriptionCallbacks(resolver, m_registr ation));
62 } else { 66 } else {
63 pushProvider()->registerPushMessaging(m_registration->webRegistration(), new PushSubscriptionCallbacks(resolver, m_registration)); 67 networkType = WebSyncRegistration::NetworkType::NetworkTypeOnline;
64 } 68 }
69 WebSyncRegistration webSyncRegistration = WebSyncRegistration(options.id(), options.minDelay(), options.maxDelay(), options.minPeriod(), networkType, option s.allowOnBattery(), options.idleRequired());
70 backgroundSyncProvider()->registerBackgroundSync(&webSyncRegistration, new S yncRegistrationCallbacks(resolver, m_registration));
65 71
66 return promise; 72 return promise;
67 } 73 }
68 74
69 ScriptPromise PushManager::getSubscription(ScriptState* scriptState) 75 ScriptPromise SyncManager::getRegistration(blink::ScriptState* scriptState, cons t String& syncRegistrationId)
70 { 76 {
77 if (!m_registration->active())
78 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(AbortError, "Operation failed - no active Service Worker"));
79
71 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); 80 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
72 ScriptPromise promise = resolver->promise(); 81 ScriptPromise promise = resolver->promise();
73 82
74 pushProvider()->getRegistration(m_registration->webRegistration(), new PushS ubscriptionCallbacks(resolver, m_registration)); 83 backgroundSyncProvider()->getRegistration(syncRegistrationId, new SyncRegist rationCallbacks(resolver, m_registration));
84
75 return promise; 85 return promise;
76 } 86 }
77 87
78 ScriptPromise PushManager::hasPermission(ScriptState* scriptState) 88 ScriptPromise SyncManager::getRegistrations(blink::ScriptState* scriptState)
79 { 89 {
80 if (scriptState->executionContext()->isDocument()) { 90 if (!m_registration->active())
81 Document* document = toDocument(scriptState->executionContext()); 91 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 92
87 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); 93 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
88 ScriptPromise promise = resolver->promise(); 94 ScriptPromise promise = resolver->promise();
89 pushProvider()->getPermissionStatus(m_registration->webRegistration(), new P ushPermissionStatusCallbacks(resolver)); 95
96 backgroundSyncProvider()->getRegistrations(new SyncGetRegistrationsCallbacks (resolver, m_registration));
97
90 return promise; 98 return promise;
91 } 99 }
92 100
93 DEFINE_TRACE(PushManager) 101 DEFINE_TRACE(SyncManager)
94 { 102 {
95 visitor->trace(m_registration); 103 visitor->trace(m_registration);
96 } 104 }
97 105
98 } // namespace blink 106 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698