| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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.content.Context; | |
| 9 import android.content.Intent; | 8 import android.content.Intent; |
| 10 | 9 |
| 11 import com.google.protos.ipc.invalidation.Types; | 10 import com.google.protos.ipc.invalidation.Types; |
| 12 | 11 |
| 13 import org.chromium.base.BuildInfo; | 12 import org.chromium.base.BuildInfo; |
| 13 import org.chromium.base.ContextUtils; |
| 14 import org.chromium.base.Log; | 14 import org.chromium.base.Log; |
| 15 import org.chromium.base.ThreadUtils; | 15 import org.chromium.base.ThreadUtils; |
| 16 import org.chromium.base.VisibleForTesting; | 16 import org.chromium.base.VisibleForTesting; |
| 17 import org.chromium.base.annotations.CalledByNative; | 17 import org.chromium.base.annotations.CalledByNative; |
| 18 import org.chromium.base.annotations.JNINamespace; | 18 import org.chromium.base.annotations.JNINamespace; |
| 19 import org.chromium.components.sync.notifier.InvalidationClientNameProvider; | 19 import org.chromium.components.sync.notifier.InvalidationClientNameProvider; |
| 20 import org.chromium.components.sync.notifier.InvalidationIntentProtocol; | 20 import org.chromium.components.sync.notifier.InvalidationIntentProtocol; |
| 21 import org.chromium.components.sync.notifier.InvalidationPreferences; | 21 import org.chromium.components.sync.notifier.InvalidationPreferences; |
| 22 | 22 |
| 23 /** | 23 /** |
| 24 * Wrapper for invalidations::InvalidationServiceAndroid. | 24 * Wrapper for invalidations::InvalidationServiceAndroid. |
| 25 * | 25 * |
| 26 * Serves as the bridge between Java and C++ for the invalidations component. | 26 * Serves as the bridge between Java and C++ for the invalidations component. |
| 27 */ | 27 */ |
| 28 @JNINamespace("invalidation") | 28 @JNINamespace("invalidation") |
| 29 public class InvalidationService { | 29 public class InvalidationService { |
| 30 private final Context mContext; | |
| 31 | |
| 32 private final long mNativeInvalidationServiceAndroid; | 30 private final long mNativeInvalidationServiceAndroid; |
| 33 | 31 |
| 34 private static final String TAG = "cr_invalidation"; | 32 private static final String TAG = "cr_invalidation"; |
| 35 | 33 |
| 36 private InvalidationService(Context context, long nativeInvalidationServiceA
ndroid) { | 34 private InvalidationService(long nativeInvalidationServiceAndroid) { |
| 37 mContext = context.getApplicationContext(); | |
| 38 if (mContext == null) { | |
| 39 throw new NullPointerException("mContext is null."); | |
| 40 } | |
| 41 mNativeInvalidationServiceAndroid = nativeInvalidationServiceAndroid; | 35 mNativeInvalidationServiceAndroid = nativeInvalidationServiceAndroid; |
| 42 } | 36 } |
| 43 | 37 |
| 44 public void notifyInvalidationToNativeChrome( | 38 public void notifyInvalidationToNativeChrome( |
| 45 int objectSource, String objectId, long version, String payload) { | 39 int objectSource, String objectId, long version, String payload) { |
| 46 ThreadUtils.assertOnUiThread(); | 40 ThreadUtils.assertOnUiThread(); |
| 47 nativeInvalidate( | 41 nativeInvalidate( |
| 48 mNativeInvalidationServiceAndroid, objectSource, objectId, versi
on, payload); | 42 mNativeInvalidationServiceAndroid, objectSource, objectId, versi
on, payload); |
| 49 } | 43 } |
| 50 | 44 |
| 51 public void requestSyncFromNativeChromeForAllTypes() { | 45 public void requestSyncFromNativeChromeForAllTypes() { |
| 52 notifyInvalidationToNativeChrome(Types.ObjectSource.CHROME_SYNC, null, 0
L, null); | 46 notifyInvalidationToNativeChrome(Types.ObjectSource.CHROME_SYNC, null, 0
L, null); |
| 53 } | 47 } |
| 54 | 48 |
| 55 @CalledByNative | 49 @CalledByNative |
| 56 private static InvalidationService create( | 50 private static InvalidationService create(long nativeInvalidationServiceAndr
oid) { |
| 57 Context context, long nativeInvalidationServiceAndroid) { | |
| 58 ThreadUtils.assertOnUiThread(); | 51 ThreadUtils.assertOnUiThread(); |
| 59 return new InvalidationService(context, nativeInvalidationServiceAndroid
); | 52 return new InvalidationService(nativeInvalidationServiceAndroid); |
| 60 } | 53 } |
| 61 | 54 |
| 62 /** | 55 /** |
| 63 * Sets object ids for which the client should register for notification. Th
is is intended for | 56 * Sets object ids for which the client should register for notification. Th
is is intended for |
| 64 * registering non-Sync types; Sync types are registered with {@code setRegi
steredTypes}. | 57 * registering non-Sync types; Sync types are registered with {@code setRegi
steredTypes}. |
| 65 * | 58 * |
| 66 * @param objectSources The sources of the objects. | 59 * @param objectSources The sources of the objects. |
| 67 * @param objectNames The names of the objects. | 60 * @param objectNames The names of the objects. |
| 68 */ | 61 */ |
| 69 @VisibleForTesting | 62 @VisibleForTesting |
| 70 @CalledByNative | 63 @CalledByNative |
| 71 public void setRegisteredObjectIds(int[] objectSources, String[] objectNames
) { | 64 public void setRegisteredObjectIds(int[] objectSources, String[] objectNames
) { |
| 72 InvalidationPreferences invalidationPreferences = new InvalidationPrefer
ences(); | 65 InvalidationPreferences invalidationPreferences = new InvalidationPrefer
ences(); |
| 73 Account account = invalidationPreferences.getSavedSyncedAccount(); | 66 Account account = invalidationPreferences.getSavedSyncedAccount(); |
| 74 Intent registerIntent = InvalidationIntentProtocol.createRegisterIntent( | 67 Intent registerIntent = InvalidationIntentProtocol.createRegisterIntent( |
| 75 account, objectSources, objectNames); | 68 account, objectSources, objectNames); |
| 76 registerIntent.setClass( | 69 registerIntent.setClass(ContextUtils.getApplicationContext(), |
| 77 mContext, InvalidationClientService.getRegisteredClass()); | 70 InvalidationClientService.getRegisteredClass()); |
| 78 startServiceIfPossible(registerIntent); | 71 startServiceIfPossible(registerIntent); |
| 79 } | 72 } |
| 80 | 73 |
| 81 private void startServiceIfPossible(Intent intent) { | 74 private void startServiceIfPossible(Intent intent) { |
| 82 // The use of background services is restricted when the application is
not in foreground | 75 // The use of background services is restricted when the application is
not in foreground |
| 83 // for O. See crbug.com/680812. | 76 // for O. See crbug.com/680812. |
| 84 if (BuildInfo.isAtLeastO()) { | 77 if (BuildInfo.isAtLeastO()) { |
| 85 try { | 78 try { |
| 86 mContext.startService(intent); | 79 ContextUtils.getApplicationContext().startService(intent); |
| 87 } catch (IllegalStateException exception) { | 80 } catch (IllegalStateException exception) { |
| 88 Log.e(TAG, "Failed to start service from exception: ", exception
); | 81 Log.e(TAG, "Failed to start service from exception: ", exception
); |
| 89 } | 82 } |
| 90 } else { | 83 } else { |
| 91 mContext.startService(intent); | 84 ContextUtils.getApplicationContext().startService(intent); |
| 92 } | 85 } |
| 93 } | 86 } |
| 94 | 87 |
| 95 /** | 88 /** |
| 96 * Fetches the Invalidator client name. | 89 * Fetches the Invalidator client name. |
| 97 * | 90 * |
| 98 * Note that there is a naming discrepancy here. In C++, we refer to the in
validation client | 91 * Note that there is a naming discrepancy here. In C++, we refer to the in
validation client |
| 99 * identifier that is unique for every invalidation client instance in an ac
count as the client | 92 * identifier that is unique for every invalidation client instance in an ac
count as the client |
| 100 * ID. In Java, we call it the client name. | 93 * ID. In Java, we call it the client name. |
| 101 */ | 94 */ |
| 102 @CalledByNative | 95 @CalledByNative |
| 103 private byte[] getInvalidatorClientId() { | 96 private byte[] getInvalidatorClientId() { |
| 104 return InvalidationClientNameProvider.get().getInvalidatorClientName(); | 97 return InvalidationClientNameProvider.get().getInvalidatorClientName(); |
| 105 } | 98 } |
| 106 | 99 |
| 107 private native void nativeInvalidate(long nativeInvalidationServiceAndroid,
int objectSource, | 100 private native void nativeInvalidate(long nativeInvalidationServiceAndroid,
int objectSource, |
| 108 String objectId, long version, String payload); | 101 String objectId, long version, String payload); |
| 109 } | 102 } |
| OLD | NEW |