Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / android / infobars / infobar_android.cc
blob282d94de2e5cc9a1beb125ff0fa56b8b0dcffca4
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) {
31 DCHECK(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,
56 jobject obj,
57 jint action,
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) {
64 if (!owner())
65 return; // We're closing; don't call anything, it might access the owner.
66 delegate()->InfoBarDismissed();
67 RemoveSelf();
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);