| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/media/media_stream_infobar_delegate.h" | |
| 6 | |
| 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" | |
| 20 #include "url/gurl.h" | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 enum DevicePermissionActions { | |
| 25 kAllowHttps = 0, | |
| 26 kAllowHttp, | |
| 27 kDeny, | |
| 28 kCancel, | |
| 29 kPermissionActionsMax // Must always be last! | |
| 30 }; | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 MediaStreamInfoBarDelegate::~MediaStreamInfoBarDelegate() { | |
| 35 } | |
| 36 | |
| 37 // static | |
| 38 bool MediaStreamInfoBarDelegate::Create( | |
| 39 content::WebContents* web_contents, | |
| 40 scoped_ptr<MediaStreamDevicesController> controller) { | |
| 41 | |
| 42 InfoBarService* infobar_service = | |
| 43 InfoBarService::FromWebContents(web_contents); | |
| 44 if (!infobar_service) { | |
| 45 // Deny the request if there is no place to show the infobar, e.g. when | |
| 46 // the request comes from a background extension page. | |
| 47 controller->Cancelled(); | |
| 48 return false; | |
| 49 } | |
| 50 | |
| 51 scoped_ptr<infobars::InfoBar> infobar( | |
| 52 infobar_service->CreateConfirmInfoBar(scoped_ptr<ConfirmInfoBarDelegate>( | |
| 53 new MediaStreamInfoBarDelegate(controller.Pass())))); | |
| 54 for (size_t i = 0; i < infobar_service->infobar_count(); ++i) { | |
| 55 infobars::InfoBar* old_infobar = infobar_service->infobar_at(i); | |
| 56 if (old_infobar->delegate()->AsMediaStreamInfoBarDelegate()) { | |
| 57 infobar_service->ReplaceInfoBar(old_infobar, infobar.Pass()); | |
| 58 return true; | |
| 59 } | |
| 60 } | |
| 61 infobar_service->AddInfoBar(infobar.Pass()); | |
| 62 return true; | |
| 63 } | |
| 64 | |
| 65 bool MediaStreamInfoBarDelegate::IsRequestingVideoAccess() const { | |
| 66 return controller_->IsAskingForVideo(); | |
| 67 } | |
| 68 | |
| 69 bool MediaStreamInfoBarDelegate::IsRequestingMicrophoneAccess() const { | |
| 70 return controller_->IsAskingForAudio(); | |
| 71 } | |
| 72 | |
| 73 MediaStreamInfoBarDelegate::MediaStreamInfoBarDelegate( | |
| 74 scoped_ptr<MediaStreamDevicesController> controller) | |
| 75 : ConfirmInfoBarDelegate(), | |
| 76 controller_(controller.Pass()) { | |
| 77 DCHECK(controller_.get()); | |
| 78 DCHECK(controller_->IsAskingForAudio() || controller_->IsAskingForVideo()); | |
| 79 } | |
| 80 | |
| 81 infobars::InfoBarDelegate::Type | |
| 82 MediaStreamInfoBarDelegate::GetInfoBarType() const { | |
| 83 return PAGE_ACTION_TYPE; | |
| 84 } | |
| 85 | |
| 86 int MediaStreamInfoBarDelegate::GetIconId() const { | |
| 87 return controller_->IsAskingForVideo() ? IDR_INFOBAR_MEDIA_STREAM_CAMERA | |
| 88 : IDR_INFOBAR_MEDIA_STREAM_MIC; | |
| 89 } | |
| 90 | |
| 91 void MediaStreamInfoBarDelegate::InfoBarDismissed() { | |
| 92 // Deny the request if the infobar was closed with the 'x' button, since | |
| 93 // we don't want WebRTC to be waiting for an answer that will never come. | |
| 94 UMA_HISTOGRAM_ENUMERATION("Media.DevicePermissionActions", | |
| 95 kCancel, kPermissionActionsMax); | |
| 96 controller_->Cancelled(); | |
| 97 } | |
| 98 | |
| 99 MediaStreamInfoBarDelegate* | |
| 100 MediaStreamInfoBarDelegate::AsMediaStreamInfoBarDelegate() { | |
| 101 return this; | |
| 102 } | |
| 103 | |
| 104 base::string16 MediaStreamInfoBarDelegate::GetMessageText() const { | |
| 105 int message_id = IDS_MEDIA_CAPTURE_AUDIO_AND_VIDEO; | |
| 106 if (!controller_->IsAskingForAudio()) | |
| 107 message_id = IDS_MEDIA_CAPTURE_VIDEO_ONLY; | |
| 108 else if (!controller_->IsAskingForVideo()) | |
| 109 message_id = IDS_MEDIA_CAPTURE_AUDIO_ONLY; | |
| 110 return l10n_util::GetStringFUTF16( | |
| 111 message_id, base::UTF8ToUTF16(controller_->GetSecurityOriginSpec())); | |
| 112 } | |
| 113 | |
| 114 base::string16 MediaStreamInfoBarDelegate::GetButtonLabel( | |
| 115 InfoBarButton button) const { | |
| 116 return l10n_util::GetStringUTF16((button == BUTTON_OK) ? | |
| 117 IDS_MEDIA_CAPTURE_ALLOW : IDS_MEDIA_CAPTURE_BLOCK); | |
| 118 } | |
| 119 | |
| 120 bool MediaStreamInfoBarDelegate::Accept() { | |
| 121 GURL origin(controller_->GetSecurityOriginSpec()); | |
| 122 if (content::IsOriginSecure(origin)) { | |
| 123 UMA_HISTOGRAM_ENUMERATION("Media.DevicePermissionActions", | |
| 124 kAllowHttps, kPermissionActionsMax); | |
| 125 } else { | |
| 126 UMA_HISTOGRAM_ENUMERATION("Media.DevicePermissionActions", | |
| 127 kAllowHttp, kPermissionActionsMax); | |
| 128 } | |
| 129 controller_->PermissionGranted(); | |
| 130 return true; | |
| 131 } | |
| 132 | |
| 133 bool MediaStreamInfoBarDelegate::Cancel() { | |
| 134 UMA_HISTOGRAM_ENUMERATION("Media.DevicePermissionActions", | |
| 135 kDeny, kPermissionActionsMax); | |
| 136 controller_->PermissionDenied(); | |
| 137 return true; | |
| 138 } | |
| 139 | |
| 140 base::string16 MediaStreamInfoBarDelegate::GetLinkText() const { | |
| 141 return base::string16(); | |
| 142 } | |
| 143 | |
| 144 GURL MediaStreamInfoBarDelegate::GetLinkURL() const { | |
| 145 return GURL(chrome::kMediaAccessLearnMoreUrl); | |
| 146 } | |
| OLD | NEW |