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( | |
jkarlin
2015/06/18 18:13:56
caps, StatusCodeFromMojoStatus
iclelland
2015/06/19 13:44:32
oldHabitsDieHard :)
Done.
| |
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 detaches the local | |
jkarlin
2015/06/18 18:13:56
"detaches the local endpoint of the message pipe f
iclelland
2015/06/19 13:44:32
It's actually what InterfacePtr<>::PassInterface i
jkarlin
2015/06/19 13:51:42
Got it, thanks.
michaeln
2015/06/24 01:34:03
ah... now i see where that occurs :)
| |
36 // endpoint of the message pipe from the UI thread, and returns an | |
37 // InterfacePtrInfo struct that can be used to build a new InterfacePtr on a | |
38 // different thread. | |
39 // | |
40 // Failure is indicated by returning an invalid InterfacePtrInfo. | |
41 // | |
42 // This function must be called on the UI thread, as it accesses the service | |
43 // registry member of the RenderProcessHost. | |
44 template <typename MojoServiceType> | |
45 mojo::InterfacePtrInfo<MojoServiceType> ConnectOnUIThread( | |
46 int render_process_id) { | |
47 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
48 mojo::InterfacePtr<MojoServiceType> mojo_interface_ptr; | |
49 | |
50 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id); | |
51 if (host) { | |
52 content::ServiceRegistry* registry = host->GetServiceRegistry(); | |
53 if (registry) { | |
54 registry->ConnectToRemoteService(mojo::GetProxy(&mojo_interface_ptr)); | |
55 } | |
56 } | |
57 return mojo_interface_ptr.PassInterface(); | |
58 } | |
59 | |
60 } // namespace | |
61 | |
62 ServiceWorkerMojoEventDispatcher::ServiceWorkerMojoEventDispatcher() | |
63 : weak_ptr_factory_(this) { | |
64 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
65 } | |
66 | |
67 ServiceWorkerMojoEventDispatcher::~ServiceWorkerMojoEventDispatcher() { | |
68 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
69 } | |
70 | |
71 void ServiceWorkerMojoEventDispatcher::DispatchSyncEvent( | |
72 int render_process_id, | |
73 int thread_id, | |
74 const StatusCallback& callback) { | |
75 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
76 | |
77 if (!background_sync_client_.get()) { | |
78 // If we have not connected to the mojo service before, switch to the UI | |
79 // thread to connect, and then dispatch the event once the connection is | |
80 // established. | |
81 BrowserThread::PostTaskAndReplyWithResult( | |
82 BrowserThread::UI, FROM_HERE, | |
83 base::Bind(&ConnectOnUIThread<BackgroundSyncServiceClient>, | |
84 render_process_id), | |
85 base::Bind( | |
86 &ServiceWorkerMojoEventDispatcher::BindChannelAndDispatchSyncEvent, | |
87 weak_ptr_factory_.GetWeakPtr(), thread_id, callback)); | |
88 return; | |
89 } | |
90 DispatchSyncEventInternal(thread_id, callback); | |
91 } | |
92 | |
93 void ServiceWorkerMojoEventDispatcher::BindChannelAndDispatchSyncEvent( | |
94 int thread_id, | |
95 const StatusCallback& callback, | |
96 mojo::InterfacePtrInfo<BackgroundSyncServiceClient> mojo_interface_ptr) { | |
97 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
98 if (!background_sync_client_.get()) { | |
99 // If the connect call failed, mojo_interface_ptr will be an invalid | |
100 // pointer, so abort now. | |
101 if (!mojo_interface_ptr.is_valid()) { | |
102 OnEventFinished(callback, SERVICE_WORKER_EVENT_STATUS_ABORTED); | |
103 return; | |
104 } | |
105 background_sync_client_.Bind(mojo_interface_ptr.Pass()); | |
106 } | |
107 DispatchSyncEventInternal(thread_id, callback); | |
108 } | |
109 | |
110 void ServiceWorkerMojoEventDispatcher::DispatchSyncEventInternal( | |
111 int thread_id, | |
112 const StatusCallback& callback) { | |
113 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
114 | |
115 // TODO(iclelland): Replace this with the real event registration details | |
116 // crbug.com/482066 | |
117 content::SyncRegistrationPtr null_event(content::SyncRegistration::New()); | |
118 | |
119 background_sync_client_->Sync( | |
120 null_event.Pass(), thread_id, | |
121 base::Bind(&ServiceWorkerMojoEventDispatcher::OnEventFinished, | |
122 weak_ptr_factory_.GetWeakPtr(), callback)); | |
123 } | |
124 | |
125 void ServiceWorkerMojoEventDispatcher::OnEventFinished( | |
126 const StatusCallback& callback, | |
127 ServiceWorkerEventStatus result) { | |
128 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
129 callback.Run(statusCodeFromMojoStatus(result)); | |
130 } | |
131 | |
132 } // namespace content | |
OLD | NEW |