1 // Copyright 2016 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/webrtc/screen_capture_infobar_delegate_android.h"
6
7 #include "base/callback_helpers.h"
8 #include "chrome/browser/android/android_theme_resources.h"
9 #include "chrome/browser/infobars/infobar_service.h"
10 #include "chrome/browser/media/webrtc/media_capture_devices_dispatcher.h"
11 #include "chrome/browser/media/webrtc/media_stream_capture_indicator.h"
12 #include "chrome/grit/generated_resources.h"
13 #include "components/infobars/core/infobar.h"
14 #include "components/strings/grit/components_strings.h"
15 #include "components/url_formatter/elide_url.h"
16 #include "content/public/browser/desktop_media_id.h"
17 #include "content/public/browser/web_contents.h"
18 #include "third_party/blink/public/common/mediastream/media_stream_request.h"
19 #include "third_party/webrtc/modules/desktop_capture/desktop_capture_types.h"
20 #include "ui/base/l10n/l10n_util.h"
21
22 // static
Create(content::WebContents * web_contents,const content::MediaStreamRequest & request,content::MediaResponseCallback callback)23 void ScreenCaptureInfoBarDelegateAndroid::Create(
24 content::WebContents* web_contents,
25 const content::MediaStreamRequest& request,
26 content::MediaResponseCallback callback) {
27 InfoBarService* infobar_service =
28 InfoBarService::FromWebContents(web_contents);
29
30 infobar_service->AddInfoBar(infobar_service->CreateConfirmInfoBar(
31 std::unique_ptr<ConfirmInfoBarDelegate>(
32 new ScreenCaptureInfoBarDelegateAndroid(web_contents, request,
33 std::move(callback)))));
34 }
35
ScreenCaptureInfoBarDelegateAndroid(content::WebContents * web_contents,const content::MediaStreamRequest & request,content::MediaResponseCallback callback)36 ScreenCaptureInfoBarDelegateAndroid::ScreenCaptureInfoBarDelegateAndroid(
37 content::WebContents* web_contents,
38 const content::MediaStreamRequest& request,
39 content::MediaResponseCallback callback)
40 : web_contents_(web_contents),
41 request_(request),
42 callback_(std::move(callback)) {
43 DCHECK(request.video_type ==
44 blink::mojom::MediaStreamType::GUM_DESKTOP_VIDEO_CAPTURE ||
45 request.video_type ==
46 blink::mojom::MediaStreamType::DISPLAY_VIDEO_CAPTURE);
47 }
48
~ScreenCaptureInfoBarDelegateAndroid()49 ScreenCaptureInfoBarDelegateAndroid::~ScreenCaptureInfoBarDelegateAndroid() {
50 if (!callback_.is_null()) {
51 std::move(callback_).Run(
52 blink::MediaStreamDevices(),
53 blink::mojom::MediaStreamRequestResult::FAILED_DUE_TO_SHUTDOWN,
54 nullptr);
55 }
56 }
57
58 infobars::InfoBarDelegate::InfoBarIdentifier
GetIdentifier() const59 ScreenCaptureInfoBarDelegateAndroid::GetIdentifier() const {
60 return SCREEN_CAPTURE_INFOBAR_DELEGATE_ANDROID;
61 }
62
GetMessageText() const63 base::string16 ScreenCaptureInfoBarDelegateAndroid::GetMessageText() const {
64 return l10n_util::GetStringFUTF16(
65 IDS_MEDIA_CAPTURE_SCREEN_INFOBAR_TEXT,
66 url_formatter::FormatUrlForSecurityDisplay(request_.security_origin));
67 }
68
GetIconId() const69 int ScreenCaptureInfoBarDelegateAndroid::GetIconId() const {
70 return IDR_ANDROID_INFOBAR_MEDIA_STREAM_SCREEN;
71 }
72
GetButtonLabel(InfoBarButton button) const73 base::string16 ScreenCaptureInfoBarDelegateAndroid::GetButtonLabel(
74 InfoBarButton button) const {
75 return l10n_util::GetStringUTF16((button == BUTTON_OK) ? IDS_PERMISSION_ALLOW
76 : IDS_PERMISSION_DENY);
77 }
78
Accept()79 bool ScreenCaptureInfoBarDelegateAndroid::Accept() {
80 RunCallback(blink::mojom::MediaStreamRequestResult::OK);
81 return true;
82 }
83
Cancel()84 bool ScreenCaptureInfoBarDelegateAndroid::Cancel() {
85 RunCallback(blink::mojom::MediaStreamRequestResult::PERMISSION_DENIED);
86 return true;
87 }
88
InfoBarDismissed()89 void ScreenCaptureInfoBarDelegateAndroid::InfoBarDismissed() {
90 RunCallback(blink::mojom::MediaStreamRequestResult::PERMISSION_DISMISSED);
91 }
92
RunCallback(blink::mojom::MediaStreamRequestResult result)93 void ScreenCaptureInfoBarDelegateAndroid::RunCallback(
94 blink::mojom::MediaStreamRequestResult result) {
95 DCHECK(!callback_.is_null());
96
97 blink::MediaStreamDevices devices;
98 std::unique_ptr<content::MediaStreamUI> ui;
99 if (result == blink::mojom::MediaStreamRequestResult::OK) {
100 content::DesktopMediaID screen_id = content::DesktopMediaID(
101 content::DesktopMediaID::TYPE_SCREEN, webrtc::kFullDesktopScreenId);
102 devices.push_back(blink::MediaStreamDevice(request_.video_type,
103 screen_id.ToString(), "Screen"));
104
105 ui = MediaCaptureDevicesDispatcher::GetInstance()
106 ->GetMediaStreamCaptureIndicator()
107 ->RegisterMediaStream(web_contents_, devices);
108 }
109
110 std::move(callback_).Run(devices, result, std::move(ui));
111 }
112