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 | 14 |
15 // ConfirmInfoBarDelegate ----------------------------------------------------- | 15 // ConfirmInfoBarDelegate ----------------------------------------------------- |
16 | 16 |
17 // static | 17 // static |
18 InfoBar* ConfirmInfoBarDelegate::CreateInfoBar(InfoBarService* owner) { | 18 scoped_ptr<InfoBar> ConfirmInfoBarDelegate::CreateInfoBar( |
19 return new ConfirmInfoBar(owner, this); | 19 scoped_ptr<ConfirmInfoBarDelegate> delegate) { |
20 return scoped_ptr<InfoBar>(new ConfirmInfoBar(delegate.Pass())); | |
20 } | 21 } |
21 | 22 |
22 | 23 |
23 // ConfirmInfoBar ------------------------------------------------------------- | 24 // ConfirmInfoBar ------------------------------------------------------------- |
24 | 25 |
25 ConfirmInfoBar::ConfirmInfoBar(InfoBarService* owner, InfoBarDelegate* delegate) | 26 ConfirmInfoBar::ConfirmInfoBar(scoped_ptr<ConfirmInfoBarDelegate> delegate) |
26 : InfoBarAndroid(owner, delegate), | 27 : InfoBarAndroid(delegate.PassAs<InfoBarDelegate>()), |
27 delegate_(delegate->AsConfirmInfoBarDelegate()), | |
28 java_confirm_delegate_() { | 28 java_confirm_delegate_() { |
29 } | 29 } |
30 | 30 |
31 ConfirmInfoBar::~ConfirmInfoBar() { | 31 ConfirmInfoBar::~ConfirmInfoBar() { |
32 } | 32 } |
33 | 33 |
34 base::android::ScopedJavaLocalRef<jobject> ConfirmInfoBar::CreateRenderInfoBar( | 34 base::android::ScopedJavaLocalRef<jobject> ConfirmInfoBar::CreateRenderInfoBar( |
35 JNIEnv* env) { | 35 JNIEnv* env) { |
36 java_confirm_delegate_.Reset(Java_ConfirmInfoBarDelegate_create(env)); | 36 java_confirm_delegate_.Reset(Java_ConfirmInfoBarDelegate_create(env)); |
37 base::android::ScopedJavaLocalRef<jstring> ok_button_text = | 37 base::android::ScopedJavaLocalRef<jstring> ok_button_text = |
38 base::android::ConvertUTF16ToJavaString( | 38 base::android::ConvertUTF16ToJavaString( |
39 env, GetTextFor(ConfirmInfoBarDelegate::BUTTON_OK)); | 39 env, GetTextFor(ConfirmInfoBarDelegate::BUTTON_OK)); |
40 base::android::ScopedJavaLocalRef<jstring> cancel_button_text = | 40 base::android::ScopedJavaLocalRef<jstring> cancel_button_text = |
41 base::android::ConvertUTF16ToJavaString( | 41 base::android::ConvertUTF16ToJavaString( |
42 env, GetTextFor(ConfirmInfoBarDelegate::BUTTON_CANCEL)); | 42 env, GetTextFor(ConfirmInfoBarDelegate::BUTTON_CANCEL)); |
43 base::android::ScopedJavaLocalRef<jstring> message_text = | 43 base::android::ScopedJavaLocalRef<jstring> message_text = |
44 base::android::ConvertUTF16ToJavaString( | 44 base::android::ConvertUTF16ToJavaString( |
45 env, delegate_->GetMessageText()); | 45 env, GetDelegate()->GetMessageText()); |
46 | 46 |
47 return Java_ConfirmInfoBarDelegate_showConfirmInfoBar( | 47 return Java_ConfirmInfoBarDelegate_showConfirmInfoBar( |
48 env, java_confirm_delegate_.obj(), reinterpret_cast<jint>(this), | 48 env, java_confirm_delegate_.obj(), reinterpret_cast<jint>(this), |
49 GetEnumeratedIconId(), message_text.obj(), ok_button_text.obj(), | 49 GetEnumeratedIconId(), message_text.obj(), ok_button_text.obj(), |
50 cancel_button_text.obj()); | 50 cancel_button_text.obj()); |
51 } | 51 } |
52 | 52 |
53 void ConfirmInfoBar::ProcessButton(int action, | 53 void ConfirmInfoBar::ProcessButton(int action, |
54 const std::string& action_value) { | 54 const std::string& action_value) { |
55 // TODO(pkasting): The subsequent check isn't necessary if Android infobar | |
56 // buttons become unclickable once the infobar is unowned. | |
Miguel Garcia
2013/10/07 15:19:00
just to make sure I understand correctly. Are you
Peter Kasting
2013/10/07 20:19:24
I'm not actually suggesting you do anything in par
| |
57 if (!owner()) | |
58 return; // We're closing; don't call anything, it might access the owner. | |
59 | |
55 DCHECK((action == InfoBarAndroid::ACTION_OK) || | 60 DCHECK((action == InfoBarAndroid::ACTION_OK) || |
56 (action == InfoBarAndroid::ACTION_CANCEL)); | 61 (action == InfoBarAndroid::ACTION_CANCEL)); |
62 ConfirmInfoBarDelegate* delegate = GetDelegate(); | |
57 if ((action == InfoBarAndroid::ACTION_OK) ? | 63 if ((action == InfoBarAndroid::ACTION_OK) ? |
58 delegate_->Accept() : delegate_->Cancel()) | 64 delegate->Accept() : delegate->Cancel()) |
59 CloseInfoBar(); | 65 CloseInfoBar(); |
60 } | 66 } |
61 | 67 |
68 ConfirmInfoBarDelegate* ConfirmInfoBar::GetDelegate() { | |
69 return delegate()->AsConfirmInfoBarDelegate(); | |
70 } | |
71 | |
62 string16 ConfirmInfoBar::GetTextFor( | 72 string16 ConfirmInfoBar::GetTextFor( |
63 ConfirmInfoBarDelegate::InfoBarButton button) { | 73 ConfirmInfoBarDelegate::InfoBarButton button) { |
64 return (delegate_->GetButtons() & button) ? | 74 ConfirmInfoBarDelegate* delegate = GetDelegate(); |
65 delegate_->GetButtonLabel(button) : string16(); | 75 return (delegate->GetButtons() & button) ? |
76 delegate->GetButtonLabel(button) : string16(); | |
66 } | 77 } |
67 | 78 |
68 | 79 |
69 // Native JNI methods --------------------------------------------------------- | 80 // Native JNI methods --------------------------------------------------------- |
70 | 81 |
71 bool RegisterConfirmInfoBarDelegate(JNIEnv* env) { | 82 bool RegisterConfirmInfoBarDelegate(JNIEnv* env) { |
72 return RegisterNativesImpl(env); | 83 return RegisterNativesImpl(env); |
73 } | 84 } |
OLD | NEW |