OLD | NEW |
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; | 10 import android.content.Context; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 */ | 45 */ |
46 @VisibleForTesting | 46 @VisibleForTesting |
47 public static final String FEATURE_IS_CHILD_ACCOUNT_KEY = "service_uca"; | 47 public static final String FEATURE_IS_CHILD_ACCOUNT_KEY = "service_uca"; |
48 | 48 |
49 private static final Object sLock = new Object(); | 49 private static final Object sLock = new Object(); |
50 | 50 |
51 private static AccountManagerHelper sAccountManagerHelper; | 51 private static AccountManagerHelper sAccountManagerHelper; |
52 | 52 |
53 private final AccountManagerDelegate mAccountManager; | 53 private final AccountManagerDelegate mAccountManager; |
54 | 54 |
55 private Context mApplicationContext; | |
56 | |
57 /** | 55 /** |
58 * A simple callback for getAuthToken. | 56 * A simple callback for getAuthToken. |
59 */ | 57 */ |
60 public interface GetAuthTokenCallback { | 58 public interface GetAuthTokenCallback { |
61 /** | 59 /** |
62 * 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
. |
63 * | 61 * |
64 * @param token Auth token, guaranteed not to be null. | 62 * @param token Auth token, guaranteed not to be null. |
65 */ | 63 */ |
66 void tokenAvailable(String token); | 64 void tokenAvailable(String token); |
67 | 65 |
68 /** | 66 /** |
69 * Invoked on the UI thread if no token is available. | 67 * Invoked on the UI thread if no token is available. |
70 * | 68 * |
71 * @param isTransientError Indicates if the error is transient (network
timeout or | 69 * @param isTransientError Indicates if the error is transient (network
timeout or |
72 * unavailable, etc) or persistent (bad credentials, permission denied,
etc). | 70 * unavailable, etc) or persistent (bad credentials, permission denied,
etc). |
73 */ | 71 */ |
74 void tokenUnavailable(boolean isTransientError); | 72 void tokenUnavailable(boolean isTransientError); |
75 } | 73 } |
76 | 74 |
77 /** | 75 /** |
78 * @param context the Android context | |
79 * @param accountManager the account manager to use as a backend service | 76 * @param accountManager the account manager to use as a backend service |
80 */ | 77 */ |
81 private AccountManagerHelper(Context context, AccountManagerDelegate account
Manager) { | 78 private AccountManagerHelper(AccountManagerDelegate accountManager) { |
82 mApplicationContext = context.getApplicationContext(); | |
83 mAccountManager = accountManager; | 79 mAccountManager = accountManager; |
84 } | 80 } |
85 | 81 |
86 /** | 82 /** |
87 * Initialize AccountManagerHelper with a custom AccountManagerDelegate. | 83 * Initialize AccountManagerHelper with a custom AccountManagerDelegate. |
88 * Ensures that the singleton AccountManagerHelper hasn't been created yet. | 84 * Ensures that the singleton AccountManagerHelper hasn't been created yet. |
89 * This can be overriden in tests using the overrideAccountManagerHelperForT
ests method. | 85 * This can be overriden in tests using the overrideAccountManagerHelperForT
ests method. |
90 * | 86 * |
91 * @param context the applicationContext is retrieved from the context used
as an argument. | |
92 * @param delegate the custom AccountManagerDelegate to use. | 87 * @param delegate the custom AccountManagerDelegate to use. |
93 */ | 88 */ |
94 public static void initializeAccountManagerHelper( | 89 public static void initializeAccountManagerHelper(AccountManagerDelegate del
egate) { |
95 Context context, AccountManagerDelegate delegate) { | |
96 synchronized (sLock) { | 90 synchronized (sLock) { |
97 assert sAccountManagerHelper == null; | 91 assert sAccountManagerHelper == null; |
98 sAccountManagerHelper = new AccountManagerHelper(context, delegate); | 92 sAccountManagerHelper = new AccountManagerHelper(delegate); |
99 } | 93 } |
100 } | 94 } |
101 | 95 |
102 /** | 96 /** |
103 * A getter method for AccountManagerHelper singleton which also initializes
it if not wasn't | 97 * A getter method for AccountManagerHelper singleton which also initializes
it if not wasn't |
104 * already initialized. | 98 * already initialized. |
105 * | 99 * |
106 * @param context the applicationContext is retrieved from the context used
as an argument. | |
107 * @return a singleton instance of the AccountManagerHelper | 100 * @return a singleton instance of the AccountManagerHelper |
108 */ | 101 */ |
109 public static AccountManagerHelper get(Context context) { | 102 public static AccountManagerHelper get() { |
110 synchronized (sLock) { | 103 synchronized (sLock) { |
111 if (sAccountManagerHelper == null) { | 104 if (sAccountManagerHelper == null) { |
112 sAccountManagerHelper = new AccountManagerHelper( | 105 sAccountManagerHelper = |
113 context, new SystemAccountManagerDelegate(context)); | 106 new AccountManagerHelper(new SystemAccountManagerDelegat
e()); |
114 } | 107 } |
115 } | 108 } |
116 return sAccountManagerHelper; | 109 return sAccountManagerHelper; |
117 } | 110 } |
118 | 111 |
119 /** | 112 /** |
120 * Override AccountManagerHelper with a custom AccountManagerDelegate in tes
ts. | 113 * Override AccountManagerHelper with a custom AccountManagerDelegate in tes
ts. |
121 * Unlike initializeAccountManagerHelper, this will override the existing in
stance of | 114 * Unlike initializeAccountManagerHelper, this will override the existing in
stance of |
122 * AccountManagerHelper if any. Only for use in Tests. | 115 * AccountManagerHelper if any. Only for use in Tests. |
123 * | 116 * |
124 * @param context the applicationContext is retrieved from the context used
as an argument. | 117 * @param context the applicationContext is retrieved from the context used
as an argument. |
125 * @param delegate the custom AccountManagerDelegate to use. | 118 * @param delegate the custom AccountManagerDelegate to use. |
126 */ | 119 */ |
127 @VisibleForTesting | 120 @VisibleForTesting |
128 public static void overrideAccountManagerHelperForTests( | 121 public static void overrideAccountManagerHelperForTests( |
129 Context context, AccountManagerDelegate delegate) { | 122 Context context, AccountManagerDelegate delegate) { |
130 synchronized (sLock) { | 123 synchronized (sLock) { |
131 sAccountManagerHelper = new AccountManagerHelper(context, delegate); | 124 sAccountManagerHelper = new AccountManagerHelper(delegate); |
132 } | 125 } |
133 } | 126 } |
134 | 127 |
135 /** | 128 /** |
136 * Creates an Account object for the given name. | 129 * Creates an Account object for the given name. |
137 */ | 130 */ |
138 public static Account createAccountFromName(String name) { | 131 public static Account createAccountFromName(String name) { |
139 return new Account(name, GOOGLE_ACCOUNT_TYPE); | 132 return new Account(name, GOOGLE_ACCOUNT_TYPE); |
140 } | 133 } |
141 | 134 |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
457 public void onConnectionTypeChanged(int connectionType) { | 450 public void onConnectionTypeChanged(int connectionType) { |
458 assert mNumTries.get() < MAX_TRIES; | 451 assert mNumTries.get() < MAX_TRIES; |
459 if (NetworkChangeNotifier.isOnline()) { | 452 if (NetworkChangeNotifier.isOnline()) { |
460 // The network is back; stop listening and try again. | 453 // The network is back; stop listening and try again. |
461 NetworkChangeNotifier.removeConnectionTypeObserver(this); | 454 NetworkChangeNotifier.removeConnectionTypeObserver(this); |
462 attempt(); | 455 attempt(); |
463 } | 456 } |
464 } | 457 } |
465 } | 458 } |
466 } | 459 } |
OLD | NEW |