Commit 193e685d authored by Mohamed Amir Yosef's avatar Mohamed Amir Yosef Committed by Commit Bot

[FCM] Store subscription laziness information in SharedPreferences

This CL adds the functionality to store the ids of lazy subscriptions
in SharedPeferences. Later CL will consume this information to decide
whether the FCM message should be deferred until there are visible
activities or not.

Bug: 882887
Change-Id: Ic28dbfeb2b77020cd9b5e02b79f055e477d55aed
Reviewed-on: https://chromium-review.googlesource.com/1221532Reviewed-by: default avatarPeter Conn <peconn@chromium.org>
Reviewed-by: default avatarKenneth Russell <kbr@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Commit-Queue: Mohamed Amir Yosef <mamir@chromium.org>
Cr-Commit-Position: refs/heads/master@{#592774}
parent 24627deb
...@@ -39,3 +39,12 @@ android_library("instance_id_driver_test_support_java") { ...@@ -39,3 +39,12 @@ android_library("instance_id_driver_test_support_java") {
java_files = [ "javatests/src/org/chromium/components/gcm_driver/instance_id/FakeInstanceIDWithSubtype.java" ] java_files = [ "javatests/src/org/chromium/components/gcm_driver/instance_id/FakeInstanceIDWithSubtype.java" ]
} }
junit_binary("components_instance_id_junit_tests") {
java_files = [ "junit/src/org/chromium/components/gcm_driver/instance_id/InstanceIDBridgeTest.java" ]
deps = [
":instance_id_driver_java",
"//base:base_java",
"//base:base_junit_test_support",
]
}
...@@ -4,13 +4,19 @@ ...@@ -4,13 +4,19 @@
package org.chromium.components.gcm_driver.instance_id; package org.chromium.components.gcm_driver.instance_id;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import org.chromium.base.AsyncTask; import org.chromium.base.AsyncTask;
import org.chromium.base.ContextUtils;
import org.chromium.base.VisibleForTesting;
import org.chromium.base.annotations.CalledByNative; import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace; import org.chromium.base.annotations.JNINamespace;
import java.io.IOException; import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
/** /**
...@@ -19,6 +25,7 @@ import java.util.concurrent.ExecutionException; ...@@ -19,6 +25,7 @@ import java.util.concurrent.ExecutionException;
*/ */
@JNINamespace("instance_id") @JNINamespace("instance_id")
public class InstanceIDBridge { public class InstanceIDBridge {
private static final String FCM_LAZY_SUBSCRIPTIONS = "fcm_lazy_subscriptions";
private final String mSubtype; private final String mSubtype;
private long mNativeInstanceIDAndroid; private long mNativeInstanceIDAndroid;
/** /**
...@@ -34,6 +41,37 @@ public class InstanceIDBridge { ...@@ -34,6 +41,37 @@ public class InstanceIDBridge {
mNativeInstanceIDAndroid = nativeInstanceIDAndroid; mNativeInstanceIDAndroid = nativeInstanceIDAndroid;
} }
/**
* Stores the information about lazy subscriptions in SharedPreferences.
*/
@VisibleForTesting
public void storeLazinessInformation(final String authorizedEntity, boolean isLazy) {
if (isLazy) {
SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences();
Set<String> lazyIds = new HashSet<>(
sharedPrefs.getStringSet(FCM_LAZY_SUBSCRIPTIONS, Collections.emptySet()));
lazyIds.add(buildSubscriptionUniqueId(mSubtype, authorizedEntity));
sharedPrefs.edit().putStringSet(FCM_LAZY_SUBSCRIPTIONS, lazyIds).apply();
}
// TODO(https://crbug.com/882887): Check if that
// subscription was marked lazy before and handle the change
// accordingly.
}
/**
* Returns whether the subscription with the |appId| and |senderId| is lazy.
*/
public static boolean isSubscriptionLazy(final String appId, final String senderId) {
SharedPreferences sharedPrefs = ContextUtils.getAppSharedPreferences();
Set<String> lazyIds = new HashSet<>(
sharedPrefs.getStringSet(FCM_LAZY_SUBSCRIPTIONS, Collections.emptySet()));
return lazyIds.contains(buildSubscriptionUniqueId(appId, senderId));
}
private static String buildSubscriptionUniqueId(final String appId, final String senderId) {
return appId + senderId;
}
/** /**
* Returns a wrapped {@link InstanceIDWithSubtype}. Multiple InstanceIDBridge instances may * Returns a wrapped {@link InstanceIDWithSubtype}. Multiple InstanceIDBridge instances may
* share an underlying InstanceIDWithSubtype. * share an underlying InstanceIDWithSubtype.
...@@ -106,12 +144,12 @@ public class InstanceIDBridge { ...@@ -106,12 +144,12 @@ public class InstanceIDBridge {
for (int i = 0; i < extrasStrings.length; i += 2) { for (int i = 0; i < extrasStrings.length; i += 2) {
extras.putString(extrasStrings[i], extrasStrings[i + 1]); extras.putString(extrasStrings[i], extrasStrings[i + 1]);
} }
new BridgeAsyncTask<String>() { new BridgeAsyncTask<String>() {
@Override @Override
protected String doBackgroundWork() { protected String doBackgroundWork() {
try { try {
// TODO(https://crbug.com/882887): Mark this subscription as lazy and store storeLazinessInformation(authorizedEntity, isLazy);
// that information in SharedPreferences.
return mInstanceID.getToken(authorizedEntity, scope, extras); return mInstanceID.getToken(authorizedEntity, scope, extras);
} catch (IOException ex) { } catch (IOException ex) {
return ""; return "";
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.components.gcm_driver.instance_id;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.chromium.base.test.BaseRobolectricTestRunner;
/**
* Unit tests for InstanceIDBridge.
*/
@RunWith(BaseRobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class InstanceIDBridgeTest {
/**
* Tests that lazy subscriptions are stored.
*/
@Test
public void testMarkSubscriptionAsLazy() {
final String appId = "app_id";
final String senderId = "sender_id";
InstanceIDBridge instanceIDBridge =
InstanceIDBridge.create(/*nativeInstanceIDAndroid=*/0L, appId);
instanceIDBridge.storeLazinessInformation(senderId, true);
assertEquals(true, InstanceIDBridge.isSubscriptionLazy(appId, senderId));
}
/**
* Tests that unlazy subscriptions are stored.
*/
@Test
public void testMarkSubscriptionAsNotLazy() {
final String appId = "app_id";
final String senderId = "sender_id";
InstanceIDBridge instanceIDBridge =
InstanceIDBridge.create(/*nativeInstanceIDAndroid=*/0L, senderId);
instanceIDBridge.storeLazinessInformation(senderId, false);
assertEquals(false, InstanceIDBridge.isSubscriptionLazy(appId, senderId));
}
/**
* Tests subscriptions are not lazy be default.
*/
@Test
public void testDefaultSubscriptionNotLazy() {
final String appId = "app_id";
final String senderId = "sender_id";
assertEquals(false, InstanceIDBridge.isSubscriptionLazy(appId, senderId));
}
}
...@@ -3224,6 +3224,9 @@ ...@@ -3224,6 +3224,9 @@
{ {
"test": "components_gcm_driver_junit_tests" "test": "components_gcm_driver_junit_tests"
}, },
{
"test": "components_instance_id_junit_tests"
},
{ {
"test": "components_invalidation_impl_junit_tests" "test": "components_invalidation_impl_junit_tests"
}, },
...@@ -20726,6 +20729,9 @@ ...@@ -20726,6 +20729,9 @@
{ {
"test": "components_gcm_driver_junit_tests" "test": "components_gcm_driver_junit_tests"
}, },
{
"test": "components_instance_id_junit_tests"
},
{ {
"test": "components_invalidation_impl_junit_tests" "test": "components_invalidation_impl_junit_tests"
}, },
......
...@@ -7286,6 +7286,9 @@ ...@@ -7286,6 +7286,9 @@
{ {
"test": "components_gcm_driver_junit_tests" "test": "components_gcm_driver_junit_tests"
}, },
{
"test": "components_instance_id_junit_tests"
},
{ {
"test": "components_invalidation_impl_junit_tests" "test": "components_invalidation_impl_junit_tests"
}, },
......
...@@ -383,6 +383,10 @@ ...@@ -383,6 +383,10 @@
"label": "//components/gcm_driver/android:components_gcm_driver_junit_tests", "label": "//components/gcm_driver/android:components_gcm_driver_junit_tests",
"type": "junit_test", "type": "junit_test",
}, },
"components_instance_id_junit_tests": {
"label": "//components/gcm_driver/instance_id/android:components_instance_id_junit_tests",
"type": "junit_test",
},
"components_invalidation_impl_junit_tests": { "components_invalidation_impl_junit_tests": {
"label": "//components/invalidation/impl:components_invalidation_impl_junit_tests", "label": "//components/invalidation/impl:components_invalidation_impl_junit_tests",
"type": "junit_test", "type": "junit_test",
......
...@@ -117,6 +117,7 @@ SKIP_GN_ISOLATE_MAP_TARGETS = { ...@@ -117,6 +117,7 @@ SKIP_GN_ISOLATE_MAP_TARGETS = {
'chrome_junit_tests', 'chrome_junit_tests',
'components_background_task_scheduler_junit_tests', 'components_background_task_scheduler_junit_tests',
'components_gcm_driver_junit_tests', 'components_gcm_driver_junit_tests',
'components_instance_id_junit_tests',
'components_invalidation_impl_junit_tests', 'components_invalidation_impl_junit_tests',
'components_policy_junit_tests', 'components_policy_junit_tests',
'components_variations_junit_tests', 'components_variations_junit_tests',
......
...@@ -649,6 +649,7 @@ ...@@ -649,6 +649,7 @@
'chrome_junit_tests': {}, 'chrome_junit_tests': {},
'components_background_task_scheduler_junit_tests': {}, 'components_background_task_scheduler_junit_tests': {},
'components_gcm_driver_junit_tests': {}, 'components_gcm_driver_junit_tests': {},
'components_instance_id_junit_tests': {},
'components_invalidation_impl_junit_tests': {}, 'components_invalidation_impl_junit_tests': {},
'components_policy_junit_tests': {}, 'components_policy_junit_tests': {},
'components_signin_junit_tests': {}, 'components_signin_junit_tests': {},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment