| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.tab; | 5 package org.chromium.chrome.browser.tab; |
| 6 | 6 |
| 7 import android.annotation.SuppressLint; |
| 7 import android.content.Context; | 8 import android.content.Context; |
| 8 import android.content.SharedPreferences; | 9 import android.content.SharedPreferences; |
| 9 | 10 |
| 10 import org.chromium.base.ContextUtils; | 11 import org.chromium.base.ContextUtils; |
| 11 import org.chromium.base.VisibleForTesting; | 12 import org.chromium.base.VisibleForTesting; |
| 12 import org.chromium.chrome.browser.tabmodel.TabModel; | 13 import org.chromium.chrome.browser.tabmodel.TabModel; |
| 13 | 14 |
| 14 import java.util.concurrent.atomic.AtomicInteger; | 15 import java.util.concurrent.atomic.AtomicInteger; |
| 15 | 16 |
| 16 /** | 17 /** |
| 17 * Maintains a monotonically increasing ID that is used for uniquely identifying
{@link Tab}s. This | 18 * Maintains a monotonically increasing ID that is used for uniquely identifying
{@link Tab}s. This |
| 18 * class is responsible for ensuring that Tabs created in the same process, acro
ss every | 19 * class is responsible for ensuring that Tabs created in the same process, acro
ss every |
| 19 * {@link TabModel}, are allocated a unique ID. Note that only the browser proc
ess should be | 20 * {@link TabModel}, are allocated a unique ID. Note that only the browser proc
ess should be |
| 20 * generating Tab IDs to prevent collisions. | 21 * generating Tab IDs to prevent collisions. |
| 21 * | 22 * |
| 22 * Calling {@link TabIdManager#incrementIdCounterTo(int)} will ensure new {@link
Tab}s get IDs | 23 * Calling {@link TabIdManager#incrementIdCounterTo(int)} will ensure new {@link
Tab}s get IDs |
| 23 * greater than or equal to the parameter passed to that method. This should be
used when doing | 24 * greater than or equal to the parameter passed to that method. This should be
used when doing |
| 24 * things like loading persisted {@link Tab}s from disk on process start to ensu
re all new | 25 * things like loading persisted {@link Tab}s from disk on process start to ensu
re all new |
| 25 * {@link Tab}s don't have id collision. | 26 * {@link Tab}s don't have id collision. |
| 26 * | 27 * |
| 27 * TODO(dfalcantara): Tab ID generation prior to M45 is haphazard and dependent
on which Activity is | 28 * TODO(dfalcantara): Tab ID generation prior to M45 is haphazard and dependent
on which Activity is |
| 28 * started first. Unify the ways the maximum Tab ID is set (
crbug.com/502384). | 29 * started first. Unify the ways the maximum Tab ID is set (
crbug.com/502384). |
| 29 */ | 30 */ |
| 30 public class TabIdManager { | 31 public class TabIdManager { |
| 31 @VisibleForTesting | 32 @VisibleForTesting |
| 32 public static final String PREF_NEXT_ID = | 33 public static final String PREF_NEXT_ID = |
| 33 "org.chromium.chrome.browser.tab.TabIdManager.NEXT_ID"; | 34 "org.chromium.chrome.browser.tab.TabIdManager.NEXT_ID"; |
| 34 | 35 |
| 35 private static final Object INSTANCE_LOCK = new Object(); | 36 private static final Object INSTANCE_LOCK = new Object(); |
| 37 // TODO(crbug.com/635567): Fix this properly. |
| 38 @SuppressLint("StaticFieldLeak") |
| 36 private static TabIdManager sInstance; | 39 private static TabIdManager sInstance; |
| 37 | 40 |
| 38 private final Context mContext; | 41 private final Context mContext; |
| 39 private final AtomicInteger mIdCounter = new AtomicInteger(); | 42 private final AtomicInteger mIdCounter = new AtomicInteger(); |
| 40 | 43 |
| 41 private SharedPreferences mPreferences; | 44 private SharedPreferences mPreferences; |
| 42 | 45 |
| 43 /** Returns the Singleton instance of the TabIdManager. */ | 46 /** Returns the Singleton instance of the TabIdManager. */ |
| 44 public static TabIdManager getInstance() { | 47 public static TabIdManager getInstance() { |
| 45 return getInstance(ContextUtils.getApplicationContext()); | 48 return getInstance(ContextUtils.getApplicationContext()); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 private TabIdManager(Context context) { | 91 private TabIdManager(Context context) { |
| 89 mContext = context; | 92 mContext = context; |
| 90 | 93 |
| 91 // Read the shared preference. This has to be done on the critical path
to ensure that the | 94 // Read the shared preference. This has to be done on the critical path
to ensure that the |
| 92 // myriad Activities that serve as entries into Chrome are all synchroni
zed on the correct | 95 // myriad Activities that serve as entries into Chrome are all synchroni
zed on the correct |
| 93 // maximum Tab ID. | 96 // maximum Tab ID. |
| 94 mPreferences = ContextUtils.getAppSharedPreferences(); | 97 mPreferences = ContextUtils.getAppSharedPreferences(); |
| 95 mIdCounter.set(mPreferences.getInt(PREF_NEXT_ID, 0)); | 98 mIdCounter.set(mPreferences.getInt(PREF_NEXT_ID, 0)); |
| 96 } | 99 } |
| 97 } | 100 } |
| OLD | NEW |