OLD | NEW |
| (Empty) |
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_android.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <utility> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/metrics/histogram_macros.h" | |
12 #include "base/strings/utf_string_conversions.h" | |
13 #include "chrome/browser/infobars/infobar_service.h" | |
14 #include "chrome/common/url_constants.h" | |
15 #include "components/content_settings/core/common/content_settings_types.h" | |
16 #include "components/google/core/browser/google_util.h" | |
17 #include "components/infobars/core/infobar.h" | |
18 #include "content/public/browser/web_contents.h" | |
19 #include "content/public/common/origin_util.h" | |
20 #include "url/gurl.h" | |
21 | |
22 namespace { | |
23 | |
24 const int kGroupedInfobarAudioPosition = 0; | |
25 const int kGroupedInfobarVideoPosition = 1; | |
26 | |
27 // Get a list of content types being requested. Note that the order of the | |
28 // resulting array corresponds to the kGroupedInfobarAudio/VideoPermission | |
29 // constants. | |
30 std::vector<ContentSettingsType> GetContentSettingsTypes( | |
31 MediaStreamDevicesController* controller) { | |
32 std::vector<ContentSettingsType> types; | |
33 if (controller->IsAskingForAudio()) | |
34 types.push_back(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC); | |
35 if (controller->IsAskingForVideo()) | |
36 types.push_back(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA); | |
37 return types; | |
38 } | |
39 | |
40 } // namespace | |
41 | |
42 MediaStreamInfoBarDelegateAndroid::~MediaStreamInfoBarDelegateAndroid() {} | |
43 | |
44 // static | |
45 bool MediaStreamInfoBarDelegateAndroid::Create( | |
46 content::WebContents* web_contents, | |
47 std::unique_ptr<MediaStreamDevicesController> controller) { | |
48 InfoBarService* infobar_service = | |
49 InfoBarService::FromWebContents(web_contents); | |
50 if (!infobar_service) { | |
51 // Deny the request if there is no place to show the infobar, e.g. when | |
52 // the request comes from a background extension page. | |
53 controller->Cancelled(); | |
54 return false; | |
55 } | |
56 | |
57 std::unique_ptr<infobars::InfoBar> infobar( | |
58 GroupedPermissionInfoBarDelegate::CreateInfoBar(infobar_service, | |
59 std::unique_ptr<GroupedPermissionInfoBarDelegate>( | |
60 new MediaStreamInfoBarDelegateAndroid(std::move(controller))))); | |
61 for (size_t i = 0; i < infobar_service->infobar_count(); ++i) { | |
62 infobars::InfoBar* old_infobar = infobar_service->infobar_at(i); | |
63 if (old_infobar->delegate()->AsMediaStreamInfoBarDelegateAndroid()) { | |
64 infobar_service->ReplaceInfoBar(old_infobar, std::move(infobar)); | |
65 return true; | |
66 } | |
67 } | |
68 infobar_service->AddInfoBar(std::move(infobar)); | |
69 return true; | |
70 } | |
71 | |
72 infobars::InfoBarDelegate::InfoBarIdentifier | |
73 MediaStreamInfoBarDelegateAndroid::GetIdentifier() const { | |
74 return MEDIA_STREAM_INFOBAR_DELEGATE_ANDROID; | |
75 } | |
76 | |
77 MediaStreamInfoBarDelegateAndroid::MediaStreamInfoBarDelegateAndroid( | |
78 std::unique_ptr<MediaStreamDevicesController> controller) | |
79 : GroupedPermissionInfoBarDelegate( | |
80 controller->GetOrigin(), | |
81 GetContentSettingsTypes(controller.get())), | |
82 controller_(std::move(controller)) { | |
83 DCHECK(controller_.get()); | |
84 DCHECK(controller_->IsAskingForAudio() || controller_->IsAskingForVideo()); | |
85 } | |
86 | |
87 void MediaStreamInfoBarDelegateAndroid::InfoBarDismissed() { | |
88 // Deny the request if the infobar was closed with the 'x' button, since | |
89 // we don't want WebRTC to be waiting for an answer that will never come. | |
90 controller_->Cancelled(); | |
91 } | |
92 | |
93 MediaStreamInfoBarDelegateAndroid* | |
94 MediaStreamInfoBarDelegateAndroid::AsMediaStreamInfoBarDelegateAndroid() { | |
95 return this; | |
96 } | |
97 | |
98 bool MediaStreamInfoBarDelegateAndroid::Accept() { | |
99 if (GetPermissionCount() == 2) { | |
100 controller_->GroupedRequestFinished( | |
101 GetAcceptState(kGroupedInfobarAudioPosition), | |
102 GetAcceptState(kGroupedInfobarVideoPosition)); | |
103 } else { | |
104 DCHECK_EQ(1, GetPermissionCount()); | |
105 controller_->PermissionGranted(); | |
106 } | |
107 return true; | |
108 } | |
109 | |
110 bool MediaStreamInfoBarDelegateAndroid::Cancel() { | |
111 controller_->PermissionDenied(); | |
112 return true; | |
113 } | |
OLD | NEW |