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

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

Issue 1096503002: [Background Sync] Converting Blink code to the MVP API (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Make sure ids are 64 bit; fix formatting anomaly Created 5 years, 8 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 2015 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/background_sync/SyncManager.h" 6 #include "modules/background_sync/PeriodicSyncManager.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/background_sync/PeriodicSyncRegistrationOptions.h"
16 #include "modules/background_sync/SyncCallbacks.h" 17 #include "modules/background_sync/SyncCallbacks.h"
17 #include "modules/background_sync/SyncRegistrationOptions.h"
18 #include "modules/serviceworkers/ServiceWorkerRegistration.h" 18 #include "modules/serviceworkers/ServiceWorkerRegistration.h"
19 #include "public/platform/Platform.h" 19 #include "public/platform/Platform.h"
20 #include "public/platform/modules/background_sync/WebSyncProvider.h" 20 #include "public/platform/modules/background_sync/WebSyncProvider.h"
21 #include "public/platform/modules/background_sync/WebSyncRegistration.h" 21 #include "public/platform/modules/background_sync/WebSyncRegistration.h"
22 #include "wtf/RefPtr.h" 22 #include "wtf/RefPtr.h"
23 23
24 24
25 namespace blink { 25 namespace blink {
26 namespace { 26 namespace {
27 27
28 /* This is the minimum period which will be allowed by the Background 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 29 * Sync manager process. It is recorded here in order to be able to
30 * respond to syncManager.minAllowablePeriod. 30 * respond to syncManager.minAllowablePeriod.
31 * The time is expressed in milliseconds, 31 * The time is expressed in milliseconds,
32 */ 32 */
33 unsigned long kMinAllowablePeriod = 12 * 60 * 60 * 1000; 33 unsigned long kMinPossiblePeriod = 12 * 60 * 60 * 1000;
34 34
35 WebSyncProvider* backgroundSyncProvider() 35 WebSyncProvider* backgroundSyncProvider()
36 { 36 {
37 WebSyncProvider* webSyncProvider = Platform::current()->backgroundSyncProvid er(); 37 WebSyncProvider* webSyncProvider = Platform::current()->backgroundSyncProvid er();
38 ASSERT(webSyncProvider); 38 ASSERT(webSyncProvider);
39 return webSyncProvider; 39 return webSyncProvider;
40 } 40 }
41 41
42 } 42 }
43 43
44 SyncManager::SyncManager(ServiceWorkerRegistration* registration) 44 PeriodicSyncManager::PeriodicSyncManager(ServiceWorkerRegistration* registration )
45 : m_registration(registration) 45 : m_registration(registration)
46 { 46 {
47 ASSERT(registration); 47 ASSERT(registration);
48 } 48 }
49 49
50 unsigned long SyncManager::minAllowablePeriod() 50 unsigned long PeriodicSyncManager::minPossiblePeriod()
51 { 51 {
52 return kMinAllowablePeriod; 52 return kMinPossiblePeriod;
jkarlin 2015/04/17 19:04:49 Please create a bug to push this value up to the b
iclelland 2015/04/22 14:05:10 Done. crbug.com/479707
53 } 53 }
54 54
55 ScriptPromise SyncManager::registerFunction(blink::ScriptState* scriptState, con st SyncRegistrationOptions& options) 55 ScriptPromise PeriodicSyncManager::registerFunction(blink::ScriptState* scriptSt ate, const PeriodicSyncRegistrationOptions& options)
56 { 56 {
57 if (!m_registration->active()) 57 if (!m_registration->active())
58 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(AbortError, "Registration failed - no active Service Worker")); 58 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(AbortError, "Registration failed - no active Service Worker"));
59 59
60 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); 60 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
61 ScriptPromise promise = resolver->promise(); 61 ScriptPromise promise = resolver->promise();
62 62
63 WebSyncRegistration::NetworkType networkType; 63 WebSyncRegistration::NetworkState networkState;
64 String minRequiredNetwork = options.minRequiredNetwork(); 64 String networkStateString = options.networkState();
65 if (minRequiredNetwork == "network-any") { 65 if (networkStateString == "any") {
66 networkType = WebSyncRegistration::NetworkType::NetworkTypeAny; 66 networkState = WebSyncRegistration::NetworkState::NetworkStateAny;
67 } else if (minRequiredNetwork == "network-non-mobile") { 67 } else if (networkStateString == "avoid-cellular") {
68 networkType = WebSyncRegistration::NetworkType::NetworkTypeNonMobile; 68 networkState = WebSyncRegistration::NetworkState::NetworkStateAvoidCellu lar;
69 } else if (minRequiredNetwork == "network-offline") {
70 networkType = WebSyncRegistration::NetworkType::NetworkTypeOffline;
71 } else { 69 } else {
72 networkType = WebSyncRegistration::NetworkType::NetworkTypeOnline; 70 networkState = WebSyncRegistration::NetworkState::NetworkStateOnline;
73 } 71 }
74 WebSyncRegistration webSyncRegistration(options.id(), options.minDelay(), op tions.maxDelay(), options.minPeriod(), networkType, options.allowOnBattery(), op tions.idleRequired()); 72 WebSyncRegistration::PowerState powerState;
73 String powerStateString = options.powerState();
74 if (powerStateString == "avoid-draining") {
75 powerState = WebSyncRegistration::PowerState::PowerStateAvoidDraining;
76 } else {
77 powerState = WebSyncRegistration::PowerState::PowerStateAuto;
78 }
79 WebSyncRegistration webSyncRegistration(WebSyncRegistration::UNREGISTERED_SY NC_ID, WebSyncRegistration::PeriodicityPeriodic, options.tag(), options.minPerio d(), networkState, powerState);
75 backgroundSyncProvider()->registerBackgroundSync(&webSyncRegistration, m_reg istration->webRegistration(), new SyncRegistrationCallbacks(resolver, m_registra tion)); 80 backgroundSyncProvider()->registerBackgroundSync(&webSyncRegistration, m_reg istration->webRegistration(), new SyncRegistrationCallbacks(resolver, m_registra tion));
76 81
77 return promise; 82 return promise;
78 } 83 }
79 84
80 ScriptPromise SyncManager::getRegistration(blink::ScriptState* scriptState, cons t String& syncRegistrationId) 85 ScriptPromise PeriodicSyncManager::getRegistration(blink::ScriptState* scriptSta te, const String& syncRegistrationTag)
81 { 86 {
82 if (!m_registration->active()) 87 if (!m_registration->active())
83 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(AbortError, "Operation failed - no active Service Worker")); 88 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(AbortError, "Operation failed - no active Service Worker"));
84 89
85 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); 90 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
86 ScriptPromise promise = resolver->promise(); 91 ScriptPromise promise = resolver->promise();
87 92
88 backgroundSyncProvider()->getRegistration(syncRegistrationId, m_registration ->webRegistration(), new SyncRegistrationCallbacks(resolver, m_registration)); 93 backgroundSyncProvider()->getRegistration(blink::WebSyncRegistration::Period icityPeriodic, syncRegistrationTag, m_registration->webRegistration(), new SyncR egistrationCallbacks(resolver, m_registration));
89 94
90 return promise; 95 return promise;
91 } 96 }
92 97
93 ScriptPromise SyncManager::getRegistrations(blink::ScriptState* scriptState) 98 ScriptPromise PeriodicSyncManager::getRegistrations(blink::ScriptState* scriptSt ate)
94 { 99 {
95 if (!m_registration->active()) 100 if (!m_registration->active())
96 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(AbortError, "Operation failed - no active Service Worker")); 101 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(AbortError, "Operation failed - no active Service Worker"));
97 102
98 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); 103 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
99 ScriptPromise promise = resolver->promise(); 104 ScriptPromise promise = resolver->promise();
100 105
101 backgroundSyncProvider()->getRegistrations(m_registration->webRegistration() , new SyncGetRegistrationsCallbacks(resolver, m_registration)); 106 backgroundSyncProvider()->getRegistrations(blink::WebSyncRegistration::Perio dicityPeriodic, m_registration->webRegistration(), new SyncGetRegistrationsCallb acks(resolver, m_registration));
102 107
103 return promise; 108 return promise;
104 } 109 }
105 110
106 DEFINE_TRACE(SyncManager) 111 ScriptPromise PeriodicSyncManager::permissionState(blink::ScriptState* scriptSta te)
112 {
113 if (!m_registration->active())
114 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(AbortError, "Operation failed - no active Service Worker"));
115
116 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
117 ScriptPromise promise = resolver->promise();
118 resolver->resolve(String::fromUTF8("granted"));
jkarlin 2015/04/17 19:04:49 This needs to call up to the browser process. Plea
iclelland 2015/04/22 14:05:10 Done. crbug.com/479708 (Also for SyncManager.cpp)
119 return promise;
120 }
121
122 DEFINE_TRACE(PeriodicSyncManager)
107 { 123 {
108 visitor->trace(m_registration); 124 visitor->trace(m_registration);
109 } 125 }
110 126
111 } // namespace blink 127 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698