OLD | NEW |
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/renderer/background_sync/background_sync_client_impl.h" | 5 #include "content/renderer/background_sync/background_sync_client_impl.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "content/child/background_sync/background_sync_provider.h" | 9 #include "content/child/background_sync/background_sync_provider.h" |
10 #include "content/child/background_sync/background_sync_type_converters.h" | 10 #include "content/child/background_sync/background_sync_type_converters.h" |
11 #include "content/renderer/service_worker/service_worker_context_client.h" | 11 #include "content/renderer/service_worker/service_worker_context_client.h" |
12 #include "third_party/WebKit/public/platform/Platform.h" | 12 #include "third_party/WebKit/public/platform/Platform.h" |
13 #include "third_party/WebKit/public/platform/WebThread.h" | 13 #include "third_party/WebKit/public/platform/WebThread.h" |
14 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncProv
ider.h" | 14 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncProv
ider.h" |
15 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncRegi
stration.h" | 15 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncRegi
stration.h" |
16 #include "third_party/WebKit/public/web/modules/serviceworker/WebServiceWorkerCo
ntextProxy.h" | 16 #include "third_party/WebKit/public/web/modules/serviceworker/WebServiceWorkerCo
ntextProxy.h" |
17 | 17 |
18 namespace content { | 18 namespace content { |
19 | 19 |
20 // static | 20 // static |
21 void BackgroundSyncClientImpl::Create( | 21 void BackgroundSyncClientImpl::Create( |
22 mojo::InterfaceRequest<BackgroundSyncServiceClient> request) { | 22 mojo::InterfaceRequest<BackgroundSyncServiceClient> request) { |
23 new BackgroundSyncClientImpl(std::move(request)); | 23 new BackgroundSyncClientImpl(std::move(request)); |
24 } | 24 } |
25 | 25 |
26 BackgroundSyncClientImpl::~BackgroundSyncClientImpl() {} | 26 BackgroundSyncClientImpl::~BackgroundSyncClientImpl() {} |
27 | 27 |
28 BackgroundSyncClientImpl::BackgroundSyncClientImpl( | 28 BackgroundSyncClientImpl::BackgroundSyncClientImpl( |
29 mojo::InterfaceRequest<BackgroundSyncServiceClient> request) | 29 mojo::InterfaceRequest<BackgroundSyncServiceClient> request) |
30 : binding_(this, std::move(request)), callback_seq_num_(0) {} | 30 : binding_(this, std::move(request)) {} |
31 | 31 |
32 void BackgroundSyncClientImpl::Sync( | 32 void BackgroundSyncClientImpl::Sync( |
33 int64_t handle_id, | 33 const mojo::String& tag, |
34 content::BackgroundSyncEventLastChance last_chance, | 34 content::BackgroundSyncEventLastChance last_chance, |
35 const SyncCallback& callback) { | 35 const SyncCallback& callback) { |
36 DCHECK(!blink::Platform::current()->mainThread()->isCurrentThread()); | 36 DCHECK(!blink::Platform::current()->mainThread()->isCurrentThread()); |
37 // Get a registration for the given handle_id from the provider. This way | |
38 // the provider knows about the handle and can delete it once Blink releases | |
39 // it. | |
40 // TODO(jkarlin): Change the WebSyncPlatform to support | |
41 // DuplicateRegistrationHandle and then this cast can go. | |
42 BackgroundSyncProvider* provider = static_cast<BackgroundSyncProvider*>( | |
43 blink::Platform::current()->backgroundSyncProvider()); | |
44 | |
45 if (provider == nullptr) { | |
46 // This can happen if the renderer is shutting down when sync fires. See | |
47 // crbug.com/543898. No need to fire the callback as the browser will | |
48 // automatically fail all pending sync events when the mojo connection | |
49 // closes. | |
50 return; | |
51 } | |
52 | |
53 // TODO(jkarlin): Find a way to claim the handle safely without requiring a | |
54 // round-trip IPC. | |
55 int64_t id = ++callback_seq_num_; | |
56 sync_callbacks_[id] = callback; | |
57 provider->DuplicateRegistrationHandle( | |
58 handle_id, base::Bind(&BackgroundSyncClientImpl::SyncDidGetRegistration, | |
59 base::Unretained(this), id, last_chance)); | |
60 } | |
61 | |
62 void BackgroundSyncClientImpl::SyncDidGetRegistration( | |
63 int64_t callback_id, | |
64 content::BackgroundSyncEventLastChance last_chance, | |
65 BackgroundSyncError error, | |
66 SyncRegistrationPtr registration) { | |
67 SyncCallback callback; | |
68 auto it = sync_callbacks_.find(callback_id); | |
69 DCHECK(it != sync_callbacks_.end()); | |
70 callback = it->second; | |
71 sync_callbacks_.erase(it); | |
72 | |
73 if (error != BackgroundSyncError::NONE) { | |
74 callback.Run(ServiceWorkerEventStatus::ABORTED); | |
75 return; | |
76 } | |
77 | 37 |
78 ServiceWorkerContextClient* client = | 38 ServiceWorkerContextClient* client = |
79 ServiceWorkerContextClient::ThreadSpecificInstance(); | 39 ServiceWorkerContextClient::ThreadSpecificInstance(); |
80 if (!client) { | 40 if (!client) { |
81 callback.Run(ServiceWorkerEventStatus::ABORTED); | 41 callback.Run(ServiceWorkerEventStatus::ABORTED); |
82 return; | 42 return; |
83 } | 43 } |
84 | 44 |
85 scoped_ptr<blink::WebSyncRegistration> web_registration = | |
86 mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(registration); | |
87 | |
88 blink::WebServiceWorkerContextProxy::LastChanceOption web_last_chance = | 45 blink::WebServiceWorkerContextProxy::LastChanceOption web_last_chance = |
89 mojo::ConvertTo<blink::WebServiceWorkerContextProxy::LastChanceOption>( | 46 mojo::ConvertTo<blink::WebServiceWorkerContextProxy::LastChanceOption>( |
90 last_chance); | 47 last_chance); |
91 | 48 |
92 client->DispatchSyncEvent(*web_registration, web_last_chance, callback); | 49 client->DispatchSyncEvent(tag, web_last_chance, callback); |
93 } | 50 } |
94 | 51 |
95 } // namespace content | 52 } // namespace content |
OLD | NEW |