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/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 { | |
| 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( |
| 47 content::WebContents* web_contents, | 29 content::WebContents* web_contents, |
| 48 std::unique_ptr<MediaStreamDevicesController> controller) { | 30 std::unique_ptr<MediaStreamDevicesController> controller) { |
| 49 InfoBarService* infobar_service = | 31 InfoBarService* infobar_service = |
| 50 InfoBarService::FromWebContents(web_contents); | 32 InfoBarService::FromWebContents(web_contents); |
| 51 if (!infobar_service) { | 33 if (!infobar_service) { |
| 52 // Deny the request if there is no place to show the infobar, e.g. when | 34 // Deny the request if there is no place to show the infobar, e.g. when |
| 53 // the request comes from a background extension page. | 35 // the request comes from a background extension page. |
| 54 controller->Cancelled(); | 36 controller->Cancelled(); |
| 55 return false; | 37 return false; |
| 56 } | 38 } |
| 57 | 39 |
| 40 Profile* profile = | |
|
dominickn
2016/09/21 05:09:16
Nit: inline this call into the new MediaStreamInfo
lshang
2016/09/21 06:57:54
Done.
| |
| 41 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | |
| 42 | |
| 58 std::unique_ptr<infobars::InfoBar> infobar( | 43 std::unique_ptr<infobars::InfoBar> infobar( |
| 59 GroupedPermissionInfoBarDelegate::CreateInfoBar(infobar_service, | 44 CreatePermissionInfoBar(std::unique_ptr<PermissionInfoBarDelegate>( |
| 60 std::unique_ptr<GroupedPermissionInfoBarDelegate>( | 45 new MediaStreamInfoBarDelegateAndroid(profile, |
| 61 new MediaStreamInfoBarDelegateAndroid(std::move(controller))))); | 46 std::move(controller))))); |
| 62 for (size_t i = 0; i < infobar_service->infobar_count(); ++i) { | 47 for (size_t i = 0; i < infobar_service->infobar_count(); ++i) { |
| 63 infobars::InfoBar* old_infobar = infobar_service->infobar_at(i); | 48 infobars::InfoBar* old_infobar = infobar_service->infobar_at(i); |
| 64 if (old_infobar->delegate()->AsMediaStreamInfoBarDelegateAndroid()) { | 49 if (old_infobar->delegate()->AsMediaStreamInfoBarDelegateAndroid()) { |
| 65 infobar_service->ReplaceInfoBar(old_infobar, std::move(infobar)); | 50 infobar_service->ReplaceInfoBar(old_infobar, std::move(infobar)); |
| 66 return true; | 51 return true; |
| 67 } | 52 } |
| 68 } | 53 } |
| 69 infobar_service->AddInfoBar(std::move(infobar)); | 54 infobar_service->AddInfoBar(std::move(infobar)); |
| 70 return true; | 55 return true; |
| 71 } | 56 } |
| 72 | 57 |
| 73 void MediaStreamInfoBarDelegateAndroid::RecordPermissionAcceptedUma( | 58 void MediaStreamInfoBarDelegateAndroid::DoNothing(bool update_content_setting, |
| 74 int position, | 59 PermissionAction decision) {} |
| 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 | 60 |
| 91 infobars::InfoBarDelegate::InfoBarIdentifier | 61 infobars::InfoBarDelegate::InfoBarIdentifier |
| 92 MediaStreamInfoBarDelegateAndroid::GetIdentifier() const { | 62 MediaStreamInfoBarDelegateAndroid::GetIdentifier() const { |
| 93 return MEDIA_STREAM_INFOBAR_DELEGATE_ANDROID; | 63 return MEDIA_STREAM_INFOBAR_DELEGATE_ANDROID; |
| 94 } | 64 } |
| 95 | 65 |
| 66 infobars::InfoBarDelegate::Type | |
| 67 MediaStreamInfoBarDelegateAndroid::GetInfoBarType() const { | |
| 68 return PAGE_ACTION_TYPE; | |
| 69 } | |
| 70 | |
| 71 int MediaStreamInfoBarDelegateAndroid::GetIconId() const { | |
| 72 return controller_->IsAskingForVideo() ? IDR_INFOBAR_MEDIA_STREAM_CAMERA | |
| 73 : IDR_INFOBAR_MEDIA_STREAM_MIC; | |
| 74 } | |
| 75 | |
| 96 MediaStreamInfoBarDelegateAndroid::MediaStreamInfoBarDelegateAndroid( | 76 MediaStreamInfoBarDelegateAndroid::MediaStreamInfoBarDelegateAndroid( |
| 77 Profile* profile, | |
| 97 std::unique_ptr<MediaStreamDevicesController> controller) | 78 std::unique_ptr<MediaStreamDevicesController> controller) |
| 98 : GroupedPermissionInfoBarDelegate( | 79 : PermissionInfoBarDelegate( |
| 99 controller->GetOrigin(), | 80 controller->GetOrigin(), |
| 100 GetContentSettingsTypes(controller.get())), | 81 // The content setting type and permission type here are only passed |
| 82 // in to fit into PermissionInfoBarDelegate, even though media infobar | |
| 83 // controls both mic and camera. This is a temporary state for easy | |
| 84 // refactoring. | |
| 85 // TODO(lshang): Merge MediaStreamInfoBarDelegateAndroid into | |
| 86 // GroupedPermissionInfoBarDelegate. See crbug.com/606138. | |
| 87 content::PermissionType::AUDIO_CAPTURE, | |
| 88 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC, | |
| 89 false, | |
| 90 profile, | |
| 91 // This is only passed in to fit into PermissionInfoBarDelegate. | |
| 92 // Infobar accepted/denied/dismissed is handled by controller, not via | |
| 93 // callbacks. | |
| 94 base::Bind(&MediaStreamInfoBarDelegateAndroid::DoNothing, | |
|
dominickn
2016/09/21 05:09:16
Nit: can you declare DoNothing in the anonymous na
lshang
2016/09/21 06:57:54
Done.
| |
| 95 base::Unretained(this))), | |
| 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) | |
| 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()) |
| 125 RecordPermissionAcceptedUma(kGroupedInfobarAudioPosition, | 141 PermissionUmaUtil::PermissionPromptAcceptedWithPersistenceToggle( |
| 126 persist_permission); | 142 content::PermissionType::AUDIO_CAPTURE, persist_permission); |
| 127 RecordPermissionAcceptedUma(kGroupedInfobarVideoPosition, | 143 if (controller_->IsAskingForVideo()) |
| 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 } | |
| OLD | NEW |