Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / chrome / browser / permissions / permission_update_infobar_delegate_android.cc
blobc1ca52a90e41d8f1086a3a8c84d8c96119641f23
1 // Copyright 2015 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.
5 #include "chrome/browser/permissions/permission_update_infobar_delegate_android.h"
7 #include <string>
9 #include "base/android/jni_array.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "chrome/browser/android/preferences/pref_service_bridge.h"
12 #include "chrome/browser/infobars/infobar_service.h"
13 #include "chrome/grit/chromium_strings.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "chrome/grit/theme_resources.h"
16 #include "components/infobars/core/infobar.h"
17 #include "content/public/browser/android/content_view_core.h"
18 #include "content/public/browser/web_contents.h"
19 #include "jni/PermissionUpdateInfoBarDelegate_jni.h"
20 #include "ui/android/window_android.h"
21 #include "ui/base/l10n/l10n_util.h"
23 // static
24 infobars::InfoBar* PermissionUpdateInfoBarDelegate::Create(
25 content::WebContents* web_contents,
26 const std::vector<ContentSettingsType>& content_settings_types,
27 const PermissionUpdatedCallback& callback) {
28 DCHECK(ShouldShowPermissionInfobar(web_contents, content_settings_types))
29 << "Caller should check ShouldShowPermissionInfobar before creating the "
30 << "infobar.";
32 InfoBarService* infobar_service =
33 InfoBarService::FromWebContents(web_contents);
34 if (!infobar_service) {
35 callback.Run(false);
36 return nullptr;
39 return infobar_service->AddInfoBar(infobar_service->CreateConfirmInfoBar(
40 scoped_ptr<ConfirmInfoBarDelegate>(new PermissionUpdateInfoBarDelegate(
41 web_contents, content_settings_types, callback))));
44 // static
45 bool PermissionUpdateInfoBarDelegate::ShouldShowPermissionInfobar(
46 content::WebContents* web_contents,
47 const std::vector<ContentSettingsType>& content_settings_types) {
48 if (!web_contents)
49 return false;
51 content::ContentViewCore* cvc =
52 content::ContentViewCore::FromWebContents(web_contents);
53 if (!cvc || !cvc->GetWindowAndroid())
54 return false;
55 ui::WindowAndroid* window_android = cvc->GetWindowAndroid();
57 for (ContentSettingsType content_settings_type : content_settings_types) {
58 std::string android_permission =
59 PrefServiceBridge::GetAndroidPermissionForContentSetting(
60 content_settings_type);
62 if (!android_permission.empty() &&
63 !window_android->HasPermission(android_permission)) {
64 return true;
68 return false;
71 // static
72 bool PermissionUpdateInfoBarDelegate::RegisterPermissionUpdateInfoBarDelegate(
73 JNIEnv* env) {
74 return RegisterNativesImpl(env);
77 void PermissionUpdateInfoBarDelegate::OnPermissionResult(
78 JNIEnv* env, jobject obj, jboolean all_permissions_granted) {
79 callback_.Run(all_permissions_granted);
80 infobar()->RemoveSelf();
83 PermissionUpdateInfoBarDelegate::PermissionUpdateInfoBarDelegate(
84 content::WebContents* web_contents,
85 const std::vector<ContentSettingsType>& content_settings_types,
86 const PermissionUpdatedCallback& callback)
87 : ConfirmInfoBarDelegate(),
88 content_settings_types_(content_settings_types),
89 callback_(callback) {
90 std::vector<int> content_settings_type_values;
91 for (ContentSettingsType type : content_settings_types)
92 content_settings_type_values.push_back(type);
94 JNIEnv* env = base::android::AttachCurrentThread();
95 java_delegate_.Reset(Java_PermissionUpdateInfoBarDelegate_create(
96 env,
97 reinterpret_cast<intptr_t>(this),
98 web_contents->GetJavaWebContents().obj(),
99 base::android::ToJavaIntArray(env, content_settings_type_values).obj()));
101 content::ContentViewCore* cvc =
102 content::ContentViewCore::FromWebContents(web_contents);
103 window_android_ = cvc->GetWindowAndroid();
106 PermissionUpdateInfoBarDelegate::~PermissionUpdateInfoBarDelegate() {
107 Java_PermissionUpdateInfoBarDelegate_onNativeDestroyed(
108 base::android::AttachCurrentThread(), java_delegate_.obj());
111 int PermissionUpdateInfoBarDelegate::GetIconID() const {
112 return IDR_INFOBAR_WARNING;
115 base::string16 PermissionUpdateInfoBarDelegate::GetMessageText() const {
116 int missing_permission_count = 0;
117 int message_id = IDS_INFOBAR_MISSING_MULTIPLE_PERMISSIONS_TEXT;
119 for (ContentSettingsType content_settings_type : content_settings_types_) {
120 std::string android_permission =
121 PrefServiceBridge::GetAndroidPermissionForContentSetting(
122 content_settings_type);
124 if (!android_permission.empty() &&
125 !window_android_->HasPermission(android_permission)) {
126 missing_permission_count++;
128 switch (content_settings_type) {
129 case CONTENT_SETTINGS_TYPE_GEOLOCATION:
130 message_id = IDS_INFOBAR_MISSING_LOCATION_PERMISSION_TEXT;
131 break;
132 case CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC:
133 message_id = IDS_INFOBAR_MISSING_MICROPHONE_PERMISSION_TEXT;
134 break;
135 case CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA:
136 message_id = IDS_INFOBAR_MISSING_CAMERA_PERMISSION_TEXT;
137 break;
138 default:
139 NOTREACHED();
140 message_id = IDS_INFOBAR_MISSING_MULTIPLE_PERMISSIONS_TEXT;
144 if (missing_permission_count > 1) {
145 return l10n_util::GetStringUTF16(
146 IDS_INFOBAR_MISSING_MULTIPLE_PERMISSIONS_TEXT);
150 return l10n_util::GetStringUTF16(message_id);
153 int PermissionUpdateInfoBarDelegate::GetButtons() const {
154 return BUTTON_OK;
157 base::string16 PermissionUpdateInfoBarDelegate::GetButtonLabel(
158 InfoBarButton button) const {
159 DCHECK_EQ(button, BUTTON_OK);
160 return l10n_util::GetStringUTF16(IDS_INFOBAR_UPDATE_PERMISSIONS_BUTTON_TEXT);
163 bool PermissionUpdateInfoBarDelegate::Accept() {
164 Java_PermissionUpdateInfoBarDelegate_requestPermissions(
165 base::android::AttachCurrentThread(), java_delegate_.obj());
166 return false;
169 bool PermissionUpdateInfoBarDelegate::Cancel() {
170 callback_.Run(false);
171 return true;