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_ABORTED) { | |
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 unbinds the local side | |
michaeln
2015/06/24 00:12:00
(mojo noob question) Where does the 'unbinding' ha
iclelland
2015/06/24 14:29:30
By calling .PassInterface(), and allowing the Inte
| |
36 // of the connection from the UI thread, and returns an InterfacePtrInfo struct | |
37 // that can be used to build a new InterfacePtr on a different thread. | |
38 // | |
39 // Failure is indicated by returning an invalid InterfacePtrInfo. | |
40 // | |
41 // This function must be called on the UI thread, as it accesses the service | |
42 // registry member of the RenderProcessHost. | |
43 template <typename MojoServiceType> | |
44 mojo::InterfacePtrInfo<MojoServiceType> ConnectOnUIThread( | |
45 int render_process_id) { | |
46 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
47 mojo::InterfacePtr<MojoServiceType> mojo_interface_ptr; | |
48 | |
49 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id); | |
50 if (host) { | |
michaeln
2015/06/24 00:12:00
we generally prefer early returns
if (!host)
re
| |
51 content::ServiceRegistry* registry = host->GetServiceRegistry(); | |
michaeln
2015/06/24 00:12:00
i don't think the content:: prefix is needed
| |
52 if (registry) { | |
53 registry->ConnectToRemoteService(mojo::GetProxy(&mojo_interface_ptr)); | |
michaeln
2015/06/24 00:12:01
How expensive is it to 'connect' to a remote servi
| |
54 } | |
55 } | |
56 return mojo_interface_ptr.PassInterface(); | |
57 } | |
58 | |
59 } // namespace | |
60 | |
61 ServiceWorkerMojoEventDispatcher::ServiceWorkerMojoEventDispatcher() | |
62 : weak_ptr_factory_(this) { | |
63 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
64 } | |
65 | |
66 ServiceWorkerMojoEventDispatcher::~ServiceWorkerMojoEventDispatcher() { | |
67 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
68 } | |
69 | |
70 void ServiceWorkerMojoEventDispatcher::DispatchSyncEvent( | |
71 int render_process_id, | |
michaeln
2015/06/24 00:12:00
is this param needed? is there not one dispatcher
iclelland
2015/06/24 14:29:30
Yes, there should be one dispatcher per renderer (
| |
72 int thread_id, | |
73 const StatusCallback& callback) { | |
74 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
75 | |
76 if (!background_sync_client_.get()) { | |
77 // If we have not connected to the mojo service before, switch to the UI | |
78 // thread to connect, and then dispatch the event once the connection is | |
79 // established. | |
80 BrowserThread::PostTaskAndReplyWithResult( | |
michaeln
2015/06/24 00:12:00
What if we get another DispatchSyncEvent() call pr
iclelland
2015/06/24 14:29:30
In that case, the second connection will be droppe
| |
81 BrowserThread::UI, FROM_HERE, | |
82 base::Bind(&ConnectOnUIThread<BackgroundSyncServiceClient>, | |
83 render_process_id), | |
84 base::Bind( | |
85 &ServiceWorkerMojoEventDispatcher::BindChannelAndDispatchSyncEvent, | |
86 weak_ptr_factory_.GetWeakPtr(), thread_id, callback)); | |
87 return; | |
88 } | |
89 DispatchSyncEventInternal(thread_id, callback); | |
90 } | |
91 | |
92 void ServiceWorkerMojoEventDispatcher::BindChannelAndDispatchSyncEvent( | |
93 int thread_id, | |
94 const StatusCallback& callback, | |
95 mojo::InterfacePtrInfo<BackgroundSyncServiceClient> mojo_interface_ptr) { | |
96 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
97 if (!background_sync_client_.get()) { | |
michaeln
2015/06/24 00:12:01
I see, additional connect results are dropped here
iclelland
2015/06/24 14:29:30
Yes, that should be a safe pattern; it's a little
| |
98 // If the connect call failed, mojo_interface_ptr will be an invalid | |
99 // pointer, so abort now. | |
100 if (!mojo_interface_ptr.is_valid()) { | |
101 OnEventFinished(callback, SERVICE_WORKER_EVENT_STATUS_ABORTED); | |
102 return; | |
103 } | |
104 background_sync_client_.Bind(mojo_interface_ptr.Pass()); | |
105 } | |
106 DispatchSyncEventInternal(thread_id, callback); | |
107 } | |
108 | |
109 void ServiceWorkerMojoEventDispatcher::DispatchSyncEventInternal( | |
110 int thread_id, | |
111 const StatusCallback& callback) { | |
112 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
113 | |
114 // TODO(iclelland): Replace this with the real event registration details | |
115 // crbug.com/482066 | |
116 content::SyncRegistrationPtr null_event(content::SyncRegistration::New()); | |
117 | |
118 background_sync_client_->Sync( | |
119 null_event.Pass(), thread_id, | |
120 base::Bind(&ServiceWorkerMojoEventDispatcher::OnEventFinished, | |
121 weak_ptr_factory_.GetWeakPtr(), callback)); | |
122 } | |
123 | |
124 void ServiceWorkerMojoEventDispatcher::OnEventFinished( | |
125 const StatusCallback& callback, | |
126 ServiceWorkerEventStatus result) { | |
127 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
128 callback.Run(StatusCodeFromMojoStatus(result)); | |
129 } | |
130 | |
131 } // namespace content | |
OLD | NEW |