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_weak_ref.h"
10 #include "chrome/browser/translate/chrome_translate_client.h"
11 #include "components/translate/core/browser/translate_infobar_delegate.h"
12 #include "jni/TranslateInfoBarDelegate_jni.h"
14 // ChromeTranslateClient
15 // ----------------------------------------------------------
17 scoped_ptr
<infobars::InfoBar
> ChromeTranslateClient::CreateInfoBar(
18 scoped_ptr
<translate::TranslateInfoBarDelegate
> delegate
) const {
19 return make_scoped_ptr(new TranslateInfoBar(delegate
.Pass()));
23 // TranslateInfoBar -----------------------------------------------------------
25 TranslateInfoBar::TranslateInfoBar(
26 scoped_ptr
<translate::TranslateInfoBarDelegate
> delegate
)
27 : InfoBarAndroid(delegate
.Pass()), java_translate_delegate_() {
30 TranslateInfoBar::~TranslateInfoBar() {
33 ScopedJavaLocalRef
<jobject
> TranslateInfoBar::CreateRenderInfoBar(JNIEnv
* env
) {
34 java_translate_delegate_
.Reset(Java_TranslateInfoBarDelegate_create(env
));
35 translate::TranslateInfoBarDelegate
* delegate
= GetDelegate();
36 std::vector
<base::string16
> languages
;
37 languages
.reserve(delegate
->num_languages());
38 for (size_t i
= 0; i
< delegate
->num_languages(); ++i
)
39 languages
.push_back(delegate
->language_name_at(i
));
41 base::android::ScopedJavaLocalRef
<jobjectArray
> java_languages
=
42 base::android::ToJavaArrayOfStrings(env
, languages
);
43 return Java_TranslateInfoBarDelegate_showTranslateInfoBar(
44 env
, java_translate_delegate_
.obj(), reinterpret_cast<intptr_t>(this),
45 delegate
->translate_step(), delegate
->original_language_index(),
46 delegate
->target_language_index(), delegate
->ShouldAlwaysTranslate(),
47 ShouldDisplayNeverTranslateInfoBarOnCancel(),
48 delegate
->triggered_from_menu(), java_languages
.obj());
51 void TranslateInfoBar::ProcessButton(int action
,
52 const std::string
& action_value
) {
54 return; // We're closing; don't call anything, it might access the owner.
56 translate::TranslateInfoBarDelegate
* delegate
= GetDelegate();
57 if (action
== InfoBarAndroid::ACTION_TRANSLATE
) {
58 delegate
->Translate();
62 if (action
== InfoBarAndroid::ACTION_CANCEL
)
63 delegate
->TranslationDeclined();
64 else if (action
== InfoBarAndroid::ACTION_TRANSLATE_SHOW_ORIGINAL
)
65 delegate
->RevertTranslation();
67 DCHECK_EQ(InfoBarAndroid::ACTION_NONE
, action
);
72 void TranslateInfoBar::PassJavaInfoBar(InfoBarAndroid
* source
) {
73 translate::TranslateInfoBarDelegate
* delegate
= GetDelegate();
74 DCHECK_NE(translate::TRANSLATE_STEP_BEFORE_TRANSLATE
,
75 delegate
->translate_step());
77 // Ask the former bar to transfer ownership to us.
78 DCHECK(source
!= NULL
);
79 static_cast<TranslateInfoBar
*>(source
)->TransferOwnership(
80 this, delegate
->translate_step());
83 void TranslateInfoBar::ApplyTranslateOptions(JNIEnv
* env
,
85 int source_language_index
,
86 int target_language_index
,
87 bool always_translate
,
88 bool never_translate_language
,
89 bool never_translate_site
) {
90 translate::TranslateInfoBarDelegate
* delegate
= GetDelegate();
91 delegate
->UpdateOriginalLanguageIndex(source_language_index
);
92 delegate
->UpdateTargetLanguageIndex(target_language_index
);
94 if (delegate
->ShouldAlwaysTranslate() != always_translate
)
95 delegate
->ToggleAlwaysTranslate();
97 if (never_translate_language
&& delegate
->IsTranslatableLanguageByPrefs())
98 delegate
->ToggleTranslatableLanguageByPrefs();
100 if (never_translate_site
&& !delegate
->IsSiteBlacklisted())
101 delegate
->ToggleSiteBlacklist();
104 void TranslateInfoBar::TransferOwnership(TranslateInfoBar
* destination
,
105 translate::TranslateStep new_type
) {
106 JNIEnv
* env
= base::android::AttachCurrentThread();
107 if (Java_TranslateInfoBarDelegate_changeTranslateInfoBarTypeAndPointer(
108 env
, java_translate_delegate_
.obj(),
109 reinterpret_cast<intptr_t>(destination
), new_type
)) {
110 ReassignJavaInfoBar(destination
);
111 destination
->SetJavaDelegate(java_translate_delegate_
.Release());
115 void TranslateInfoBar::SetJavaDelegate(jobject delegate
) {
116 JNIEnv
* env
= base::android::AttachCurrentThread();
117 java_translate_delegate_
.Reset(env
, delegate
);
120 bool TranslateInfoBar::ShouldDisplayNeverTranslateInfoBarOnCancel() {
121 translate::TranslateInfoBarDelegate
* delegate
= GetDelegate();
122 return (delegate
->translate_step() ==
123 translate::TRANSLATE_STEP_BEFORE_TRANSLATE
) &&
124 delegate
->ShouldShowNeverTranslateShortcut();
127 translate::TranslateInfoBarDelegate
* TranslateInfoBar::GetDelegate() {
128 return delegate()->AsTranslateInfoBarDelegate();
132 // Native JNI methods ---------------------------------------------------------
134 bool RegisterTranslateInfoBarDelegate(JNIEnv
* env
) {
135 return RegisterNativesImpl(env
);