| 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; | 5 package org.chromium.chrome.browser; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.content.SharedPreferences; | 8 import android.content.SharedPreferences; |
| 9 import android.os.AsyncTask; | 9 import android.os.AsyncTask; |
| 10 import android.os.StrictMode; | 10 import android.os.StrictMode; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 * Automatic GCM use is disabled by tests, and also by this class if it is d
etermined on | 48 * Automatic GCM use is disabled by tests, and also by this class if it is d
etermined on |
| 49 * creation that the installed Play Services library is out of date. | 49 * creation that the installed Play Services library is out of date. |
| 50 */ | 50 */ |
| 51 private static boolean sGCMEnabled = true; | 51 private static boolean sGCMEnabled = true; |
| 52 | 52 |
| 53 @VisibleForTesting | 53 @VisibleForTesting |
| 54 protected AsyncTask<Void, Void, Void> mLaunchBrowserIfStoppedTask; | 54 protected AsyncTask<Void, Void, Void> mLaunchBrowserIfStoppedTask; |
| 55 | 55 |
| 56 /** | 56 /** |
| 57 * Create a BackgroundSyncLauncher object, which is owned by C++. | 57 * Create a BackgroundSyncLauncher object, which is owned by C++. |
| 58 * @param context The app context. |
| 58 */ | 59 */ |
| 59 @VisibleForTesting | 60 @VisibleForTesting |
| 60 @CalledByNative | 61 @CalledByNative |
| 61 protected static BackgroundSyncLauncher create() { | 62 protected static BackgroundSyncLauncher create(Context context) { |
| 62 if (sInstance != null) { | 63 if (sInstance != null) { |
| 63 throw new IllegalStateException("Already instantiated"); | 64 throw new IllegalStateException("Already instantiated"); |
| 64 } | 65 } |
| 65 | 66 |
| 66 sInstance = new BackgroundSyncLauncher(); | 67 sInstance = new BackgroundSyncLauncher(context); |
| 67 return sInstance; | 68 return sInstance; |
| 68 } | 69 } |
| 69 | 70 |
| 70 /** | 71 /** |
| 71 * Called when the C++ counterpart is deleted. | 72 * Called when the C++ counterpart is deleted. |
| 72 */ | 73 */ |
| 73 @VisibleForTesting | 74 @VisibleForTesting |
| 74 @CalledByNative | 75 @CalledByNative |
| 75 protected void destroy() { | 76 protected void destroy() { |
| 76 assert sInstance == this; | 77 assert sInstance == this; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 151 } | 152 } |
| 152 | 153 |
| 153 /** | 154 /** |
| 154 * Returns true if the native browser has started and created an instance of
{@link | 155 * Returns true if the native browser has started and created an instance of
{@link |
| 155 * BackgroundSyncLauncher}. | 156 * BackgroundSyncLauncher}. |
| 156 */ | 157 */ |
| 157 protected static boolean hasInstance() { | 158 protected static boolean hasInstance() { |
| 158 return sInstance != null; | 159 return sInstance != null; |
| 159 } | 160 } |
| 160 | 161 |
| 161 protected BackgroundSyncLauncher() { | 162 protected BackgroundSyncLauncher(Context context) { |
| 162 mScheduler = GcmNetworkManager.getInstance(ContextUtils.getApplicationCo
ntext()); | 163 mScheduler = GcmNetworkManager.getInstance(context); |
| 163 launchBrowserIfStopped(false, 0); | 164 launchBrowserIfStopped(false, 0); |
| 164 } | 165 } |
| 165 | 166 |
| 166 private static boolean canUseGooglePlayServices() { | 167 private static boolean canUseGooglePlayServices(Context context) { |
| 167 return ExternalAuthUtils.getInstance().canUseGooglePlayServices( | 168 return ExternalAuthUtils.getInstance().canUseGooglePlayServices( |
| 168 ContextUtils.getApplicationContext(), new UserRecoverableErrorHa
ndler.Silent()); | 169 context, new UserRecoverableErrorHandler.Silent()); |
| 169 } | 170 } |
| 170 | 171 |
| 171 /** | 172 /** |
| 172 * Returns true if the Background Sync Manager should be automatically disab
led on startup. | 173 * Returns true if the Background Sync Manager should be automatically disab
led on startup. |
| 173 * This is currently only the case if Play Services is not up to date, since
any sync attempts | 174 * This is currently only the case if Play Services is not up to date, since
any sync attempts |
| 174 * which fail cannot be reregistered. Better to wait until Play Services is
updated before | 175 * which fail cannot be reregistered. Better to wait until Play Services is
updated before |
| 175 * attempting them. | 176 * attempting them. |
| 176 * | 177 * |
| 178 * @param context The application context. |
| 177 */ | 179 */ |
| 178 @CalledByNative | 180 @CalledByNative |
| 179 private static boolean shouldDisableBackgroundSync() { | 181 private static boolean shouldDisableBackgroundSync(Context context) { |
| 180 // Check to see if Play Services is up to date, and disable GCM if not. | 182 // Check to see if Play Services is up to date, and disable GCM if not. |
| 181 // This will not automatically set {@link sGCMEnabled} to true, in case
it has been | 183 // This will not automatically set {@link sGCMEnabled} to true, in case
it has been |
| 182 // disabled in tests. | 184 // disabled in tests. |
| 183 if (sGCMEnabled) { | 185 if (sGCMEnabled) { |
| 184 boolean isAvailable = true; | 186 boolean isAvailable = true; |
| 185 if (!canUseGooglePlayServices()) { | 187 if (!canUseGooglePlayServices(context)) { |
| 186 setGCMEnabled(false); | 188 setGCMEnabled(false); |
| 187 Log.i(TAG, "Disabling Background Sync because Play Services is n
ot up to date."); | 189 Log.i(TAG, "Disabling Background Sync because Play Services is n
ot up to date."); |
| 188 isAvailable = false; | 190 isAvailable = false; |
| 189 } | 191 } |
| 190 RecordHistogram.recordBooleanHistogram( | 192 RecordHistogram.recordBooleanHistogram( |
| 191 "BackgroundSync.LaunchTask.PlayServicesAvailable", isAvailab
le); | 193 "BackgroundSync.LaunchTask.PlayServicesAvailable", isAvailab
le); |
| 192 } | 194 } |
| 193 return !sGCMEnabled; | 195 return !sGCMEnabled; |
| 194 } | 196 } |
| 195 | 197 |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 } | 263 } |
| 262 }; | 264 }; |
| 263 BackgroundSyncLauncher.shouldLaunchBrowserIfStopped(callback); | 265 BackgroundSyncLauncher.shouldLaunchBrowserIfStopped(callback); |
| 264 } | 266 } |
| 265 | 267 |
| 266 @VisibleForTesting | 268 @VisibleForTesting |
| 267 static void setGCMEnabled(boolean enabled) { | 269 static void setGCMEnabled(boolean enabled) { |
| 268 sGCMEnabled = enabled; | 270 sGCMEnabled = enabled; |
| 269 } | 271 } |
| 270 } | 272 } |
| OLD | NEW |