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 "chrome/browser/ui/media_stream_infobar_delegate.h" |
| 6 |
5 #include <algorithm> | 7 #include <algorithm> |
6 #include <functional> | 8 #include <functional> |
7 | 9 |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "chrome/browser/ui/media_stream_infobar_delegate.h" | 11 #include "base/memory/scoped_ptr.h" |
10 #include "content/public/common/media_stream_request.h" | 12 #include "chrome/browser/media/media_stream_devices_controller.h" |
11 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
12 #include "grit/theme_resources_standard.h" | 14 #include "grit/theme_resources_standard.h" |
13 #include "ui/base/resource/resource_bundle.h" | 15 #include "ui/base/resource/resource_bundle.h" |
14 | 16 |
15 namespace { | |
16 | |
17 // A predicate that checks if a StreamDeviceInfo object has the same ID as the | |
18 // device ID specified at construction. | |
19 class DeviceIdEquals { | |
20 public: | |
21 explicit DeviceIdEquals(const std::string& device_id) | |
22 : device_id_(device_id) { | |
23 } | |
24 | |
25 bool operator() (const content::MediaStreamDevice& device) { | |
26 return device.device_id == device_id_; | |
27 } | |
28 | |
29 private: | |
30 std::string device_id_; | |
31 }; | |
32 | |
33 } // namespace | |
34 | |
35 MediaStreamInfoBarDelegate::MediaStreamInfoBarDelegate( | 17 MediaStreamInfoBarDelegate::MediaStreamInfoBarDelegate( |
36 InfoBarTabHelper* tab_helper, | 18 InfoBarTabHelper* tab_helper, |
37 const content::MediaStreamRequest* request, | 19 MediaStreamDevicesController* controller) |
38 const content::MediaResponseCallback& callback) | |
39 : InfoBarDelegate(tab_helper), | 20 : InfoBarDelegate(tab_helper), |
40 request_(request), | 21 controller_(controller) { |
41 callback_(callback) { | 22 DCHECK(controller_.get()); |
42 DCHECK(request_); | |
43 has_audio_ = request_->devices.count( | |
44 content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE) != 0; | |
45 has_video_ = request_->devices.count( | |
46 content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE) != 0; | |
47 } | 23 } |
48 | 24 |
49 MediaStreamInfoBarDelegate::~MediaStreamInfoBarDelegate() { | 25 MediaStreamInfoBarDelegate::~MediaStreamInfoBarDelegate() {} |
| 26 |
| 27 bool MediaStreamInfoBarDelegate::has_audio() const { |
| 28 return controller_->has_audio(); |
| 29 } |
| 30 |
| 31 bool MediaStreamInfoBarDelegate::has_video() const { |
| 32 return controller_->has_video(); |
50 } | 33 } |
51 | 34 |
52 content::MediaStreamDevices | 35 content::MediaStreamDevices |
53 MediaStreamInfoBarDelegate::GetAudioDevices() const { | 36 MediaStreamInfoBarDelegate::GetAudioDevices() const { |
54 if (!has_audio_) | 37 return controller_->GetAudioDevices(); |
55 return content::MediaStreamDevices(); | |
56 content::MediaStreamDeviceMap::const_iterator it = | |
57 request_->devices.find(content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE); | |
58 DCHECK(it != request_->devices.end()); | |
59 return it->second; | |
60 } | 38 } |
61 | 39 |
62 content::MediaStreamDevices | 40 content::MediaStreamDevices |
63 MediaStreamInfoBarDelegate::GetVideoDevices() const { | 41 MediaStreamInfoBarDelegate::GetVideoDevices() const { |
64 if (!has_video_) | 42 return controller_->GetVideoDevices(); |
65 return content::MediaStreamDevices(); | |
66 content::MediaStreamDeviceMap::const_iterator it = | |
67 request_->devices.find(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE); | |
68 DCHECK(it != request_->devices.end()); | |
69 return it->second; | |
70 } | 43 } |
71 | 44 |
72 const GURL& MediaStreamInfoBarDelegate::GetSecurityOrigin() const { | 45 const GURL& MediaStreamInfoBarDelegate::GetSecurityOrigin() const { |
73 return request_->security_origin; | 46 return controller_->GetSecurityOrigin(); |
74 } | 47 } |
75 | 48 |
76 void MediaStreamInfoBarDelegate::Accept(const std::string& audio_id, | 49 void MediaStreamInfoBarDelegate::Accept(const std::string& audio_id, |
77 const std::string& video_id) { | 50 const std::string& video_id, |
78 content::MediaStreamDevices devices; | 51 bool always_allow) { |
79 | 52 controller_->Accept(audio_id, video_id, always_allow); |
80 if (has_audio_) { | |
81 AddDeviceWithId(content::MEDIA_STREAM_DEVICE_TYPE_AUDIO_CAPTURE, | |
82 audio_id, &devices); | |
83 } | |
84 if (has_video_) { | |
85 AddDeviceWithId(content::MEDIA_STREAM_DEVICE_TYPE_VIDEO_CAPTURE, | |
86 video_id, &devices); | |
87 } | |
88 | |
89 callback_.Run(devices); | |
90 } | 53 } |
91 | 54 |
92 void MediaStreamInfoBarDelegate::Deny() { | 55 void MediaStreamInfoBarDelegate::Deny() { |
93 callback_.Run(content::MediaStreamDevices()); | 56 controller_->Deny(); |
94 } | |
95 | |
96 void MediaStreamInfoBarDelegate::AddDeviceWithId( | |
97 content::MediaStreamDeviceType type, | |
98 const std::string& id, | |
99 content::MediaStreamDevices* devices) { | |
100 DCHECK(devices); | |
101 content::MediaStreamDeviceMap::const_iterator device_it = | |
102 request_->devices.find(type); | |
103 if (device_it != request_->devices.end()) { | |
104 content::MediaStreamDevices::const_iterator it = std::find_if( | |
105 device_it->second.begin(), device_it->second.end(), DeviceIdEquals(id)); | |
106 if (it != device_it->second.end()) | |
107 devices->push_back(*it); | |
108 } | |
109 } | 57 } |
110 | 58 |
111 // MediaStreamInfoBarDelegate::CreateInfoBar is implemented in platform-specific | 59 // MediaStreamInfoBarDelegate::CreateInfoBar is implemented in platform-specific |
112 // files. | 60 // files. |
113 | 61 |
114 void MediaStreamInfoBarDelegate::InfoBarDismissed() { | 62 void MediaStreamInfoBarDelegate::InfoBarDismissed() { |
115 // Deny the request if the infobar was closed with the 'x' button, since | 63 // Deny the request if the infobar was closed with the 'x' button, since |
116 // we don't want WebRTC to be waiting for an answer that will never come. | 64 // we don't want WebRTC to be waiting for an answer that will never come. |
117 Deny(); | 65 Deny(); |
118 } | 66 } |
119 | 67 |
120 gfx::Image* MediaStreamInfoBarDelegate::GetIcon() const { | 68 gfx::Image* MediaStreamInfoBarDelegate::GetIcon() const { |
121 return &ResourceBundle::GetSharedInstance().GetNativeImageNamed(has_video_ ? | 69 return &ResourceBundle::GetSharedInstance().GetNativeImageNamed(has_video() ? |
122 IDR_INFOBAR_MEDIA_STREAM_CAMERA : IDR_INFOBAR_MEDIA_STREAM_MIC); | 70 IDR_INFOBAR_MEDIA_STREAM_CAMERA : IDR_INFOBAR_MEDIA_STREAM_MIC); |
123 } | 71 } |
124 | 72 |
125 InfoBarDelegate::Type MediaStreamInfoBarDelegate::GetInfoBarType() const { | 73 InfoBarDelegate::Type MediaStreamInfoBarDelegate::GetInfoBarType() const { |
126 return PAGE_ACTION_TYPE; | 74 return PAGE_ACTION_TYPE; |
127 } | 75 } |
128 | 76 |
129 MediaStreamInfoBarDelegate* | 77 MediaStreamInfoBarDelegate* |
130 MediaStreamInfoBarDelegate::AsMediaStreamInfoBarDelegate() { | 78 MediaStreamInfoBarDelegate::AsMediaStreamInfoBarDelegate() { |
131 return this; | 79 return this; |
132 } | 80 } |
OLD | NEW |