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 mojo { |
| 13 |
| 14 // TODO(iclelland): Make these enums equivalent so that conversion can be a |
| 15 // static cast. |
| 16 content::ServiceWorkerStatusCode |
| 17 TypeConverter<content::ServiceWorkerStatusCode, |
| 18 content::ServiceWorkerEventStatus>:: |
| 19 Convert(content::ServiceWorkerEventStatus status) { |
| 20 content::ServiceWorkerStatusCode status_code = content::SERVICE_WORKER_OK; |
| 21 if (status == content::SERVICE_WORKER_EVENT_STATUS_COMPLETED) { |
| 22 status_code = content::SERVICE_WORKER_OK; |
| 23 } else if (status == content::SERVICE_WORKER_EVENT_STATUS_REJECTED) { |
| 24 status_code = content::SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED; |
| 25 } else if (status == content::SERVICE_WORKER_EVENT_STATUS_ABORTED) { |
| 26 status_code = content::SERVICE_WORKER_ERROR_ABORT; |
| 27 } else { |
| 28 NOTREACHED(); |
| 29 } |
| 30 return status_code; |
| 31 } |
| 32 |
| 33 } // namespace |
| 34 |
| 35 namespace content { |
| 36 |
| 37 namespace { |
| 38 |
| 39 // Establishes a connection to a mojo service in the given render process. |
| 40 // |
| 41 // This function must be called on the UI thread, as it accesses the service |
| 42 // registry member of the RenderProcessHost. However, the connection itself |
| 43 // should be used on the thread that the InterfaceRequest was created on. |
| 44 template <typename MojoServiceType> |
| 45 void ConnectOnUIThread(mojo::InterfaceRequest<MojoServiceType> request, |
| 46 int render_process_id) { |
| 47 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 48 |
| 49 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id); |
| 50 ServiceRegistry* registry = host->GetServiceRegistry(); |
| 51 registry->ConnectToRemoteService(request.Pass()); |
| 52 } |
| 53 |
| 54 } // namespace |
| 55 |
| 56 ServiceWorkerMojoEventDispatcher::ServiceWorkerMojoEventDispatcher( |
| 57 int render_process_id) |
| 58 : render_process_id_(render_process_id), weak_factory_(this) { |
| 59 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 60 } |
| 61 |
| 62 ServiceWorkerMojoEventDispatcher::~ServiceWorkerMojoEventDispatcher() { |
| 63 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 64 } |
| 65 |
| 66 void ServiceWorkerMojoEventDispatcher::DispatchSyncEvent( |
| 67 int thread_id, |
| 68 const StatusCallback& callback) { |
| 69 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 70 |
| 71 if (!background_sync_client_.get()) { |
| 72 // If we have not connected to the mojo service before, switch to the UI |
| 73 // thread to connect. This can happen asynchronously, and the Sync() call |
| 74 // will occur once the connection is established. |
| 75 BrowserThread::PostTask( |
| 76 BrowserThread::UI, FROM_HERE, |
| 77 base::Bind(&ConnectOnUIThread<BackgroundSyncServiceClient>, |
| 78 base::Passed(mojo::GetProxy(&background_sync_client_)), |
| 79 render_process_id_)); |
| 80 } |
| 81 |
| 82 // TODO(iclelland): Replace this with the real event registration details |
| 83 // crbug.com/482066 |
| 84 content::SyncRegistrationPtr null_event(content::SyncRegistration::New()); |
| 85 |
| 86 background_sync_client_->Sync( |
| 87 null_event.Pass(), thread_id, |
| 88 base::Bind(&ServiceWorkerMojoEventDispatcher::OnEventFinished, |
| 89 weak_factory_.GetWeakPtr(), callback)); |
| 90 } |
| 91 |
| 92 void ServiceWorkerMojoEventDispatcher::OnEventFinished( |
| 93 const StatusCallback& callback, |
| 94 ServiceWorkerEventStatus result) { |
| 95 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 96 callback.Run(mojo::ConvertTo<ServiceWorkerStatusCode>(result)); |
| 97 } |
| 98 |
| 99 } // namespace content |
OLD | NEW |