1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/android/infobars/translate_infobar.h"
#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_helper.h"
#include "chrome/browser/translate/translate_infobar_delegate.h"
#include "grit/generated_resources.h"
#include "jni/TranslateInfoBarDelegate_jni.h"
#include "ui/base/l10n/l10n_util.h"
// InfoBarDelegateAndroid -----------------------------------------------------
// static
InfoBar* TranslateInfoBarDelegate::CreateInfoBar(InfoBarService* owner) {
return new TranslateInfoBar(owner, this);
}
// TranslateInfoBar -----------------------------------------------------------
TranslateInfoBar::TranslateInfoBar(InfoBarService* owner,
TranslateInfoBarDelegate* delegate)
: InfoBarAndroid(owner, delegate),
delegate_(delegate),
java_translate_delegate_() {
}
TranslateInfoBar::~TranslateInfoBar() {
}
ScopedJavaLocalRef<jobject> TranslateInfoBar::CreateRenderInfoBar(JNIEnv* env) {
java_translate_delegate_.Reset(Java_TranslateInfoBarDelegate_create(env));
std::vector<string16> languages(delegate_->num_languages());
for (size_t i = 0; i < delegate_->num_languages(); ++i)
languages[i] = delegate_->language_name_at(i);
base::android::ScopedJavaLocalRef<jobjectArray> java_languages =
base::android::ToJavaArrayOfStrings(env, languages);
return Java_TranslateInfoBarDelegate_showTranslateInfoBar(
env,
java_translate_delegate_.obj(),
reinterpret_cast<jint>(this),
delegate_->infobar_type(),
delegate_->original_language_index(),
delegate_->target_language_index(),
delegate_->ShouldAlwaysTranslate(),
ShouldDisplayNeverTranslateInfoBarOnCancel(),
java_languages.obj());
}
void TranslateInfoBar::ProcessButton(
int action, const std::string& action_value) {
if (!owner())
return; // We're closing; don't call anything, it might access the owner.
if (action == InfoBarAndroid::ACTION_TRANSLATE) {
delegate_->Translate();
return;
}
if (action == InfoBarAndroid::ACTION_CANCEL)
delegate_->TranslationDeclined();
else if (action == InfoBarAndroid::ACTION_TRANSLATE_SHOW_ORIGINAL)
delegate_->RevertTranslation();
else
DCHECK_EQ(InfoBarAndroid::ACTION_NONE, action);
RemoveSelf();
}
void TranslateInfoBar::PassJavaInfoBar(InfoBarAndroid* source) {
DCHECK_NE(
delegate_->infobar_type(), TranslateInfoBarDelegate::BEFORE_TRANSLATE);
// Ask the former bar to transfer ownership to us.
DCHECK(source != NULL);
static_cast<TranslateInfoBar*>(source)->TransferOwnership(
this, delegate_->infobar_type());
}
void TranslateInfoBar::ApplyTranslateOptions(JNIEnv* env,
jobject obj,
int source_language_index,
int target_language_index,
bool always_translate,
bool never_translate_language,
bool never_translate_site) {
delegate_->set_original_language_index(source_language_index);
delegate_->set_target_language_index(target_language_index);
if (delegate_->ShouldAlwaysTranslate() != always_translate)
delegate_->ToggleAlwaysTranslate();
if (never_translate_language && delegate_->IsTranslatableLanguageByPrefs())
delegate_->ToggleTranslatableLanguageByPrefs();
if (never_translate_site && !delegate_->IsSiteBlacklisted())
delegate_->ToggleSiteBlacklist();
}
void TranslateInfoBar::TransferOwnership(
TranslateInfoBar* destination,
TranslateInfoBarDelegate::Type new_type) {
JNIEnv* env = base::android::AttachCurrentThread();
if (Java_TranslateInfoBarDelegate_changeTranslateInfoBarTypeAndPointer(
env, java_translate_delegate_.obj(),
reinterpret_cast<jint>(destination), new_type)) {
ReassignJavaInfoBar(destination);
destination->SetJavaDelegate(java_translate_delegate_.Release());
}
}
void TranslateInfoBar::SetJavaDelegate(jobject delegate) {
JNIEnv* env = base::android::AttachCurrentThread();
java_translate_delegate_.Reset(env, delegate);
}
bool TranslateInfoBar::ShouldDisplayNeverTranslateInfoBarOnCancel() {
return
(delegate_->infobar_type() ==
TranslateInfoBarDelegate::BEFORE_TRANSLATE) &&
(delegate_->ShouldShowNeverTranslateShortcut());
}
// Native JNI methods ---------------------------------------------------------
bool RegisterTranslateInfoBarDelegate(JNIEnv* env) {
return RegisterNativesImpl(env);
}
|