OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/ui/android/infobars/simple_confirm_infobar_builder.h" |
| 6 |
| 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" |
| 9 #include "base/android/scoped_java_ref.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "chrome/browser/android/tab_android.h" |
| 13 #include "chrome/browser/infobars/infobar_service.h" |
| 14 #include "components/infobars/core/confirm_infobar_delegate.h" |
| 15 #include "components/infobars/core/infobar.h" |
| 16 #include "jni/SimpleConfirmInfoBarBuilder_jni.h" |
| 17 #include "ui/gfx/android/java_bitmap.h" |
| 18 #include "ui/gfx/image/image.h" |
| 19 #include "ui/gfx/vector_icons_public.h" |
| 20 |
| 21 namespace { |
| 22 |
| 23 // Delegate for a simple ConfirmInfoBar triggered via JNI. |
| 24 class SimpleConfirmInfoBarDelegate : public ConfirmInfoBarDelegate { |
| 25 public: |
| 26 SimpleConfirmInfoBarDelegate( |
| 27 const JavaParamRef<jobject>& j_listener, |
| 28 infobars::InfoBarDelegate::InfoBarIdentifier infobar_identifier, |
| 29 const gfx::Image& bitmap, |
| 30 const base::string16& message_str, |
| 31 const base::string16& primary_str, |
| 32 const base::string16& secondary_str, |
| 33 bool auto_expire); |
| 34 |
| 35 ~SimpleConfirmInfoBarDelegate() override; |
| 36 |
| 37 // ConfirmInfoBarDelegate: |
| 38 infobars::InfoBarDelegate::InfoBarIdentifier GetIdentifier() const override; |
| 39 gfx::Image GetIcon() const override; |
| 40 bool ShouldExpire(const NavigationDetails& details) const override; |
| 41 void InfoBarDismissed() override; |
| 42 base::string16 GetMessageText() const override; |
| 43 int GetButtons() const override; |
| 44 base::string16 GetButtonLabel(InfoBarButton button) const override; |
| 45 bool Accept() override; |
| 46 bool Cancel() override; |
| 47 |
| 48 private: |
| 49 base::android::ScopedJavaGlobalRef<jobject> java_listener_; |
| 50 infobars::InfoBarDelegate::InfoBarIdentifier identifier_; |
| 51 gfx::Image icon_bitmap_; |
| 52 base::string16 message_str_; |
| 53 base::string16 primary_str_; |
| 54 base::string16 secondary_str_; |
| 55 bool auto_expire_; |
| 56 |
| 57 DISALLOW_COPY_AND_ASSIGN(SimpleConfirmInfoBarDelegate); |
| 58 }; |
| 59 |
| 60 SimpleConfirmInfoBarDelegate::SimpleConfirmInfoBarDelegate( |
| 61 const JavaParamRef<jobject>& j_listener, |
| 62 infobars::InfoBarDelegate::InfoBarIdentifier identifier, |
| 63 const gfx::Image& bitmap, |
| 64 const base::string16& message_str, |
| 65 const base::string16& primary_str, |
| 66 const base::string16& secondary_str, |
| 67 bool auto_expire) |
| 68 : identifier_(identifier), |
| 69 icon_bitmap_(bitmap), |
| 70 message_str_(message_str), |
| 71 primary_str_(primary_str), |
| 72 secondary_str_(secondary_str), |
| 73 auto_expire_(auto_expire) { |
| 74 java_listener_.Reset(j_listener); |
| 75 } |
| 76 |
| 77 SimpleConfirmInfoBarDelegate::~SimpleConfirmInfoBarDelegate() { |
| 78 } |
| 79 |
| 80 infobars::InfoBarDelegate::InfoBarIdentifier |
| 81 SimpleConfirmInfoBarDelegate::GetIdentifier() const { |
| 82 return identifier_; |
| 83 } |
| 84 |
| 85 gfx::Image SimpleConfirmInfoBarDelegate::GetIcon() const { |
| 86 return icon_bitmap_.IsEmpty() ? ConfirmInfoBarDelegate::GetIcon() |
| 87 : icon_bitmap_; |
| 88 } |
| 89 |
| 90 bool SimpleConfirmInfoBarDelegate::ShouldExpire( |
| 91 const NavigationDetails& details) const { |
| 92 return auto_expire_ && ConfirmInfoBarDelegate::ShouldExpire(details); |
| 93 } |
| 94 |
| 95 void SimpleConfirmInfoBarDelegate::InfoBarDismissed() { |
| 96 Java_SimpleConfirmInfoBarBuilder_onInfoBarDismissed( |
| 97 base::android::AttachCurrentThread(), java_listener_.obj()); |
| 98 } |
| 99 |
| 100 base::string16 SimpleConfirmInfoBarDelegate::GetMessageText() const { |
| 101 return message_str_; |
| 102 } |
| 103 |
| 104 int SimpleConfirmInfoBarDelegate::GetButtons() const { |
| 105 return (primary_str_.empty() ? 0 : BUTTON_OK) | |
| 106 (secondary_str_.empty() ? 0 : BUTTON_CANCEL); |
| 107 } |
| 108 |
| 109 base::string16 |
| 110 SimpleConfirmInfoBarDelegate::GetButtonLabel(InfoBarButton button) const { |
| 111 return button == BUTTON_OK ? primary_str_ : secondary_str_; |
| 112 } |
| 113 |
| 114 bool SimpleConfirmInfoBarDelegate::Accept() { |
| 115 Java_SimpleConfirmInfoBarBuilder_onInfoBarButtonClicked( |
| 116 base::android::AttachCurrentThread(), java_listener_.obj(), true); |
| 117 return true; |
| 118 } |
| 119 |
| 120 bool SimpleConfirmInfoBarDelegate::Cancel() { |
| 121 Java_SimpleConfirmInfoBarBuilder_onInfoBarButtonClicked( |
| 122 base::android::AttachCurrentThread(), java_listener_.obj(), false); |
| 123 return true; |
| 124 } |
| 125 |
| 126 } // anonymous namespace |
| 127 |
| 128 // Native JNI methods --------------------------------------------------------- |
| 129 |
| 130 void Create(JNIEnv* env, |
| 131 const JavaParamRef<jclass>& j_caller, |
| 132 const JavaParamRef<jobject>& j_tab, |
| 133 jint j_identifier, |
| 134 const JavaParamRef<jobject>& j_icon, |
| 135 const JavaParamRef<jstring>& j_message, |
| 136 const JavaParamRef<jstring>& j_primary, |
| 137 const JavaParamRef<jstring>& j_secondary, |
| 138 jboolean auto_expire, |
| 139 const JavaParamRef<jobject>& j_listener) { |
| 140 infobars::InfoBarDelegate::InfoBarIdentifier infobar_identifier = |
| 141 static_cast<infobars::InfoBarDelegate::InfoBarIdentifier>(j_identifier); |
| 142 |
| 143 gfx::Image icon_bitmap; |
| 144 if (j_icon) { |
| 145 icon_bitmap = gfx::Image::CreateFrom1xBitmap( |
| 146 gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(j_icon))); |
| 147 } |
| 148 |
| 149 base::string16 message_str = j_message.is_null() |
| 150 ? base::string16() |
| 151 : base::android::ConvertJavaStringToUTF16(env, j_message); |
| 152 base::string16 primary_str = j_primary.is_null() |
| 153 ? base::string16() |
| 154 : base::android::ConvertJavaStringToUTF16(env, j_primary); |
| 155 base::string16 secondary_str = j_secondary.is_null() |
| 156 ? base::string16() |
| 157 : base::android::ConvertJavaStringToUTF16(env, j_secondary); |
| 158 |
| 159 InfoBarService* service = InfoBarService::FromWebContents( |
| 160 TabAndroid::GetNativeTab(env, j_tab)->web_contents()); |
| 161 service->AddInfoBar(service->CreateConfirmInfoBar( |
| 162 make_scoped_ptr(new SimpleConfirmInfoBarDelegate( |
| 163 j_listener, |
| 164 infobar_identifier, |
| 165 icon_bitmap, |
| 166 message_str, |
| 167 primary_str, |
| 168 secondary_str, |
| 169 auto_expire)))); |
| 170 } |
| 171 |
| 172 bool RegisterSimpleConfirmInfoBarBuilder(JNIEnv* env) { |
| 173 return RegisterNativesImpl(env); |
| 174 } |
OLD | NEW |