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

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

Issue 2446063002: Implement a modal permission dialog on Android gated by a feature. (Closed)
Patch Set: Allow user gesture requirement to be overridden by variations Created 4 years, 2 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_dialog_delegate.cc
diff --git a/chrome/browser/permissions/permission_dialog_delegate.cc b/chrome/browser/permissions/permission_dialog_delegate.cc
new file mode 100644
index 0000000000000000000000000000000000000000..18eb2bfdefe632f8cc2ddb82ddace6424ca34dd8
--- /dev/null
+++ b/chrome/browser/permissions/permission_dialog_delegate.cc
@@ -0,0 +1,169 @@
+// Copyright 2016 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_dialog_delegate.h"
+
+#include <utility>
+
+#include "base/android/jni_android.h"
+#include "base/android/jni_string.h"
+#include "base/feature_list.h"
+#include "build/build_config.h"
+#include "chrome/browser/android/resource_mapper.h"
+#include "chrome/browser/geolocation/geolocation_infobar_delegate_android.h"
+#include "chrome/browser/media/midi_permission_infobar_delegate_android.h"
+#include "chrome/browser/media/protected_media_identifier_infobar_delegate_android.h"
+#include "chrome/browser/notifications/notification_permission_infobar_delegate.h"
+#include "chrome/common/chrome_features.h"
+#include "components/variations/variations_associated_data.h"
+#include "content/public/browser/android/content_view_core.h"
+#include "content/public/browser/web_contents.h"
+#include "jni/PermissionDialogController_jni.h"
+#include "jni/PermissionDialogDelegate_jni.h"
+#include "ui/android/window_android.h"
+#include "ui/base/window_open_disposition.h"
+
+using base::android::ConvertUTF16ToJavaString;
+
+namespace {
+
+// Key for querying variations for whether a modal should require a gesture.
+const char kModalParamsUserGestureKey[] = "require_gesture";
+
+}
+
+// static
+void PermissionDialogDelegate::Create(
+ content::PermissionType type,
+ content::WebContents* web_contents,
+ const GURL& requesting_frame,
+ bool user_gesture,
+ Profile* profile,
+ const PermissionSetCallback& callback) {
+ DCHECK(web_contents);
+
+ std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate = nullptr;
+ switch (type) {
+ case content::PermissionType::GEOLOCATION:
+ infobar_delegate.reset(new GeolocationInfoBarDelegateAndroid(
+ requesting_frame, user_gesture, profile, callback));
+ break;
+#if defined(ENABLE_NOTIFICATIONS)
+ case content::PermissionType::NOTIFICATIONS:
+ case content::PermissionType::PUSH_MESSAGING:
+ infobar_delegate.reset(new NotificationPermissionInfoBarDelegate(
+ requesting_frame, user_gesture, profile, callback));
gone 2016/10/27 20:33:35 indentation is off here and below
dominickn 2016/10/27 23:37:13 Done.
+ break;
+#endif // ENABLE_NOTIFICATIONS
+ case content::PermissionType::MIDI_SYSEX:
+ infobar_delegate.reset(new MidiPermissionInfoBarDelegateAndroid(
+ requesting_frame, user_gesture, profile, callback));
+ break;
+ case content::PermissionType::PROTECTED_MEDIA_IDENTIFIER:
+ infobar_delegate.reset(new ProtectedMediaIdentifierInfoBarDelegateAndroid(
+ requesting_frame, user_gesture, profile, callback));
+ break;
+ default:
+ NOTREACHED();
+ }
+
+ PermissionDialogDelegate* dialog_delegate =
+ new PermissionDialogDelegate(web_contents, std::move(infobar_delegate));
+
+ JNIEnv* env = base::android::AttachCurrentThread();
+ base::android::ScopedJavaLocalRef<jobject> j_delegate =
+ dialog_delegate->CreateJavaDelegate(env);
+
+ // Java queues the dialog request if neccesary, and owns the the lifetime of
+ // the delegate.
gone 2016/10/27 20:33:35 Comment needs reworking. Not sure you can own the
dominickn 2016/10/27 23:37:13 Done.
+ Java_PermissionDialogController_createDialog(env, j_delegate.obj());
+}
+
+// static
+bool PermissionDialogDelegate::ShouldShowModalPrompt(bool has_user_gesture) {
+ if (!base::FeatureList::IsEnabled(features::kModalPermissionPrompts))
+ return false;
+
+ // Enforce a user gesture requirement by default. Only relax this if
+ // variations has a require_gesture key, and that key is one of 0 or false.
+ std::string require_gesture = variations::GetVariationParamValueByFeature(
+ features::kModalPermissionPrompts, kModalParamsUserGestureKey);
+ if (require_gesture == "0" || require_gesture == "false")
+ return true;
+ return has_user_gesture;
+}
+
+// static
+bool PermissionDialogDelegate::RegisterPermissionDialogDelegate(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+ScopedJavaLocalRef<jobject> PermissionDialogDelegate::CreateJavaDelegate(
+ JNIEnv* env) {
+ content::ContentViewCore* cvc =
+ content::ContentViewCore::FromWebContents(web_contents());
+ DCHECK(cvc);
+
+ return Java_PermissionDialogDelegate_create(
+ env, reinterpret_cast<uintptr_t>(this),
+ cvc->GetWindowAndroid()->GetJavaObject(),
+ ResourceMapper::MapFromChromiumId(infobar_delegate_->GetIconId()),
+ ConvertUTF16ToJavaString(env, infobar_delegate_->GetMessageText()),
+ ConvertUTF16ToJavaString(env, infobar_delegate_->GetLinkText()),
+ ConvertUTF16ToJavaString(env, infobar_delegate_->GetButtonLabel(
+ PermissionInfoBarDelegate::BUTTON_OK)),
+ ConvertUTF16ToJavaString(env,
+ infobar_delegate_->GetButtonLabel(
+ PermissionInfoBarDelegate::BUTTON_CANCEL)),
+ infobar_delegate_->ShouldShowPersistenceToggle());
+}
+
+void PermissionDialogDelegate::Accept(JNIEnv* env,
+ const JavaParamRef<jobject>& obj,
+ jboolean persist) {
+ if (infobar_delegate_->ShouldShowPersistenceToggle())
+ infobar_delegate_->set_persist(persist);
+ infobar_delegate_->Accept();
+}
+
+void PermissionDialogDelegate::Cancel(JNIEnv* env,
+ const JavaParamRef<jobject>& obj,
+ jboolean persist) {
+ if (infobar_delegate_->ShouldShowPersistenceToggle())
+ infobar_delegate_->set_persist(persist);
+ infobar_delegate_->Cancel();
+}
+
+void PermissionDialogDelegate::Dismissed(JNIEnv* env,
+ const JavaParamRef<jobject>& obj) {
+ infobar_delegate_->InfoBarDismissed();
+}
+
+void PermissionDialogDelegate::LinkClicked(JNIEnv* env,
+ const JavaParamRef<jobject>& obj) {
+ // Don't call delegate_->LinkClicked() because that relies on having an
+ // InfoBarService as an owner() to open the link. That will fail since the
+ // wrapped delegate has no owner (is hasn't been added as an infobar).
gone 2016/10/27 20:33:35 is hasn't
dominickn 2016/10/27 23:37:13 Done.
+ if (web_contents()) {
+ web_contents()->OpenURL(content::OpenURLParams(
+ infobar_delegate_->GetLinkURL(), content::Referrer(),
+ WindowOpenDisposition::NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_LINK,
+ false));
+ }
+}
+
+void PermissionDialogDelegate::Destroy(JNIEnv* env,
+ const JavaParamRef<jobject>& obj) {
+ delete this;
+}
+
+PermissionDialogDelegate::PermissionDialogDelegate(
+ content::WebContents* web_contents,
+ std::unique_ptr<PermissionInfoBarDelegate> infobar_delegate)
+ : content::WebContentsObserver(web_contents),
+ infobar_delegate_(std::move(infobar_delegate)) {
+ DCHECK(infobar_delegate_);
+}
+
+PermissionDialogDelegate::~PermissionDialogDelegate() {}

Powered by Google App Engine
This is Rietveld 408576698