OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/child/background_sync/background_sync_provider.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "content/child/background_sync/background_sync_type_converters.h" | |
10 #include "content/child/service_worker/web_service_worker_registration_impl.h" | |
11 #include "content/child/worker_task_runner.h" | |
12 #include "content/public/common/service_registry.h" | |
13 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncErro r.h" | |
14 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncRegi stration.h" | |
15 | |
16 namespace content { | |
17 namespace { | |
18 | |
19 // Returns the id of the given |service_worker_registration|, which | |
20 // is only available on the implementation of the interface. | |
21 int64 GetServiceWorkerRegistrationId( | |
22 blink::WebServiceWorkerRegistration* service_worker_registration) { | |
23 return static_cast<WebServiceWorkerRegistrationImpl*>( | |
24 service_worker_registration)->registration_id(); | |
25 } | |
26 | |
27 } // namespace | |
28 | |
29 BackgroundSyncProvider::BackgroundSyncProvider( | |
30 ServiceRegistry* service_registry) | |
31 : service_registry_(service_registry) { | |
32 DCHECK(service_registry); | |
33 } | |
34 | |
35 BackgroundSyncProvider::~BackgroundSyncProvider() { | |
36 } | |
37 | |
38 void BackgroundSyncProvider::registerBackgroundSync( | |
39 const blink::WebSyncRegistration* options, | |
40 blink::WebServiceWorkerRegistration* service_worker_registration, | |
41 blink::WebSyncRegistrationCallbacks* callbacks) { | |
42 DCHECK(options); | |
43 DCHECK(service_worker_registration); | |
44 DCHECK(callbacks); | |
45 int64 service_worker_registration_id = | |
46 GetServiceWorkerRegistrationId(service_worker_registration); | |
47 scoped_ptr<const blink::WebSyncRegistration> optionsPtr = | |
48 make_scoped_ptr(options); | |
jkarlin
2015/05/12 14:48:33
nit: This can be scoped_ptr<const blink::WebSyncRe
iclelland
2015/05/12 14:59:47
Yes. Probably saves a temporary, too. Thanks.
Done
| |
49 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacksPtr = | |
50 make_scoped_ptr(callbacks); | |
51 | |
52 // base::Unretained is safe here, as the mojo channel will be deleted (and | |
53 // will wipe its callbacks) before 'this' is deleted. | |
54 GetBackgroundSyncServicePtr()->Register( | |
55 mojo::ConvertTo<SyncRegistrationPtr>(*(optionsPtr.get())), | |
56 service_worker_registration_id, | |
57 base::Bind(&BackgroundSyncProvider::RegisterCallback, | |
58 base::Unretained(this), base::Passed(callbacksPtr.Pass()))); | |
59 } | |
60 | |
61 void BackgroundSyncProvider::unregisterBackgroundSync( | |
62 blink::WebSyncRegistration::Periodicity periodicity, | |
63 int64_t id, | |
64 const blink::WebString& tag, | |
65 blink::WebServiceWorkerRegistration* service_worker_registration, | |
66 blink::WebSyncUnregistrationCallbacks* callbacks) { | |
67 DCHECK(service_worker_registration); | |
68 DCHECK(callbacks); | |
69 int64 service_worker_registration_id = | |
70 GetServiceWorkerRegistrationId(service_worker_registration); | |
71 scoped_ptr<blink::WebSyncUnregistrationCallbacks> callbacksPtr = | |
72 make_scoped_ptr(callbacks); | |
73 | |
74 // base::Unretained is safe here, as the mojo channel will be deleted (and | |
75 // will wipe its callbacks) before 'this' is deleted. | |
76 GetBackgroundSyncServicePtr()->Unregister( | |
77 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), id, tag.utf8(), | |
78 service_worker_registration_id, | |
79 base::Bind(&BackgroundSyncProvider::UnregisterCallback, | |
80 base::Unretained(this), base::Passed(callbacksPtr.Pass()))); | |
81 } | |
82 | |
83 void BackgroundSyncProvider::getRegistration( | |
84 blink::WebSyncRegistration::Periodicity periodicity, | |
85 const blink::WebString& tag, | |
86 blink::WebServiceWorkerRegistration* service_worker_registration, | |
87 blink::WebSyncRegistrationCallbacks* callbacks) { | |
88 DCHECK(service_worker_registration); | |
89 DCHECK(callbacks); | |
90 int64 service_worker_registration_id = | |
91 GetServiceWorkerRegistrationId(service_worker_registration); | |
92 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacksPtr = | |
93 make_scoped_ptr(callbacks); | |
94 | |
95 // base::Unretained is safe here, as the mojo channel will be deleted (and | |
96 // will wipe its callbacks) before 'this' is deleted. | |
97 GetBackgroundSyncServicePtr()->GetRegistration( | |
98 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), tag.utf8(), | |
99 service_worker_registration_id, | |
100 base::Bind(&BackgroundSyncProvider::RegisterCallback, | |
101 base::Unretained(this), base::Passed(callbacksPtr.Pass()))); | |
102 } | |
103 | |
104 void BackgroundSyncProvider::getRegistrations( | |
105 blink::WebSyncRegistration::Periodicity periodicity, | |
106 blink::WebServiceWorkerRegistration* service_worker_registration, | |
107 blink::WebSyncGetRegistrationsCallbacks* callbacks) { | |
108 DCHECK(service_worker_registration); | |
109 DCHECK(callbacks); | |
110 int64 service_worker_registration_id = | |
111 GetServiceWorkerRegistrationId(service_worker_registration); | |
112 scoped_ptr<blink::WebSyncGetRegistrationsCallbacks> callbacksPtr = | |
113 make_scoped_ptr(callbacks); | |
114 | |
115 // base::Unretained is safe here, as the mojo channel will be deleted (and | |
116 // will wipe its callbacks) before 'this' is deleted. | |
117 GetBackgroundSyncServicePtr()->GetRegistrations( | |
118 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), | |
119 service_worker_registration_id, | |
120 base::Bind(&BackgroundSyncProvider::GetRegistrationsCallback, | |
121 base::Unretained(this), base::Passed(callbacksPtr.Pass()))); | |
122 } | |
123 | |
124 void BackgroundSyncProvider::RegisterCallback( | |
125 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacks, | |
126 const SyncRegistrationPtr& options) { | |
127 // TODO(iclelland): Pass through the various errors from the manager to here | |
128 // and handle them appropriately. | |
129 scoped_ptr<blink::WebSyncRegistration> result; | |
130 if (!options.is_null()) | |
131 result = mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options); | |
132 | |
133 callbacks->onSuccess(result.release()); | |
134 } | |
135 | |
136 void BackgroundSyncProvider::UnregisterCallback( | |
137 scoped_ptr<blink::WebSyncUnregistrationCallbacks> callbacks, | |
138 bool success) { | |
139 // TODO(iclelland): Pass through the various errors from the manager to here | |
140 // and handle them appropriately. | |
141 if (success) { | |
142 callbacks->onSuccess(new bool(success)); | |
143 } else { | |
144 callbacks->onError( | |
145 new blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown, | |
146 "Sync registration does not exist")); | |
147 } | |
148 } | |
149 | |
150 void BackgroundSyncProvider::GetRegistrationsCallback( | |
151 scoped_ptr<blink::WebSyncGetRegistrationsCallbacks> callbacks, | |
152 const mojo::Array<SyncRegistrationPtr>& registrations) { | |
153 // TODO(iclelland): Pass through the various errors from the manager to here | |
154 // and handle them appropriately. | |
155 blink::WebVector<blink::WebSyncRegistration*>* results = | |
156 new blink::WebVector<blink::WebSyncRegistration*>(registrations.size()); | |
157 for (size_t i = 0; i < registrations.size(); ++i) { | |
158 (*results)[i] = mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>( | |
159 registrations[i]).release(); | |
160 } | |
161 callbacks->onSuccess(results); | |
162 } | |
163 | |
164 BackgroundSyncServicePtr& | |
165 BackgroundSyncProvider::GetBackgroundSyncServicePtr() { | |
166 if (!background_sync_service_.get()) | |
167 service_registry_->ConnectToRemoteService(&background_sync_service_); | |
168 return background_sync_service_; | |
169 } | |
170 | |
171 } // namespace content | |
OLD | NEW |