OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "chrome/browser/permissions/permission_update_infobar_delegate_android.
h" |
| 6 |
| 7 #include <string> |
| 8 |
| 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" |
| 22 |
| 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."; |
| 31 |
| 32 InfoBarService* infobar_service = |
| 33 InfoBarService::FromWebContents(web_contents); |
| 34 if (!infobar_service) { |
| 35 callback.Run(false); |
| 36 return nullptr; |
| 37 } |
| 38 |
| 39 return infobar_service->AddInfoBar(infobar_service->CreateConfirmInfoBar( |
| 40 scoped_ptr<ConfirmInfoBarDelegate>(new PermissionUpdateInfoBarDelegate( |
| 41 web_contents, content_settings_types, callback)))); |
| 42 } |
| 43 |
| 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; |
| 50 |
| 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(); |
| 56 |
| 57 for (ContentSettingsType content_settings_type : content_settings_types) { |
| 58 std::string android_permission = |
| 59 PrefServiceBridge::GetAndroidPermissionForContentSetting( |
| 60 content_settings_type); |
| 61 |
| 62 if (!android_permission.empty() && |
| 63 !window_android->HasPermission(android_permission)) { |
| 64 return true; |
| 65 } |
| 66 } |
| 67 |
| 68 return false; |
| 69 } |
| 70 |
| 71 // static |
| 72 bool PermissionUpdateInfoBarDelegate::RegisterPermissionUpdateInfoBarDelegate( |
| 73 JNIEnv* env) { |
| 74 return RegisterNativesImpl(env); |
| 75 } |
| 76 |
| 77 void PermissionUpdateInfoBarDelegate::OnPermissionResult( |
| 78 JNIEnv* env, jobject obj, jboolean all_permissions_granted) { |
| 79 callback_.Run(all_permissions_granted); |
| 80 infobar()->RemoveSelf(); |
| 81 } |
| 82 |
| 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); |
| 93 |
| 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())); |
| 100 |
| 101 content::ContentViewCore* cvc = |
| 102 content::ContentViewCore::FromWebContents(web_contents); |
| 103 window_android_ = cvc->GetWindowAndroid(); |
| 104 } |
| 105 |
| 106 PermissionUpdateInfoBarDelegate::~PermissionUpdateInfoBarDelegate() { |
| 107 Java_PermissionUpdateInfoBarDelegate_onNativeDestroyed( |
| 108 base::android::AttachCurrentThread(), java_delegate_.obj()); |
| 109 } |
| 110 |
| 111 int PermissionUpdateInfoBarDelegate::GetIconID() const { |
| 112 return IDR_INFOBAR_WARNING; |
| 113 } |
| 114 |
| 115 base::string16 PermissionUpdateInfoBarDelegate::GetMessageText() const { |
| 116 int missing_permission_count = 0; |
| 117 int message_id = IDS_INFOBAR_MISSING_MULTIPLE_PERMISSIONS_TEXT; |
| 118 |
| 119 for (ContentSettingsType content_settings_type : content_settings_types_) { |
| 120 std::string android_permission = |
| 121 PrefServiceBridge::GetAndroidPermissionForContentSetting( |
| 122 content_settings_type); |
| 123 |
| 124 if (!android_permission.empty() && |
| 125 !window_android_->HasPermission(android_permission)) { |
| 126 missing_permission_count++; |
| 127 |
| 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; |
| 141 } |
| 142 } |
| 143 |
| 144 if (missing_permission_count > 1) { |
| 145 return l10n_util::GetStringUTF16( |
| 146 IDS_INFOBAR_MISSING_MULTIPLE_PERMISSIONS_TEXT); |
| 147 } |
| 148 } |
| 149 |
| 150 return l10n_util::GetStringUTF16(message_id); |
| 151 } |
| 152 |
| 153 int PermissionUpdateInfoBarDelegate::GetButtons() const { |
| 154 return BUTTON_OK; |
| 155 } |
| 156 |
| 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); |
| 161 } |
| 162 |
| 163 bool PermissionUpdateInfoBarDelegate::Accept() { |
| 164 Java_PermissionUpdateInfoBarDelegate_requestPermissions( |
| 165 base::android::AttachCurrentThread(), java_delegate_.obj()); |
| 166 return false; |
| 167 } |
| 168 |
| 169 bool PermissionUpdateInfoBarDelegate::Cancel() { |
| 170 callback_.Run(false); |
| 171 return true; |
| 172 } |
OLD | NEW |