OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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.invalidation; | 5 package org.chromium.chrome.browser.invalidation; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.Intent; | 8 import android.content.Intent; |
9 import android.os.AsyncTask; | 9 import android.os.AsyncTask; |
10 import android.os.Handler; | 10 import android.os.Handler; |
11 import android.os.SystemClock; | 11 import android.os.SystemClock; |
12 | 12 |
13 import com.google.ipc.invalidation.ticl.android2.channel.AndroidGcmController; | 13 import com.google.ipc.invalidation.ticl.android2.channel.AndroidGcmController; |
14 | 14 |
15 import org.chromium.base.ApplicationState; | 15 import org.chromium.base.ApplicationState; |
16 import org.chromium.base.ApplicationStatus; | 16 import org.chromium.base.ApplicationStatus; |
| 17 import org.chromium.base.BuildInfo; |
17 import org.chromium.base.FieldTrialList; | 18 import org.chromium.base.FieldTrialList; |
| 19 import org.chromium.base.Log; |
18 import org.chromium.base.VisibleForTesting; | 20 import org.chromium.base.VisibleForTesting; |
19 import org.chromium.chrome.browser.ChromeFeatureList; | 21 import org.chromium.chrome.browser.ChromeFeatureList; |
20 import org.chromium.chrome.browser.sync.ProfileSyncService; | 22 import org.chromium.chrome.browser.sync.ProfileSyncService; |
21 import org.chromium.components.invalidation.InvalidationClientService; | 23 import org.chromium.components.invalidation.InvalidationClientService; |
22 import org.chromium.components.signin.ChromeSigninController; | 24 import org.chromium.components.signin.ChromeSigninController; |
23 import org.chromium.components.sync.AndroidSyncSettings; | 25 import org.chromium.components.sync.AndroidSyncSettings; |
24 import org.chromium.components.sync.ModelType; | 26 import org.chromium.components.sync.ModelType; |
25 import org.chromium.components.sync.notifier.InvalidationIntentProtocol; | 27 import org.chromium.components.sync.notifier.InvalidationIntentProtocol; |
26 | 28 |
27 import java.util.HashSet; | 29 import java.util.HashSet; |
28 | 30 |
29 /** | 31 /** |
30 * Controller used to send start, stop, and registration-change commands to the
invalidation | 32 * Controller used to send start, stop, and registration-change commands to the
invalidation |
31 * client library used by Sync. | 33 * client library used by Sync. |
32 */ | 34 */ |
33 public class InvalidationController implements ApplicationStatus.ApplicationStat
eListener { | 35 public class InvalidationController implements ApplicationStatus.ApplicationStat
eListener { |
| 36 private static final String TAG = "cr_invalidation"; |
| 37 |
34 /** | 38 /** |
35 * Timer which can be paused. When the timer is paused, the execution of its
scheduled task is | 39 * Timer which can be paused. When the timer is paused, the execution of its
scheduled task is |
36 * delayed till the timer is resumed. | 40 * delayed till the timer is resumed. |
37 */ | 41 */ |
38 private static class Timer { | 42 private static class Timer { |
39 private Handler mHandler; | 43 private Handler mHandler; |
40 | 44 |
41 /** | 45 /** |
42 * Runnable which is added to the handler's message queue. | 46 * Runnable which is added to the handler's message queue. |
43 */ | 47 */ |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 typesToRegister.remove(ModelType.SESSIONS); | 191 typesToRegister.remove(ModelType.SESSIONS); |
188 typesToRegister.remove(ModelType.FAVICON_TRACKING); | 192 typesToRegister.remove(ModelType.FAVICON_TRACKING); |
189 typesToRegister.remove(ModelType.FAVICON_IMAGES); | 193 typesToRegister.remove(ModelType.FAVICON_IMAGES); |
190 } | 194 } |
191 | 195 |
192 Intent registerIntent = InvalidationIntentProtocol.createRegisterIntent( | 196 Intent registerIntent = InvalidationIntentProtocol.createRegisterIntent( |
193 ChromeSigninController.get(mContext).getSignedInUser(), | 197 ChromeSigninController.get(mContext).getSignedInUser(), |
194 typesToRegister); | 198 typesToRegister); |
195 registerIntent.setClass( | 199 registerIntent.setClass( |
196 mContext, InvalidationClientService.getRegisteredClass()); | 200 mContext, InvalidationClientService.getRegisteredClass()); |
| 201 |
| 202 if (shouldRestrictBackgroundServices()) { |
| 203 Log.e(TAG, "Failed to register types"); |
| 204 return; |
| 205 } |
197 mContext.startService(registerIntent); | 206 mContext.startService(registerIntent); |
198 } | 207 } |
199 | 208 |
200 /** | 209 /** |
201 * Registers for Google Cloud Messaging (GCM) for Invalidations. | 210 * Registers for Google Cloud Messaging (GCM) for Invalidations. |
202 */ | 211 */ |
203 private void ensureGcmIsInitialized() { | 212 private void ensureGcmIsInitialized() { |
204 if (mGcmInitialized) return; | 213 if (mGcmInitialized) return; |
205 mGcmInitialized = true; | 214 mGcmInitialized = true; |
206 new AsyncTask<Void, Void, Void>() { | 215 new AsyncTask<Void, Void, Void>() { |
207 @Override | 216 @Override |
208 protected Void doInBackground(Void... arg0) { | 217 protected Void doInBackground(Void... arg0) { |
209 AndroidGcmController.get(mContext).initializeGcm(mUseGcmUpstream
); | 218 AndroidGcmController.get(mContext).initializeGcm(mUseGcmUpstream
); |
210 return null; | 219 return null; |
211 } | 220 } |
212 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); | 221 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
213 } | 222 } |
214 | 223 |
215 @VisibleForTesting | 224 @VisibleForTesting |
216 public boolean isGcmInitialized() { | 225 public boolean isGcmInitialized() { |
217 return mGcmInitialized; | 226 return mGcmInitialized; |
218 } | 227 } |
219 | 228 |
220 /** | 229 /** |
221 * Starts the invalidation client without updating the registered invalidati
on types. | 230 * Starts the invalidation client without updating the registered invalidati
on types. |
222 */ | 231 */ |
223 private void start() { | 232 private void start() { |
| 233 if (shouldRestrictBackgroundServices()) { |
| 234 Log.e(TAG, "Failed to start invalidation client"); |
| 235 return; |
| 236 } |
| 237 |
224 mStarted = true; | 238 mStarted = true; |
225 mEnableSessionInvalidationsTimer.resume(); | 239 mEnableSessionInvalidationsTimer.resume(); |
226 Intent intent = new Intent( | 240 Intent intent = new Intent( |
227 mContext, InvalidationClientService.getRegisteredClass()); | 241 mContext, InvalidationClientService.getRegisteredClass()); |
228 mContext.startService(intent); | 242 mContext.startService(intent); |
229 } | 243 } |
230 | 244 |
231 /** | 245 /** |
232 * Stops the invalidation client. | 246 * Stops the invalidation client. |
233 */ | 247 */ |
234 public void stop() { | 248 public void stop() { |
| 249 if (shouldRestrictBackgroundServices()) { |
| 250 Log.e(TAG, "Failed to stop invalidation client"); |
| 251 return; |
| 252 } |
| 253 |
235 mStarted = false; | 254 mStarted = false; |
236 mEnableSessionInvalidationsTimer.pause(); | 255 mEnableSessionInvalidationsTimer.pause(); |
237 Intent intent = new Intent( | 256 Intent intent = new Intent( |
238 mContext, InvalidationClientService.getRegisteredClass()); | 257 mContext, InvalidationClientService.getRegisteredClass()); |
239 intent.putExtra(InvalidationIntentProtocol.EXTRA_STOP, true); | 258 intent.putExtra(InvalidationIntentProtocol.EXTRA_STOP, true); |
240 mContext.startService(intent); | 259 mContext.startService(intent); |
241 } | 260 } |
242 | 261 |
243 /** | 262 /** |
244 * Returns whether the invalidation client has been started. | 263 * Returns whether the invalidation client has been started. |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 if (appContext == null) throw new NullPointerException("Unable to get ap
plication context"); | 351 if (appContext == null) throw new NullPointerException("Unable to get ap
plication context"); |
333 mContext = appContext; | 352 mContext = appContext; |
334 mUseGcmUpstream = canUseGcmUpstream; | 353 mUseGcmUpstream = canUseGcmUpstream; |
335 mCanDisableSessionInvalidations = canDisableSessionInvalidations; | 354 mCanDisableSessionInvalidations = canDisableSessionInvalidations; |
336 mSessionInvalidationsEnabled = !mCanDisableSessionInvalidations; | 355 mSessionInvalidationsEnabled = !mCanDisableSessionInvalidations; |
337 mEnableSessionInvalidationsTimer = new Timer(); | 356 mEnableSessionInvalidationsTimer = new Timer(); |
338 | 357 |
339 ApplicationStatus.registerApplicationStateListener(this); | 358 ApplicationStatus.registerApplicationStateListener(this); |
340 } | 359 } |
341 | 360 |
| 361 private boolean shouldRestrictBackgroundServices() { |
| 362 // Restricts the use of background services when not in foreground. See
crbug.com/680812. |
| 363 return BuildInfo.isGreaterThanN() && !ApplicationStatus.hasVisibleActivi
ties(); |
| 364 } |
| 365 |
342 @Override | 366 @Override |
343 public void onApplicationStateChange(int newState) { | 367 public void onApplicationStateChange(int newState) { |
344 // The isSyncEnabled() check is used to check whether the InvalidationCo
ntroller would be | 368 // The isSyncEnabled() check is used to check whether the InvalidationCo
ntroller would be |
345 // started if it did not stop itself when the application is paused. | 369 // started if it did not stop itself when the application is paused. |
346 if (AndroidSyncSettings.isSyncEnabled(mContext)) { | 370 if (AndroidSyncSettings.isSyncEnabled(mContext)) { |
347 if (newState == ApplicationState.HAS_RUNNING_ACTIVITIES) { | 371 if (newState == ApplicationState.HAS_RUNNING_ACTIVITIES) { |
348 start(); | 372 start(); |
349 } else if (newState == ApplicationState.HAS_PAUSED_ACTIVITIES) { | 373 } else if (newState == ApplicationState.HAS_PAUSED_ACTIVITIES) { |
350 stop(); | 374 stop(); |
351 } | 375 } |
352 } | 376 } |
353 } | 377 } |
354 } | 378 } |
OLD | NEW |