Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/permissions/permission_util.h" | |
| 16 #include "chrome/browser/profiles/profile.h" | |
|
dominickn
2016/09/20 08:01:36
If you're not calling methods on Profile, you can
lshang
2016/09/21 03:47:39
Still need this after we change to get Profile fro
| |
| 15 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
| 16 #include "components/content_settings/core/common/content_settings_types.h" | 18 #include "chrome/grit/generated_resources.h" |
|
dominickn
2016/09/20 08:01:36
Nit: I think you still need this header for CONTEN
lshang
2016/09/21 03:47:39
Done.
| |
| 19 #include "chrome/grit/theme_resources.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 { | |
| 24 | |
| 25 const int kGroupedInfobarAudioPosition = 0; | |
| 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 | |
| 41 } // namespace | |
| 42 | |
| 43 MediaStreamInfoBarDelegateAndroid::~MediaStreamInfoBarDelegateAndroid() {} | |
| 44 | |
| 45 // static | 27 // static |
| 46 bool MediaStreamInfoBarDelegateAndroid::Create( | 28 bool MediaStreamInfoBarDelegateAndroid::Create( |
| 29 Profile* profile, | |
|
dominickn
2016/09/20 08:01:36
Don't add the Profile* argument here. Instead, cal
lshang
2016/09/21 03:47:39
Done. Get Profile from WebContents.
| |
| 47 content::WebContents* web_contents, | 30 content::WebContents* web_contents, |
| 48 std::unique_ptr<MediaStreamDevicesController> controller) { | 31 std::unique_ptr<MediaStreamDevicesController> controller) { |
| 49 InfoBarService* infobar_service = | 32 InfoBarService* infobar_service = |
| 50 InfoBarService::FromWebContents(web_contents); | 33 InfoBarService::FromWebContents(web_contents); |
| 51 if (!infobar_service) { | 34 if (!infobar_service) { |
| 52 // Deny the request if there is no place to show the infobar, e.g. when | 35 // Deny the request if there is no place to show the infobar, e.g. when |
| 53 // the request comes from a background extension page. | 36 // the request comes from a background extension page. |
| 54 controller->Cancelled(); | 37 controller->Cancelled(); |
| 55 return false; | 38 return false; |
| 56 } | 39 } |
| 57 | 40 |
| 58 std::unique_ptr<infobars::InfoBar> infobar( | 41 std::unique_ptr<infobars::InfoBar> infobar( |
| 59 GroupedPermissionInfoBarDelegate::CreateInfoBar(infobar_service, | 42 CreatePermissionInfoBar(std::unique_ptr<PermissionInfoBarDelegate>( |
| 60 std::unique_ptr<GroupedPermissionInfoBarDelegate>( | 43 new MediaStreamInfoBarDelegateAndroid(profile, |
| 61 new MediaStreamInfoBarDelegateAndroid(std::move(controller))))); | 44 std::move(controller))))); |
| 62 for (size_t i = 0; i < infobar_service->infobar_count(); ++i) { | 45 for (size_t i = 0; i < infobar_service->infobar_count(); ++i) { |
| 63 infobars::InfoBar* old_infobar = infobar_service->infobar_at(i); | 46 infobars::InfoBar* old_infobar = infobar_service->infobar_at(i); |
| 64 if (old_infobar->delegate()->AsMediaStreamInfoBarDelegateAndroid()) { | 47 if (old_infobar->delegate()->AsMediaStreamInfoBarDelegateAndroid()) { |
| 65 infobar_service->ReplaceInfoBar(old_infobar, std::move(infobar)); | 48 infobar_service->ReplaceInfoBar(old_infobar, std::move(infobar)); |
| 66 return true; | 49 return true; |
| 67 } | 50 } |
| 68 } | 51 } |
| 69 infobar_service->AddInfoBar(std::move(infobar)); | 52 infobar_service->AddInfoBar(std::move(infobar)); |
| 70 return true; | 53 return true; |
| 71 } | 54 } |
| 72 | 55 |
| 73 void MediaStreamInfoBarDelegateAndroid::RecordPermissionAcceptedUma( | 56 bool MediaStreamInfoBarDelegateAndroid::ShouldShowPersistenceToggle() const { |
| 74 int position, | 57 return PermissionUtil::ShouldShowPersistenceToggle(); |
| 75 bool persist) { | |
| 76 PermissionUmaUtil::PermissionPromptAcceptedWithPersistenceToggle( | |
| 77 controller_->GetPermissionTypeForContentSettingsType( | |
| 78 GetContentSettingType(position)), | |
| 79 persist); | |
| 80 } | 58 } |
| 81 | 59 |
| 82 void MediaStreamInfoBarDelegateAndroid::RecordPermissionDeniedUma( | 60 void MediaStreamInfoBarDelegateAndroid::DoNothing(bool update_content_setting, |
| 83 int position, | 61 PermissionAction decision) {} |
| 84 bool persist) { | |
| 85 PermissionUmaUtil::PermissionPromptDeniedWithPersistenceToggle( | |
| 86 controller_->GetPermissionTypeForContentSettingsType( | |
| 87 GetContentSettingType(position)), | |
| 88 persist); | |
| 89 } | |
| 90 | 62 |
| 91 infobars::InfoBarDelegate::InfoBarIdentifier | 63 infobars::InfoBarDelegate::InfoBarIdentifier |
| 92 MediaStreamInfoBarDelegateAndroid::GetIdentifier() const { | 64 MediaStreamInfoBarDelegateAndroid::GetIdentifier() const { |
| 93 return MEDIA_STREAM_INFOBAR_DELEGATE_ANDROID; | 65 return MEDIA_STREAM_INFOBAR_DELEGATE_ANDROID; |
| 94 } | 66 } |
| 95 | 67 |
| 68 infobars::InfoBarDelegate::Type | |
| 69 MediaStreamInfoBarDelegateAndroid::GetInfoBarType() const { | |
| 70 return PAGE_ACTION_TYPE; | |
| 71 } | |
| 72 | |
| 73 int MediaStreamInfoBarDelegateAndroid::GetIconId() const { | |
| 74 return controller_->IsAskingForVideo() ? IDR_INFOBAR_MEDIA_STREAM_CAMERA | |
| 75 : IDR_INFOBAR_MEDIA_STREAM_MIC; | |
| 76 } | |
| 77 | |
| 96 MediaStreamInfoBarDelegateAndroid::MediaStreamInfoBarDelegateAndroid( | 78 MediaStreamInfoBarDelegateAndroid::MediaStreamInfoBarDelegateAndroid( |
| 79 Profile* profile, | |
| 97 std::unique_ptr<MediaStreamDevicesController> controller) | 80 std::unique_ptr<MediaStreamDevicesController> controller) |
| 98 : GroupedPermissionInfoBarDelegate( | 81 : PermissionInfoBarDelegate( |
| 99 controller->GetOrigin(), | 82 controller->GetOrigin(), |
| 100 GetContentSettingsTypes(controller.get())), | 83 // The content setting type and permission type here are only passed |
| 101 controller_(std::move(controller)) { | 84 // in to fit into PermissionInfoBarDelegate, even though media infobar |
| 85 // controls both mic and camera. This is a temporary state for easy | |
| 86 // refactoring. | |
| 87 // TODO(lshang): Merge MediaStreamInfoBarDelegateAndroid into | |
| 88 // GroupedPermissionInfoBarDelegate. See crbug.com/606138. | |
| 89 content::PermissionType::AUDIO_CAPTURE, | |
| 90 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC, | |
| 91 false, | |
| 92 profile, | |
| 93 // This is only passed in to fit into PermissionInfoBarDelegate. | |
| 94 // Infobar accepted/denied/dismissed is handled by controller, not via | |
| 95 // callbacks. | |
| 96 base::Bind(&MediaStreamInfoBarDelegateAndroid::DoNothing, | |
| 97 base::Unretained(this))), | |
| 98 controller_(std::move(controller)), | |
| 99 persist_(true) { | |
| 102 DCHECK(controller_.get()); | 100 DCHECK(controller_.get()); |
| 103 DCHECK(controller_->IsAskingForAudio() || controller_->IsAskingForVideo()); | 101 DCHECK(controller_->IsAskingForAudio() || controller_->IsAskingForVideo()); |
| 104 } | 102 } |
| 105 | 103 |
| 104 MediaStreamInfoBarDelegateAndroid::~MediaStreamInfoBarDelegateAndroid() {} | |
| 105 | |
| 106 void MediaStreamInfoBarDelegateAndroid::InfoBarDismissed() { | 106 void MediaStreamInfoBarDelegateAndroid::InfoBarDismissed() { |
| 107 // Deny the request if the infobar was closed with the 'x' button, since | 107 // 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. | 108 // we don't want WebRTC to be waiting for an answer that will never come. |
| 109 controller_->Cancelled(); | 109 controller_->Cancelled(); |
| 110 } | 110 } |
| 111 | 111 |
| 112 MediaStreamInfoBarDelegateAndroid* | 112 MediaStreamInfoBarDelegateAndroid* |
| 113 MediaStreamInfoBarDelegateAndroid::AsMediaStreamInfoBarDelegateAndroid() { | 113 MediaStreamInfoBarDelegateAndroid::AsMediaStreamInfoBarDelegateAndroid() { |
| 114 return this; | 114 return this; |
| 115 } | 115 } |
| 116 | 116 |
| 117 base::string16 MediaStreamInfoBarDelegateAndroid::GetMessageText() const { | |
| 118 return controller_->GetMessageText(); | |
| 119 } | |
| 120 | |
| 121 base::string16 MediaStreamInfoBarDelegateAndroid::GetButtonLabel( | |
| 122 InfoBarButton button) const { | |
| 123 return l10n_util::GetStringUTF16((button == BUTTON_OK) | |
| 124 ? IDS_MEDIA_CAPTURE_ALLOW | |
| 125 : IDS_MEDIA_CAPTURE_BLOCK); | |
| 126 } | |
| 127 | |
| 128 int MediaStreamInfoBarDelegateAndroid::GetMessageResourceId() const { | |
| 129 if (!controller_->IsAskingForAudio()) | |
| 130 return IDS_MEDIA_CAPTURE_VIDEO_ONLY; | |
| 131 else if (!controller_->IsAskingForVideo()) | |
| 132 return IDS_MEDIA_CAPTURE_AUDIO_ONLY; | |
| 133 return IDS_MEDIA_CAPTURE_AUDIO_AND_VIDEO; | |
| 134 } | |
| 135 | |
| 117 bool MediaStreamInfoBarDelegateAndroid::Accept() { | 136 bool MediaStreamInfoBarDelegateAndroid::Accept() { |
| 118 bool persist_permission = true; | 137 bool persist_permission = true; |
| 119 if (ShouldShowPersistenceToggle()) { | 138 if (PermissionUtil::ShouldShowPersistenceToggle()) { |
| 120 persist_permission = persist(); | 139 persist_permission = persist(); |
| 121 | 140 |
| 122 // TODO(dominickn): fold these metrics calls into | 141 // TODO(dominickn): fold these metrics calls into |
| 123 // PermissionUmaUtil::PermissionGranted. See crbug.com/638076. | 142 // PermissionUmaUtil::PermissionGranted. See crbug.com/638076. |
| 124 if (GetPermissionCount() == 2) { | 143 if (controller_->IsAskingForAudio()) |
| 125 RecordPermissionAcceptedUma(kGroupedInfobarAudioPosition, | 144 PermissionUmaUtil::PermissionPromptAcceptedWithPersistenceToggle( |
| 126 persist_permission); | 145 content::PermissionType::AUDIO_CAPTURE, persist_); |
| 127 RecordPermissionAcceptedUma(kGroupedInfobarVideoPosition, | 146 if (controller_->IsAskingForVideo()) |
| 128 persist_permission); | 147 PermissionUmaUtil::PermissionPromptAcceptedWithPersistenceToggle( |
| 129 } else { | 148 content::PermissionType::VIDEO_CAPTURE, persist_); |
| 130 DCHECK_EQ(1, GetPermissionCount()); | |
| 131 RecordPermissionAcceptedUma(0, persist_permission); | |
| 132 } | |
| 133 } | 149 } |
| 134 | 150 |
| 135 controller_->set_persist(persist_permission); | 151 controller_->set_persist(persist_permission); |
| 136 if (GetPermissionCount() == 2) { | 152 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; | 153 return true; |
| 145 } | 154 } |
| 146 | 155 |
| 147 bool MediaStreamInfoBarDelegateAndroid::Cancel() { | 156 bool MediaStreamInfoBarDelegateAndroid::Cancel() { |
| 148 bool persist_permission = true; | 157 bool persist_permission = true; |
| 149 if (ShouldShowPersistenceToggle()) { | 158 if (PermissionUtil::ShouldShowPersistenceToggle()) { |
| 150 persist_permission = persist(); | 159 persist_permission = persist(); |
| 151 | 160 |
| 152 // TODO(dominickn): fold these metrics calls into | 161 // TODO(dominickn): fold these metrics calls into |
| 153 // PermissionUmaUtil::PermissionGranted. See crbug.com/638076. | 162 // PermissionUmaUtil::PermissionGranted. See crbug.com/638076. |
| 154 if (GetPermissionCount() == 2) { | 163 if (controller_->IsAskingForAudio()) |
| 155 RecordPermissionDeniedUma(kGroupedInfobarAudioPosition, | 164 PermissionUmaUtil::PermissionPromptDeniedWithPersistenceToggle( |
| 156 persist_permission); | 165 content::PermissionType::AUDIO_CAPTURE, persist_); |
| 157 RecordPermissionDeniedUma(kGroupedInfobarVideoPosition, | 166 if (controller_->IsAskingForVideo()) |
| 158 persist_permission); | 167 PermissionUmaUtil::PermissionPromptDeniedWithPersistenceToggle( |
| 159 } else { | 168 content::PermissionType::VIDEO_CAPTURE, persist_); |
| 160 DCHECK_EQ(1, GetPermissionCount()); | |
| 161 RecordPermissionDeniedUma(0, persist_permission); | |
| 162 } | |
| 163 } | 169 } |
| 170 | |
| 164 controller_->set_persist(persist_permission); | 171 controller_->set_persist(persist_permission); |
| 165 controller_->PermissionDenied(); | 172 controller_->PermissionDenied(); |
| 166 return true; | 173 return true; |
| 167 } | 174 } |
| 175 | |
| 176 base::string16 MediaStreamInfoBarDelegateAndroid::GetLinkText() const { | |
| 177 return base::string16(); | |
| 178 } | |
| 179 | |
| 180 GURL MediaStreamInfoBarDelegateAndroid::GetLinkURL() const { | |
| 181 return GURL(chrome::kMediaAccessLearnMoreUrl); | |
| 182 } | |
| 183 | |
| 184 std::vector<int> MediaStreamInfoBarDelegateAndroid::content_setting() const { | |
| 185 std::vector<int> types; | |
| 186 if (controller_->IsAskingForAudio()) | |
| 187 types.push_back(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC); | |
| 188 if (controller_->IsAskingForVideo()) | |
| 189 types.push_back(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA); | |
| 190 return types; | |
| 191 } | |
| OLD | NEW |