Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1837)

Unified Diff: chrome/browser/permissions/permission_update_infobar_delegate_android.cc

Issue 1240923002: Add an infobar to resolve android runtime permission mismatches. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/permissions/permission_update_infobar_delegate_android.cc
diff --git a/chrome/browser/permissions/permission_update_infobar_delegate_android.cc b/chrome/browser/permissions/permission_update_infobar_delegate_android.cc
new file mode 100644
index 0000000000000000000000000000000000000000..a9fec143cedafc6f430905c1dd10803cace79f0a
--- /dev/null
+++ b/chrome/browser/permissions/permission_update_infobar_delegate_android.cc
@@ -0,0 +1,179 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/permissions/permission_update_infobar_delegate_android.h"
+
+#include <string>
+
+#include "base/android/jni_array.h"
+#include "base/memory/scoped_ptr.h"
+#include "chrome/browser/android/preferences/pref_service_bridge.h"
+#include "chrome/browser/infobars/infobar_service.h"
+#include "chrome/grit/chromium_strings.h"
+#include "chrome/grit/generated_resources.h"
+#include "chrome/grit/theme_resources.h"
+#include "components/infobars/core/infobar.h"
+#include "content/public/browser/android/content_view_core.h"
+#include "content/public/browser/web_contents.h"
+#include "jni/PermissionUpdateInfoBarDelegate_jni.h"
+#include "ui/android/window_android.h"
+#include "ui/base/l10n/l10n_util.h"
+
+// static
+infobars::InfoBar* PermissionUpdateInfoBarDelegate::Create(
+ content::WebContents* web_contents,
+ const std::vector<ContentSettingsType>& content_settings_types,
+ const PermissionUpdatedCallback& callback) {
+ DCHECK(ShouldShowPermissionInfobar(web_contents, content_settings_types))
+ << "Caller shoudl check ShouldShowPermissionInfobar before creating the "
gone 2015/07/17 17:41:40 you shoudl fix this typo
Ted C 2015/07/17 20:37:18 :-) Dnoe
+ << "infobar.";
+
+ InfoBarService* infobar_service =
+ InfoBarService::FromWebContents(web_contents);
+ if (!infobar_service) {
+ callback.Run(false);
+ return nullptr;
+ }
+
+ // Trim the list of content settings types to just those that are missing the
+ // corresponding android permission.
gone 2015/07/17 17:41:39 what happens if the infobar is already on screen w
Ted C 2015/07/17 20:37:18 Fair point. I removed the trimming here and propa
+ content::ContentViewCore* cvc =
+ content::ContentViewCore::FromWebContents(web_contents);
+ ui::WindowAndroid* window_android = cvc->GetWindowAndroid();
+ std::vector<ContentSettingsType> invalid_content_settings_types;
+ for (ContentSettingsType content_settings_type : content_settings_types) {
+ std::string android_permission =
+ PrefServiceBridge::GetAndroidPermissionForContentSetting(
+ content_settings_type);
+
+ if (!android_permission.empty() &&
+ !window_android->HasPermission(android_permission)) {
+ invalid_content_settings_types.push_back(content_settings_type);
+ }
+ }
+
+ if (invalid_content_settings_types.empty()) {
+ NOTREACHED();
gone 2015/07/17 17:41:40 does a NOTREACHED() crash, or is that only on debu
Ted C 2015/07/17 20:37:18 It is a DCHECK(false), so only debug builds.
+ callback.Run(false);
+ return nullptr;
+ }
+
+ return infobar_service->AddInfoBar(infobar_service->CreateConfirmInfoBar(
+ scoped_ptr<ConfirmInfoBarDelegate>(new PermissionUpdateInfoBarDelegate(
+ web_contents, invalid_content_settings_types, callback))));
+}
+
+// static
+bool PermissionUpdateInfoBarDelegate::ShouldShowPermissionInfobar(
+ content::WebContents* web_contents,
+ const std::vector<ContentSettingsType>& content_settings_types) {
+ if (!web_contents)
+ return false;
+
+ content::ContentViewCore* cvc =
+ content::ContentViewCore::FromWebContents(web_contents);
+ if (!cvc || !cvc->GetWindowAndroid())
+ return false;
+ ui::WindowAndroid* window_android = cvc->GetWindowAndroid();
+
+ for (ContentSettingsType content_settings_type : content_settings_types) {
+ std::string android_permission =
+ PrefServiceBridge::GetAndroidPermissionForContentSetting(
+ content_settings_type);
+
+ if (!android_permission.empty() &&
+ !window_android->HasPermission(android_permission)) {
+ return true;
+ }
+ }
+
+ return false;
+}
+
+// static
+bool PermissionUpdateInfoBarDelegate::RegisterPermissionUpdateInfoBarDelegate(
+ JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+void PermissionUpdateInfoBarDelegate::OnPermissionResult(
+ JNIEnv* env, jobject obj, jboolean all_permissions_granted) {
+ callback_.Run(all_permissions_granted);
+ infobar()->RemoveSelf();
+}
+
+PermissionUpdateInfoBarDelegate::PermissionUpdateInfoBarDelegate(
+ content::WebContents* web_contents,
+ const std::vector<ContentSettingsType>& content_settings_types,
+ const PermissionUpdatedCallback& callback)
+ : ConfirmInfoBarDelegate(),
+ content_settings_types_(content_settings_types),
+ callback_(callback) {
+
gone 2015/07/17 17:41:40 nit: intentional newline?
Ted C 2015/07/17 20:37:18 Nope...removed
+ std::vector<int> content_settings_type_values;
+ for (ContentSettingsType type : content_settings_types)
+ content_settings_type_values.push_back(type);
+
+ JNIEnv* env = base::android::AttachCurrentThread();
+ java_delegate_.Reset(Java_PermissionUpdateInfoBarDelegate_create(
+ env,
+ reinterpret_cast<intptr_t>(this),
+ web_contents->GetJavaWebContents().obj(),
+ base::android::ToJavaIntArray(env, content_settings_type_values).obj()));
+}
+
+PermissionUpdateInfoBarDelegate::~PermissionUpdateInfoBarDelegate() {
+ Java_PermissionUpdateInfoBarDelegate_onNativeDestroyed(
+ base::android::AttachCurrentThread(), java_delegate_.obj());
+}
+
+int PermissionUpdateInfoBarDelegate::GetIconID() const {
+ return IDR_INFOBAR_WARNING;
+}
+
+base::string16 PermissionUpdateInfoBarDelegate::GetMessageText() const {
+ if (content_settings_types_.size() > 1) {
+ return l10n_util::GetStringUTF16(
+ IDS_INFOBAR_MISSING_MULTIPLE_PERMISSIONS_TEXT);
+ }
+
+ int message_id;
+ switch (content_settings_types_[0]) {
+ case CONTENT_SETTINGS_TYPE_GEOLOCATION:
+ message_id = IDS_INFOBAR_MISSING_LOCATION_PERMISSION_TEXT;
+ break;
+ case CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC:
+ message_id = IDS_INFOBAR_MISSING_MICROPHONE_PERMISSION_TEXT;
+ break;
+ case CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA:
+ message_id = IDS_INFOBAR_MISSING_CAMERA_PERMISSION_TEXT;
+ break;
+ default:
+ NOTREACHED();
+ message_id = IDS_INFOBAR_MISSING_MULTIPLE_PERMISSIONS_TEXT;
+ }
+
+ return l10n_util::GetStringUTF16(message_id);
+}
+
+int PermissionUpdateInfoBarDelegate::GetButtons() const {
+ return BUTTON_OK;
+}
+
+base::string16 PermissionUpdateInfoBarDelegate::GetButtonLabel(
+ InfoBarButton button) const {
+ DCHECK_EQ(button, BUTTON_OK);
+ return l10n_util::GetStringUTF16(IDS_INFOBAR_UPDATE_PERMISSIONS_BUTTON_TEXT);
+}
+
+bool PermissionUpdateInfoBarDelegate::Accept() {
+ Java_PermissionUpdateInfoBarDelegate_requestPermissions(
+ base::android::AttachCurrentThread(), java_delegate_.obj());
+ return false;
+}
+
+bool PermissionUpdateInfoBarDelegate::Cancel() {
+ callback_.Run(false);
+ return true;
+}

Powered by Google App Engine
This is Rietveld 408576698