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.
5 #include "chrome/browser/ui/android/infobars/confirm_infobar.h"
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"
15 // ConfirmInfoBarDelegate -----------------------------------------------------
18 scoped_ptr
<InfoBar
> ConfirmInfoBarDelegate::CreateInfoBar(
19 scoped_ptr
<ConfirmInfoBarDelegate
> delegate
) {
20 return scoped_ptr
<InfoBar
>(new ConfirmInfoBar(delegate
.Pass()));
24 // ConfirmInfoBar -------------------------------------------------------------
26 ConfirmInfoBar::ConfirmInfoBar(scoped_ptr
<ConfirmInfoBarDelegate
> delegate
)
27 : InfoBarAndroid(delegate
.PassAs
<InfoBarDelegate
>()),
28 java_confirm_delegate_() {
31 ConfirmInfoBar::~ConfirmInfoBar() {
34 base::android::ScopedJavaLocalRef
<jobject
> ConfirmInfoBar::CreateRenderInfoBar(
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());
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());
57 void ConfirmInfoBar::OnLinkClicked(JNIEnv
* env
, jobject obj
) {
59 return; // We're closing; don't call anything, it might access the owner.
61 if (GetDelegate()->LinkClicked(NEW_FOREGROUND_TAB
))
65 void ConfirmInfoBar::ProcessButton(int action
,
66 const std::string
& action_value
) {
68 return; // We're closing; don't call anything, it might access the owner.
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())
78 ConfirmInfoBarDelegate
* ConfirmInfoBar::GetDelegate() {
79 return delegate()->AsConfirmInfoBarDelegate();
82 base::string16
ConfirmInfoBar::GetTextFor(
83 ConfirmInfoBarDelegate::InfoBarButton button
) {
84 ConfirmInfoBarDelegate
* delegate
= GetDelegate();
85 return (delegate
->GetButtons() & button
) ?
86 delegate
->GetButtonLabel(button
) : base::string16();
90 // Native JNI methods ---------------------------------------------------------
92 bool RegisterConfirmInfoBarDelegate(JNIEnv
* env
) {
93 return RegisterNativesImpl(env
);