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/infobar_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/strings/string_util.h"
10 #include "chrome/browser/android/resource_mapper.h"
11 #include "chrome/browser/infobars/infobar_service.h"
12 #include "components/infobars/core/infobar.h"
13 #include "components/infobars/core/infobar_delegate.h"
14 #include "jni/InfoBar_jni.h"
17 // InfoBarAndroid -------------------------------------------------------------
19 InfoBarAndroid::InfoBarAndroid(scoped_ptr
<infobars::InfoBarDelegate
> delegate
)
20 : infobars::InfoBar(delegate
.Pass()) {
23 InfoBarAndroid::~InfoBarAndroid() {
24 if (!java_info_bar_
.is_null()) {
25 JNIEnv
* env
= base::android::AttachCurrentThread();
26 Java_InfoBar_onNativeDestroyed(env
, java_info_bar_
.obj());
30 void InfoBarAndroid::ReassignJavaInfoBar(InfoBarAndroid
* replacement
) {
32 if (!java_info_bar_
.is_null()) {
33 replacement
->SetJavaInfoBar(java_info_bar_
);
34 java_info_bar_
.Reset();
38 void InfoBarAndroid::SetJavaInfoBar(
39 const base::android::JavaRef
<jobject
>& java_info_bar
) {
40 DCHECK(java_info_bar_
.is_null());
41 java_info_bar_
.Reset(java_info_bar
);
42 JNIEnv
* env
= base::android::AttachCurrentThread();
43 Java_InfoBar_setNativeInfoBar(env
, java_info_bar
.obj(),
44 reinterpret_cast<intptr_t>(this));
47 jobject
InfoBarAndroid::GetJavaInfoBar() {
48 return java_info_bar_
.obj();
51 bool InfoBarAndroid::HasSetJavaInfoBar() const {
52 return !java_info_bar_
.is_null();
55 void InfoBarAndroid::OnButtonClicked(JNIEnv
* env
,
58 jstring action_value
) {
59 std::string value
= base::android::ConvertJavaStringToUTF8(env
, action_value
);
60 ProcessButton(action
, value
);
63 void InfoBarAndroid::OnCloseButtonClicked(JNIEnv
* env
, jobject obj
) {
65 return; // We're closing; don't call anything, it might access the owner.
66 delegate()->InfoBarDismissed();
70 void InfoBarAndroid::CloseJavaInfoBar() {
71 if (!java_info_bar_
.is_null()) {
72 JNIEnv
* env
= base::android::AttachCurrentThread();
73 Java_InfoBar_closeInfoBar(env
, java_info_bar_
.obj());
77 int InfoBarAndroid::GetEnumeratedIconId() {
78 return ResourceMapper::MapFromChromiumId(delegate()->GetIconId());
82 // Native JNI methods ---------------------------------------------------------
84 bool RegisterNativeInfoBar(JNIEnv
* env
) {
85 return RegisterNativesImpl(env
);