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/infobar_service.h"
12 #include "components/infobars/core/confirm_infobar_delegate.h"
13 #include "jni/ConfirmInfoBarDelegate_jni.h"
14 #include "ui/gfx/android/java_bitmap.h"
15 #include "ui/gfx/image/image.h"
17 // InfoBarService -------------------------------------------------------------
19 scoped_ptr
<infobars::InfoBar
> InfoBarService::CreateConfirmInfoBar(
20 scoped_ptr
<ConfirmInfoBarDelegate
> delegate
) {
21 return make_scoped_ptr(new ConfirmInfoBar(delegate
.Pass()));
25 // ConfirmInfoBar -------------------------------------------------------------
27 ConfirmInfoBar::ConfirmInfoBar(scoped_ptr
<ConfirmInfoBarDelegate
> delegate
)
28 : InfoBarAndroid(delegate
.Pass()), 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 ScopedJavaLocalRef
<jobject
> java_bitmap
;
52 if (!delegate
->GetIcon().IsEmpty()) {
53 java_bitmap
= gfx::ConvertToJavaBitmap(delegate
->GetIcon().ToSkBitmap());
56 return Java_ConfirmInfoBarDelegate_showConfirmInfoBar(
57 env
, java_confirm_delegate_
.obj(), reinterpret_cast<intptr_t>(this),
58 GetEnumeratedIconId(), java_bitmap
.obj(), message_text
.obj(),
59 link_text
.obj(), ok_button_text
.obj(), cancel_button_text
.obj());
62 void ConfirmInfoBar::OnLinkClicked(JNIEnv
* env
, jobject obj
) {
64 return; // We're closing; don't call anything, it might access the owner.
66 if (GetDelegate()->LinkClicked(NEW_FOREGROUND_TAB
))
70 void ConfirmInfoBar::ProcessButton(int action
,
71 const std::string
& action_value
) {
73 return; // We're closing; don't call anything, it might access the owner.
75 DCHECK((action
== InfoBarAndroid::ACTION_OK
) ||
76 (action
== InfoBarAndroid::ACTION_CANCEL
));
77 ConfirmInfoBarDelegate
* delegate
= GetDelegate();
78 if ((action
== InfoBarAndroid::ACTION_OK
) ?
79 delegate
->Accept() : delegate
->Cancel())
83 ConfirmInfoBarDelegate
* ConfirmInfoBar::GetDelegate() {
84 return delegate()->AsConfirmInfoBarDelegate();
87 base::string16
ConfirmInfoBar::GetTextFor(
88 ConfirmInfoBarDelegate::InfoBarButton button
) {
89 ConfirmInfoBarDelegate
* delegate
= GetDelegate();
90 return (delegate
->GetButtons() & button
) ?
91 delegate
->GetButtonLabel(button
) : base::string16();
95 // Native JNI methods ---------------------------------------------------------
97 bool RegisterConfirmInfoBarDelegate(JNIEnv
* env
) {
98 return RegisterNativesImpl(env
);