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

Side by Side Diff: components/signin/core/browser/android/java/src/org/chromium/components/signin/AccountManagerHelper.java

Issue 2872743003: Change AccountManagerHelper initialization
Patch Set: Created 3 years, 7 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 2011 The Chromium Authors. All rights reserved. 1 // Copyright 2011 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.components.signin; 5 package org.chromium.components.signin;
6 6
7 import android.accounts.Account; 7 import android.accounts.Account;
8 import android.accounts.AuthenticatorDescription; 8 import android.accounts.AuthenticatorDescription;
9 import android.app.Activity; 9 import android.app.Activity;
10 import android.content.Context;
11 import android.os.AsyncTask; 10 import android.os.AsyncTask;
12 import android.support.annotation.Nullable; 11 import android.support.annotation.Nullable;
13 12
14 import org.chromium.base.Callback; 13 import org.chromium.base.Callback;
15 import org.chromium.base.Log; 14 import org.chromium.base.Log;
16 import org.chromium.base.VisibleForTesting; 15 import org.chromium.base.VisibleForTesting;
17 import org.chromium.net.NetworkChangeNotifier; 16 import org.chromium.net.NetworkChangeNotifier;
18 17
19 import java.util.ArrayList; 18 import java.util.ArrayList;
20 import java.util.List; 19 import java.util.List;
(...skipping 16 matching lines...) Expand all
37 private static final String GOOGLEMAIL_COM = "googlemail.com"; 36 private static final String GOOGLEMAIL_COM = "googlemail.com";
38 public static final String GOOGLE_ACCOUNT_TYPE = "com.google"; 37 public static final String GOOGLE_ACCOUNT_TYPE = "com.google";
39 38
40 /** 39 /**
41 * An account feature (corresponding to a Gaia service flag) that specifies whether the account 40 * An account feature (corresponding to a Gaia service flag) that specifies whether the account
42 * is a child account. 41 * is a child account.
43 */ 42 */
44 @VisibleForTesting 43 @VisibleForTesting
45 public static final String FEATURE_IS_CHILD_ACCOUNT_KEY = "service_uca"; 44 public static final String FEATURE_IS_CHILD_ACCOUNT_KEY = "service_uca";
46 45
47 private static final AtomicReference<AccountManagerHelper> sInstance = new A tomicReference<>(); 46 private static final AtomicReference<AccountManagerHelper> sAtomicInstance =
47 new AtomicReference<>();
48
49 private static final Object sLock = new Object();
50 private static Throwable sInstanceInitializedBy;
51 private static Throwable sTestingInstanceInitializedBy;
48 52
49 private final AccountManagerDelegate mDelegate; 53 private final AccountManagerDelegate mDelegate;
50 54
51 /** 55 /**
52 * A simple callback for getAuthToken. 56 * A simple callback for getAuthToken.
53 */ 57 */
54 public interface GetAuthTokenCallback { 58 public interface GetAuthTokenCallback {
55 /** 59 /**
56 * Invoked on the UI thread if a token is provided by the AccountManager . 60 * Invoked on the UI thread if a token is provided by the AccountManager .
57 * 61 *
(...skipping 11 matching lines...) Expand all
69 } 73 }
70 74
71 /** 75 /**
72 * @param delegate the account manager to use as a backend service 76 * @param delegate the account manager to use as a backend service
73 */ 77 */
74 private AccountManagerHelper(AccountManagerDelegate delegate) { 78 private AccountManagerHelper(AccountManagerDelegate delegate) {
75 mDelegate = delegate; 79 mDelegate = delegate;
76 } 80 }
77 81
78 /** 82 /**
79 * Initialize AccountManagerHelper with a custom AccountManagerDelegate. 83 * Initialize AccountManagerHelper with a custom AccountManagerDelegate. Ens ures that it wasn't
80 * Ensures that the singleton AccountManagerHelper hasn't been created yet. 84 * already initialized.
81 * This can be overriden in tests using the overrideAccountManagerHelperForT ests method. 85 * Tests can override this, see {@link #overrideAccountManagerHelperForTests }.
86 *
87 * @param delegate the custom AccountManagerDelegate to use.
88 *
89 * @see #overrideAccountManagerHelperForTests
90 */
91 public static void initializeAccountManagerHelper(AccountManagerDelegate del egate) {
92 assert delegate != null;
93 synchronized (sLock) {
94 if (sInstanceInitializedBy != null) {
95 throw new IllegalStateException(
96 "AccountManagerHelper is already initialized.", sInstanc eInitializedBy);
97 }
98 sInstanceInitializedBy = new Throwable();
99 if (sTestingInstanceInitializedBy != null) return;
100 sAtomicInstance.set(new AccountManagerHelper(delegate));
101 }
102 }
103
104 /**
105 * Override AccountManagerHelper with a custom AccountManagerDelegate for te sts. Only for use
106 * in tests. This should be called before call to {@link #initializeAccountM anagerHelper}
107 * to make sure instance returned by {@link #get} doesn't change on the fly.
82 * 108 *
83 * @param delegate the custom AccountManagerDelegate to use. 109 * @param delegate the custom AccountManagerDelegate to use.
84 */ 110 */
85 public static void initializeAccountManagerHelper(AccountManagerDelegate del egate) { 111 @VisibleForTesting
86 if (!sInstance.compareAndSet(null, new AccountManagerHelper(delegate))) { 112 public static void overrideAccountManagerHelperForTests(AccountManagerDelega te delegate) {
87 throw new IllegalStateException("AccountManagerHelper is already ini tialized!"); 113 assert delegate != null;
114 synchronized (sLock) {
115 Throwable initializedBy = sInstanceInitializedBy != null
116 ? sInstanceInitializedBy
117 : sTestingInstanceInitializedBy;
118 if (initializedBy != null) {
119 throw new IllegalStateException(
120 "AccountManagerHelper is already initialized.", initiali zedBy);
121 }
122 sTestingInstanceInitializedBy = new Throwable();
123 sAtomicInstance.set(new AccountManagerHelper(delegate));
88 } 124 }
89 } 125 }
90 126
91 /** 127 /**
92 * Singleton instance getter. Singleton must be initialized before calling t his 128 * Singleton instance getter. Singleton must be initialized before calling t his
93 * (by initializeAccountManagerHelper or overrideAccountManagerHelperForTest s). 129 * (by initializeAccountManagerHelper or overrideAccountManagerHelperForTest s).
130 * It is guaranteed that this method always returns the same instance during app lifetime.
94 * 131 *
95 * @return a singleton instance 132 * @return a singleton instance
96 */ 133 */
97 public static AccountManagerHelper get() { 134 public static AccountManagerHelper get() {
98 AccountManagerHelper instance = sInstance.get(); 135 AccountManagerHelper instance = sAtomicInstance.get();
99 assert instance != null : "AccountManagerHelper is not initialized!"; 136 assert instance != null : "AccountManagerHelper is not initialized!";
100 return instance; 137 return instance;
101 } 138 }
102 139
103 /** 140 /**
104 * Override AccountManagerHelper with a custom AccountManagerDelegate in tes ts.
105 * Unlike initializeAccountManagerHelper, this will override the existing in stance of
106 * AccountManagerHelper if any. Only for use in Tests.
107 *
108 * @param context the applicationContext is retrieved from the context used as an argument.
109 * @param delegate the custom AccountManagerDelegate to use.
110 */
111 @VisibleForTesting
112 public static void overrideAccountManagerHelperForTests(
113 Context context, AccountManagerDelegate delegate) {
114 sInstance.set(new AccountManagerHelper(delegate));
115 }
116
117 /**
118 * Resets custom AccountManagerHelper set with {@link #overrideAccountManage rHelperForTests}.
119 * Only for use in Tests.
120 */
121 @VisibleForTesting
122 public static void resetAccountManagerHelperForTests() {
123 sInstance.set(null);
124 }
125
126 /**
127 * Creates an Account object for the given name. 141 * Creates an Account object for the given name.
128 */ 142 */
129 public static Account createAccountFromName(String name) { 143 public static Account createAccountFromName(String name) {
130 return new Account(name, GOOGLE_ACCOUNT_TYPE); 144 return new Account(name, GOOGLE_ACCOUNT_TYPE);
131 } 145 }
132 146
133 /** 147 /**
134 * This method is deprecated; please use the asynchronous version below inst ead. 148 * This method is deprecated; please use the asynchronous version below inst ead.
135 * 149 *
136 * See http://crbug.com/517697 for details. 150 * See http://crbug.com/517697 for details.
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 public void onConnectionTypeChanged(int connectionType) { 462 public void onConnectionTypeChanged(int connectionType) {
449 assert mNumTries.get() < MAX_TRIES; 463 assert mNumTries.get() < MAX_TRIES;
450 if (NetworkChangeNotifier.isOnline()) { 464 if (NetworkChangeNotifier.isOnline()) {
451 // The network is back; stop listening and try again. 465 // The network is back; stop listening and try again.
452 NetworkChangeNotifier.removeConnectionTypeObserver(this); 466 NetworkChangeNotifier.removeConnectionTypeObserver(this);
453 attempt(); 467 attempt();
454 } 468 }
455 } 469 }
456 } 470 }
457 } 471 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698