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