| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_compact_infobar.h" | 5 #include "chrome/browser/ui/android/infobars/translate_compact_infobar.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 return; // We're closing; don't call anything, it might access the owner. | 64 return; // We're closing; don't call anything, it might access the owner. |
| 65 | 65 |
| 66 translate::TranslateInfoBarDelegate* delegate = GetDelegate(); | 66 translate::TranslateInfoBarDelegate* delegate = GetDelegate(); |
| 67 if (action == InfoBarAndroid::ACTION_TRANSLATE) { | 67 if (action == InfoBarAndroid::ACTION_TRANSLATE) { |
| 68 action_flags_ |= FLAG_TRANSLATE; | 68 action_flags_ |= FLAG_TRANSLATE; |
| 69 delegate->Translate(); | 69 delegate->Translate(); |
| 70 if (!delegate->ShouldAlwaysTranslate() && ShouldAutoAlwaysTranslate()) { | 70 if (!delegate->ShouldAlwaysTranslate() && ShouldAutoAlwaysTranslate()) { |
| 71 JNIEnv* env = base::android::AttachCurrentThread(); | 71 JNIEnv* env = base::android::AttachCurrentThread(); |
| 72 Java_TranslateCompactInfoBar_setAutoAlwaysTranslate(env, | 72 Java_TranslateCompactInfoBar_setAutoAlwaysTranslate(env, |
| 73 GetJavaInfoBar()); | 73 GetJavaInfoBar()); |
| 74 |
| 75 // Auto-always is triggered by the line above. Need to increment the |
| 76 // auto-always counter. |
| 77 delegate->IncrementTranslationAutoAlwaysCount(); |
| 78 // Reset translateAcceptedCount so that auto-always could be triggered |
| 79 // again. |
| 80 delegate->ResetTranslationAcceptedCount(); |
| 74 } | 81 } |
| 75 } else if (action == InfoBarAndroid::ACTION_TRANSLATE_SHOW_ORIGINAL) { | 82 } else if (action == InfoBarAndroid::ACTION_TRANSLATE_SHOW_ORIGINAL) { |
| 76 action_flags_ |= FLAG_REVERT; | 83 action_flags_ |= FLAG_REVERT; |
| 77 delegate->RevertWithoutClosingInfobar(); | 84 delegate->RevertWithoutClosingInfobar(); |
| 78 } else { | 85 } else { |
| 79 DCHECK_EQ(InfoBarAndroid::ACTION_NONE, action); | 86 DCHECK_EQ(InfoBarAndroid::ACTION_NONE, action); |
| 80 } | 87 } |
| 81 } | 88 } |
| 82 | 89 |
| 83 void TranslateCompactInfoBar::SetJavaInfoBar( | 90 void TranslateCompactInfoBar::SetJavaInfoBar( |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 action_flags_ |= FLAG_NEVER_SITE; | 137 action_flags_ |= FLAG_NEVER_SITE; |
| 131 delegate->ToggleSiteBlacklist(); | 138 delegate->ToggleSiteBlacklist(); |
| 132 } | 139 } |
| 133 } else { | 140 } else { |
| 134 DCHECK(false); | 141 DCHECK(false); |
| 135 } | 142 } |
| 136 } | 143 } |
| 137 | 144 |
| 138 bool TranslateCompactInfoBar::ShouldAutoAlwaysTranslate() { | 145 bool TranslateCompactInfoBar::ShouldAutoAlwaysTranslate() { |
| 139 translate::TranslateInfoBarDelegate* delegate = GetDelegate(); | 146 translate::TranslateInfoBarDelegate* delegate = GetDelegate(); |
| 140 return (delegate->GetTranslationAcceptedCount() == kAcceptCountThreshold); | 147 return (delegate->GetTranslationAcceptedCount() == kAcceptCountThreshold && |
| 148 delegate->GetTranslationAutoAlwaysCount() < kMaxNumberOfAutoAlways); |
| 141 } | 149 } |
| 142 | 150 |
| 143 jboolean TranslateCompactInfoBar::ShouldAutoNeverTranslate( | 151 jboolean TranslateCompactInfoBar::ShouldAutoNeverTranslate( |
| 144 JNIEnv* env, | 152 JNIEnv* env, |
| 145 const base::android::JavaParamRef<jobject>& obj, | 153 const base::android::JavaParamRef<jobject>& obj, |
| 146 jboolean menu_expanded) { | 154 jboolean menu_expanded) { |
| 147 // Flip menu expanded bit. | 155 // Flip menu expanded bit. |
| 148 if (menu_expanded) | 156 if (menu_expanded) |
| 149 action_flags_ |= FLAG_EXPAND_MENU; | 157 action_flags_ |= FLAG_EXPAND_MENU; |
| 150 | 158 |
| 151 if (!IsDeclinedByUser()) | 159 if (!IsDeclinedByUser()) |
| 152 return false; | 160 return false; |
| 153 | 161 |
| 154 translate::TranslateInfoBarDelegate* delegate = GetDelegate(); | 162 translate::TranslateInfoBarDelegate* delegate = GetDelegate(); |
| 155 // Don't trigger if it's off the record or already blocked. | 163 // Don't trigger if it's off the record or already blocked. |
| 156 if (delegate->is_off_the_record() || | 164 if (delegate->is_off_the_record() || |
| 157 !delegate->IsTranslatableLanguageByPrefs()) | 165 !delegate->IsTranslatableLanguageByPrefs()) |
| 158 return false; | 166 return false; |
| 159 | 167 |
| 160 return (delegate->GetTranslationDeniedCount() == kDeniedCountThreshold); | 168 int auto_never_count = delegate->GetTranslationAutoNeverCount(); |
| 169 |
| 170 // At the beginning (auto_never_count == 0), deniedCount starts at 0 and is |
| 171 // off-by-one (because this checking is done before increment). However, after |
| 172 // auto-never is triggered once (auto_never_count > 0), deniedCount starts at |
| 173 // 1. So there is no off-by-one by then. |
| 174 int off_by_one = auto_never_count == 0 ? 1 : 0; |
| 175 |
| 176 bool never_translate = (delegate->GetTranslationDeniedCount() + off_by_one == |
| 177 kDeniedCountThreshold && |
| 178 auto_never_count < kMaxNumberOfAutoNever); |
| 179 if (never_translate) { |
| 180 // Auto-never will be triggered. Need to increment the auto-never counter. |
| 181 delegate->IncrementTranslationAutoNeverCount(); |
| 182 // Reset translateDeniedCount so that auto-never could be triggered again. |
| 183 delegate->ResetTranslationDeniedCount(); |
| 184 } |
| 185 return never_translate; |
| 161 } | 186 } |
| 162 | 187 |
| 163 translate::TranslateInfoBarDelegate* TranslateCompactInfoBar::GetDelegate() { | 188 translate::TranslateInfoBarDelegate* TranslateCompactInfoBar::GetDelegate() { |
| 164 return delegate()->AsTranslateInfoBarDelegate(); | 189 return delegate()->AsTranslateInfoBarDelegate(); |
| 165 } | 190 } |
| 166 | 191 |
| 167 void TranslateCompactInfoBar::OnTranslateStepChanged( | 192 void TranslateCompactInfoBar::OnTranslateStepChanged( |
| 168 translate::TranslateStep step, | 193 translate::TranslateStep step, |
| 169 translate::TranslateErrors::Type error_type) { | 194 translate::TranslateErrors::Type error_type) { |
| 170 if (!owner()) | 195 if (!owner()) |
| (...skipping 11 matching lines...) Expand all Loading... |
| 182 // Whether there is any affirmative action bit. | 207 // Whether there is any affirmative action bit. |
| 183 return action_flags_ == FLAG_NONE; | 208 return action_flags_ == FLAG_NONE; |
| 184 } | 209 } |
| 185 | 210 |
| 186 // Native JNI methods --------------------------------------------------------- | 211 // Native JNI methods --------------------------------------------------------- |
| 187 | 212 |
| 188 // static | 213 // static |
| 189 bool RegisterTranslateCompactInfoBar(JNIEnv* env) { | 214 bool RegisterTranslateCompactInfoBar(JNIEnv* env) { |
| 190 return RegisterNativesImpl(env); | 215 return RegisterNativesImpl(env); |
| 191 } | 216 } |
| OLD | NEW |