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