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

Side by Side Diff: content/child/background_sync/background_sync_provider.cc

Issue 1960843002: [OnionSoup] Move background_sync.mojom from //content to //third_party/WebKit (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address dcheng@'s comment Created 4 years, 7 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 "content/child/background_sync/background_sync_provider.h" 5 #include "content/child/background_sync/background_sync_provider.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/lazy_instance.h" 13 #include "base/lazy_instance.h"
14 #include "base/single_thread_task_runner.h" 14 #include "base/single_thread_task_runner.h"
15 #include "base/threading/thread_local.h" 15 #include "base/threading/thread_local.h"
16 #include "content/child/background_sync/background_sync_type_converters.h" 16 #include "content/child/background_sync/background_sync_type_converters.h"
17 #include "content/child/child_thread_impl.h" 17 #include "content/child/child_thread_impl.h"
18 #include "content/child/service_worker/web_service_worker_registration_impl.h" 18 #include "content/child/service_worker/web_service_worker_registration_impl.h"
19 #include "content/public/common/background_sync.mojom.h"
20 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncErro r.h" 19 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncErro r.h"
21 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncRegi stration.h" 20 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncRegi stration.h"
22 21
23 using base::LazyInstance; 22 using base::LazyInstance;
24 using base::ThreadLocalPointer; 23 using base::ThreadLocalPointer;
25 24
26 namespace content { 25 namespace content {
27 namespace { 26 namespace {
28 27
29 // Returns the id of the given |service_worker_registration|, which 28 // Returns the id of the given |service_worker_registration|, which
30 // is only available on the implementation of the interface. 29 // is only available on the implementation of the interface.
31 int64_t GetServiceWorkerRegistrationId( 30 int64_t GetServiceWorkerRegistrationId(
32 blink::WebServiceWorkerRegistration* service_worker_registration) { 31 blink::WebServiceWorkerRegistration* service_worker_registration) {
33 return static_cast<WebServiceWorkerRegistrationImpl*>( 32 return static_cast<WebServiceWorkerRegistrationImpl*>(
34 service_worker_registration)->registration_id(); 33 service_worker_registration)->registration_id();
35 } 34 }
36 35
37 void ConnectToServiceOnMainThread( 36 void ConnectToServiceOnMainThread(
38 mojo::InterfaceRequest<mojom::BackgroundSyncService> request) { 37 mojo::InterfaceRequest<blink::mojom::BackgroundSyncService> request) {
39 DCHECK(ChildThreadImpl::current()); 38 DCHECK(ChildThreadImpl::current());
40 ChildThreadImpl::current()->service_registry()->ConnectToRemoteService( 39 ChildThreadImpl::current()->service_registry()->ConnectToRemoteService(
41 std::move(request)); 40 std::move(request));
42 } 41 }
43 42
44 LazyInstance<ThreadLocalPointer<BackgroundSyncProvider>>::Leaky 43 LazyInstance<ThreadLocalPointer<BackgroundSyncProvider>>::Leaky
45 g_sync_provider_tls = LAZY_INSTANCE_INITIALIZER; 44 g_sync_provider_tls = LAZY_INSTANCE_INITIALIZER;
46 45
47 } // namespace 46 } // namespace
48 47
(...skipping 12 matching lines...) Expand all
61 BackgroundSyncProvider* 60 BackgroundSyncProvider*
62 BackgroundSyncProvider::GetOrCreateThreadSpecificInstance( 61 BackgroundSyncProvider::GetOrCreateThreadSpecificInstance(
63 base::SingleThreadTaskRunner* main_thread_task_runner) { 62 base::SingleThreadTaskRunner* main_thread_task_runner) {
64 DCHECK(main_thread_task_runner); 63 DCHECK(main_thread_task_runner);
65 if (g_sync_provider_tls.Pointer()->Get()) 64 if (g_sync_provider_tls.Pointer()->Get())
66 return g_sync_provider_tls.Pointer()->Get(); 65 return g_sync_provider_tls.Pointer()->Get();
67 66
68 bool have_worker_id = (WorkerThread::GetCurrentId() > 0); 67 bool have_worker_id = (WorkerThread::GetCurrentId() > 0);
69 if (!main_thread_task_runner->BelongsToCurrentThread() && !have_worker_id) { 68 if (!main_thread_task_runner->BelongsToCurrentThread() && !have_worker_id) {
70 // On a worker thread, this could happen if this method is called 69 // On a worker thread, this could happen if this method is called
71 // very late (say by a garbage collected mojom::SyncRegistration). 70 // very late (say by a garbage collected blink::mojom::SyncRegistration).
72 return nullptr; 71 return nullptr;
73 } 72 }
74 73
75 BackgroundSyncProvider* instance = 74 BackgroundSyncProvider* instance =
76 new BackgroundSyncProvider(main_thread_task_runner); 75 new BackgroundSyncProvider(main_thread_task_runner);
77 76
78 if (have_worker_id) { 77 if (have_worker_id) {
79 // For worker threads, use the observer interface to clean up when workers 78 // For worker threads, use the observer interface to clean up when workers
80 // are stopped. 79 // are stopped.
81 WorkerThread::AddObserver(instance); 80 WorkerThread::AddObserver(instance);
(...skipping 10 matching lines...) Expand all
92 DCHECK(service_worker_registration); 91 DCHECK(service_worker_registration);
93 DCHECK(callbacks); 92 DCHECK(callbacks);
94 int64_t service_worker_registration_id = 93 int64_t service_worker_registration_id =
95 GetServiceWorkerRegistrationId(service_worker_registration); 94 GetServiceWorkerRegistrationId(service_worker_registration);
96 std::unique_ptr<const blink::WebSyncRegistration> optionsPtr(options); 95 std::unique_ptr<const blink::WebSyncRegistration> optionsPtr(options);
97 std::unique_ptr<blink::WebSyncRegistrationCallbacks> callbacksPtr(callbacks); 96 std::unique_ptr<blink::WebSyncRegistrationCallbacks> callbacksPtr(callbacks);
98 97
99 // base::Unretained is safe here, as the mojo channel will be deleted (and 98 // base::Unretained is safe here, as the mojo channel will be deleted (and
100 // will wipe its callbacks) before 'this' is deleted. 99 // will wipe its callbacks) before 'this' is deleted.
101 GetBackgroundSyncServicePtr()->Register( 100 GetBackgroundSyncServicePtr()->Register(
102 mojo::ConvertTo<mojom::SyncRegistrationPtr>(*(optionsPtr.get())), 101 mojo::ConvertTo<blink::mojom::SyncRegistrationPtr>(*(optionsPtr.get())),
103 service_worker_registration_id, 102 service_worker_registration_id,
104 base::Bind(&BackgroundSyncProvider::RegisterCallback, 103 base::Bind(&BackgroundSyncProvider::RegisterCallback,
105 base::Unretained(this), 104 base::Unretained(this),
106 base::Passed(std::move(callbacksPtr)))); 105 base::Passed(std::move(callbacksPtr))));
107 } 106 }
108 107
109 void BackgroundSyncProvider::getRegistrations( 108 void BackgroundSyncProvider::getRegistrations(
110 blink::WebServiceWorkerRegistration* service_worker_registration, 109 blink::WebServiceWorkerRegistration* service_worker_registration,
111 blink::WebSyncGetRegistrationsCallbacks* callbacks) { 110 blink::WebSyncGetRegistrationsCallbacks* callbacks) {
112 DCHECK(service_worker_registration); 111 DCHECK(service_worker_registration);
(...skipping 11 matching lines...) Expand all
124 base::Unretained(this), 123 base::Unretained(this),
125 base::Passed(std::move(callbacksPtr)))); 124 base::Passed(std::move(callbacksPtr))));
126 } 125 }
127 126
128 void BackgroundSyncProvider::WillStopCurrentWorkerThread() { 127 void BackgroundSyncProvider::WillStopCurrentWorkerThread() {
129 delete this; 128 delete this;
130 } 129 }
131 130
132 void BackgroundSyncProvider::RegisterCallback( 131 void BackgroundSyncProvider::RegisterCallback(
133 std::unique_ptr<blink::WebSyncRegistrationCallbacks> callbacks, 132 std::unique_ptr<blink::WebSyncRegistrationCallbacks> callbacks,
134 mojom::BackgroundSyncError error, 133 blink::mojom::BackgroundSyncError error,
135 const mojom::SyncRegistrationPtr& options) { 134 const blink::mojom::SyncRegistrationPtr& options) {
136 // TODO(iclelland): Determine the correct error message to return in each case 135 // TODO(iclelland): Determine the correct error message to return in each case
137 std::unique_ptr<blink::WebSyncRegistration> result; 136 std::unique_ptr<blink::WebSyncRegistration> result;
138 switch (error) { 137 switch (error) {
139 case mojom::BackgroundSyncError::NONE: 138 case blink::mojom::BackgroundSyncError::NONE:
140 if (!options.is_null()) 139 if (!options.is_null())
141 result = mojo::ConvertTo<std::unique_ptr<blink::WebSyncRegistration>>( 140 result = mojo::ConvertTo<std::unique_ptr<blink::WebSyncRegistration>>(
142 options); 141 options);
143 callbacks->onSuccess(std::move(result)); 142 callbacks->onSuccess(std::move(result));
144 break; 143 break;
145 case mojom::BackgroundSyncError::NOT_FOUND: 144 case blink::mojom::BackgroundSyncError::NOT_FOUND:
146 NOTREACHED(); 145 NOTREACHED();
147 break; 146 break;
148 case mojom::BackgroundSyncError::STORAGE: 147 case blink::mojom::BackgroundSyncError::STORAGE:
149 callbacks->onError( 148 callbacks->onError(
150 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown, 149 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
151 "Background Sync is disabled.")); 150 "Background Sync is disabled."));
152 break; 151 break;
153 case mojom::BackgroundSyncError::NOT_ALLOWED: 152 case blink::mojom::BackgroundSyncError::NOT_ALLOWED:
154 callbacks->onError( 153 callbacks->onError(
155 blink::WebSyncError(blink::WebSyncError::ErrorTypeNoPermission, 154 blink::WebSyncError(blink::WebSyncError::ErrorTypeNoPermission,
156 "Attempted to register a sync event without a " 155 "Attempted to register a sync event without a "
157 "window or registration tag too long.")); 156 "window or registration tag too long."));
158 break; 157 break;
159 case mojom::BackgroundSyncError::PERMISSION_DENIED: 158 case blink::mojom::BackgroundSyncError::PERMISSION_DENIED:
160 callbacks->onError( 159 callbacks->onError(
161 blink::WebSyncError(blink::WebSyncError::ErrorTypePermissionDenied, 160 blink::WebSyncError(blink::WebSyncError::ErrorTypePermissionDenied,
162 "Permission denied.")); 161 "Permission denied."));
163 break; 162 break;
164 case mojom::BackgroundSyncError::NO_SERVICE_WORKER: 163 case blink::mojom::BackgroundSyncError::NO_SERVICE_WORKER:
165 callbacks->onError( 164 callbacks->onError(
166 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown, 165 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
167 "No service worker is active.")); 166 "No service worker is active."));
168 break; 167 break;
169 } 168 }
170 } 169 }
171 170
172 void BackgroundSyncProvider::GetRegistrationsCallback( 171 void BackgroundSyncProvider::GetRegistrationsCallback(
173 std::unique_ptr<blink::WebSyncGetRegistrationsCallbacks> callbacks, 172 std::unique_ptr<blink::WebSyncGetRegistrationsCallbacks> callbacks,
174 mojom::BackgroundSyncError error, 173 blink::mojom::BackgroundSyncError error,
175 const mojo::Array<mojom::SyncRegistrationPtr>& registrations) { 174 const mojo::Array<blink::mojom::SyncRegistrationPtr>& registrations) {
176 // TODO(iclelland): Determine the correct error message to return in each case 175 // TODO(iclelland): Determine the correct error message to return in each case
177 switch (error) { 176 switch (error) {
178 case mojom::BackgroundSyncError::NONE: { 177 case blink::mojom::BackgroundSyncError::NONE: {
179 blink::WebVector<blink::WebSyncRegistration*> results( 178 blink::WebVector<blink::WebSyncRegistration*> results(
180 registrations.size()); 179 registrations.size());
181 for (size_t i = 0; i < registrations.size(); ++i) { 180 for (size_t i = 0; i < registrations.size(); ++i) {
182 results[i] = 181 results[i] =
183 mojo::ConvertTo<std::unique_ptr<blink::WebSyncRegistration>>( 182 mojo::ConvertTo<std::unique_ptr<blink::WebSyncRegistration>>(
184 registrations[i]) 183 registrations[i])
185 .release(); 184 .release();
186 } 185 }
187 callbacks->onSuccess(results); 186 callbacks->onSuccess(results);
188 break; 187 break;
189 } 188 }
190 case mojom::BackgroundSyncError::NOT_FOUND: 189 case blink::mojom::BackgroundSyncError::NOT_FOUND:
191 case mojom::BackgroundSyncError::NOT_ALLOWED: 190 case blink::mojom::BackgroundSyncError::NOT_ALLOWED:
192 case mojom::BackgroundSyncError::PERMISSION_DENIED: 191 case blink::mojom::BackgroundSyncError::PERMISSION_DENIED:
193 // These errors should never be returned from 192 // These errors should never be returned from
194 // BackgroundSyncManager::GetRegistrations 193 // BackgroundSyncManager::GetRegistrations
195 NOTREACHED(); 194 NOTREACHED();
196 break; 195 break;
197 case mojom::BackgroundSyncError::STORAGE: 196 case blink::mojom::BackgroundSyncError::STORAGE:
198 callbacks->onError( 197 callbacks->onError(
199 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown, 198 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
200 "Background Sync is disabled.")); 199 "Background Sync is disabled."));
201 break; 200 break;
202 case mojom::BackgroundSyncError::NO_SERVICE_WORKER: 201 case blink::mojom::BackgroundSyncError::NO_SERVICE_WORKER:
203 callbacks->onError( 202 callbacks->onError(
204 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown, 203 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
205 "No service worker is active.")); 204 "No service worker is active."));
206 break; 205 break;
207 } 206 }
208 } 207 }
209 208
210 mojom::BackgroundSyncServicePtr& 209 blink::mojom::BackgroundSyncServicePtr&
211 BackgroundSyncProvider::GetBackgroundSyncServicePtr() { 210 BackgroundSyncProvider::GetBackgroundSyncServicePtr() {
212 if (!background_sync_service_.get()) { 211 if (!background_sync_service_.get()) {
213 mojo::InterfaceRequest<mojom::BackgroundSyncService> request = 212 mojo::InterfaceRequest<blink::mojom::BackgroundSyncService> request =
214 mojo::GetProxy(&background_sync_service_); 213 mojo::GetProxy(&background_sync_service_);
215 main_thread_task_runner_->PostTask( 214 main_thread_task_runner_->PostTask(
216 FROM_HERE, 215 FROM_HERE,
217 base::Bind(&ConnectToServiceOnMainThread, base::Passed(&request))); 216 base::Bind(&ConnectToServiceOnMainThread, base::Passed(&request)));
218 } 217 }
219 return background_sync_service_; 218 return background_sync_service_;
220 } 219 }
221 220
222 } // namespace content 221 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698