Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(571)

Side by Side Diff: content/renderer/pepper/pepper_device_enumeration_host_helper.cc

Issue 2490653002: Make Pepper use Mojo-based support for media-device enumeration and monitoring. (Closed)
Patch Set: rebase Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/pepper/pepper_device_enumeration_host_helper.h" 5 #include "content/renderer/pepper/pepper_device_enumeration_host_helper.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/memory/weak_ptr.h" 11 #include "base/memory/weak_ptr.h"
12 #include "base/single_thread_task_runner.h" 12 #include "base/single_thread_task_runner.h"
13 #include "base/threading/thread_task_runner_handle.h" 13 #include "base/threading/thread_task_runner_handle.h"
14 #include "ipc/ipc_message.h" 14 #include "ipc/ipc_message.h"
15 #include "ppapi/c/pp_errors.h" 15 #include "ppapi/c/pp_errors.h"
16 #include "ppapi/host/dispatch_host_message.h" 16 #include "ppapi/host/dispatch_host_message.h"
17 #include "ppapi/host/host_message_context.h" 17 #include "ppapi/host/host_message_context.h"
18 #include "ppapi/host/ppapi_host.h" 18 #include "ppapi/host/ppapi_host.h"
19 #include "ppapi/host/resource_host.h" 19 #include "ppapi/host/resource_host.h"
20 #include "ppapi/proxy/ppapi_messages.h" 20 #include "ppapi/proxy/ppapi_messages.h"
21 #include "ppapi/shared_impl/ppb_device_ref_shared.h" 21 #include "ppapi/shared_impl/ppb_device_ref_shared.h"
22 22
23 using ppapi::host::HostMessageContext; 23 using ppapi::host::HostMessageContext;
24 24
25 namespace content { 25 namespace content {
26 26
27 // Makes sure that StopEnumerateDevices() is called for each EnumerateDevices(). 27 // Makes sure that StopEnumerateDevices() is called for each EnumerateDevices().
28 class PepperDeviceEnumerationHostHelper::ScopedRequest 28 class PepperDeviceEnumerationHostHelper::ScopedEnumerationRequest
29 : public base::SupportsWeakPtr<ScopedRequest> { 29 : public base::SupportsWeakPtr<ScopedEnumerationRequest> {
30 public: 30 public:
31 // |owner| must outlive this object. 31 // |owner| must outlive this object.
32 ScopedRequest(PepperDeviceEnumerationHostHelper* owner, 32 ScopedEnumerationRequest(PepperDeviceEnumerationHostHelper* owner,
33 const Delegate::EnumerateDevicesCallback& callback) 33 const Delegate::DevicesCallback& callback)
34 : owner_(owner), 34 : owner_(owner),
35 callback_(callback), 35 callback_(callback),
36 requested_(false), 36 requested_(false),
37 request_id_(0),
38 sync_call_(false) { 37 sync_call_(false) {
39 if (!owner_->document_url_.is_valid()) 38 if (!owner_->document_url_.is_valid())
40 return; 39 return;
41 40
42 requested_ = true; 41 requested_ = true;
43 42
44 // Note that the callback passed into 43 // Note that the callback passed into
45 // PepperDeviceEnumerationHostHelper::Delegate::EnumerateDevices() may be 44 // PepperDeviceEnumerationHostHelper::Delegate::EnumerateDevices() may be
46 // called synchronously. In that case, |request_id_| hasn't been updated 45 // called synchronously. In that case, |callback| may destroy this
47 // when the callback is called. Moreover, |callback| may destroy this
48 // object. So we don't pass in |callback| directly. Instead, we use 46 // object. So we don't pass in |callback| directly. Instead, we use
49 // EnumerateDevicesCallbackBody() to ensure that we always call |callback| 47 // EnumerateDevicesCallbackBody() to ensure that we always call |callback|
50 // asynchronously. 48 // asynchronously.
51 sync_call_ = true; 49 sync_call_ = true;
52 DCHECK(owner_->delegate_); 50 DCHECK(owner_->delegate_);
53 request_id_ = owner_->delegate_->EnumerateDevices( 51 owner_->delegate_->EnumerateDevices(
54 owner_->device_type_, 52 owner_->device_type_, owner_->document_url_,
55 owner_->document_url_, 53 base::Bind(&ScopedEnumerationRequest::EnumerateDevicesCallbackBody,
56 base::Bind(&ScopedRequest::EnumerateDevicesCallbackBody, AsWeakPtr())); 54 AsWeakPtr()));
57 sync_call_ = false; 55 sync_call_ = false;
58 } 56 }
59 57
60 ~ScopedRequest() { 58 bool requested() const { return requested_; }
59
60 private:
61 void EnumerateDevicesCallbackBody(
62 const std::vector<ppapi::DeviceRefData>& devices) {
63 if (sync_call_) {
64 base::ThreadTaskRunnerHandle::Get()->PostTask(
65 FROM_HERE,
66 base::Bind(&ScopedEnumerationRequest::EnumerateDevicesCallbackBody,
67 AsWeakPtr(), devices));
68 } else {
69 callback_.Run(devices);
70 // This object may have been destroyed at this point.
71 }
72 }
73
74 PepperDeviceEnumerationHostHelper* owner_;
bbudge 2016/11/10 18:38:55 This isn't used outside of the constructor. Can we
Guido Urdaneta 2016/11/10 19:01:17 Done.
75 PepperDeviceEnumerationHostHelper::Delegate::DevicesCallback callback_;
76 bool requested_;
77 bool sync_call_;
78
79 DISALLOW_COPY_AND_ASSIGN(ScopedEnumerationRequest);
80 };
81
82 // Makes sure that StopMonitoringDevices() is called for each
83 // StartMonitoringDevices().
84 class PepperDeviceEnumerationHostHelper::ScopedMonitoringRequest
85 : public base::SupportsWeakPtr<ScopedMonitoringRequest> {
86 public:
87 // |owner| must outlive this object.
88 ScopedMonitoringRequest(PepperDeviceEnumerationHostHelper* owner,
89 const Delegate::DevicesCallback& callback)
90 : owner_(owner),
91 callback_(callback),
92 requested_(false),
93 subscription_id_(0) {
94 if (!owner_->document_url_.is_valid())
95 return;
96
97 requested_ = true;
98
99 DCHECK(owner_->delegate_);
100 // |callback| is never called synchronously by StartMonitoringDevices(),
101 // so it is OK to pass it directly, even if |callback| destroys |this|.
102 subscription_id_ = owner_->delegate_->StartMonitoringDevices(
103 owner_->device_type_, owner_->document_url_, callback);
104 }
105
106 ~ScopedMonitoringRequest() {
61 if (requested_ && owner_->delegate_) { 107 if (requested_ && owner_->delegate_) {
bbudge 2016/11/10 18:38:54 Maybe DCHECK(owner) here?
Guido Urdaneta 2016/11/10 19:01:17 Added DCHECK in constructor and made the field con
62 owner_->delegate_->StopEnumerateDevices(request_id_); 108 owner_->delegate_->StopMonitoringDevices(owner_->device_type_,
109 subscription_id_);
63 } 110 }
64 } 111 }
65 112
66 bool requested() const { return requested_; } 113 bool requested() const { return requested_; }
67 114
68 private: 115 private:
69 void EnumerateDevicesCallbackBody( 116 PepperDeviceEnumerationHostHelper* owner_;
70 int request_id, 117 PepperDeviceEnumerationHostHelper::Delegate::DevicesCallback callback_;
71 const std::vector<ppapi::DeviceRefData>& devices) { 118 bool requested_;
72 if (sync_call_) { 119 int subscription_id_;
73 base::ThreadTaskRunnerHandle::Get()->PostTask(
74 FROM_HERE, base::Bind(&ScopedRequest::EnumerateDevicesCallbackBody,
75 AsWeakPtr(), request_id, devices));
76 } else {
77 DCHECK_EQ(request_id_, request_id);
78 callback_.Run(request_id, devices);
79 // This object may have been destroyed at this point.
80 }
81 }
82 120
83 PepperDeviceEnumerationHostHelper* owner_; 121 DISALLOW_COPY_AND_ASSIGN(ScopedMonitoringRequest);
84 PepperDeviceEnumerationHostHelper::Delegate::EnumerateDevicesCallback
85 callback_;
86 bool requested_;
87 int request_id_;
88 bool sync_call_;
89
90 DISALLOW_COPY_AND_ASSIGN(ScopedRequest);
91 }; 122 };
92 123
93 PepperDeviceEnumerationHostHelper::PepperDeviceEnumerationHostHelper( 124 PepperDeviceEnumerationHostHelper::PepperDeviceEnumerationHostHelper(
94 ppapi::host::ResourceHost* resource_host, 125 ppapi::host::ResourceHost* resource_host,
95 base::WeakPtr<Delegate> delegate, 126 base::WeakPtr<Delegate> delegate,
96 PP_DeviceType_Dev device_type, 127 PP_DeviceType_Dev device_type,
97 const GURL& document_url) 128 const GURL& document_url)
98 : resource_host_(resource_host), 129 : resource_host_(resource_host),
99 delegate_(delegate), 130 delegate_(delegate),
100 device_type_(device_type), 131 device_type_(device_type),
(...skipping 28 matching lines...) Expand all
129 160
130 *handled = false; 161 *handled = false;
131 return PP_ERROR_FAILED; 162 return PP_ERROR_FAILED;
132 } 163 }
133 164
134 int32_t PepperDeviceEnumerationHostHelper::OnEnumerateDevices( 165 int32_t PepperDeviceEnumerationHostHelper::OnEnumerateDevices(
135 HostMessageContext* context) { 166 HostMessageContext* context) {
136 if (enumerate_devices_context_.is_valid()) 167 if (enumerate_devices_context_.is_valid())
137 return PP_ERROR_INPROGRESS; 168 return PP_ERROR_INPROGRESS;
138 169
139 enumerate_.reset(new ScopedRequest( 170 enumerate_.reset(new ScopedEnumerationRequest(
140 this, 171 this,
141 base::Bind(&PepperDeviceEnumerationHostHelper::OnEnumerateDevicesComplete, 172 base::Bind(&PepperDeviceEnumerationHostHelper::OnEnumerateDevicesComplete,
142 base::Unretained(this)))); 173 base::Unretained(this))));
143 if (!enumerate_->requested()) 174 if (!enumerate_->requested())
144 return PP_ERROR_FAILED; 175 return PP_ERROR_FAILED;
145 176
146 enumerate_devices_context_ = context->MakeReplyMessageContext(); 177 enumerate_devices_context_ = context->MakeReplyMessageContext();
147 return PP_OK_COMPLETIONPENDING; 178 return PP_OK_COMPLETIONPENDING;
148 } 179 }
149 180
150 int32_t PepperDeviceEnumerationHostHelper::OnMonitorDeviceChange( 181 int32_t PepperDeviceEnumerationHostHelper::OnMonitorDeviceChange(
151 HostMessageContext* /* context */, 182 HostMessageContext* /* context */,
152 uint32_t callback_id) { 183 uint32_t callback_id) {
153 monitor_.reset(new ScopedRequest( 184 monitor_.reset(new ScopedMonitoringRequest(
154 this, 185 this, base::Bind(&PepperDeviceEnumerationHostHelper::OnNotifyDeviceChange,
155 base::Bind(&PepperDeviceEnumerationHostHelper::OnNotifyDeviceChange, 186 base::Unretained(this), callback_id)));
156 base::Unretained(this),
157 callback_id)));
158 187
159 return monitor_->requested() ? PP_OK : PP_ERROR_FAILED; 188 return monitor_->requested() ? PP_OK : PP_ERROR_FAILED;
160 } 189 }
161 190
162 int32_t PepperDeviceEnumerationHostHelper::OnStopMonitoringDeviceChange( 191 int32_t PepperDeviceEnumerationHostHelper::OnStopMonitoringDeviceChange(
163 HostMessageContext* /* context */) { 192 HostMessageContext* /* context */) {
164 monitor_.reset(NULL); 193 monitor_.reset(NULL);
165 return PP_OK; 194 return PP_OK;
166 } 195 }
167 196
168 void PepperDeviceEnumerationHostHelper::OnEnumerateDevicesComplete( 197 void PepperDeviceEnumerationHostHelper::OnEnumerateDevicesComplete(
169 int /* request_id */,
170 const std::vector<ppapi::DeviceRefData>& devices) { 198 const std::vector<ppapi::DeviceRefData>& devices) {
171 DCHECK(enumerate_devices_context_.is_valid()); 199 DCHECK(enumerate_devices_context_.is_valid());
172 200
173 enumerate_.reset(NULL); 201 enumerate_.reset(NULL);
174 202
175 enumerate_devices_context_.params.set_result(PP_OK); 203 enumerate_devices_context_.params.set_result(PP_OK);
176 resource_host_->host()->SendReply( 204 resource_host_->host()->SendReply(
177 enumerate_devices_context_, 205 enumerate_devices_context_,
178 PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply(devices)); 206 PpapiPluginMsg_DeviceEnumeration_EnumerateDevicesReply(devices));
179 enumerate_devices_context_ = ppapi::host::ReplyMessageContext(); 207 enumerate_devices_context_ = ppapi::host::ReplyMessageContext();
180 } 208 }
181 209
182 void PepperDeviceEnumerationHostHelper::OnNotifyDeviceChange( 210 void PepperDeviceEnumerationHostHelper::OnNotifyDeviceChange(
183 uint32_t callback_id, 211 uint32_t callback_id,
184 int /* request_id */,
185 const std::vector<ppapi::DeviceRefData>& devices) { 212 const std::vector<ppapi::DeviceRefData>& devices) {
186 resource_host_->host()->SendUnsolicitedReply( 213 resource_host_->host()->SendUnsolicitedReply(
187 resource_host_->pp_resource(), 214 resource_host_->pp_resource(),
188 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange(callback_id, 215 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange(callback_id,
189 devices)); 216 devices));
190 } 217 }
191 218
192 } // namespace content 219 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698