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.components.invalidation; | 5 package org.chromium.components.invalidation; |
6 | 6 |
7 import android.accounts.Account; | 7 import android.accounts.Account; |
8 import android.app.PendingIntent; | 8 import android.app.PendingIntent; |
9 import android.content.ContentResolver; | 9 import android.content.ContentResolver; |
10 import android.content.Context; | 10 import android.content.Context; |
11 import android.content.Intent; | 11 import android.content.Intent; |
12 import android.content.pm.ApplicationInfo; | 12 import android.content.pm.ApplicationInfo; |
13 import android.content.pm.PackageManager; | 13 import android.content.pm.PackageManager; |
14 import android.content.pm.PackageManager.NameNotFoundException; | 14 import android.content.pm.PackageManager.NameNotFoundException; |
15 import android.os.Bundle; | 15 import android.os.Bundle; |
16 | 16 |
17 import com.google.ipc.invalidation.external.client.InvalidationListener.Registra tionState; | 17 import com.google.ipc.invalidation.external.client.InvalidationListener.Registra tionState; |
18 import com.google.ipc.invalidation.external.client.contrib.AndroidListener; | 18 import com.google.ipc.invalidation.external.client.contrib.AndroidListener; |
19 import com.google.ipc.invalidation.external.client.types.ErrorInfo; | 19 import com.google.ipc.invalidation.external.client.types.ErrorInfo; |
20 import com.google.ipc.invalidation.external.client.types.Invalidation; | 20 import com.google.ipc.invalidation.external.client.types.Invalidation; |
21 import com.google.ipc.invalidation.external.client.types.ObjectId; | 21 import com.google.ipc.invalidation.external.client.types.ObjectId; |
22 import com.google.protos.ipc.invalidation.Types.ClientType; | 22 import com.google.protos.ipc.invalidation.Types.ClientType; |
23 | 23 |
24 import org.chromium.base.ApplicationStatus; | 24 import org.chromium.base.ApplicationStatus; |
25 import org.chromium.base.BuildInfo; | |
25 import org.chromium.base.CollectionUtil; | 26 import org.chromium.base.CollectionUtil; |
26 import org.chromium.base.ContextUtils; | 27 import org.chromium.base.ContextUtils; |
27 import org.chromium.base.Log; | 28 import org.chromium.base.Log; |
28 import org.chromium.base.VisibleForTesting; | 29 import org.chromium.base.VisibleForTesting; |
29 import org.chromium.components.signin.AccountManagerHelper; | 30 import org.chromium.components.signin.AccountManagerHelper; |
30 import org.chromium.components.signin.ChromeSigninController; | 31 import org.chromium.components.signin.ChromeSigninController; |
31 import org.chromium.components.sync.AndroidSyncSettings; | 32 import org.chromium.components.sync.AndroidSyncSettings; |
32 import org.chromium.components.sync.ModelTypeHelper; | 33 import org.chromium.components.sync.ModelTypeHelper; |
33 import org.chromium.components.sync.SyncConstants; | 34 import org.chromium.components.sync.SyncConstants; |
34 import org.chromium.components.sync.notifier.InvalidationClientNameProvider; | 35 import org.chromium.components.sync.notifier.InvalidationClientNameProvider; |
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
310 } | 311 } |
311 | 312 |
312 /** | 313 /** |
313 * Starts a new client, destroying any existing client. {@code owningAccount } is the account | 314 * Starts a new client, destroying any existing client. {@code owningAccount } is the account |
314 * of the user for which the client is being created; it will be persisted u sing | 315 * of the user for which the client is being created; it will be persisted u sing |
315 * {@link InvalidationPreferences#setAccount}. | 316 * {@link InvalidationPreferences#setAccount}. |
316 */ | 317 */ |
317 private void startClient() { | 318 private void startClient() { |
318 byte[] clientName = InvalidationClientNameProvider.get().getInvalidatorC lientName(); | 319 byte[] clientName = InvalidationClientNameProvider.get().getInvalidatorC lientName(); |
319 Intent startIntent = AndroidListener.createStartIntent(this, CLIENT_TYPE , clientName); | 320 Intent startIntent = AndroidListener.createStartIntent(this, CLIENT_TYPE , clientName); |
321 | |
322 if (shouldRestrictBackgroundServices()) { | |
323 Log.e(TAG, "Failed to start client"); | |
324 return; | |
325 } | |
320 startService(startIntent); | 326 startService(startIntent); |
321 setIsClientStarted(true); | 327 setIsClientStarted(true); |
322 } | 328 } |
323 | 329 |
324 /** Stops the notification client. */ | 330 /** Stops the notification client. */ |
325 private void stopClient() { | 331 private void stopClient() { |
332 if (shouldRestrictBackgroundServices()) { | |
333 Log.e(TAG, "Failed to stop client"); | |
334 return; | |
335 } | |
326 startService(AndroidListener.createStopIntent(this)); | 336 startService(AndroidListener.createStopIntent(this)); |
327 setIsClientStarted(false); | 337 setIsClientStarted(false); |
328 setClientId(null); | 338 setClientId(null); |
329 } | 339 } |
330 | 340 |
331 /** Sets the saved sync account in {@link InvalidationPreferences} to {@code owningAccount}. */ | 341 /** Sets the saved sync account in {@link InvalidationPreferences} to {@code owningAccount}. */ |
332 private void setAccount(Account owningAccount) { | 342 private void setAccount(Account owningAccount) { |
333 InvalidationPreferences invPrefs = new InvalidationPreferences(); | 343 InvalidationPreferences invPrefs = new InvalidationPreferences(); |
334 EditContext editContext = invPrefs.edit(); | 344 EditContext editContext = invPrefs.edit(); |
335 invPrefs.setAccount(editContext, owningAccount); | 345 invPrefs.setAccount(editContext, owningAccount); |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
533 return "oauth2:" + SyncConstants.CHROME_SYNC_OAUTH2_SCOPE; | 543 return "oauth2:" + SyncConstants.CHROME_SYNC_OAUTH2_SCOPE; |
534 } | 544 } |
535 | 545 |
536 private static void setClientId(byte[] clientId) { | 546 private static void setClientId(byte[] clientId) { |
537 sClientId = clientId; | 547 sClientId = clientId; |
538 } | 548 } |
539 | 549 |
540 private static void setIsClientStarted(boolean isStarted) { | 550 private static void setIsClientStarted(boolean isStarted) { |
541 sIsClientStarted = isStarted; | 551 sIsClientStarted = isStarted; |
542 } | 552 } |
553 | |
554 private boolean shouldRestrictBackgroundServices() { | |
555 // Restricts the use of background services when not in foreground. See crbug.com/680812. | |
556 return BuildInfo.isGreaterThanN() && !isChromeInForeground(); | |
Nicolas Zea
2017/03/30 18:39:35
Looks like this will actually return true when the
Khushal
2017/03/30 19:13:16
This looks like an error. It should be isAtLeastO.
| |
557 } | |
543 } | 558 } |
OLD | NEW |