Componentize ShortcutsBackend
[chromium-blink-merge.git] / chrome / browser / media / media_stream_infobar_delegate.cc
blob6c9c1c03b1ea0f0930872221de00198bd7829c28
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 "grit/components_strings.h"
17 #include "grit/theme_resources.h"
18 #include "ui/base/l10n/l10n_util.h"
19 #include "url/gurl.h"
21 namespace {
23 enum DevicePermissionActions {
24 kAllowHttps = 0,
25 kAllowHttp,
26 kDeny,
27 kCancel,
28 kPermissionActionsMax // Must always be last!
31 } // namespace
33 MediaStreamInfoBarDelegate::~MediaStreamInfoBarDelegate() {
36 // static
37 bool MediaStreamInfoBarDelegate::Create(
38 content::WebContents* web_contents,
39 const content::MediaStreamRequest& request,
40 const content::MediaResponseCallback& callback) {
41 scoped_ptr<MediaStreamDevicesController> controller(
42 new MediaStreamDevicesController(web_contents, request, callback));
43 if (controller->DismissInfoBarAndTakeActionOnSettings())
44 return false;
46 InfoBarService* infobar_service =
47 InfoBarService::FromWebContents(web_contents);
48 if (!infobar_service) {
49 // Deny the request if there is no place to show the infobar, e.g. when
50 // the request comes from a background extension page.
51 controller->Deny(false, content::MEDIA_DEVICE_INVALID_STATE);
52 return false;
55 scoped_ptr<infobars::InfoBar> infobar(
56 infobar_service->CreateConfirmInfoBar(scoped_ptr<ConfirmInfoBarDelegate>(
57 new MediaStreamInfoBarDelegate(controller.Pass()))));
58 for (size_t i = 0; i < infobar_service->infobar_count(); ++i) {
59 infobars::InfoBar* old_infobar = infobar_service->infobar_at(i);
60 if (old_infobar->delegate()->AsMediaStreamInfoBarDelegate()) {
61 infobar_service->ReplaceInfoBar(old_infobar, infobar.Pass());
62 return true;
65 infobar_service->AddInfoBar(infobar.Pass());
66 return true;
69 bool MediaStreamInfoBarDelegate::IsRequestingVideoAccess() const {
70 return controller_->HasVideo();
73 bool MediaStreamInfoBarDelegate::IsRequestingMicrophoneAccess() const {
74 return controller_->HasAudio();
77 MediaStreamInfoBarDelegate::MediaStreamInfoBarDelegate(
78 scoped_ptr<MediaStreamDevicesController> controller)
79 : ConfirmInfoBarDelegate(),
80 controller_(controller.Pass()) {
81 DCHECK(controller_.get());
82 DCHECK(controller_->HasAudio() || controller_->HasVideo());
85 infobars::InfoBarDelegate::Type
86 MediaStreamInfoBarDelegate::GetInfoBarType() const {
87 return PAGE_ACTION_TYPE;
90 int MediaStreamInfoBarDelegate::GetIconID() const {
91 return controller_->HasVideo() ?
92 IDR_INFOBAR_MEDIA_STREAM_CAMERA : IDR_INFOBAR_MEDIA_STREAM_MIC;
95 void MediaStreamInfoBarDelegate::InfoBarDismissed() {
96 // Deny the request if the infobar was closed with the 'x' button, since
97 // we don't want WebRTC to be waiting for an answer that will never come.
98 UMA_HISTOGRAM_ENUMERATION("Media.DevicePermissionActions",
99 kCancel, kPermissionActionsMax);
100 controller_->Deny(false, content::MEDIA_DEVICE_PERMISSION_DISMISSED);
103 MediaStreamInfoBarDelegate*
104 MediaStreamInfoBarDelegate::AsMediaStreamInfoBarDelegate() {
105 return this;
108 base::string16 MediaStreamInfoBarDelegate::GetMessageText() const {
109 int message_id = IDS_MEDIA_CAPTURE_AUDIO_AND_VIDEO;
110 if (!controller_->HasAudio())
111 message_id = IDS_MEDIA_CAPTURE_VIDEO_ONLY;
112 else if (!controller_->HasVideo())
113 message_id = IDS_MEDIA_CAPTURE_AUDIO_ONLY;
114 return l10n_util::GetStringFUTF16(
115 message_id, base::UTF8ToUTF16(controller_->GetSecurityOriginSpec()));
118 base::string16 MediaStreamInfoBarDelegate::GetButtonLabel(
119 InfoBarButton button) const {
120 return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
121 IDS_MEDIA_CAPTURE_ALLOW : IDS_MEDIA_CAPTURE_BLOCK);
124 bool MediaStreamInfoBarDelegate::Accept() {
125 GURL origin(controller_->GetSecurityOriginSpec());
126 if (origin.SchemeIsSecure()) {
127 UMA_HISTOGRAM_ENUMERATION("Media.DevicePermissionActions",
128 kAllowHttps, kPermissionActionsMax);
129 } else {
130 UMA_HISTOGRAM_ENUMERATION("Media.DevicePermissionActions",
131 kAllowHttp, kPermissionActionsMax);
133 controller_->Accept(true);
134 return true;
137 bool MediaStreamInfoBarDelegate::Cancel() {
138 UMA_HISTOGRAM_ENUMERATION("Media.DevicePermissionActions",
139 kDeny, kPermissionActionsMax);
140 controller_->Deny(true, content::MEDIA_DEVICE_PERMISSION_DENIED);
141 return true;
144 base::string16 MediaStreamInfoBarDelegate::GetLinkText() const {
145 return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
148 bool MediaStreamInfoBarDelegate::LinkClicked(
149 WindowOpenDisposition disposition) {
150 InfoBarService::WebContentsFromInfoBar(infobar())->OpenURL(
151 content::OpenURLParams(
152 GURL(chrome::kMediaAccessLearnMoreUrl),
153 content::Referrer(),
154 (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition,
155 ui::PAGE_TRANSITION_LINK, false));
157 return false; // Do not dismiss the info bar.