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 "chrome/browser/media/media_stream_infobar_delegate.h"
7 #include "base/logging.h"
8 #include "base/metrics/histogram.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/infobars/infobar_service.h"
11 #include "chrome/common/url_constants.h"
12 #include "chrome/grit/generated_resources.h"
13 #include "components/google/core/browser/google_util.h"
14 #include "components/infobars/core/infobar.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/common/origin_util.h"
17 #include "grit/components_strings.h"
18 #include "grit/theme_resources.h"
19 #include "ui/base/l10n/l10n_util.h"
24 enum DevicePermissionActions
{
29 kPermissionActionsMax
// Must always be last!
34 MediaStreamInfoBarDelegate::~MediaStreamInfoBarDelegate() {
38 bool MediaStreamInfoBarDelegate::Create(
39 content::WebContents
* web_contents
,
40 const content::MediaStreamRequest
& request
,
41 const content::MediaResponseCallback
& callback
) {
42 scoped_ptr
<MediaStreamDevicesController
> controller(
43 new MediaStreamDevicesController(web_contents
, request
, callback
));
44 if (!controller
->IsAskingForAudio() && !controller
->IsAskingForVideo())
47 InfoBarService
* infobar_service
=
48 InfoBarService::FromWebContents(web_contents
);
49 if (!infobar_service
) {
50 // Deny the request if there is no place to show the infobar, e.g. when
51 // the request comes from a background extension page.
52 controller
->Cancelled();
56 scoped_ptr
<infobars::InfoBar
> infobar(
57 infobar_service
->CreateConfirmInfoBar(scoped_ptr
<ConfirmInfoBarDelegate
>(
58 new MediaStreamInfoBarDelegate(controller
.Pass()))));
59 for (size_t i
= 0; i
< infobar_service
->infobar_count(); ++i
) {
60 infobars::InfoBar
* old_infobar
= infobar_service
->infobar_at(i
);
61 if (old_infobar
->delegate()->AsMediaStreamInfoBarDelegate()) {
62 infobar_service
->ReplaceInfoBar(old_infobar
, infobar
.Pass());
66 infobar_service
->AddInfoBar(infobar
.Pass());
70 bool MediaStreamInfoBarDelegate::IsRequestingVideoAccess() const {
71 return controller_
->IsAskingForVideo();
74 bool MediaStreamInfoBarDelegate::IsRequestingMicrophoneAccess() const {
75 return controller_
->IsAskingForAudio();
78 MediaStreamInfoBarDelegate::MediaStreamInfoBarDelegate(
79 scoped_ptr
<MediaStreamDevicesController
> controller
)
80 : ConfirmInfoBarDelegate(),
81 controller_(controller
.Pass()) {
82 DCHECK(controller_
.get());
83 DCHECK(controller_
->IsAskingForAudio() || controller_
->IsAskingForVideo());
86 infobars::InfoBarDelegate::Type
87 MediaStreamInfoBarDelegate::GetInfoBarType() const {
88 return PAGE_ACTION_TYPE
;
91 int MediaStreamInfoBarDelegate::GetIconID() const {
92 return controller_
->IsAskingForVideo() ? IDR_INFOBAR_MEDIA_STREAM_CAMERA
93 : IDR_INFOBAR_MEDIA_STREAM_MIC
;
96 void MediaStreamInfoBarDelegate::InfoBarDismissed() {
97 // Deny the request if the infobar was closed with the 'x' button, since
98 // we don't want WebRTC to be waiting for an answer that will never come.
99 UMA_HISTOGRAM_ENUMERATION("Media.DevicePermissionActions",
100 kCancel
, kPermissionActionsMax
);
101 controller_
->Cancelled();
104 MediaStreamInfoBarDelegate
*
105 MediaStreamInfoBarDelegate::AsMediaStreamInfoBarDelegate() {
109 base::string16
MediaStreamInfoBarDelegate::GetMessageText() const {
110 int message_id
= IDS_MEDIA_CAPTURE_AUDIO_AND_VIDEO
;
111 if (!controller_
->IsAskingForAudio())
112 message_id
= IDS_MEDIA_CAPTURE_VIDEO_ONLY
;
113 else if (!controller_
->IsAskingForVideo())
114 message_id
= IDS_MEDIA_CAPTURE_AUDIO_ONLY
;
115 return l10n_util::GetStringFUTF16(
116 message_id
, base::UTF8ToUTF16(controller_
->GetSecurityOriginSpec()));
119 base::string16
MediaStreamInfoBarDelegate::GetButtonLabel(
120 InfoBarButton button
) const {
121 return l10n_util::GetStringUTF16((button
== BUTTON_OK
) ?
122 IDS_MEDIA_CAPTURE_ALLOW
: IDS_MEDIA_CAPTURE_BLOCK
);
125 bool MediaStreamInfoBarDelegate::Accept() {
126 GURL
origin(controller_
->GetSecurityOriginSpec());
127 if (content::IsOriginSecure(origin
)) {
128 UMA_HISTOGRAM_ENUMERATION("Media.DevicePermissionActions",
129 kAllowHttps
, kPermissionActionsMax
);
131 UMA_HISTOGRAM_ENUMERATION("Media.DevicePermissionActions",
132 kAllowHttp
, kPermissionActionsMax
);
134 controller_
->PermissionGranted();
138 bool MediaStreamInfoBarDelegate::Cancel() {
139 UMA_HISTOGRAM_ENUMERATION("Media.DevicePermissionActions",
140 kDeny
, kPermissionActionsMax
);
141 controller_
->PermissionDenied();
145 base::string16
MediaStreamInfoBarDelegate::GetLinkText() const {
146 return base::string16();
149 bool MediaStreamInfoBarDelegate::LinkClicked(
150 WindowOpenDisposition disposition
) {
151 InfoBarService::WebContentsFromInfoBar(infobar())->OpenURL(
152 content::OpenURLParams(
153 GURL(chrome::kMediaAccessLearnMoreUrl
),
155 (disposition
== CURRENT_TAB
) ? NEW_FOREGROUND_TAB
: disposition
,
156 ui::PAGE_TRANSITION_LINK
, false));
158 return false; // Do not dismiss the info bar.