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

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/infobar/TranslateInfoBar.java

Issue 1150193004: Straighten up life cycle of native InfoBar pointers (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressed david's comments Created 5 years, 6 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 package org.chromium.chrome.browser.infobar; 5 package org.chromium.chrome.browser.infobar;
6 6
7 import android.content.Context; 7 import android.content.Context;
8 import android.text.Spannable; 8 import android.text.Spannable;
9 import android.text.SpannableString; 9 import android.text.SpannableString;
10 import android.text.SpannableStringBuilder; 10 import android.text.SpannableStringBuilder;
11 import android.text.TextUtils; 11 import android.text.TextUtils;
12 import android.text.style.ClickableSpan; 12 import android.text.style.ClickableSpan;
13 import android.view.View; 13 import android.view.View;
14 import android.widget.CheckBox; 14 import android.widget.CheckBox;
15 15
16 import org.chromium.base.CalledByNative;
16 import org.chromium.chrome.R; 17 import org.chromium.chrome.R;
17 import org.chromium.ui.base.DeviceFormFactor; 18 import org.chromium.ui.base.DeviceFormFactor;
18 19
19 /** 20 /**
20 * Java version of the translate infobar 21 * Java version of the translate infobar
21 */ 22 */
22 public class TranslateInfoBar extends InfoBar implements SubPanelListener { 23 public class TranslateInfoBar extends InfoBar implements SubPanelListener {
23 // Needs to be kept in sync with the Type enum in translate_infobar_delegate .h. 24 // Needs to be kept in sync with the Type enum in translate_infobar_delegate .h.
24 public static final int BEFORE_TRANSLATE_INFOBAR = 0; 25 public static final int BEFORE_TRANSLATE_INFOBAR = 0;
25 public static final int TRANSLATING_INFOBAR = 1; 26 public static final int TRANSLATING_INFOBAR = 1;
26 public static final int AFTER_TRANSLATE_INFOBAR = 2; 27 public static final int AFTER_TRANSLATE_INFOBAR = 2;
27 public static final int TRANSLATE_ERROR_INFOBAR = 3; 28 public static final int TRANSLATE_ERROR_INFOBAR = 3;
28 public static final int MAX_INFOBAR_INDEX = 4; 29 public static final int MAX_INFOBAR_INDEX = 4;
29 30
30 // Defines what subpanel needs to be shown, if any 31 // Defines what subpanel needs to be shown, if any
31 public static final int NO_PANEL = 0; 32 public static final int NO_PANEL = 0;
32 public static final int LANGUAGE_PANEL = 1; 33 public static final int LANGUAGE_PANEL = 1;
33 public static final int NEVER_PANEL = 2; 34 public static final int NEVER_PANEL = 2;
34 public static final int ALWAYS_PANEL = 3; 35 public static final int ALWAYS_PANEL = 3;
35 public static final int MAX_PANEL_INDEX = 4; 36 public static final int MAX_PANEL_INDEX = 4;
36 37
38 private long mNativeTranslateInfoBarPtr;
37 private int mInfoBarType; 39 private int mInfoBarType;
38 private final TranslateOptions mOptions; 40 private final TranslateOptions mOptions;
39 private int mOptionsPanelViewType; 41 private int mOptionsPanelViewType;
40 private TranslateSubPanel mSubPanel; 42 private TranslateSubPanel mSubPanel;
41 private final boolean mShouldShowNeverBar; 43 private final boolean mShouldShowNeverBar;
42 private final TranslateInfoBarDelegate mTranslateDelegate;
43 44
44 public TranslateInfoBar(long nativeInfoBarPtr, TranslateInfoBarDelegate dele gate, 45 @CalledByNative
45 int infoBarType, int sourceLanguageIndex, int targetLanguageIndex, 46 private static InfoBar show(int translateBarType, int sourceLanguageIndex,
46 boolean autoTranslatePair, boolean shouldShowNeverBar, 47 int targetLanguageIndex, boolean autoTranslatePair, boolean showNeve rInfobar,
47 boolean triggeredFromMenu, String[] languages) { 48 boolean triggeredFromMenu, String[] languages) {
49 return new TranslateInfoBar(translateBarType, sourceLanguageIndex, targe tLanguageIndex,
50 autoTranslatePair, showNeverInfobar, triggeredFromMenu, language s);
51 }
52
53 public TranslateInfoBar(int infoBarType, int sourceLanguageIndex, int target LanguageIndex,
David Trainor- moved to gerrit 2015/06/03 19:38:45 Javadoc. Or make private because it's built from
Changwan Ryu 2015/06/04 01:42:27 Changed to private.
54 boolean autoTranslatePair, boolean shouldShowNeverBar, boolean trigg eredFromMenu,
55 String[] languages) {
48 super(null, R.drawable.infobar_translate, null, null); 56 super(null, R.drawable.infobar_translate, null, null);
49 mTranslateDelegate = delegate;
50 mOptions = new TranslateOptions(sourceLanguageIndex, targetLanguageIndex , languages, 57 mOptions = new TranslateOptions(sourceLanguageIndex, targetLanguageIndex , languages,
51 autoTranslatePair, triggeredFromMenu); 58 autoTranslatePair, triggeredFromMenu);
52 mInfoBarType = infoBarType; 59 mInfoBarType = infoBarType;
53 mShouldShowNeverBar = shouldShowNeverBar; 60 mShouldShowNeverBar = shouldShowNeverBar;
54 mOptionsPanelViewType = NO_PANEL; 61 mOptionsPanelViewType = NO_PANEL;
55 setNativeInfoBar(nativeInfoBarPtr);
56 } 62 }
57 63
58 @Override 64 @Override
59 public void onCloseButtonClicked() { 65 public void onCloseButtonClicked() {
60 if (getInfoBarType() == BEFORE_TRANSLATE_INFOBAR && mOptionsPanelViewTyp e == NO_PANEL) { 66 if (getInfoBarType() == BEFORE_TRANSLATE_INFOBAR && mOptionsPanelViewTyp e == NO_PANEL) {
61 // Make it behave exactly as the Nope Button. 67 // Make it behave exactly as the Nope Button.
62 onButtonClicked(false); 68 onButtonClicked(false);
63 } else { 69 } else {
64 nativeOnCloseButtonClicked(mNativeInfoBarPtr); 70 super.onCloseButtonClicked();
65 } 71 }
66 } 72 }
67 73
68 @Override 74 @Override
69 public void onButtonClicked(boolean isPrimaryButton) { 75 public void onButtonClicked(boolean isPrimaryButton) {
70 if (mSubPanel != null) { 76 if (mSubPanel != null) {
71 mSubPanel.onButtonClicked(isPrimaryButton); 77 mSubPanel.onButtonClicked(isPrimaryButton);
72 return; 78 return;
73 } 79 }
74 80
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 // Apply options and close the infobar. 207 // Apply options and close the infobar.
202 onTranslateInfoBarButtonClicked(action); 208 onTranslateInfoBarButtonClicked(action);
203 } 209 }
204 } 210 }
205 211
206 private void onTranslateInfoBarButtonClicked(int action) { 212 private void onTranslateInfoBarButtonClicked(int action) {
207 onOptionsChanged(); 213 onOptionsChanged();
208 214
209 // We need to re-check if the pointer is null now because applying optio ns (like never 215 // We need to re-check if the pointer is null now because applying optio ns (like never
210 // translate this site) can sometimes trigger closing the InfoBar. 216 // translate this site) can sometimes trigger closing the InfoBar.
211 if (mNativeInfoBarPtr == 0) return; 217 onButtonClicked(action, "");
212 nativeOnButtonClicked(mNativeInfoBarPtr, action, "");
213 } 218 }
214 219
215 @Override 220 @Override
216 public void setControlsEnabled(boolean state) { 221 public void setControlsEnabled(boolean state) {
217 super.setControlsEnabled(state); 222 super.setControlsEnabled(state);
218 223
219 // Handle the "Always Translate" checkbox. 224 // Handle the "Always Translate" checkbox.
220 ContentWrapperView wrapper = getContentWrapper(false); 225 ContentWrapperView wrapper = getContentWrapper(false);
221 if (wrapper != null) { 226 if (wrapper != null) {
222 CheckBox checkBox = (CheckBox) wrapper.findViewById(R.id.infobar_ext ra_check); 227 CheckBox checkBox = (CheckBox) wrapper.findViewById(R.id.infobar_ext ra_check);
223 if (checkBox != null) checkBox.setEnabled(state); 228 if (checkBox != null) checkBox.setEnabled(state);
224 } 229 }
225 } 230 }
226 231
227 @Override 232 @Override
228 public void onOptionsChanged() { 233 public void onOptionsChanged() {
229 if (mNativeInfoBarPtr == 0) return; 234 if (mNativeTranslateInfoBarPtr == 0) return;
230 235
231 if (mOptions.optionsChanged()) { 236 if (mOptions.optionsChanged()) {
232 mTranslateDelegate.applyTranslateOptions(mNativeInfoBarPtr, 237 nativeApplyTranslateOptions(mNativeTranslateInfoBarPtr, mOptions.sou rceLanguageIndex(),
233 mOptions.sourceLanguageIndex(), 238 mOptions.targetLanguageIndex(), mOptions.alwaysTranslateLang uageState(),
234 mOptions.targetLanguageIndex(), 239 mOptions.neverTranslateLanguageState(), mOptions.neverTransl ateDomainState());
235 mOptions.alwaysTranslateLanguageState(),
236 mOptions.neverTranslateLanguageState(),
237 mOptions.neverTranslateDomainState());
238 } 240 }
239 } 241 }
240 242
241 private boolean needsNeverPanel() { 243 private boolean needsNeverPanel() {
242 return (getInfoBarType() == TranslateInfoBar.BEFORE_TRANSLATE_INFOBAR 244 return (getInfoBarType() == TranslateInfoBar.BEFORE_TRANSLATE_INFOBAR
243 && mShouldShowNeverBar); 245 && mShouldShowNeverBar);
244 } 246 }
245 247
246 private boolean needsAlwaysPanel() { 248 private boolean needsAlwaysPanel() {
247 return (getInfoBarType() == TranslateInfoBar.AFTER_TRANSLATE_INFOBAR 249 return (getInfoBarType() == TranslateInfoBar.AFTER_TRANSLATE_INFOBAR
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 } 326 }
325 }, 0, linkText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 327 }, 0, linkText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
326 result.append(formattedChange); 328 result.append(formattedChange);
327 return result; 329 return result;
328 } 330 }
329 331
330 int getInfoBarType() { 332 int getInfoBarType() {
331 return mInfoBarType; 333 return mInfoBarType;
332 } 334 }
333 335
334 void changeInfoBarTypeAndNativePointer(int infoBarType, long newNativePointe r) { 336 @CalledByNative
337 private void setNativePtr(long nativePtr) {
338 mNativeTranslateInfoBarPtr = nativePtr;
339 }
340
341 @CalledByNative
342 private void onNativeDestroyed() {
David Trainor- moved to gerrit 2015/06/03 19:38:45 Same comment as before
Changwan Ryu 2015/06/04 01:42:27 Done.
343 mNativeTranslateInfoBarPtr = 0;
344 }
345
346 @CalledByNative
347 private void changeTranslateInfoBarType(int infoBarType) {
335 if (infoBarType >= 0 && infoBarType < MAX_INFOBAR_INDEX) { 348 if (infoBarType >= 0 && infoBarType < MAX_INFOBAR_INDEX) {
336 mInfoBarType = infoBarType; 349 mInfoBarType = infoBarType;
337 replaceNativePointer(newNativePointer);
338 updateViewForCurrentState(createView()); 350 updateViewForCurrentState(createView());
339 } else { 351 } else {
340 assert false : "Trying to change the InfoBar to a type that is inval id."; 352 assert false : "Trying to change the InfoBar to a type that is inval id.";
341 } 353 }
342 } 354 }
355
356 @Override
357 protected void finalize() throws Throwable {
David Trainor- moved to gerrit 2015/06/03 19:38:45 Same comment as before
Changwan Ryu 2015/06/04 01:42:27 Done.
358 assert mNativeTranslateInfoBarPtr == 0;
359 super.finalize();
360 }
361
362 private native void nativeApplyTranslateOptions(long nativeTranslateInfoBar,
363 int sourceLanguageIndex, int targetLanguageIndex, boolean alwaysTran slate,
364 boolean neverTranslateLanguage, boolean neverTranslateSite);
343 } 365 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698