Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(260)

Side by Side Diff: chrome/browser/media/webrtc/media_stream_infobar_delegate_android.cc

Issue 2341953004: Decouple MediaStreamInfoBarDelegate from GroupedPermissionInfoBarDelegate (Closed)
Patch Set: nits change Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/media/webrtc/media_stream_infobar_delegate_android.h" 5 #include "chrome/browser/media/webrtc/media_stream_infobar_delegate_android.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/metrics/histogram_macros.h" 11 #include "base/metrics/histogram_macros.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/infobars/infobar_service.h" 13 #include "chrome/browser/infobars/infobar_service.h"
14 #include "chrome/browser/permissions/permission_uma_util.h" 14 #include "chrome/browser/permissions/permission_uma_util.h"
15 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/url_constants.h" 16 #include "chrome/common/url_constants.h"
17 #include "chrome/grit/generated_resources.h"
18 #include "chrome/grit/theme_resources.h"
16 #include "components/content_settings/core/common/content_settings_types.h" 19 #include "components/content_settings/core/common/content_settings_types.h"
17 #include "components/google/core/browser/google_util.h" 20 #include "components/google/core/browser/google_util.h"
18 #include "components/infobars/core/infobar.h" 21 #include "components/infobars/core/infobar.h"
19 #include "content/public/browser/web_contents.h" 22 #include "content/public/browser/web_contents.h"
20 #include "content/public/common/origin_util.h" 23 #include "content/public/common/origin_util.h"
24 #include "ui/base/l10n/l10n_util.h"
21 #include "url/gurl.h" 25 #include "url/gurl.h"
22 26
23 namespace { 27 namespace {
24 28
25 const int kGroupedInfobarAudioPosition = 0; 29 void DoNothing(bool update_content_setting, PermissionAction decision) {}
26 const int kGroupedInfobarVideoPosition = 1;
27
28 // Get a list of content types being requested. Note that the order of the
29 // resulting array corresponds to the kGroupedInfobarAudio/VideoPermission
30 // constants.
31 std::vector<ContentSettingsType> GetContentSettingsTypes(
32 MediaStreamDevicesController* controller) {
33 std::vector<ContentSettingsType> types;
34 if (controller->IsAskingForAudio())
35 types.push_back(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC);
36 if (controller->IsAskingForVideo())
37 types.push_back(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA);
38 return types;
39 }
40 30
41 } // namespace 31 } // namespace
42 32
43 MediaStreamInfoBarDelegateAndroid::~MediaStreamInfoBarDelegateAndroid() {}
44
45 // static 33 // static
46 bool MediaStreamInfoBarDelegateAndroid::Create( 34 bool MediaStreamInfoBarDelegateAndroid::Create(
47 content::WebContents* web_contents, 35 content::WebContents* web_contents,
48 std::unique_ptr<MediaStreamDevicesController> controller) { 36 std::unique_ptr<MediaStreamDevicesController> controller) {
49 InfoBarService* infobar_service = 37 InfoBarService* infobar_service =
50 InfoBarService::FromWebContents(web_contents); 38 InfoBarService::FromWebContents(web_contents);
51 if (!infobar_service) { 39 if (!infobar_service) {
52 // Deny the request if there is no place to show the infobar, e.g. when 40 // Deny the request if there is no place to show the infobar, e.g. when
53 // the request comes from a background extension page. 41 // the request comes from a background extension page.
54 controller->Cancelled(); 42 controller->Cancelled();
55 return false; 43 return false;
56 } 44 }
57 45
58 std::unique_ptr<infobars::InfoBar> infobar( 46 std::unique_ptr<infobars::InfoBar> infobar(
59 GroupedPermissionInfoBarDelegate::CreateInfoBar(infobar_service, 47 CreatePermissionInfoBar(std::unique_ptr<PermissionInfoBarDelegate>(
60 std::unique_ptr<GroupedPermissionInfoBarDelegate>( 48 new MediaStreamInfoBarDelegateAndroid(
Sergey Ulanov 2016/09/24 00:05:00 Use base::MakeUnique here
lshang 2016/09/25 23:49:10 Can't use MakeUnique here because constructor of M
61 new MediaStreamInfoBarDelegateAndroid(std::move(controller))))); 49 Profile::FromBrowserContext(web_contents->GetBrowserContext()),
50 std::move(controller)))));
62 for (size_t i = 0; i < infobar_service->infobar_count(); ++i) { 51 for (size_t i = 0; i < infobar_service->infobar_count(); ++i) {
63 infobars::InfoBar* old_infobar = infobar_service->infobar_at(i); 52 infobars::InfoBar* old_infobar = infobar_service->infobar_at(i);
64 if (old_infobar->delegate()->AsMediaStreamInfoBarDelegateAndroid()) { 53 if (old_infobar->delegate()->AsMediaStreamInfoBarDelegateAndroid()) {
65 infobar_service->ReplaceInfoBar(old_infobar, std::move(infobar)); 54 infobar_service->ReplaceInfoBar(old_infobar, std::move(infobar));
66 return true; 55 return true;
67 } 56 }
68 } 57 }
69 infobar_service->AddInfoBar(std::move(infobar)); 58 infobar_service->AddInfoBar(std::move(infobar));
70 return true; 59 return true;
71 } 60 }
72 61
73 void MediaStreamInfoBarDelegateAndroid::RecordPermissionAcceptedUma(
74 int position,
75 bool persist) {
76 PermissionUmaUtil::PermissionPromptAcceptedWithPersistenceToggle(
77 controller_->GetPermissionTypeForContentSettingsType(
78 GetContentSettingType(position)),
79 persist);
80 }
81
82 void MediaStreamInfoBarDelegateAndroid::RecordPermissionDeniedUma(
83 int position,
84 bool persist) {
85 PermissionUmaUtil::PermissionPromptDeniedWithPersistenceToggle(
86 controller_->GetPermissionTypeForContentSettingsType(
87 GetContentSettingType(position)),
88 persist);
89 }
90
91 infobars::InfoBarDelegate::InfoBarIdentifier 62 infobars::InfoBarDelegate::InfoBarIdentifier
92 MediaStreamInfoBarDelegateAndroid::GetIdentifier() const { 63 MediaStreamInfoBarDelegateAndroid::GetIdentifier() const {
93 return MEDIA_STREAM_INFOBAR_DELEGATE_ANDROID; 64 return MEDIA_STREAM_INFOBAR_DELEGATE_ANDROID;
94 } 65 }
95 66
67 infobars::InfoBarDelegate::Type
68 MediaStreamInfoBarDelegateAndroid::GetInfoBarType() const {
69 return PAGE_ACTION_TYPE;
gone 2016/09/21 20:37:38 This needs its own ID for tracking in infobar_dele
lshang 2016/09/22 00:06:42 InfoBarDelegate::Type currently only has two value
gone 2016/09/22 00:08:41 Whoops, my mistake. Was thinking about the functi
70 }
71
72 int MediaStreamInfoBarDelegateAndroid::GetIconId() const {
73 return controller_->IsAskingForVideo() ? IDR_INFOBAR_MEDIA_STREAM_CAMERA
74 : IDR_INFOBAR_MEDIA_STREAM_MIC;
75 }
76
96 MediaStreamInfoBarDelegateAndroid::MediaStreamInfoBarDelegateAndroid( 77 MediaStreamInfoBarDelegateAndroid::MediaStreamInfoBarDelegateAndroid(
78 Profile* profile,
97 std::unique_ptr<MediaStreamDevicesController> controller) 79 std::unique_ptr<MediaStreamDevicesController> controller)
98 : GroupedPermissionInfoBarDelegate( 80 : PermissionInfoBarDelegate(
99 controller->GetOrigin(), 81 controller->GetOrigin(),
100 GetContentSettingsTypes(controller.get())), 82 // The content setting type and permission type here are only passed
83 // in to fit into PermissionInfoBarDelegate, even though media infobar
84 // controls both mic and camera. This is a temporary state for easy
85 // refactoring.
86 // TODO(lshang): Merge MediaStreamInfoBarDelegateAndroid into
87 // GroupedPermissionInfoBarDelegate. See crbug.com/606138.
88 content::PermissionType::AUDIO_CAPTURE,
89 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC,
90 false,
91 profile,
92 // This is only passed in to fit into PermissionInfoBarDelegate.
93 // Infobar accepted/denied/dismissed is handled by controller, not via
94 // callbacks.
95 base::Bind(&DoNothing)),
101 controller_(std::move(controller)) { 96 controller_(std::move(controller)) {
102 DCHECK(controller_.get()); 97 DCHECK(controller_.get());
103 DCHECK(controller_->IsAskingForAudio() || controller_->IsAskingForVideo()); 98 DCHECK(controller_->IsAskingForAudio() || controller_->IsAskingForVideo());
104 } 99 }
105 100
101 MediaStreamInfoBarDelegateAndroid::~MediaStreamInfoBarDelegateAndroid() {}
102
106 void MediaStreamInfoBarDelegateAndroid::InfoBarDismissed() { 103 void MediaStreamInfoBarDelegateAndroid::InfoBarDismissed() {
107 // Deny the request if the infobar was closed with the 'x' button, since 104 // Deny the request if the infobar was closed with the 'x' button, since
108 // we don't want WebRTC to be waiting for an answer that will never come. 105 // we don't want WebRTC to be waiting for an answer that will never come.
109 controller_->Cancelled(); 106 controller_->Cancelled();
110 } 107 }
111 108
112 MediaStreamInfoBarDelegateAndroid* 109 MediaStreamInfoBarDelegateAndroid*
113 MediaStreamInfoBarDelegateAndroid::AsMediaStreamInfoBarDelegateAndroid() { 110 MediaStreamInfoBarDelegateAndroid::AsMediaStreamInfoBarDelegateAndroid() {
114 return this; 111 return this;
115 } 112 }
116 113
114 base::string16 MediaStreamInfoBarDelegateAndroid::GetMessageText() const {
115 return controller_->GetMessageText();
116 }
117
118 base::string16 MediaStreamInfoBarDelegateAndroid::GetButtonLabel(
119 InfoBarButton button) const {
120 return l10n_util::GetStringUTF16((button == BUTTON_OK)
Sergey Ulanov 2016/09/24 00:05:00 Maybe use switch statement to select the message -
lshang 2016/09/25 23:49:10 Done.
121 ? IDS_MEDIA_CAPTURE_ALLOW
122 : IDS_MEDIA_CAPTURE_BLOCK);
123 }
124
125 int MediaStreamInfoBarDelegateAndroid::GetMessageResourceId() const {
126 if (!controller_->IsAskingForAudio())
127 return IDS_MEDIA_CAPTURE_VIDEO_ONLY;
128 else if (!controller_->IsAskingForVideo())
129 return IDS_MEDIA_CAPTURE_AUDIO_ONLY;
130 return IDS_MEDIA_CAPTURE_AUDIO_AND_VIDEO;
131 }
132
117 bool MediaStreamInfoBarDelegateAndroid::Accept() { 133 bool MediaStreamInfoBarDelegateAndroid::Accept() {
118 bool persist_permission = true; 134 bool persist_permission = true;
119 if (ShouldShowPersistenceToggle()) { 135 if (ShouldShowPersistenceToggle()) {
120 persist_permission = persist(); 136 persist_permission = persist();
121 137
122 // TODO(dominickn): fold these metrics calls into 138 // TODO(dominickn): fold these metrics calls into
123 // PermissionUmaUtil::PermissionGranted. See crbug.com/638076. 139 // PermissionUmaUtil::PermissionGranted. See crbug.com/638076.
124 if (GetPermissionCount() == 2) { 140 if (controller_->IsAskingForAudio())
Sergey Ulanov 2016/09/24 00:05:00 Add {} https://google.github.io/styleguide/cppguid
lshang 2016/09/25 23:49:10 Done.
125 RecordPermissionAcceptedUma(kGroupedInfobarAudioPosition, 141 PermissionUmaUtil::PermissionPromptAcceptedWithPersistenceToggle(
126 persist_permission); 142 content::PermissionType::AUDIO_CAPTURE, persist_permission);
127 RecordPermissionAcceptedUma(kGroupedInfobarVideoPosition, 143 if (controller_->IsAskingForVideo())
Sergey Ulanov 2016/09/24 00:05:00 same here and for similar expressions below.
lshang 2016/09/25 23:49:10 Done.
128 persist_permission); 144 PermissionUmaUtil::PermissionPromptAcceptedWithPersistenceToggle(
129 } else { 145 content::PermissionType::VIDEO_CAPTURE, persist_permission);
130 DCHECK_EQ(1, GetPermissionCount());
131 RecordPermissionAcceptedUma(0, persist_permission);
132 }
133 } 146 }
134 147
135 controller_->set_persist(persist_permission); 148 controller_->set_persist(persist_permission);
136 if (GetPermissionCount() == 2) { 149 controller_->PermissionGranted();
137 controller_->GroupedRequestFinished(
138 GetAcceptState(kGroupedInfobarAudioPosition),
139 GetAcceptState(kGroupedInfobarVideoPosition));
140 } else {
141 DCHECK_EQ(1, GetPermissionCount());
142 controller_->PermissionGranted();
143 }
144 return true; 150 return true;
145 } 151 }
146 152
147 bool MediaStreamInfoBarDelegateAndroid::Cancel() { 153 bool MediaStreamInfoBarDelegateAndroid::Cancel() {
148 bool persist_permission = true; 154 bool persist_permission = true;
149 if (ShouldShowPersistenceToggle()) { 155 if (ShouldShowPersistenceToggle()) {
150 persist_permission = persist(); 156 persist_permission = persist();
151 157
152 // TODO(dominickn): fold these metrics calls into 158 // TODO(dominickn): fold these metrics calls into
153 // PermissionUmaUtil::PermissionGranted. See crbug.com/638076. 159 // PermissionUmaUtil::PermissionGranted. See crbug.com/638076.
154 if (GetPermissionCount() == 2) { 160 if (controller_->IsAskingForAudio())
155 RecordPermissionDeniedUma(kGroupedInfobarAudioPosition, 161 PermissionUmaUtil::PermissionPromptDeniedWithPersistenceToggle(
156 persist_permission); 162 content::PermissionType::AUDIO_CAPTURE, persist_permission);
157 RecordPermissionDeniedUma(kGroupedInfobarVideoPosition, 163 if (controller_->IsAskingForVideo())
158 persist_permission); 164 PermissionUmaUtil::PermissionPromptDeniedWithPersistenceToggle(
159 } else { 165 content::PermissionType::VIDEO_CAPTURE, persist_permission);
160 DCHECK_EQ(1, GetPermissionCount());
161 RecordPermissionDeniedUma(0, persist_permission);
162 }
163 } 166 }
164 controller_->set_persist(persist_permission); 167 controller_->set_persist(persist_permission);
165 controller_->PermissionDenied(); 168 controller_->PermissionDenied();
166 return true; 169 return true;
167 } 170 }
171
172 base::string16 MediaStreamInfoBarDelegateAndroid::GetLinkText() const {
173 return base::string16();
174 }
175
176 GURL MediaStreamInfoBarDelegateAndroid::GetLinkURL() const {
177 return GURL(chrome::kMediaAccessLearnMoreUrl);
178 }
179
180 std::vector<int> MediaStreamInfoBarDelegateAndroid::content_settings() const {
181 std::vector<int> types;
182 if (controller_->IsAskingForAudio())
183 types.push_back(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC);
184 if (controller_->IsAskingForVideo())
185 types.push_back(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA);
186 return types;
187 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698