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

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

Issue 1358063004: Use mojo to connect to BackgroundSyncManager object (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add back Leaky to TLS instances of provider Created 5 years, 2 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 "base/bind.h" 7 #include "base/bind.h"
8 #include "base/lazy_instance.h"
8 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/threading/thread_local.h"
9 #include "content/child/background_sync/background_sync_type_converters.h" 12 #include "content/child/background_sync/background_sync_type_converters.h"
13 #include "content/child/child_thread_impl.h"
10 #include "content/child/service_worker/web_service_worker_registration_impl.h" 14 #include "content/child/service_worker/web_service_worker_registration_impl.h"
11 #include "content/child/worker_task_runner.h"
12 #include "content/public/common/background_sync.mojom.h" 15 #include "content/public/common/background_sync.mojom.h"
13 #include "content/public/common/permission_status.mojom.h" 16 #include "content/public/common/permission_status.mojom.h"
14 #include "content/public/common/service_registry.h"
15 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncErro r.h" 17 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncErro r.h"
16 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncRegi stration.h" 18 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncRegi stration.h"
17 19
20 using base::LazyInstance;
21 using base::ThreadLocalPointer;
22
18 namespace content { 23 namespace content {
19 namespace { 24 namespace {
20 25
21 // Returns the id of the given |service_worker_registration|, which 26 // Returns the id of the given |service_worker_registration|, which
22 // is only available on the implementation of the interface. 27 // is only available on the implementation of the interface.
23 int64 GetServiceWorkerRegistrationId( 28 int64 GetServiceWorkerRegistrationId(
24 blink::WebServiceWorkerRegistration* service_worker_registration) { 29 blink::WebServiceWorkerRegistration* service_worker_registration) {
25 return static_cast<WebServiceWorkerRegistrationImpl*>( 30 return static_cast<WebServiceWorkerRegistrationImpl*>(
26 service_worker_registration)->registration_id(); 31 service_worker_registration)->registration_id();
27 } 32 }
28 33
34 void ConnectToServiceOnMainThread(
35 mojo::InterfaceRequest<BackgroundSyncService> request) {
36 DCHECK(ChildThreadImpl::current());
37 ChildThreadImpl::current()->service_registry()->ConnectToRemoteService(
38 request.Pass());
39 }
40
41 LazyInstance<ThreadLocalPointer<BackgroundSyncProvider>>::Leaky
42 g_sync_provider_tls = LAZY_INSTANCE_INITIALIZER;
43
29 } // namespace 44 } // namespace
30 45
31 BackgroundSyncProvider::BackgroundSyncProvider( 46 BackgroundSyncProvider::~BackgroundSyncProvider() {
32 ServiceRegistry* service_registry) 47 g_sync_provider_tls.Pointer()->Set(nullptr);
33 : service_registry_(service_registry), background_sync_service_(14) {
34 DCHECK(service_registry);
35 } 48 }
36 49
37 BackgroundSyncProvider::~BackgroundSyncProvider() { 50 // static
51 BackgroundSyncProvider*
52 BackgroundSyncProvider::GetOrCreateThreadSpecificInstance(
53 base::SingleThreadTaskRunner* main_thread_task_runner) {
54 DCHECK(main_thread_task_runner);
55 if (g_sync_provider_tls.Pointer()->Get())
56 return g_sync_provider_tls.Pointer()->Get();
57
58 bool have_worker_id = (WorkerThread::GetCurrentId() > 0);
59 if (!main_thread_task_runner->BelongsToCurrentThread() && !have_worker_id) {
60 // On a worker thread, this could happen if this method is called
61 // very late (say by a garbage collected SyncRegistration).
62 return nullptr;
63 }
64
65 BackgroundSyncProvider* instance =
66 new BackgroundSyncProvider(main_thread_task_runner);
67
68 if (have_worker_id) {
69 // For worker threads, use the observer interface to clean up when workers
70 // are stopped.
71 WorkerThread::AddObserver(instance);
72 }
73
74 return instance;
38 } 75 }
39 76
40 void BackgroundSyncProvider::registerBackgroundSync( 77 void BackgroundSyncProvider::registerBackgroundSync(
41 const blink::WebSyncRegistration* options, 78 const blink::WebSyncRegistration* options,
42 blink::WebServiceWorkerRegistration* service_worker_registration, 79 blink::WebServiceWorkerRegistration* service_worker_registration,
43 bool requested_from_service_worker, 80 bool requested_from_service_worker,
44 blink::WebSyncRegistrationCallbacks* callbacks) { 81 blink::WebSyncRegistrationCallbacks* callbacks) {
45 DCHECK(options); 82 DCHECK(options);
46 DCHECK(service_worker_registration); 83 DCHECK(service_worker_registration);
47 DCHECK(callbacks); 84 DCHECK(callbacks);
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 } 193 }
157 194
158 void BackgroundSyncProvider::DuplicateRegistrationHandle( 195 void BackgroundSyncProvider::DuplicateRegistrationHandle(
159 int64_t handle_id, 196 int64_t handle_id,
160 const BackgroundSyncService::DuplicateRegistrationHandleCallback& 197 const BackgroundSyncService::DuplicateRegistrationHandleCallback&
161 callback) { 198 callback) {
162 GetBackgroundSyncServicePtr()->DuplicateRegistrationHandle(handle_id, 199 GetBackgroundSyncServicePtr()->DuplicateRegistrationHandle(handle_id,
163 callback); 200 callback);
164 } 201 }
165 202
203 void BackgroundSyncProvider::WillStopCurrentWorkerThread() {
204 delete this;
205 }
206
207 BackgroundSyncProvider::BackgroundSyncProvider(
208 const scoped_refptr<base::SingleThreadTaskRunner> main_task_runner)
209 : main_thread_task_runner_(main_task_runner) {
210 DCHECK(main_task_runner);
211 g_sync_provider_tls.Pointer()->Set(this);
212 }
213
166 void BackgroundSyncProvider::RegisterCallback( 214 void BackgroundSyncProvider::RegisterCallback(
167 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacks, 215 scoped_ptr<blink::WebSyncRegistrationCallbacks> callbacks,
168 BackgroundSyncError error, 216 BackgroundSyncError error,
169 const SyncRegistrationPtr& options) { 217 const SyncRegistrationPtr& options) {
170 // TODO(iclelland): Determine the correct error message to return in each case 218 // TODO(iclelland): Determine the correct error message to return in each case
171 scoped_ptr<blink::WebSyncRegistration> result; 219 scoped_ptr<blink::WebSyncRegistration> result;
172 switch (error) { 220 switch (error) {
173 case BACKGROUND_SYNC_ERROR_NONE: 221 case BACKGROUND_SYNC_ERROR_NONE:
174 if (!options.is_null()) 222 if (!options.is_null())
175 result = 223 result =
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 callbacks->onError( 417 callbacks->onError(
370 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown, 418 blink::WebSyncError(blink::WebSyncError::ErrorTypeUnknown,
371 "No service worker is active.")); 419 "No service worker is active."));
372 break; 420 break;
373 } 421 }
374 } 422 }
375 423
376 BackgroundSyncServicePtr& 424 BackgroundSyncServicePtr&
377 BackgroundSyncProvider::GetBackgroundSyncServicePtr() { 425 BackgroundSyncProvider::GetBackgroundSyncServicePtr() {
378 if (!background_sync_service_.get()) { 426 if (!background_sync_service_.get()) {
379 service_registry_->ConnectToRemoteService( 427 mojo::InterfaceRequest<BackgroundSyncService> request =
380 mojo::GetProxy(&background_sync_service_)); 428 mojo::GetProxy(&background_sync_service_);
429 main_thread_task_runner_->PostTask(
430 FROM_HERE,
431 base::Bind(&ConnectToServiceOnMainThread, base::Passed(&request)));
381 } 432 }
382 return background_sync_service_; 433 return background_sync_service_;
383 } 434 }
384 435
385 } // namespace content 436 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698