OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/android/infobars/confirm_infobar.h" | 5 #include "chrome/browser/ui/android/infobars/confirm_infobar.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "chrome/browser/android/resource_mapper.h" | 10 #include "chrome/browser/android/resource_mapper.h" |
11 #include "chrome/browser/infobars/confirm_infobar_delegate.h" | 11 #include "chrome/browser/infobars/confirm_infobar_delegate.h" |
12 #include "jni/ConfirmInfoBarDelegate_jni.h" | 12 #include "jni/ConfirmInfoBarDelegate_jni.h" |
13 | 13 |
14 using base::android::ConvertUTF16ToJavaString; | 14 |
15 using base::android::ScopedJavaLocalRef; | 15 // ConfirmInfoBarDelegate ----------------------------------------------------- |
16 | 16 |
17 // static | 17 // static |
18 InfoBar* ConfirmInfoBarDelegate::CreateInfoBar(InfoBarService* owner) { | 18 InfoBar* ConfirmInfoBarDelegate::CreateInfoBar(InfoBarService* owner) { |
19 return new ConfirmInfoBar(owner, this); | 19 return new ConfirmInfoBar(owner, this); |
20 } | 20 } |
21 | 21 |
| 22 |
| 23 // ConfirmInfoBar ------------------------------------------------------------- |
| 24 |
22 ConfirmInfoBar::ConfirmInfoBar(InfoBarService* owner, InfoBarDelegate* delegate) | 25 ConfirmInfoBar::ConfirmInfoBar(InfoBarService* owner, InfoBarDelegate* delegate) |
23 : InfoBarAndroid(owner, delegate), | 26 : InfoBarAndroid(owner, delegate), |
24 delegate_(delegate->AsConfirmInfoBarDelegate()), | 27 delegate_(delegate->AsConfirmInfoBarDelegate()), |
25 java_confirm_delegate_() {} | 28 java_confirm_delegate_() { |
| 29 } |
26 | 30 |
27 ConfirmInfoBar::~ConfirmInfoBar() {} | 31 ConfirmInfoBar::~ConfirmInfoBar() { |
| 32 } |
28 | 33 |
29 ScopedJavaLocalRef<jobject> ConfirmInfoBar::CreateRenderInfoBar(JNIEnv* env) { | 34 base::android::ScopedJavaLocalRef<jobject> ConfirmInfoBar::CreateRenderInfoBar( |
| 35 JNIEnv* env) { |
30 java_confirm_delegate_.Reset(Java_ConfirmInfoBarDelegate_create(env)); | 36 java_confirm_delegate_.Reset(Java_ConfirmInfoBarDelegate_create(env)); |
31 ScopedJavaLocalRef<jstring> ok_button_text = | 37 base::android::ScopedJavaLocalRef<jstring> ok_button_text = |
32 ConvertUTF16ToJavaString(env, GetTextFor(InfoBarAndroid::ACTION_OK)); | 38 base::android::ConvertUTF16ToJavaString( |
33 ScopedJavaLocalRef<jstring> cancel_button_text = | 39 env, GetTextFor(ConfirmInfoBarDelegate::BUTTON_OK)); |
34 ConvertUTF16ToJavaString(env, GetTextFor(InfoBarAndroid::ACTION_CANCEL)); | 40 base::android::ScopedJavaLocalRef<jstring> cancel_button_text = |
35 ScopedJavaLocalRef<jstring> message_text = | 41 base::android::ConvertUTF16ToJavaString( |
36 ConvertUTF16ToJavaString(env, GetMessage()); | 42 env, GetTextFor(ConfirmInfoBarDelegate::BUTTON_CANCEL)); |
| 43 base::android::ScopedJavaLocalRef<jstring> message_text = |
| 44 base::android::ConvertUTF16ToJavaString( |
| 45 env, delegate_->GetMessageText()); |
37 | 46 |
38 return Java_ConfirmInfoBarDelegate_showConfirmInfoBar( | 47 return Java_ConfirmInfoBarDelegate_showConfirmInfoBar( |
39 env, | 48 env, java_confirm_delegate_.obj(), reinterpret_cast<jint>(this), |
40 java_confirm_delegate_.obj(), | 49 GetEnumeratedIconId(), message_text.obj(), ok_button_text.obj(), |
41 reinterpret_cast<jint>(this), | |
42 GetEnumeratedIconId(), | |
43 message_text.obj(), | |
44 ok_button_text.obj(), | |
45 cancel_button_text.obj()); | 50 cancel_button_text.obj()); |
46 } | 51 } |
47 | 52 |
48 void ConfirmInfoBar::ProcessButton(int action, | 53 void ConfirmInfoBar::ProcessButton(int action, |
49 const std::string& action_value) { | 54 const std::string& action_value) { |
50 DCHECK(action == InfoBarAndroid::ACTION_OK || | 55 DCHECK((action == InfoBarAndroid::ACTION_OK) || |
51 action == InfoBarAndroid::ACTION_CANCEL); | 56 (action == InfoBarAndroid::ACTION_CANCEL)); |
52 if ((action == InfoBarAndroid::ACTION_OK) ? delegate_->Accept() | 57 if ((action == InfoBarAndroid::ACTION_OK) ? |
53 : delegate_->Cancel()) | 58 delegate_->Accept() : delegate_->Cancel()) |
54 CloseInfoBar(); | 59 CloseInfoBar(); |
55 } | 60 } |
56 | 61 |
57 string16 ConfirmInfoBar::GetTextFor(ActionType action) { | 62 string16 ConfirmInfoBar::GetTextFor( |
58 int buttons = delegate_->GetButtons(); | 63 ConfirmInfoBarDelegate::InfoBarButton button) { |
59 switch (action) { | 64 return (delegate_->GetButtons() & button) ? |
60 case InfoBarAndroid::ACTION_OK: | 65 delegate_->GetButtonLabel(button) : string16(); |
61 if (buttons & ConfirmInfoBarDelegate::BUTTON_OK) | |
62 return delegate_->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_OK); | |
63 break; | |
64 case InfoBarAndroid::ACTION_CANCEL: | |
65 if (buttons & ConfirmInfoBarDelegate::BUTTON_CANCEL) | |
66 return delegate_->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_CANCEL); | |
67 break; | |
68 default: | |
69 break; | |
70 } | |
71 return string16(); | |
72 } | 66 } |
73 | 67 |
74 string16 ConfirmInfoBar::GetMessage() { | |
75 return delegate_->GetMessageText(); | |
76 } | |
77 | 68 |
78 // ----------------------------------------------------------------------------- | 69 // Native JNI methods --------------------------------------------------------- |
79 // Native JNI methods for confirm delegate. | 70 |
80 // ----------------------------------------------------------------------------- | |
81 bool RegisterConfirmInfoBarDelegate(JNIEnv* env) { | 71 bool RegisterConfirmInfoBarDelegate(JNIEnv* env) { |
82 return RegisterNativesImpl(env); | 72 return RegisterNativesImpl(env); |
83 } | 73 } |
OLD | NEW |