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/infobar_android.h"
6
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"
15
16
17 // InfoBarAndroid -------------------------------------------------------------
18
InfoBarAndroid(scoped_ptr<infobars::InfoBarDelegate> delegate)19 InfoBarAndroid::InfoBarAndroid(scoped_ptr<infobars::InfoBarDelegate> delegate)
20 : infobars::InfoBar(delegate.Pass()) {
21 }
22
~InfoBarAndroid()23 InfoBarAndroid::~InfoBarAndroid() {
24 }
25
ReassignJavaInfoBar(InfoBarAndroid * replacement)26 void InfoBarAndroid::ReassignJavaInfoBar(InfoBarAndroid* replacement) {
27 DCHECK(replacement);
28 if (!java_info_bar_.is_null()) {
29 replacement->set_java_infobar(java_info_bar_);
30 java_info_bar_.Reset();
31 }
32 }
33
set_java_infobar(const base::android::JavaRef<jobject> & java_info_bar)34 void InfoBarAndroid::set_java_infobar(
35 const base::android::JavaRef<jobject>& java_info_bar) {
36 DCHECK(java_info_bar_.is_null());
37 java_info_bar_.Reset(java_info_bar);
38 }
39
HasSetJavaInfoBar() const40 bool InfoBarAndroid::HasSetJavaInfoBar() const {
41 return !java_info_bar_.is_null();
42 }
43
OnButtonClicked(JNIEnv * env,jobject obj,jint action,jstring action_value)44 void InfoBarAndroid::OnButtonClicked(JNIEnv* env,
45 jobject obj,
46 jint action,
47 jstring action_value) {
48 std::string value = base::android::ConvertJavaStringToUTF8(env, action_value);
49 ProcessButton(action, value);
50 }
51
OnCloseButtonClicked(JNIEnv * env,jobject obj)52 void InfoBarAndroid::OnCloseButtonClicked(JNIEnv* env, jobject obj) {
53 if (!owner())
54 return; // We're closing; don't call anything, it might access the owner.
55 delegate()->InfoBarDismissed();
56 RemoveSelf();
57 }
58
CloseJavaInfoBar()59 void InfoBarAndroid::CloseJavaInfoBar() {
60 if (!java_info_bar_.is_null()) {
61 JNIEnv* env = base::android::AttachCurrentThread();
62 Java_InfoBar_closeInfoBar(env, java_info_bar_.obj());
63 }
64 }
65
GetEnumeratedIconId()66 int InfoBarAndroid::GetEnumeratedIconId() {
67 return ResourceMapper::MapFromChromiumId(delegate()->GetIconID());
68 }
69
70
71 // Native JNI methods ---------------------------------------------------------
72
RegisterNativeInfoBar(JNIEnv * env)73 bool RegisterNativeInfoBar(JNIEnv* env) {
74 return RegisterNativesImpl(env);
75 }
76