Chromium Code Reviews| 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 // Because this checking is done before deniedCount is incremented, | |
| 171 // deniedCount will be off by 1 when auto-never hasn't ever been triggered. | |
| 172 int off_by_one = auto_never_count == 0 ? 1 : 0; | |
|
mdjones
2017/06/09 17:43:37
Just out of curiosity, why is this only true if au
Marti Wong
2017/06/13 06:28:40
At the beginning, DeniedCount starts at 0. And the
| |
| 173 | |
| 174 bool return_value = (delegate->GetTranslationDeniedCount() + off_by_one == | |
|
mdjones
2017/06/09 17:43:36
nit: "return_value" could probably be a bit more d
Marti Wong
2017/06/13 06:28:40
Done.
| |
| 175 kDeniedCountThreshold && | |
| 176 auto_never_count < kMaxNumberOfAutoNever); | |
| 177 if (return_value) { | |
| 178 // Auto-never will be triggered. Need to increment the auto-never counter. | |
| 179 delegate->IncrementTranslationAutoNeverCount(); | |
| 180 // Reset translateDeniedCount so that auto-never could be triggered again. | |
| 181 delegate->ResetTranslationDeniedCount(); | |
| 182 } | |
| 183 return return_value; | |
| 161 } | 184 } |
| 162 | 185 |
| 163 translate::TranslateInfoBarDelegate* TranslateCompactInfoBar::GetDelegate() { | 186 translate::TranslateInfoBarDelegate* TranslateCompactInfoBar::GetDelegate() { |
| 164 return delegate()->AsTranslateInfoBarDelegate(); | 187 return delegate()->AsTranslateInfoBarDelegate(); |
| 165 } | 188 } |
| 166 | 189 |
| 167 void TranslateCompactInfoBar::OnTranslateStepChanged( | 190 void TranslateCompactInfoBar::OnTranslateStepChanged( |
| 168 translate::TranslateStep step, | 191 translate::TranslateStep step, |
| 169 translate::TranslateErrors::Type error_type) { | 192 translate::TranslateErrors::Type error_type) { |
| 170 if (!owner()) | 193 if (!owner()) |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 182 // Whether there is any affirmative action bit. | 205 // Whether there is any affirmative action bit. |
| 183 return action_flags_ == FLAG_NONE; | 206 return action_flags_ == FLAG_NONE; |
| 184 } | 207 } |
| 185 | 208 |
| 186 // Native JNI methods --------------------------------------------------------- | 209 // Native JNI methods --------------------------------------------------------- |
| 187 | 210 |
| 188 // static | 211 // static |
| 189 bool RegisterTranslateCompactInfoBar(JNIEnv* env) { | 212 bool RegisterTranslateCompactInfoBar(JNIEnv* env) { |
| 190 return RegisterNativesImpl(env); | 213 return RegisterNativesImpl(env); |
| 191 } | 214 } |
| OLD | NEW |