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