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.h"
12 #include "chrome/browser/infobars/infobar_delegate.h"
13 #include "chrome/browser/infobars/infobar_service.h"
14 #include "jni/InfoBar_jni.h"
15
16
17 // InfoBar --------------------------------------------------------------------
18
19 // Static constants defined in infobar.h. We don't really use them for anything
20 // but they are required. The values are copied from the GTK implementation.
21 const int InfoBar::kSeparatorLineHeight = 1;
22 const int InfoBar::kDefaultArrowTargetHeight = 9;
23 const int InfoBar::kMaximumArrowTargetHeight = 24;
24 const int InfoBar::kDefaultArrowTargetHalfWidth = kDefaultArrowTargetHeight;
25 const int InfoBar::kMaximumArrowTargetHalfWidth = 14;
26 const int InfoBar::kDefaultBarTargetHeight = 36;
27
28
29 // InfoBarAndroid -------------------------------------------------------------
30
InfoBarAndroid(scoped_ptr<InfoBarDelegate> delegate)31 InfoBarAndroid::InfoBarAndroid(scoped_ptr<InfoBarDelegate> delegate)
32 : InfoBar(delegate.Pass()) {
33 }
34
~InfoBarAndroid()35 InfoBarAndroid::~InfoBarAndroid() {
36 }
37
ReassignJavaInfoBar(InfoBarAndroid * replacement)38 void InfoBarAndroid::ReassignJavaInfoBar(InfoBarAndroid* replacement) {
39 DCHECK(replacement);
40 if (!java_info_bar_.is_null()) {
41 replacement->set_java_infobar(java_info_bar_);
42 java_info_bar_.Reset();
43 }
44 }
45
set_java_infobar(const base::android::JavaRef<jobject> & java_info_bar)46 void InfoBarAndroid::set_java_infobar(
47 const base::android::JavaRef<jobject>& java_info_bar) {
48 DCHECK(java_info_bar_.is_null());
49 java_info_bar_.Reset(java_info_bar);
50 }
51
HasSetJavaInfoBar() const52 bool InfoBarAndroid::HasSetJavaInfoBar() const {
53 return !java_info_bar_.is_null();
54 }
55
OnButtonClicked(JNIEnv * env,jobject obj,jint action,jstring action_value)56 void InfoBarAndroid::OnButtonClicked(JNIEnv* env,
57 jobject obj,
58 jint action,
59 jstring action_value) {
60 std::string value = base::android::ConvertJavaStringToUTF8(env, action_value);
61 ProcessButton(action, value);
62 }
63
OnCloseButtonClicked(JNIEnv * env,jobject obj)64 void InfoBarAndroid::OnCloseButtonClicked(JNIEnv* env, jobject obj) {
65 delegate()->InfoBarDismissed();
66 RemoveSelf();
67 }
68
CloseJavaInfoBar()69 void InfoBarAndroid::CloseJavaInfoBar() {
70 if (!java_info_bar_.is_null()) {
71 JNIEnv* env = base::android::AttachCurrentThread();
72 Java_InfoBar_closeInfoBar(env, java_info_bar_.obj());
73 }
74 }
75
GetEnumeratedIconId()76 int InfoBarAndroid::GetEnumeratedIconId() {
77 return ResourceMapper::MapFromChromiumId(delegate()->GetIconID());
78 }
79
80
81 // Native JNI methods ---------------------------------------------------------
82
RegisterNativeInfoBar(JNIEnv * env)83 bool RegisterNativeInfoBar(JNIEnv* env) {
84 return RegisterNativesImpl(env);
85 }
86