OLD | NEW |
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" |
(...skipping 14 matching lines...) Expand all Loading... |
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::ScopedEnumerationRequest | 28 class PepperDeviceEnumerationHostHelper::ScopedEnumerationRequest |
29 : public base::SupportsWeakPtr<ScopedEnumerationRequest> { | 29 : public base::SupportsWeakPtr<ScopedEnumerationRequest> { |
30 public: | 30 public: |
31 // |owner| must outlive this object. | 31 // |owner| must outlive this object. |
32 ScopedEnumerationRequest(PepperDeviceEnumerationHostHelper* owner, | 32 ScopedEnumerationRequest(PepperDeviceEnumerationHostHelper* owner, |
33 const Delegate::DevicesCallback& callback) | 33 const Delegate::DevicesCallback& callback) |
34 : callback_(callback), requested_(false), sync_call_(false) { | 34 : callback_(callback), requested_(false), sync_call_(false) { |
35 if (!owner->document_url_.is_valid()) | 35 if (!owner->delegate_) { |
| 36 // If no delegate, return an empty list of devices. |
| 37 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 38 FROM_HERE, |
| 39 base::Bind(&ScopedEnumerationRequest::EnumerateDevicesCallbackBody, |
| 40 AsWeakPtr(), std::vector<ppapi::DeviceRefData>())); |
36 return; | 41 return; |
37 | 42 } |
38 if (!owner->delegate_) | |
39 return; | |
40 | 43 |
41 requested_ = true; | 44 requested_ = true; |
42 | 45 |
43 // Note that the callback passed into | 46 // Note that the callback passed into |
44 // PepperDeviceEnumerationHostHelper::Delegate::EnumerateDevices() may be | 47 // PepperDeviceEnumerationHostHelper::Delegate::EnumerateDevices() may be |
45 // called synchronously. In that case, |callback| may destroy this | 48 // called synchronously. In that case, |callback| may destroy this |
46 // object. So we don't pass in |callback| directly. Instead, we use | 49 // object. So we don't pass in |callback| directly. Instead, we use |
47 // EnumerateDevicesCallbackBody() to ensure that we always call |callback| | 50 // EnumerateDevicesCallbackBody() to ensure that we always call |callback| |
48 // asynchronously. | 51 // asynchronously. |
49 sync_call_ = true; | 52 sync_call_ = true; |
50 owner->delegate_->EnumerateDevices( | 53 owner->delegate_->EnumerateDevices( |
51 owner->device_type_, owner->document_url_, | 54 owner->device_type_, |
52 base::Bind(&ScopedEnumerationRequest::EnumerateDevicesCallbackBody, | 55 base::Bind(&ScopedEnumerationRequest::EnumerateDevicesCallbackBody, |
53 AsWeakPtr())); | 56 AsWeakPtr())); |
54 sync_call_ = false; | 57 sync_call_ = false; |
55 } | 58 } |
56 | 59 |
57 bool requested() const { return requested_; } | 60 bool requested() const { return requested_; } |
58 | 61 |
59 private: | 62 private: |
60 void EnumerateDevicesCallbackBody( | 63 void EnumerateDevicesCallbackBody( |
61 const std::vector<ppapi::DeviceRefData>& devices) { | 64 const std::vector<ppapi::DeviceRefData>& devices) { |
(...skipping 21 matching lines...) Expand all Loading... |
83 : public base::SupportsWeakPtr<ScopedMonitoringRequest> { | 86 : public base::SupportsWeakPtr<ScopedMonitoringRequest> { |
84 public: | 87 public: |
85 // |owner| must outlive this object. | 88 // |owner| must outlive this object. |
86 ScopedMonitoringRequest(PepperDeviceEnumerationHostHelper* owner, | 89 ScopedMonitoringRequest(PepperDeviceEnumerationHostHelper* owner, |
87 const Delegate::DevicesCallback& callback) | 90 const Delegate::DevicesCallback& callback) |
88 : owner_(owner), | 91 : owner_(owner), |
89 callback_(callback), | 92 callback_(callback), |
90 requested_(false), | 93 requested_(false), |
91 subscription_id_(0) { | 94 subscription_id_(0) { |
92 DCHECK(owner_); | 95 DCHECK(owner_); |
93 if (!owner_->document_url_.is_valid()) | 96 if (!owner->delegate_) { |
94 return; | 97 return; |
95 | 98 } |
96 if (!owner->delegate_) | |
97 return; | |
98 | 99 |
99 requested_ = true; | 100 requested_ = true; |
100 | 101 |
101 // |callback| is never called synchronously by StartMonitoringDevices(), | 102 // |callback| is never called synchronously by StartMonitoringDevices(), |
102 // so it is OK to pass it directly, even if |callback| destroys |this|. | 103 // so it is OK to pass it directly, even if |callback| destroys |this|. |
103 subscription_id_ = owner_->delegate_->StartMonitoringDevices( | 104 subscription_id_ = owner_->delegate_->StartMonitoringDevices( |
104 owner_->device_type_, owner_->document_url_, callback); | 105 owner_->device_type_, callback); |
105 } | 106 } |
106 | 107 |
107 ~ScopedMonitoringRequest() { | 108 ~ScopedMonitoringRequest() { |
108 if (requested_ && owner_->delegate_) { | 109 if (requested_ && owner_->delegate_) { |
109 owner_->delegate_->StopMonitoringDevices(owner_->device_type_, | 110 owner_->delegate_->StopMonitoringDevices(owner_->device_type_, |
110 subscription_id_); | 111 subscription_id_); |
111 } | 112 } |
112 } | 113 } |
113 | 114 |
114 bool requested() const { return requested_; } | 115 bool requested() const { return requested_; } |
115 | 116 |
116 private: | 117 private: |
117 PepperDeviceEnumerationHostHelper* const owner_; | 118 PepperDeviceEnumerationHostHelper* const owner_; |
118 PepperDeviceEnumerationHostHelper::Delegate::DevicesCallback callback_; | 119 PepperDeviceEnumerationHostHelper::Delegate::DevicesCallback callback_; |
119 bool requested_; | 120 bool requested_; |
120 int subscription_id_; | 121 int subscription_id_; |
121 | 122 |
122 DISALLOW_COPY_AND_ASSIGN(ScopedMonitoringRequest); | 123 DISALLOW_COPY_AND_ASSIGN(ScopedMonitoringRequest); |
123 }; | 124 }; |
124 | 125 |
125 PepperDeviceEnumerationHostHelper::PepperDeviceEnumerationHostHelper( | 126 PepperDeviceEnumerationHostHelper::PepperDeviceEnumerationHostHelper( |
126 ppapi::host::ResourceHost* resource_host, | 127 ppapi::host::ResourceHost* resource_host, |
127 base::WeakPtr<Delegate> delegate, | 128 base::WeakPtr<Delegate> delegate, |
128 PP_DeviceType_Dev device_type, | 129 PP_DeviceType_Dev device_type, |
129 const GURL& document_url) | 130 const GURL& document_url) |
130 : resource_host_(resource_host), | 131 : resource_host_(resource_host), |
131 delegate_(delegate), | 132 delegate_(delegate), |
132 device_type_(device_type), | 133 device_type_(device_type) {} |
133 document_url_(document_url) {} | |
134 | 134 |
135 PepperDeviceEnumerationHostHelper::~PepperDeviceEnumerationHostHelper() {} | 135 PepperDeviceEnumerationHostHelper::~PepperDeviceEnumerationHostHelper() {} |
136 | 136 |
137 bool PepperDeviceEnumerationHostHelper::HandleResourceMessage( | 137 bool PepperDeviceEnumerationHostHelper::HandleResourceMessage( |
138 const IPC::Message& msg, | 138 const IPC::Message& msg, |
139 HostMessageContext* context, | 139 HostMessageContext* context, |
140 int32_t* result) { | 140 int32_t* result) { |
141 bool return_value = false; | 141 bool return_value = false; |
142 *result = InternalHandleResourceMessage(msg, context, &return_value); | 142 *result = InternalHandleResourceMessage(msg, context, &return_value); |
143 return return_value; | 143 return return_value; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 void PepperDeviceEnumerationHostHelper::OnNotifyDeviceChange( | 211 void PepperDeviceEnumerationHostHelper::OnNotifyDeviceChange( |
212 uint32_t callback_id, | 212 uint32_t callback_id, |
213 const std::vector<ppapi::DeviceRefData>& devices) { | 213 const std::vector<ppapi::DeviceRefData>& devices) { |
214 resource_host_->host()->SendUnsolicitedReply( | 214 resource_host_->host()->SendUnsolicitedReply( |
215 resource_host_->pp_resource(), | 215 resource_host_->pp_resource(), |
216 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange(callback_id, | 216 PpapiPluginMsg_DeviceEnumeration_NotifyDeviceChange(callback_id, |
217 devices)); | 217 devices)); |
218 } | 218 } |
219 | 219 |
220 } // namespace content | 220 } // namespace content |
OLD | NEW |