Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1111)

Side by Side Diff: chrome/browser/ui/android/infobars/translate_infobar.cc

Issue 2762393003: Creates translate_compact_infobar class, which holds the logic for the new translate UI (Closed)
Patch Set: a Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/android/infobars/translate_infobar.h" 5 #include "chrome/browser/ui/android/infobars/translate_infobar.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/android/jni_android.h" 11 #include "base/android/jni_android.h"
12 #include "base/android/jni_array.h" 12 #include "base/android/jni_array.h"
13 #include "base/android/jni_string.h" 13 #include "base/android/jni_string.h"
14 #include "base/android/jni_weak_ref.h" 14 #include "base/android/jni_weak_ref.h"
15 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
16 #include "chrome/browser/translate/chrome_translate_client.h" 16 #include "chrome/browser/translate/chrome_translate_client.h"
17 #include "chrome/browser/ui/android/infobars/translate_compact_infobar.h"
18 #include "chrome/browser/ui/android/infobars/translate_utils.h"
17 #include "components/translate/core/browser/translate_infobar_delegate.h" 19 #include "components/translate/core/browser/translate_infobar_delegate.h"
18 #include "jni/TranslateCompactInfoBar_jni.h"
19 #include "jni/TranslateInfoBar_jni.h" 20 #include "jni/TranslateInfoBar_jni.h"
20 21
21 using base::android::JavaParamRef; 22 using base::android::JavaParamRef;
22 using base::android::ScopedJavaLocalRef; 23 using base::android::ScopedJavaLocalRef;
23 24
24 // ChromeTranslateClient 25 // ChromeTranslateClient
25 // ---------------------------------------------------------- 26 // ----------------------------------------------------------
26 27
27 std::unique_ptr<infobars::InfoBar> ChromeTranslateClient::CreateInfoBar( 28 std::unique_ptr<infobars::InfoBar> ChromeTranslateClient::CreateInfoBar(
28 std::unique_ptr<translate::TranslateInfoBarDelegate> delegate) const { 29 std::unique_ptr<translate::TranslateInfoBarDelegate> delegate) const {
29 return base::MakeUnique<TranslateInfoBar>(std::move(delegate)); 30 if (translate::TranslateInfoBarDelegate::IsCompactUIEnabled())
31 return base::MakeUnique<TranslateCompactInfoBar>(std::move(delegate));
32 else
33 return base::MakeUnique<TranslateInfoBar>(std::move(delegate));
30 } 34 }
31 35
32 36
33 // TranslateInfoBar ----------------------------------------------------------- 37 // TranslateInfoBar -----------------------------------------------------------
34 38
35 TranslateInfoBar::TranslateInfoBar( 39 TranslateInfoBar::TranslateInfoBar(
36 std::unique_ptr<translate::TranslateInfoBarDelegate> delegate) 40 std::unique_ptr<translate::TranslateInfoBarDelegate> delegate)
37 : InfoBarAndroid(std::move(delegate)) {} 41 : InfoBarAndroid(std::move(delegate)) {}
38 42
39 TranslateInfoBar::~TranslateInfoBar() { 43 TranslateInfoBar::~TranslateInfoBar() {
40 } 44 }
41 45
42 ScopedJavaLocalRef<jobject> TranslateInfoBar::CreateRenderInfoBar(JNIEnv* env) { 46 ScopedJavaLocalRef<jobject> TranslateInfoBar::CreateRenderInfoBar(JNIEnv* env) {
43 translate::TranslateInfoBarDelegate* delegate = GetDelegate(); 47 translate::TranslateInfoBarDelegate* delegate = GetDelegate();
44 std::vector<base::string16> languages;
45 std::vector<std::string> codes;
46 languages.reserve(delegate->num_languages());
47 codes.reserve(delegate->num_languages());
48 for (size_t i = 0; i < delegate->num_languages(); ++i) {
49 languages.push_back(delegate->language_name_at(i));
50 codes.push_back(delegate->language_code_at(i));
51 }
52 DCHECK(codes.size() == languages.size());
53 base::android::ScopedJavaLocalRef<jobjectArray> java_languages = 48 base::android::ScopedJavaLocalRef<jobjectArray> java_languages =
54 base::android::ToJavaArrayOfStrings(env, languages); 49 TranslateUtils::GetJavaLanguages(env, delegate);
55 base::android::ScopedJavaLocalRef<jobjectArray> java_codes = 50 base::android::ScopedJavaLocalRef<jobjectArray> java_codes =
56 base::android::ToJavaArrayOfStrings(env, codes); 51 TranslateUtils::GetJavaLanguageCodes(env, delegate);
57 52
58 ScopedJavaLocalRef<jstring> source_language_code = 53 ScopedJavaLocalRef<jstring> source_language_code =
59 base::android::ConvertUTF8ToJavaString( 54 TranslateUtils::GetLanguageCode(env, delegate->original_language_code());
gone 2017/03/24 20:34:30 Why does this need to be pulled out? It's a singl
ramyasharma 2017/03/27 03:36:59 Done.
60 env, delegate->original_language_code());
61 55
62 ScopedJavaLocalRef<jstring> target_language_code = 56 ScopedJavaLocalRef<jstring> target_language_code =
63 base::android::ConvertUTF8ToJavaString(env, 57 TranslateUtils::GetLanguageCode(env, delegate->target_language_code());
64 delegate->target_language_code());
65 58
66 if (translate::TranslateInfoBarDelegate::IsCompactUIEnabled()) { 59 return Java_TranslateInfoBar_show(
67 return Java_TranslateCompactInfoBar_create(env); 60 env, delegate->translate_step(), source_language_code,
68 } else { 61 target_language_code, delegate->ShouldAlwaysTranslate(),
69 return Java_TranslateInfoBar_show( 62 ShouldDisplayNeverTranslateInfoBarOnCancel(),
70 env, delegate->translate_step(), source_language_code, 63 delegate->triggered_from_menu(), java_languages, java_codes);
71 target_language_code, delegate->ShouldAlwaysTranslate(),
72 ShouldDisplayNeverTranslateInfoBarOnCancel(),
73 delegate->triggered_from_menu(), java_languages, java_codes);
74 }
75 } 64 }
76 65
77 void TranslateInfoBar::ProcessButton(int action) { 66 void TranslateInfoBar::ProcessButton(int action) {
78 if (!owner()) 67 if (!owner())
79 return; // We're closing; don't call anything, it might access the owner. 68 return; // We're closing; don't call anything, it might access the owner.
80 69
81 translate::TranslateInfoBarDelegate* delegate = GetDelegate(); 70 translate::TranslateInfoBarDelegate* delegate = GetDelegate();
82 if (action == InfoBarAndroid::ACTION_TRANSLATE) { 71 if (action == InfoBarAndroid::ACTION_TRANSLATE) {
83 delegate->Translate(); 72 delegate->Translate();
84 return; 73 return;
(...skipping 17 matching lines...) Expand all
102 // Ask the former bar to transfer ownership to us. 91 // Ask the former bar to transfer ownership to us.
103 DCHECK(source != NULL); 92 DCHECK(source != NULL);
104 static_cast<TranslateInfoBar*>(source)->TransferOwnership( 93 static_cast<TranslateInfoBar*>(source)->TransferOwnership(
105 this, delegate->translate_step()); 94 this, delegate->translate_step());
106 } 95 }
107 96
108 void TranslateInfoBar::SetJavaInfoBar( 97 void TranslateInfoBar::SetJavaInfoBar(
109 const base::android::JavaRef<jobject>& java_info_bar) { 98 const base::android::JavaRef<jobject>& java_info_bar) {
110 InfoBarAndroid::SetJavaInfoBar(java_info_bar); 99 InfoBarAndroid::SetJavaInfoBar(java_info_bar);
111 JNIEnv* env = base::android::AttachCurrentThread(); 100 JNIEnv* env = base::android::AttachCurrentThread();
112 if (translate::TranslateInfoBarDelegate::IsCompactUIEnabled()) {
113 Java_TranslateCompactInfoBar_setNativePtr(env, java_info_bar,
114 reinterpret_cast<intptr_t>(this));
115 } else {
116 Java_TranslateInfoBar_setNativePtr(env, java_info_bar, 101 Java_TranslateInfoBar_setNativePtr(env, java_info_bar,
117 reinterpret_cast<intptr_t>(this)); 102 reinterpret_cast<intptr_t>(this));
118 }
119 } 103 }
120 104
121 void TranslateInfoBar::ApplyTranslateOptions( 105 void TranslateInfoBar::ApplyTranslateOptions(
122 JNIEnv* env, 106 JNIEnv* env,
123 const JavaParamRef<jobject>& obj, 107 const JavaParamRef<jobject>& obj,
124 const JavaParamRef<jstring>& source_language_code, 108 const JavaParamRef<jstring>& source_language_code,
125 const JavaParamRef<jstring>& target_language_code, 109 const JavaParamRef<jstring>& target_language_code,
126 bool always_translate, 110 bool always_translate,
127 bool never_translate_language, 111 bool never_translate_language,
128 bool never_translate_site) { 112 bool never_translate_site) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 translate::TranslateInfoBarDelegate* TranslateInfoBar::GetDelegate() { 147 translate::TranslateInfoBarDelegate* TranslateInfoBar::GetDelegate() {
164 return delegate()->AsTranslateInfoBarDelegate(); 148 return delegate()->AsTranslateInfoBarDelegate();
165 } 149 }
166 150
167 151
168 // Native JNI methods --------------------------------------------------------- 152 // Native JNI methods ---------------------------------------------------------
169 153
170 bool RegisterTranslateInfoBarDelegate(JNIEnv* env) { 154 bool RegisterTranslateInfoBarDelegate(JNIEnv* env) {
171 return RegisterNativesImpl(env); 155 return RegisterNativesImpl(env);
172 } 156 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698