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

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

Issue 2473483012: Move content/child/background_sync to Blink. (Closed)
Patch Set: Code review changes Created 4 years, 1 month 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 "modules/background_sync/SyncManager.h" 5 #include "modules/background_sync/SyncManager.h"
6 6
7 #include "bindings/core/v8/CallbackPromiseAdapter.h" 7 #include "bindings/core/v8/CallbackPromiseAdapter.h"
8 #include "bindings/core/v8/ScriptPromise.h" 8 #include "bindings/core/v8/ScriptPromise.h"
9 #include "bindings/core/v8/ScriptPromiseResolver.h" 9 #include "bindings/core/v8/ScriptPromiseResolver.h"
10 #include "bindings/core/v8/ScriptState.h" 10 #include "bindings/core/v8/ScriptState.h"
11 #include "core/dom/DOMException.h" 11 #include "core/dom/DOMException.h"
12 #include "core/dom/ExceptionCode.h" 12 #include "core/dom/ExceptionCode.h"
13 #include "core/dom/ExecutionContext.h" 13 #include "core/dom/ExecutionContext.h"
14 #include "modules/background_sync/SyncCallbacks.h" 14 #include "modules/background_sync/SyncCallbacks.h"
15 #include "modules/serviceworkers/ServiceWorkerRegistration.h" 15 #include "modules/serviceworkers/ServiceWorkerRegistration.h"
16 #include "public/platform/Platform.h" 16 #include "public/platform/Platform.h"
17 #include "public/platform/modules/background_sync/WebSyncProvider.h" 17 #include "wtf/ThreadSpecific.h"
18 #include "public/platform/modules/background_sync/WebSyncRegistration.h"
19 #include "wtf/RefPtr.h"
20 18
21 namespace blink { 19 namespace blink {
22 namespace {
23 20
24 WebSyncProvider* backgroundSyncProvider() { 21 // static
25 WebSyncProvider* webSyncProvider = 22 BackgroundSyncProvider* SyncManager::backgroundSyncProvider() {
26 Platform::current()->backgroundSyncProvider(); 23 DEFINE_THREAD_SAFE_STATIC_LOCAL(ThreadSpecific<BackgroundSyncProvider>,
haraken 2016/11/08 07:11:36 nhiroki@: We might want to consider introducing a
nhiroki 2016/11/09 05:26:55 Filed an issue: https://bugs.chromium.org/p/chromi
27 ASSERT(webSyncProvider); 24 syncProvider,
28 return webSyncProvider; 25 new ThreadSpecific<BackgroundSyncProvider>);
29 } 26 DCHECK(syncProvider);
27 return syncProvider;
30 } 28 }
31 29
32 SyncManager::SyncManager(ServiceWorkerRegistration* registration) 30 SyncManager::SyncManager(ServiceWorkerRegistration* registration)
33 : m_registration(registration) { 31 : m_registration(registration) {
34 ASSERT(registration); 32 DCHECK(registration);
35 } 33 }
36 34
37 ScriptPromise SyncManager::registerFunction(ScriptState* scriptState, 35 ScriptPromise SyncManager::registerFunction(ScriptState* scriptState,
38 ExecutionContext* context, 36 ExecutionContext* context,
39 const String& tag) { 37 const String& tag) {
40 // TODO(jkarlin): Wait for the registration to become active instead of 38 // TODO(jkarlin): Wait for the registration to become active instead of
41 // rejecting. See crbug.com/542437. 39 // rejecting. See crbug.com/542437.
42 if (!m_registration->active()) 40 if (!m_registration->active())
43 return ScriptPromise::rejectWithDOMException( 41 return ScriptPromise::rejectWithDOMException(
44 scriptState, 42 scriptState,
45 DOMException::create(AbortError, 43 DOMException::create(AbortError,
46 "Registration failed - no active Service Worker")); 44 "Registration failed - no active Service Worker"));
47 45
48 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 46 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
49 ScriptPromise promise = resolver->promise(); 47 ScriptPromise promise = resolver->promise();
50 48
51 WebSyncRegistration* webSyncRegistration = new WebSyncRegistration( 49 mojom::blink::SyncRegistrationPtr syncRegistration =
52 WebSyncRegistration::UNREGISTERED_SYNC_ID /* id */, tag, 50 mojom::blink::SyncRegistration::New();
53 WebSyncRegistration::NetworkStateOnline /* networkState */ 51 syncRegistration->id = SyncManager::kUnregisteredSyncID;
54 ); 52 syncRegistration->tag = tag;
53 syncRegistration->network_state =
54 blink::mojom::BackgroundSyncNetworkState::ONLINE;
55
55 backgroundSyncProvider()->registerBackgroundSync( 56 backgroundSyncProvider()->registerBackgroundSync(
56 webSyncRegistration, m_registration->webRegistration(), 57 std::move(syncRegistration), m_registration->webRegistration(),
57 new SyncRegistrationCallbacks(resolver, m_registration)); 58 std::unique_ptr<SyncRegistrationCallbacks>(
jbroman 2016/11/07 19:48:10 super-nit: You might consider using makeUnique (fr
adithyas 2016/11/08 16:47:47 Done! I like makeUnique better in this case :)
59 new SyncRegistrationCallbacks(resolver, m_registration)));
58 60
59 return promise; 61 return promise;
60 } 62 }
61 63
62 ScriptPromise SyncManager::getTags(ScriptState* scriptState) { 64 ScriptPromise SyncManager::getTags(ScriptState* scriptState) {
63 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 65 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
64 ScriptPromise promise = resolver->promise(); 66 ScriptPromise promise = resolver->promise();
65 67
66 backgroundSyncProvider()->getRegistrations( 68 backgroundSyncProvider()->getRegistrations(
67 m_registration->webRegistration(), 69 m_registration->webRegistration(),
68 new SyncGetRegistrationsCallbacks(resolver, m_registration)); 70 std::unique_ptr<SyncGetRegistrationsCallbacks>(
71 new SyncGetRegistrationsCallbacks(resolver, m_registration)));
69 72
70 return promise; 73 return promise;
71 } 74 }
72 75
73 DEFINE_TRACE(SyncManager) { 76 DEFINE_TRACE(SyncManager) {
74 visitor->trace(m_registration); 77 visitor->trace(m_registration);
75 } 78 }
76 79
77 } // namespace blink 80 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698