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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/ui/android/infobars/confirm_infobar.h" 6 7 #include "base/android/jni_android.h" 8 #include "base/android/jni_string.h" 9 #include "base/logging.h" 10 #include "chrome/browser/android/resource_mapper.h" 11 #include "chrome/browser/infobars/confirm_infobar_delegate.h" 12 #include "jni/ConfirmInfoBarDelegate_jni.h" 13 14 15 // ConfirmInfoBarDelegate ----------------------------------------------------- 16 17 // static 18 scoped_ptr<InfoBar> ConfirmInfoBarDelegate::CreateInfoBar( 19 scoped_ptr<ConfirmInfoBarDelegate> delegate) { 20 return scoped_ptr<InfoBar>(new ConfirmInfoBar(delegate.Pass())); 21 } 22 23 24 // ConfirmInfoBar ------------------------------------------------------------- 25 26 ConfirmInfoBar::ConfirmInfoBar(scoped_ptr<ConfirmInfoBarDelegate> delegate) 27 : InfoBarAndroid(delegate.PassAs<InfoBarDelegate>()), 28 java_confirm_delegate_() { 29 } 30 31 ConfirmInfoBar::~ConfirmInfoBar() { 32 } 33 34 base::android::ScopedJavaLocalRef<jobject> ConfirmInfoBar::CreateRenderInfoBar( 35 JNIEnv* env) { 36 java_confirm_delegate_.Reset(Java_ConfirmInfoBarDelegate_create(env)); 37 base::android::ScopedJavaLocalRef<jstring> ok_button_text = 38 base::android::ConvertUTF16ToJavaString( 39 env, GetTextFor(ConfirmInfoBarDelegate::BUTTON_OK)); 40 base::android::ScopedJavaLocalRef<jstring> cancel_button_text = 41 base::android::ConvertUTF16ToJavaString( 42 env, GetTextFor(ConfirmInfoBarDelegate::BUTTON_CANCEL)); 43 ConfirmInfoBarDelegate* delegate = GetDelegate(); 44 base::android::ScopedJavaLocalRef<jstring> message_text = 45 base::android::ConvertUTF16ToJavaString( 46 env, delegate->GetMessageText()); 47 base::android::ScopedJavaLocalRef<jstring> link_text = 48 base::android::ConvertUTF16ToJavaString( 49 env, delegate->GetLinkText()); 50 51 return Java_ConfirmInfoBarDelegate_showConfirmInfoBar( 52 env, java_confirm_delegate_.obj(), reinterpret_cast<intptr_t>(this), 53 GetEnumeratedIconId(), message_text.obj(), link_text.obj(), 54 ok_button_text.obj(), cancel_button_text.obj()); 55 } 56 57 void ConfirmInfoBar::OnLinkClicked(JNIEnv* env, jobject obj) { 58 if (!owner()) 59 return; // We're closing; don't call anything, it might access the owner. 60 61 if (GetDelegate()->LinkClicked(NEW_FOREGROUND_TAB)) 62 RemoveSelf(); 63 } 64 65 void ConfirmInfoBar::ProcessButton(int action, 66 const std::string& action_value) { 67 if (!owner()) 68 return; // We're closing; don't call anything, it might access the owner. 69 70 DCHECK((action == InfoBarAndroid::ACTION_OK) || 71 (action == InfoBarAndroid::ACTION_CANCEL)); 72 ConfirmInfoBarDelegate* delegate = GetDelegate(); 73 if ((action == InfoBarAndroid::ACTION_OK) ? 74 delegate->Accept() : delegate->Cancel()) 75 RemoveSelf(); 76 } 77 78 ConfirmInfoBarDelegate* ConfirmInfoBar::GetDelegate() { 79 return delegate()->AsConfirmInfoBarDelegate(); 80 } 81 82 base::string16 ConfirmInfoBar::GetTextFor( 83 ConfirmInfoBarDelegate::InfoBarButton button) { 84 ConfirmInfoBarDelegate* delegate = GetDelegate(); 85 return (delegate->GetButtons() & button) ? 86 delegate->GetButtonLabel(button) : base::string16(); 87 } 88 89 90 // Native JNI methods --------------------------------------------------------- 91 92 bool RegisterConfirmInfoBarDelegate(JNIEnv* env) { 93 return RegisterNativesImpl(env); 94 } 95