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.
4
5#include "chrome/browser/ui/android/infobars/translate_infobar.h"
6
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"
14
15
16// TranslateInfoBarDelegate ---------------------------------------------------
17
18// static
19scoped_ptr<InfoBar> TranslateInfoBarDelegate::CreateInfoBar(
20    scoped_ptr<TranslateInfoBarDelegate> delegate) {
21  return scoped_ptr<InfoBar>(new TranslateInfoBar(delegate.Pass()));
22}
23
24
25// TranslateInfoBar -----------------------------------------------------------
26
27TranslateInfoBar::TranslateInfoBar(
28    scoped_ptr<TranslateInfoBarDelegate> delegate)
29    : InfoBarAndroid(delegate.PassAs<InfoBarDelegate>()),
30      java_translate_delegate_() {
31}
32
33TranslateInfoBar::~TranslateInfoBar() {
34}
35
36ScopedJavaLocalRef<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));
43
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());
51}
52
53void TranslateInfoBar::ProcessButton(int action,
54                                     const std::string& action_value) {
55  if (!owner())
56    return;  // We're closing; don't call anything, it might access the owner.
57
58  TranslateInfoBarDelegate* delegate = GetDelegate();
59  if (action == InfoBarAndroid::ACTION_TRANSLATE) {
60    delegate->Translate();
61    return;
62  }
63
64  if (action == InfoBarAndroid::ACTION_CANCEL)
65    delegate->TranslationDeclined();
66  else if (action == InfoBarAndroid::ACTION_TRANSLATE_SHOW_ORIGINAL)
67    delegate->RevertTranslation();
68  else
69    DCHECK_EQ(InfoBarAndroid::ACTION_NONE, action);
70
71  RemoveSelf();
72}
73
74void TranslateInfoBar::PassJavaInfoBar(InfoBarAndroid* source) {
75  TranslateInfoBarDelegate* delegate = GetDelegate();
76  DCHECK_NE(TranslateInfoBarDelegate::BEFORE_TRANSLATE,
77            delegate->infobar_type());
78
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());
83}
84
85void TranslateInfoBar::ApplyTranslateOptions(JNIEnv* env,
86                                             jobject obj,
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);
95
96  if (delegate->ShouldAlwaysTranslate() != always_translate)
97    delegate->ToggleAlwaysTranslate();
98
99  if (never_translate_language && delegate->IsTranslatableLanguageByPrefs())
100    delegate->ToggleTranslatableLanguageByPrefs();
101
102  if (never_translate_site && !delegate->IsSiteBlacklisted())
103    delegate->ToggleSiteBlacklist();
104}
105
106void 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());
115  }
116}
117
118void TranslateInfoBar::SetJavaDelegate(jobject delegate) {
119  JNIEnv* env = base::android::AttachCurrentThread();
120  java_translate_delegate_.Reset(env, delegate);
121}
122
123bool TranslateInfoBar::ShouldDisplayNeverTranslateInfoBarOnCancel() {
124  TranslateInfoBarDelegate* delegate = GetDelegate();
125  return
126      (delegate->infobar_type() ==
127          TranslateInfoBarDelegate::BEFORE_TRANSLATE) &&
128      delegate->ShouldShowNeverTranslateShortcut();
129}
130
131TranslateInfoBarDelegate* TranslateInfoBar::GetDelegate() {
132  return delegate()->AsTranslateInfoBarDelegate();
133}
134
135
136// Native JNI methods ---------------------------------------------------------
137
138bool RegisterTranslateInfoBarDelegate(JNIEnv* env) {
139  return RegisterNativesImpl(env);
140}
141