OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.services; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.util.Log; |
| 9 |
| 10 import com.google.android.gms.auth.GoogleAuthException; |
| 11 import com.google.android.gms.auth.GoogleAuthUtil; |
| 12 |
| 13 import org.chromium.base.ApplicationState; |
| 14 import org.chromium.base.ApplicationStatus; |
| 15 import org.chromium.base.ApplicationStatus.ApplicationStateListener; |
| 16 import org.chromium.base.ThreadUtils; |
| 17 import org.chromium.base.TraceEvent; |
| 18 import org.chromium.base.VisibleForTesting; |
| 19 import org.chromium.chrome.browser.signin.AccountIdProvider; |
| 20 import org.chromium.chrome.browser.signin.SigninHelper; |
| 21 import org.chromium.chrome.browser.signin.SigninManager; |
| 22 import org.chromium.chrome.browser.sync.SyncController; |
| 23 import org.chromium.sync.signin.ChromeSigninController; |
| 24 |
| 25 import java.io.IOException; |
| 26 |
| 27 /** |
| 28 * Starts and monitors various sync and Google services related tasks. |
| 29 * - add listeners to AccountManager. |
| 30 * - sets up the Android status bar notification controller. |
| 31 * - start Tango service if sync setup is completed. |
| 32 * <p/> |
| 33 * It is intended to be an application level object and is not tied to any parti
culary |
| 34 * activity, although re-verifies some settings whe browser is launched. |
| 35 * <p/> |
| 36 * The object must be created on the main thread. |
| 37 * <p/> |
| 38 */ |
| 39 public class GoogleServicesManager implements ApplicationStateListener { |
| 40 |
| 41 private static final String TAG = "GoogleServicesManager"; |
| 42 |
| 43 @VisibleForTesting |
| 44 public static final String SESSION_TAG_PREFIX = "session_sync"; |
| 45 |
| 46 private static GoogleServicesManager sGoogleServicesManager; |
| 47 |
| 48 @VisibleForTesting |
| 49 protected final Context mContext; |
| 50 |
| 51 private final ChromeSigninController mChromeSigninController; |
| 52 |
| 53 private final SigninHelper mSigninHelper; |
| 54 |
| 55 /** |
| 56 * A helper method for retrieving the application-wide GoogleServicesManager
. |
| 57 * <p/> |
| 58 * Can only be accessed on the main thread. |
| 59 * |
| 60 * @param context the ApplicationContext is retrieved from the context used
as an argument. |
| 61 * @return a singleton instance of the GoogleServicesManager |
| 62 */ |
| 63 public static GoogleServicesManager get(Context context) { |
| 64 ThreadUtils.assertOnUiThread(); |
| 65 if (sGoogleServicesManager == null) { |
| 66 sGoogleServicesManager = new GoogleServicesManager(context); |
| 67 } |
| 68 return sGoogleServicesManager; |
| 69 } |
| 70 |
| 71 private GoogleServicesManager(Context context) { |
| 72 try { |
| 73 TraceEvent.begin("GoogleServicesManager.GoogleServicesManager"); |
| 74 ThreadUtils.assertOnUiThread(); |
| 75 // We should store the application context, as we outlive any activi
ty which may create |
| 76 // us. |
| 77 mContext = context.getApplicationContext(); |
| 78 |
| 79 mChromeSigninController = ChromeSigninController.get(mContext); |
| 80 mSigninHelper = SigninHelper.get(mContext); |
| 81 |
| 82 // The sign out flow starts by clearing the signed in user in the Ch
romeSigninController |
| 83 // on the Java side, and then performs a sign out on the native side
. If there is a |
| 84 // crash on the native side then the signin state may get out of syn
c. Make sure that |
| 85 // the native side is signed out if the Java side doesn't have a cur
rently signed in |
| 86 // user. |
| 87 SigninManager signinManager = SigninManager.get(mContext); |
| 88 if (!mChromeSigninController.isSignedIn() && signinManager.isSignedI
nOnNative()) { |
| 89 Log.w(TAG, "Signed in state got out of sync, forcing native sign
out"); |
| 90 signinManager.signOut(null, null); |
| 91 } |
| 92 |
| 93 // Register account Id provider. |
| 94 AccountIdProvider.setInstance(new AccountIdProvider() { |
| 95 @Override |
| 96 public String getAccountId(Context ctx, String accountName) { |
| 97 try { |
| 98 return GoogleAuthUtil.getAccountId(ctx, accountName); |
| 99 } catch (IOException | GoogleAuthException ex) { |
| 100 Log.e(TAG, "AccountIdProvider.getAccountId", ex); |
| 101 return null; |
| 102 } |
| 103 } |
| 104 }); |
| 105 |
| 106 // Initialize sync. |
| 107 SyncController.get(context); |
| 108 |
| 109 ApplicationStatus.registerApplicationStateListener(this); |
| 110 } finally { |
| 111 TraceEvent.end("GoogleServicesManager.GoogleServicesManager"); |
| 112 } |
| 113 } |
| 114 |
| 115 /** |
| 116 * Called once during initialization and then again for every start (warm-st
art). |
| 117 * Responsible for checking if configuration has changed since Chrome was la
st launched |
| 118 * and updates state accordingly. |
| 119 */ |
| 120 public void onMainActivityStart() { |
| 121 try { |
| 122 TraceEvent.begin("GoogleServicesManager.onMainActivityStart"); |
| 123 boolean accountsChanged = mSigninHelper.checkAndClearAccountsChanged
Pref(mContext); |
| 124 mSigninHelper.validateAccountSettings(accountsChanged); |
| 125 } finally { |
| 126 TraceEvent.end("GoogleServicesManager.onMainActivityStart"); |
| 127 } |
| 128 } |
| 129 |
| 130 @Override |
| 131 public void onApplicationStateChange(int newState) { |
| 132 if (newState == ApplicationState.HAS_RUNNING_ACTIVITIES) { |
| 133 onMainActivityStart(); |
| 134 } |
| 135 } |
| 136 } |
OLD | NEW |