Chromium Code Reviews| 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/browser/service_worker/service_worker_mojo_event_dispatcher.h" | |
| 6 | |
| 7 #include "content/public/browser/browser_thread.h" | |
| 8 #include "content/public/browser/render_process_host.h" | |
| 9 #include "content/public/common/service_registry.h" | |
| 10 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_ptr_info.h" | |
| 11 | |
| 12 namespace content { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 // Converts mojo enum content::ServiceWorkerEventStatus values to Chromium | |
| 17 // content::ServiceWorkerStatusCode values. | |
| 18 // TODO(iclelland): Make these enums equivalent so that conversion can be a | |
| 19 // static cast. | |
| 20 ServiceWorkerStatusCode statusCodeFromMojoStatus( | |
| 21 ServiceWorkerEventStatus status) { | |
| 22 ServiceWorkerStatusCode status_code = SERVICE_WORKER_OK; | |
| 23 if (status == SERVICE_WORKER_EVENT_STATUS_COMPLETED) { | |
| 24 status_code = SERVICE_WORKER_OK; | |
| 25 } else if (status == SERVICE_WORKER_EVENT_STATUS_REJECTED) { | |
| 26 status_code = SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED; | |
| 27 } else if (status == SERVICE_WORKER_EVENT_STATUS_ABORT) { | |
| 28 status_code = SERVICE_WORKER_ERROR_ABORT; | |
| 29 } else { | |
| 30 NOTREACHED(); | |
| 31 } | |
| 32 return status_code; | |
| 33 } | |
| 34 | |
| 35 // Establishes a connection to a mojo service, and then transfers the local | |
| 36 // endpoint of the message pipe to the IO thread so that it can be called from | |
| 37 // there in the future without switching threads. | |
| 38 // | |
| 39 // On success, the callback is called with a detached InterfacePtrInfo, which | |
| 40 // can be used to build a new InterfacePtr on the calling thread. Failure is | |
| 41 // indicated by calling the callback with an invalid InterfacePtrInfo. | |
| 42 // | |
| 43 // This function must be called on the UI thread, as it accesses the service | |
| 44 // registry member of the RenderProcessHost. | |
| 45 template <typename MojoServiceType> | |
| 46 void ConnectOnUIThread( | |
| 47 int render_process_id, | |
| 48 const base::Callback<void(mojo::InterfacePtrInfo<MojoServiceType>)>& | |
| 49 callback) { | |
| 50 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 51 mojo::InterfacePtr<MojoServiceType> mojo_interface_ptr; | |
| 52 | |
| 53 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id); | |
| 54 if (host) { | |
| 55 content::ServiceRegistry* registry = host->GetServiceRegistry(); | |
| 56 if (registry) { | |
| 57 registry->ConnectToRemoteService(mojo::GetProxy(&mojo_interface_ptr)); | |
| 58 } | |
| 59 } | |
| 60 BrowserThread::PostTask( | |
| 61 BrowserThread::IO, FROM_HERE, | |
| 62 base::Bind(callback, base::Passed(mojo_interface_ptr.PassInterface()))); | |
| 63 } | |
| 64 | |
| 65 } // namespace | |
| 66 | |
| 67 ServiceWorkerMojoEventDispatcher::ServiceWorkerMojoEventDispatcher() | |
| 68 : weak_ptr_factory_(this) { | |
| 69 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 70 } | |
| 71 | |
| 72 ServiceWorkerMojoEventDispatcher::~ServiceWorkerMojoEventDispatcher() { | |
| 73 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 74 } | |
| 75 | |
| 76 void ServiceWorkerMojoEventDispatcher::DispatchSyncEvent( | |
| 77 int render_process_id, | |
| 78 int thread_id, | |
| 79 const ServiceWorkerVersion::StatusCallback& callback) { | |
| 80 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 81 | |
| 82 if (!background_sync_client_.get()) { | |
| 83 // If we have not connected to the mojo service before, switch to the UI | |
| 84 // thread to connect, and then dispatch the event once the connection is | |
| 85 // established. | |
| 86 BrowserThread::PostTask( | |
|
jkarlin
2015/06/17 15:17:29
This is much better, thanks! One more suggestion.
iclelland
2015/06/18 16:01:14
That's a good API -- thanks for that pointer!
| |
| 87 BrowserThread::UI, FROM_HERE, | |
| 88 base::Bind( | |
| 89 &ConnectOnUIThread<BackgroundSyncServiceClient>, render_process_id, | |
| 90 base::Bind(&ServiceWorkerMojoEventDispatcher:: | |
| 91 BindChannelAndDispatchSyncEvent, | |
| 92 weak_ptr_factory_.GetWeakPtr(), thread_id, callback))); | |
| 93 return; | |
| 94 } | |
| 95 DispatchSyncEventInternal(thread_id, callback); | |
| 96 } | |
| 97 | |
| 98 void ServiceWorkerMojoEventDispatcher::BindChannelAndDispatchSyncEvent( | |
| 99 int thread_id, | |
| 100 const ServiceWorkerVersion::StatusCallback& callback, | |
| 101 mojo::InterfacePtrInfo<BackgroundSyncServiceClient> mojo_interface_ptr) { | |
| 102 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 103 if (!background_sync_client_.get()) { | |
| 104 // If the connect call failed, mojo_interface_ptr will be an invalid | |
| 105 // pointer, so abort now. | |
| 106 if (!mojo_interface_ptr.is_valid()) { | |
| 107 OnEventFinished(callback, SERVICE_WORKER_EVENT_STATUS_ABORT); | |
| 108 return; | |
| 109 } | |
| 110 background_sync_client_.Bind(mojo_interface_ptr.Pass()); | |
| 111 } | |
| 112 DispatchSyncEventInternal(thread_id, callback); | |
| 113 } | |
| 114 | |
| 115 void ServiceWorkerMojoEventDispatcher::DispatchSyncEventInternal( | |
| 116 int thread_id, | |
| 117 const ServiceWorkerVersion::StatusCallback& callback) { | |
| 118 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 119 | |
| 120 // TODO(iclelland): Replace this with the real event registration details | |
| 121 // crbug.com/482066 | |
| 122 content::SyncRegistrationPtr null_event(content::SyncRegistration::New()); | |
| 123 | |
| 124 background_sync_client_->Sync( | |
| 125 null_event.Pass(), thread_id, | |
| 126 base::Bind(&ServiceWorkerMojoEventDispatcher::OnEventFinished, | |
|
jkarlin
2015/06/17 15:17:29
Why not just pass the callback here? What purpose
iclelland
2015/06/18 16:01:14
The only thing it does currently is convert the ca
| |
| 127 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 128 } | |
| 129 | |
| 130 void ServiceWorkerMojoEventDispatcher::OnEventFinished( | |
| 131 const ServiceWorkerVersion::StatusCallback& callback, | |
| 132 ServiceWorkerEventStatus result) { | |
| 133 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 134 callback.Run(statusCodeFromMojoStatus(result)); | |
| 135 } | |
| 136 | |
| 137 } // namespace content | |
| OLD | NEW |