- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / media / media_stream_infobar_delegate.cc
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/google/google_util.h"
11 #include "chrome/browser/infobars/infobar_service.h"
12 #include "chrome/common/url_constants.h"
13 #include "content/public/browser/web_contents.h"
14 #include "grit/generated_resources.h"
15 #include "grit/theme_resources.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "url/gurl.h"
18
19 namespace {
20
21 enum DevicePermissionActions {
22   kAllowHttps = 0,
23   kAllowHttp,
24   kDeny,
25   kCancel,
26   kPermissionActionsMax  // Must always be last!
27 };
28
29 }  // namespace
30
31 MediaStreamInfoBarDelegate::~MediaStreamInfoBarDelegate() {
32 }
33
34 // static
35 bool MediaStreamInfoBarDelegate::Create(
36     content::WebContents* web_contents,
37     const content::MediaStreamRequest& request,
38     const content::MediaResponseCallback& callback) {
39   scoped_ptr<MediaStreamDevicesController> controller(
40       new MediaStreamDevicesController(web_contents, request, callback));
41   if (controller->DismissInfoBarAndTakeActionOnSettings())
42     return false;
43
44   InfoBarService* infobar_service =
45       InfoBarService::FromWebContents(web_contents);
46   if (!infobar_service) {
47     // Deny the request if there is no place to show the infobar, e.g. when
48     // the request comes from a background extension page.
49     controller->Deny(false);
50     return false;
51   }
52
53   scoped_ptr<InfoBarDelegate> infobar(
54       new MediaStreamInfoBarDelegate(infobar_service, controller.Pass()));
55   for (size_t i = 0; i < infobar_service->infobar_count(); ++i) {
56     InfoBarDelegate* old_infobar =
57         infobar_service->infobar_at(i)->AsMediaStreamInfoBarDelegate();
58     if (old_infobar) {
59       infobar_service->ReplaceInfoBar(old_infobar, infobar.Pass());
60       return true;
61     }
62   }
63   infobar_service->AddInfoBar(infobar.Pass());
64   return true;
65 }
66
67 MediaStreamInfoBarDelegate::MediaStreamInfoBarDelegate(
68     InfoBarService* infobar_service,
69     scoped_ptr<MediaStreamDevicesController> controller)
70     : ConfirmInfoBarDelegate(infobar_service),
71       controller_(controller.Pass()) {
72   DCHECK(controller_.get());
73   DCHECK(controller_->HasAudio() || controller_->HasVideo());
74 }
75
76 void MediaStreamInfoBarDelegate::InfoBarDismissed() {
77   // Deny the request if the infobar was closed with the 'x' button, since
78   // we don't want WebRTC to be waiting for an answer that will never come.
79   UMA_HISTOGRAM_ENUMERATION("Media.DevicePermissionActions",
80                             kCancel, kPermissionActionsMax);
81   controller_->Deny(false);
82 }
83
84 int MediaStreamInfoBarDelegate::GetIconID() const {
85   return controller_->HasVideo() ?
86       IDR_INFOBAR_MEDIA_STREAM_CAMERA : IDR_INFOBAR_MEDIA_STREAM_MIC;
87 }
88
89 InfoBarDelegate::Type MediaStreamInfoBarDelegate::GetInfoBarType() const {
90   return PAGE_ACTION_TYPE;
91 }
92
93 MediaStreamInfoBarDelegate*
94     MediaStreamInfoBarDelegate::AsMediaStreamInfoBarDelegate() {
95   return this;
96 }
97
98 string16 MediaStreamInfoBarDelegate::GetMessageText() const {
99   int message_id = IDS_MEDIA_CAPTURE_AUDIO_AND_VIDEO;
100   if (!controller_->HasAudio())
101     message_id = IDS_MEDIA_CAPTURE_VIDEO_ONLY;
102   else if (!controller_->HasVideo())
103     message_id = IDS_MEDIA_CAPTURE_AUDIO_ONLY;
104   return l10n_util::GetStringFUTF16(
105       message_id, UTF8ToUTF16(controller_->GetSecurityOriginSpec()));
106 }
107
108 string16 MediaStreamInfoBarDelegate::GetButtonLabel(
109     InfoBarButton button) const {
110   return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
111       IDS_MEDIA_CAPTURE_ALLOW : IDS_MEDIA_CAPTURE_DENY);
112 }
113
114 bool MediaStreamInfoBarDelegate::Accept() {
115   GURL origin(controller_->GetSecurityOriginSpec());
116   if (origin.SchemeIsSecure()) {
117     UMA_HISTOGRAM_ENUMERATION("Media.DevicePermissionActions",
118                               kAllowHttps, kPermissionActionsMax);
119   } else {
120     UMA_HISTOGRAM_ENUMERATION("Media.DevicePermissionActions",
121                               kAllowHttp, kPermissionActionsMax);
122   }
123   controller_->Accept(true);
124   return true;
125 }
126
127 bool MediaStreamInfoBarDelegate::Cancel() {
128   UMA_HISTOGRAM_ENUMERATION("Media.DevicePermissionActions",
129                             kDeny, kPermissionActionsMax);
130   controller_->Deny(true);
131   return true;
132 }
133
134 string16 MediaStreamInfoBarDelegate::GetLinkText() const {
135   return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
136 }
137
138 bool MediaStreamInfoBarDelegate::LinkClicked(
139     WindowOpenDisposition disposition) {
140   web_contents()->OpenURL(content::OpenURLParams(
141       google_util::AppendGoogleLocaleParam(
142           GURL(chrome::kMediaAccessLearnMoreUrl)),
143       content::Referrer(),
144       (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition,
145       content::PAGE_TRANSITION_LINK,
146       false));
147
148   return false;  // Do not dismiss the info bar.
149 }