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/translate_infobar.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h"
9 #include "base/android/jni_helper.h"
10 #include "chrome/browser/translate/translate_infobar_delegate.h"
11 #include "grit/generated_resources.h"
12 #include "jni/TranslateInfoBarDelegate_jni.h"
13 #include "ui/base/l10n/l10n_util.h"
16 // TranslateInfoBarDelegate ---------------------------------------------------
19 scoped_ptr
<InfoBar
> TranslateInfoBarDelegate::CreateInfoBar(
20 scoped_ptr
<TranslateInfoBarDelegate
> delegate
) {
21 return scoped_ptr
<InfoBar
>(new TranslateInfoBar(delegate
.Pass()));
25 // TranslateInfoBar -----------------------------------------------------------
27 TranslateInfoBar::TranslateInfoBar(
28 scoped_ptr
<TranslateInfoBarDelegate
> delegate
)
29 : InfoBarAndroid(delegate
.PassAs
<InfoBarDelegate
>()),
30 java_translate_delegate_() {
33 TranslateInfoBar::~TranslateInfoBar() {
36 ScopedJavaLocalRef
<jobject
> TranslateInfoBar::CreateRenderInfoBar(JNIEnv
* env
) {
37 java_translate_delegate_
.Reset(Java_TranslateInfoBarDelegate_create(env
));
38 TranslateInfoBarDelegate
* delegate
= GetDelegate();
39 std::vector
<base::string16
> languages
;
40 languages
.reserve(delegate
->num_languages());
41 for (size_t i
= 0; i
< delegate
->num_languages(); ++i
)
42 languages
.push_back(delegate
->language_name_at(i
));
44 base::android::ScopedJavaLocalRef
<jobjectArray
> java_languages
=
45 base::android::ToJavaArrayOfStrings(env
, languages
);
46 return Java_TranslateInfoBarDelegate_showTranslateInfoBar(
47 env
, java_translate_delegate_
.obj(), reinterpret_cast<intptr_t>(this),
48 delegate
->infobar_type(), delegate
->original_language_index(),
49 delegate
->target_language_index(), delegate
->ShouldAlwaysTranslate(),
50 ShouldDisplayNeverTranslateInfoBarOnCancel(), java_languages
.obj());
53 void TranslateInfoBar::ProcessButton(int action
,
54 const std::string
& action_value
) {
56 return; // We're closing; don't call anything, it might access the owner.
58 TranslateInfoBarDelegate
* delegate
= GetDelegate();
59 if (action
== InfoBarAndroid::ACTION_TRANSLATE
) {
60 delegate
->Translate();
64 if (action
== InfoBarAndroid::ACTION_CANCEL
)
65 delegate
->TranslationDeclined();
66 else if (action
== InfoBarAndroid::ACTION_TRANSLATE_SHOW_ORIGINAL
)
67 delegate
->RevertTranslation();
69 DCHECK_EQ(InfoBarAndroid::ACTION_NONE
, action
);
74 void TranslateInfoBar::PassJavaInfoBar(InfoBarAndroid
* source
) {
75 TranslateInfoBarDelegate
* delegate
= GetDelegate();
76 DCHECK_NE(TranslateInfoBarDelegate::BEFORE_TRANSLATE
,
77 delegate
->infobar_type());
79 // Ask the former bar to transfer ownership to us.
80 DCHECK(source
!= NULL
);
81 static_cast<TranslateInfoBar
*>(source
)->TransferOwnership(
82 this, delegate
->infobar_type());
85 void TranslateInfoBar::ApplyTranslateOptions(JNIEnv
* env
,
87 int source_language_index
,
88 int target_language_index
,
89 bool always_translate
,
90 bool never_translate_language
,
91 bool never_translate_site
) {
92 TranslateInfoBarDelegate
* delegate
= GetDelegate();
93 delegate
->UpdateOriginalLanguageIndex(source_language_index
);
94 delegate
->UpdateTargetLanguageIndex(target_language_index
);
96 if (delegate
->ShouldAlwaysTranslate() != always_translate
)
97 delegate
->ToggleAlwaysTranslate();
99 if (never_translate_language
&& delegate
->IsTranslatableLanguageByPrefs())
100 delegate
->ToggleTranslatableLanguageByPrefs();
102 if (never_translate_site
&& !delegate
->IsSiteBlacklisted())
103 delegate
->ToggleSiteBlacklist();
106 void TranslateInfoBar::TransferOwnership(
107 TranslateInfoBar
* destination
,
108 TranslateInfoBarDelegate::Type new_type
) {
109 JNIEnv
* env
= base::android::AttachCurrentThread();
110 if (Java_TranslateInfoBarDelegate_changeTranslateInfoBarTypeAndPointer(
111 env
, java_translate_delegate_
.obj(),
112 reinterpret_cast<intptr_t>(destination
), new_type
)) {
113 ReassignJavaInfoBar(destination
);
114 destination
->SetJavaDelegate(java_translate_delegate_
.Release());
118 void TranslateInfoBar::SetJavaDelegate(jobject delegate
) {
119 JNIEnv
* env
= base::android::AttachCurrentThread();
120 java_translate_delegate_
.Reset(env
, delegate
);
123 bool TranslateInfoBar::ShouldDisplayNeverTranslateInfoBarOnCancel() {
124 TranslateInfoBarDelegate
* delegate
= GetDelegate();
126 (delegate
->infobar_type() ==
127 TranslateInfoBarDelegate::BEFORE_TRANSLATE
) &&
128 delegate
->ShouldShowNeverTranslateShortcut();
131 TranslateInfoBarDelegate
* TranslateInfoBar::GetDelegate() {
132 return delegate()->AsTranslateInfoBarDelegate();
136 // Native JNI methods ---------------------------------------------------------
138 bool RegisterTranslateInfoBarDelegate(JNIEnv
* env
) {
139 return RegisterNativesImpl(env
);