| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/android/infobars/confirm_infobar.h" | 5 #include "chrome/browser/ui/android/infobars/confirm_infobar.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "chrome/browser/android/tab_android.h" |
| 12 #include "chrome/browser/infobars/infobar_service.h" | 13 #include "chrome/browser/infobars/infobar_service.h" |
| 13 #include "components/infobars/core/confirm_infobar_delegate.h" | 14 #include "components/infobars/core/confirm_infobar_delegate.h" |
| 14 #include "content/public/browser/android/content_view_core.h" | |
| 15 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
| 16 #include "jni/ConfirmInfoBar_jni.h" | 16 #include "jni/ConfirmInfoBar_jni.h" |
| 17 #include "ui/android/window_android.h" | 17 #include "ui/android/window_android.h" |
| 18 #include "ui/gfx/android/java_bitmap.h" | 18 #include "ui/gfx/android/java_bitmap.h" |
| 19 #include "ui/gfx/image/image.h" | 19 #include "ui/gfx/image/image.h" |
| 20 | 20 |
| 21 using base::android::JavaParamRef; | 21 using base::android::JavaParamRef; |
| 22 using base::android::ScopedJavaLocalRef; | 22 using base::android::ScopedJavaLocalRef; |
| 23 | 23 |
| 24 // InfoBarService ------------------------------------------------------------- | 24 // InfoBarService ------------------------------------------------------------- |
| 25 | 25 |
| 26 std::unique_ptr<infobars::InfoBar> InfoBarService::CreateConfirmInfoBar( | 26 std::unique_ptr<infobars::InfoBar> InfoBarService::CreateConfirmInfoBar( |
| 27 std::unique_ptr<ConfirmInfoBarDelegate> delegate) { | 27 std::unique_ptr<ConfirmInfoBarDelegate> delegate) { |
| 28 return base::MakeUnique<ConfirmInfoBar>(std::move(delegate)); | 28 return base::MakeUnique<ConfirmInfoBar>(std::move(delegate)); |
| 29 } | 29 } |
| 30 | 30 |
| 31 | 31 |
| 32 // ConfirmInfoBar ------------------------------------------------------------- | 32 // ConfirmInfoBar ------------------------------------------------------------- |
| 33 | 33 |
| 34 ConfirmInfoBar::ConfirmInfoBar(std::unique_ptr<ConfirmInfoBarDelegate> delegate) | 34 ConfirmInfoBar::ConfirmInfoBar(std::unique_ptr<ConfirmInfoBarDelegate> delegate) |
| 35 : InfoBarAndroid(std::move(delegate)) {} | 35 : InfoBarAndroid(std::move(delegate)) {} |
| 36 | 36 |
| 37 ConfirmInfoBar::~ConfirmInfoBar() { | 37 ConfirmInfoBar::~ConfirmInfoBar() { |
| 38 } | 38 } |
| 39 | 39 |
| 40 base::string16 ConfirmInfoBar::GetTextFor( |
| 41 ConfirmInfoBarDelegate::InfoBarButton button) { |
| 42 ConfirmInfoBarDelegate* delegate = GetDelegate(); |
| 43 return (delegate->GetButtons() & button) ? |
| 44 delegate->GetButtonLabel(button) : base::string16(); |
| 45 } |
| 46 |
| 47 ConfirmInfoBarDelegate* ConfirmInfoBar::GetDelegate() { |
| 48 return delegate()->AsConfirmInfoBarDelegate(); |
| 49 } |
| 50 |
| 51 TabAndroid* ConfirmInfoBar::GetTab() { |
| 52 content::WebContents* web_contents = |
| 53 InfoBarService::WebContentsFromInfoBar(this); |
| 54 DCHECK(web_contents); |
| 55 |
| 56 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); |
| 57 DCHECK(tab); |
| 58 return tab; |
| 59 } |
| 60 |
| 40 ScopedJavaLocalRef<jobject> ConfirmInfoBar::CreateRenderInfoBar(JNIEnv* env) { | 61 ScopedJavaLocalRef<jobject> ConfirmInfoBar::CreateRenderInfoBar(JNIEnv* env) { |
| 41 ScopedJavaLocalRef<jstring> ok_button_text = | 62 ScopedJavaLocalRef<jstring> ok_button_text = |
| 42 base::android::ConvertUTF16ToJavaString( | 63 base::android::ConvertUTF16ToJavaString( |
| 43 env, GetTextFor(ConfirmInfoBarDelegate::BUTTON_OK)); | 64 env, GetTextFor(ConfirmInfoBarDelegate::BUTTON_OK)); |
| 44 ScopedJavaLocalRef<jstring> cancel_button_text = | 65 ScopedJavaLocalRef<jstring> cancel_button_text = |
| 45 base::android::ConvertUTF16ToJavaString( | 66 base::android::ConvertUTF16ToJavaString( |
| 46 env, GetTextFor(ConfirmInfoBarDelegate::BUTTON_CANCEL)); | 67 env, GetTextFor(ConfirmInfoBarDelegate::BUTTON_CANCEL)); |
| 47 ConfirmInfoBarDelegate* delegate = GetDelegate(); | 68 ConfirmInfoBarDelegate* delegate = GetDelegate(); |
| 48 ScopedJavaLocalRef<jstring> message_text = | 69 ScopedJavaLocalRef<jstring> message_text = |
| 49 base::android::ConvertUTF16ToJavaString(env, delegate->GetMessageText()); | 70 base::android::ConvertUTF16ToJavaString(env, delegate->GetMessageText()); |
| 50 ScopedJavaLocalRef<jstring> link_text = | 71 ScopedJavaLocalRef<jstring> link_text = |
| 51 base::android::ConvertUTF16ToJavaString(env, delegate->GetLinkText()); | 72 base::android::ConvertUTF16ToJavaString(env, delegate->GetLinkText()); |
| 52 | 73 |
| 53 ScopedJavaLocalRef<jobject> java_bitmap; | 74 ScopedJavaLocalRef<jobject> java_bitmap; |
| 54 if (delegate->GetIconId() == infobars::InfoBarDelegate::kNoIconID && | 75 if (delegate->GetIconId() == infobars::InfoBarDelegate::kNoIconID && |
| 55 !delegate->GetIcon().IsEmpty()) { | 76 !delegate->GetIcon().IsEmpty()) { |
| 56 java_bitmap = gfx::ConvertToJavaBitmap(delegate->GetIcon().ToSkBitmap()); | 77 java_bitmap = gfx::ConvertToJavaBitmap(delegate->GetIcon().ToSkBitmap()); |
| 57 } | 78 } |
| 58 | 79 |
| 59 return Java_ConfirmInfoBar_create(env, GetEnumeratedIconId(), java_bitmap, | 80 return Java_ConfirmInfoBar_create(env, GetEnumeratedIconId(), java_bitmap, |
| 60 message_text, link_text, ok_button_text, | 81 message_text, link_text, ok_button_text, |
| 61 cancel_button_text); | 82 cancel_button_text); |
| 62 } | 83 } |
| 63 | 84 |
| 64 ScopedJavaLocalRef<jobject> ConfirmInfoBar::GetWindowAndroid() { | |
| 65 content::WebContents* web_contents = | |
| 66 InfoBarService::WebContentsFromInfoBar(this); | |
| 67 DCHECK(web_contents); | |
| 68 content::ContentViewCore* cvc = | |
| 69 content::ContentViewCore::FromWebContents(web_contents); | |
| 70 DCHECK(cvc); | |
| 71 return cvc->GetWindowAndroid()->GetJavaObject(); | |
| 72 } | |
| 73 | |
| 74 void ConfirmInfoBar::OnLinkClicked(JNIEnv* env, | 85 void ConfirmInfoBar::OnLinkClicked(JNIEnv* env, |
| 75 const JavaParamRef<jobject>& obj) { | 86 const JavaParamRef<jobject>& obj) { |
| 76 if (!owner()) | 87 if (!owner()) |
| 77 return; // We're closing; don't call anything, it might access the owner. | 88 return; // We're closing; don't call anything, it might access the owner. |
| 78 | 89 |
| 79 if (GetDelegate()->LinkClicked(WindowOpenDisposition::NEW_FOREGROUND_TAB)) | 90 if (GetDelegate()->LinkClicked(WindowOpenDisposition::NEW_FOREGROUND_TAB)) |
| 80 RemoveSelf(); | 91 RemoveSelf(); |
| 81 } | 92 } |
| 82 | 93 |
| 83 void ConfirmInfoBar::ProcessButton(int action) { | 94 void ConfirmInfoBar::ProcessButton(int action) { |
| 84 if (!owner()) | 95 if (!owner()) |
| 85 return; // We're closing; don't call anything, it might access the owner. | 96 return; // We're closing; don't call anything, it might access the owner. |
| 86 | 97 |
| 87 DCHECK((action == InfoBarAndroid::ACTION_OK) || | 98 DCHECK((action == InfoBarAndroid::ACTION_OK) || |
| 88 (action == InfoBarAndroid::ACTION_CANCEL)); | 99 (action == InfoBarAndroid::ACTION_CANCEL)); |
| 89 ConfirmInfoBarDelegate* delegate = GetDelegate(); | 100 ConfirmInfoBarDelegate* delegate = GetDelegate(); |
| 90 if ((action == InfoBarAndroid::ACTION_OK) ? | 101 if ((action == InfoBarAndroid::ACTION_OK) ? |
| 91 delegate->Accept() : delegate->Cancel()) | 102 delegate->Accept() : delegate->Cancel()) |
| 92 RemoveSelf(); | 103 RemoveSelf(); |
| 93 } | 104 } |
| 94 | |
| 95 ConfirmInfoBarDelegate* ConfirmInfoBar::GetDelegate() { | |
| 96 return delegate()->AsConfirmInfoBarDelegate(); | |
| 97 } | |
| 98 | |
| 99 base::string16 ConfirmInfoBar::GetTextFor( | |
| 100 ConfirmInfoBarDelegate::InfoBarButton button) { | |
| 101 ConfirmInfoBarDelegate* delegate = GetDelegate(); | |
| 102 return (delegate->GetButtons() & button) ? | |
| 103 delegate->GetButtonLabel(button) : base::string16(); | |
| 104 } | |
| OLD | NEW |