Disable ContentSettingBubbleModelTest.RPHAllow which is flaky.
[chromium-blink-merge.git] / content / renderer / pepper / pepper_device_enumeration_event_handler.cc
blob7a8dce63ca501f92144fc63d56eeeb460fb52294
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
3 // found in the LICENSE file.
5 #include "content/renderer/pepper/pepper_device_enumeration_event_handler.h"
7 #include "base/logging.h"
8 #include "ppapi/shared_impl/ppb_device_ref_shared.h"
10 namespace content {
12 namespace {
14 ppapi::DeviceRefData FromStreamDeviceInfo(const StreamDeviceInfo& info) {
15 ppapi::DeviceRefData data;
16 data.id = info.device.id;
17 data.name = info.device.name;
18 data.type = PepperDeviceEnumerationEventHandler::FromMediaStreamType(
19 info.device.type);
20 return data;
23 } // namespace
25 PepperDeviceEnumerationEventHandler::PepperDeviceEnumerationEventHandler()
26 : next_id_(1) {
29 PepperDeviceEnumerationEventHandler::~PepperDeviceEnumerationEventHandler() {
30 DCHECK(enumerate_callbacks_.empty());
31 DCHECK(open_callbacks_.empty());
34 int PepperDeviceEnumerationEventHandler::RegisterEnumerateDevicesCallback(
35 const webkit::ppapi::PluginDelegate::EnumerateDevicesCallback& callback) {
36 enumerate_callbacks_[next_id_] = callback;
37 return next_id_++;
40 void PepperDeviceEnumerationEventHandler::UnregisterEnumerateDevicesCallback(
41 int request_id) {
42 enumerate_callbacks_.erase(request_id);
45 int PepperDeviceEnumerationEventHandler::RegisterOpenDeviceCallback(
46 const PepperPluginDelegateImpl::OpenDeviceCallback& callback) {
47 open_callbacks_[next_id_] = callback;
48 return next_id_++;
51 void PepperDeviceEnumerationEventHandler::OnStreamGenerated(
52 int request_id,
53 const std::string& label,
54 const StreamDeviceInfoArray& audio_device_array,
55 const StreamDeviceInfoArray& video_device_array) {
58 void PepperDeviceEnumerationEventHandler::OnStreamGenerationFailed(
59 int request_id) {
62 void PepperDeviceEnumerationEventHandler::OnDevicesEnumerated(
63 int request_id,
64 const StreamDeviceInfoArray& device_array) {
65 NotifyDevicesEnumerated(request_id, true, device_array);
68 void PepperDeviceEnumerationEventHandler::OnDevicesEnumerationFailed(
69 int request_id) {
70 NotifyDevicesEnumerated(request_id, false, StreamDeviceInfoArray());
73 void PepperDeviceEnumerationEventHandler::OnDeviceOpened(
74 int request_id,
75 const std::string& label,
76 const StreamDeviceInfo& device_info) {
77 NotifyDeviceOpened(request_id, true, label);
80 void PepperDeviceEnumerationEventHandler::OnDeviceOpenFailed(int request_id) {
81 NotifyDeviceOpened(request_id, false, std::string());
84 // static
85 MediaStreamType PepperDeviceEnumerationEventHandler::FromPepperDeviceType(
86 PP_DeviceType_Dev type) {
87 switch (type) {
88 case PP_DEVICETYPE_DEV_INVALID:
89 return MEDIA_NO_SERVICE;
90 case PP_DEVICETYPE_DEV_AUDIOCAPTURE:
91 return MEDIA_DEVICE_AUDIO_CAPTURE;
92 case PP_DEVICETYPE_DEV_VIDEOCAPTURE:
93 return MEDIA_DEVICE_VIDEO_CAPTURE;
94 default:
95 NOTREACHED();
96 return MEDIA_NO_SERVICE;
100 // static
101 PP_DeviceType_Dev PepperDeviceEnumerationEventHandler::FromMediaStreamType(
102 MediaStreamType type) {
103 switch (type) {
104 case MEDIA_NO_SERVICE:
105 return PP_DEVICETYPE_DEV_INVALID;
106 case MEDIA_DEVICE_AUDIO_CAPTURE:
107 return PP_DEVICETYPE_DEV_AUDIOCAPTURE;
108 case MEDIA_DEVICE_VIDEO_CAPTURE:
109 return PP_DEVICETYPE_DEV_VIDEOCAPTURE;
110 default:
111 NOTREACHED();
112 return PP_DEVICETYPE_DEV_INVALID;
116 void PepperDeviceEnumerationEventHandler::NotifyDevicesEnumerated(
117 int request_id,
118 bool succeeded,
119 const StreamDeviceInfoArray& device_array) {
120 EnumerateCallbackMap::iterator iter = enumerate_callbacks_.find(request_id);
121 if (iter == enumerate_callbacks_.end()) {
122 // This might be enumerated result sent before StopEnumerateDevices is
123 // called since EnumerateDevices is persistent request.
124 return;
127 webkit::ppapi::PluginDelegate::EnumerateDevicesCallback callback =
128 iter->second;
130 std::vector<ppapi::DeviceRefData> devices;
131 if (succeeded) {
132 devices.reserve(device_array.size());
133 for (StreamDeviceInfoArray::const_iterator info =
134 device_array.begin(); info != device_array.end(); ++info) {
135 devices.push_back(FromStreamDeviceInfo(*info));
138 callback.Run(request_id, succeeded, devices);
141 void PepperDeviceEnumerationEventHandler::NotifyDeviceOpened(
142 int request_id,
143 bool succeeded,
144 const std::string& label) {
145 OpenCallbackMap::iterator iter = open_callbacks_.find(request_id);
146 if (iter == open_callbacks_.end()) {
147 NOTREACHED();
148 return;
151 PepperPluginDelegateImpl::OpenDeviceCallback callback = iter->second;
152 open_callbacks_.erase(iter);
154 callback.Run(request_id, succeeded, label);
157 } // namespace content