Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / android / infobars / translate_infobar.cc
blobeaa7af75ad469a9e4bd0f1c38197d508b65600aa
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/TranslateInfoBar_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()) {
30 TranslateInfoBar::~TranslateInfoBar() {
33 ScopedJavaLocalRef<jobject> TranslateInfoBar::CreateRenderInfoBar(JNIEnv* env) {
34 translate::TranslateInfoBarDelegate* delegate = GetDelegate();
35 std::vector<base::string16> languages;
36 languages.reserve(delegate->num_languages());
37 for (size_t i = 0; i < delegate->num_languages(); ++i)
38 languages.push_back(delegate->language_name_at(i));
40 base::android::ScopedJavaLocalRef<jobjectArray> java_languages =
41 base::android::ToJavaArrayOfStrings(env, languages);
42 return Java_TranslateInfoBar_show(
43 env, delegate->translate_step(), delegate->original_language_index(),
44 delegate->target_language_index(), delegate->ShouldAlwaysTranslate(),
45 ShouldDisplayNeverTranslateInfoBarOnCancel(),
46 delegate->triggered_from_menu(), java_languages.obj());
49 void TranslateInfoBar::ProcessButton(int action,
50 const std::string& action_value) {
51 if (!owner())
52 return; // We're closing; don't call anything, it might access the owner.
54 translate::TranslateInfoBarDelegate* delegate = GetDelegate();
55 if (action == InfoBarAndroid::ACTION_TRANSLATE) {
56 delegate->Translate();
57 return;
60 if (action == InfoBarAndroid::ACTION_CANCEL)
61 delegate->TranslationDeclined();
62 else if (action == InfoBarAndroid::ACTION_TRANSLATE_SHOW_ORIGINAL)
63 delegate->RevertTranslation();
64 else
65 DCHECK_EQ(InfoBarAndroid::ACTION_NONE, action);
67 RemoveSelf();
70 void TranslateInfoBar::PassJavaInfoBar(InfoBarAndroid* source) {
71 translate::TranslateInfoBarDelegate* delegate = GetDelegate();
72 DCHECK_NE(translate::TRANSLATE_STEP_BEFORE_TRANSLATE,
73 delegate->translate_step());
75 // Ask the former bar to transfer ownership to us.
76 DCHECK(source != NULL);
77 static_cast<TranslateInfoBar*>(source)->TransferOwnership(
78 this, delegate->translate_step());
81 void TranslateInfoBar::SetJavaInfoBar(
82 const base::android::JavaRef<jobject>& java_info_bar) {
83 InfoBarAndroid::SetJavaInfoBar(java_info_bar);
84 JNIEnv* env = base::android::AttachCurrentThread();
85 Java_TranslateInfoBar_setNativePtr(env, java_info_bar.obj(),
86 reinterpret_cast<intptr_t>(this));
89 void TranslateInfoBar::ApplyTranslateOptions(JNIEnv* env,
90 jobject obj,
91 int source_language_index,
92 int target_language_index,
93 bool always_translate,
94 bool never_translate_language,
95 bool never_translate_site) {
96 translate::TranslateInfoBarDelegate* delegate = GetDelegate();
97 delegate->UpdateOriginalLanguageIndex(source_language_index);
98 delegate->UpdateTargetLanguageIndex(target_language_index);
100 if (delegate->ShouldAlwaysTranslate() != always_translate)
101 delegate->ToggleAlwaysTranslate();
103 if (never_translate_language && delegate->IsTranslatableLanguageByPrefs())
104 delegate->ToggleTranslatableLanguageByPrefs();
106 if (never_translate_site && !delegate->IsSiteBlacklisted())
107 delegate->ToggleSiteBlacklist();
110 void TranslateInfoBar::TransferOwnership(TranslateInfoBar* destination,
111 translate::TranslateStep new_type) {
112 JNIEnv* env = base::android::AttachCurrentThread();
113 Java_TranslateInfoBar_changeTranslateInfoBarType(env, GetJavaInfoBar(),
114 new_type);
115 ReassignJavaInfoBar(destination);
118 bool TranslateInfoBar::ShouldDisplayNeverTranslateInfoBarOnCancel() {
119 translate::TranslateInfoBarDelegate* delegate = GetDelegate();
120 return (delegate->translate_step() ==
121 translate::TRANSLATE_STEP_BEFORE_TRANSLATE) &&
122 delegate->ShouldShowNeverTranslateShortcut();
125 translate::TranslateInfoBarDelegate* TranslateInfoBar::GetDelegate() {
126 return delegate()->AsTranslateInfoBarDelegate();
130 // Native JNI methods ---------------------------------------------------------
132 bool RegisterTranslateInfoBarDelegate(JNIEnv* env) {
133 return RegisterNativesImpl(env);