Skip to content

Commit

Permalink
Create a new system for fetching the intial batch of messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed May 29, 2020
1 parent d2bf539 commit a299baf
Show file tree
Hide file tree
Showing 17 changed files with 521 additions and 116 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Expand Up @@ -493,7 +493,7 @@
<service android:enabled="true" android:name="org.thoughtcrime.securesms.service.WebRtcCallService"/>
<service android:enabled="true" android:name=".service.ApplicationMigrationService"/>
<service android:enabled="true" android:exported="false" android:name=".service.KeyCachingService"/>
<service android:enabled="true" android:name=".service.IncomingMessageObserver$ForegroundService"/>
<service android:enabled="true" android:name=".messages.IncomingMessageObserver$ForegroundService"/>

<service android:name=".service.QuickResponseService"
android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
Expand Down
Expand Up @@ -50,6 +50,7 @@
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.logging.PersistentLogger;
import org.thoughtcrime.securesms.logging.SignalUncaughtExceptionHandler;
import org.thoughtcrime.securesms.messages.InitialMessageRetriever;
import org.thoughtcrime.securesms.migrations.ApplicationMigrations;
import org.thoughtcrime.securesms.notifications.MessageNotifier;
import org.thoughtcrime.securesms.notifications.NotificationChannels;
Expand All @@ -60,7 +61,7 @@
import org.thoughtcrime.securesms.ringrtc.RingRtcLogger;
import org.thoughtcrime.securesms.service.DirectoryRefreshListener;
import org.thoughtcrime.securesms.service.ExpiringMessageManager;
import org.thoughtcrime.securesms.service.IncomingMessageObserver;
import org.thoughtcrime.securesms.messages.IncomingMessageObserver;
import org.thoughtcrime.securesms.service.KeyCachingService;
import org.thoughtcrime.securesms.service.LocalBackupListener;
import org.thoughtcrime.securesms.service.RotateSenderCertificateListener;
Expand Down Expand Up @@ -153,6 +154,7 @@ public void onStart(@NonNull LifecycleOwner owner) {
KeyCachingService.onAppForegrounded(this);
ApplicationDependencies.getFrameRateTracker().begin();
ApplicationDependencies.getMegaphoneRepository().onAppForegrounded();
catchUpOnMessages();
}

@Override
Expand Down Expand Up @@ -377,6 +379,36 @@ private void initializeCleanup() {
});
}

private void catchUpOnMessages() {
InitialMessageRetriever retriever = ApplicationDependencies.getInitialMessageRetriever();

if (retriever.isCaughtUp()) {
return;
}

SignalExecutors.UNBOUNDED.execute(() -> {
long startTime = System.currentTimeMillis();

switch (retriever.begin(TimeUnit.SECONDS.toMillis(60))) {
case SUCCESS:
Log.i(TAG, "Successfully caught up on messages. " + (System.currentTimeMillis() - startTime) + " ms");
break;
case FAILURE_TIMEOUT:
Log.w(TAG, "Did not finish catching up due to a timeout. " + (System.currentTimeMillis() - startTime) + " ms");
break;
case FAILURE_ERROR:
Log.w(TAG, "Did not finish catching up due to an error. " + (System.currentTimeMillis() - startTime) + " ms");
break;
case SKIPPED_ALREADY_CAUGHT_UP:
Log.i(TAG, "Already caught up. " + (System.currentTimeMillis() - startTime) + " ms");
break;
case SKIPPED_ALREADY_RUNNING:
Log.i(TAG, "Already in the process of catching up. " + (System.currentTimeMillis() - startTime) + " ms");
break;
}
});
}

@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(DynamicLanguageContextWrapper.updateContext(base, TextSecurePreferences.getLanguage(base)));
Expand Down
Expand Up @@ -5,17 +5,18 @@
import androidx.annotation.NonNull;

import org.thoughtcrime.securesms.BuildConfig;
import org.thoughtcrime.securesms.IncomingMessageProcessor;
import org.thoughtcrime.securesms.gcm.MessageRetriever;
import org.thoughtcrime.securesms.messages.IncomingMessageProcessor;
import org.thoughtcrime.securesms.messages.BackgroundMessageRetriever;
import org.thoughtcrime.securesms.groups.GroupsV2AuthorizationMemoryValueCache;
import org.thoughtcrime.securesms.groups.v2.processing.GroupsV2StateProcessor;
import org.thoughtcrime.securesms.jobmanager.JobManager;
import org.thoughtcrime.securesms.keyvalue.KeyValueStore;
import org.thoughtcrime.securesms.keyvalue.SignalStore;
import org.thoughtcrime.securesms.megaphone.MegaphoneRepository;
import org.thoughtcrime.securesms.messages.InitialMessageRetriever;
import org.thoughtcrime.securesms.push.SignalServiceNetworkAccess;
import org.thoughtcrime.securesms.recipients.LiveRecipientCache;
import org.thoughtcrime.securesms.service.IncomingMessageObserver;
import org.thoughtcrime.securesms.messages.IncomingMessageObserver;
import org.thoughtcrime.securesms.util.EarlyMessageCache;
import org.thoughtcrime.securesms.util.FeatureFlags;
import org.thoughtcrime.securesms.util.FrameRateTracker;
Expand Down Expand Up @@ -45,7 +46,7 @@ public class ApplicationDependencies {
private static SignalServiceMessageSender messageSender;
private static SignalServiceMessageReceiver messageReceiver;
private static IncomingMessageProcessor incomingMessageProcessor;
private static MessageRetriever messageRetriever;
private static BackgroundMessageRetriever backgroundMessageRetriever;
private static LiveRecipientCache recipientCache;
private static JobManager jobManager;
private static FrameRateTracker frameRateTracker;
Expand All @@ -55,6 +56,7 @@ public class ApplicationDependencies {
private static GroupsV2StateProcessor groupsV2StateProcessor;
private static GroupsV2Operations groupsV2Operations;
private static EarlyMessageCache earlyMessageCache;
private static InitialMessageRetriever initialMessageRetriever;

public static synchronized void init(@NonNull Application application, @NonNull Provider provider) {
if (ApplicationDependencies.application != null || ApplicationDependencies.provider != null) {
Expand Down Expand Up @@ -164,14 +166,14 @@ public static synchronized void resetSignalServiceMessageReceiver() {
return incomingMessageProcessor;
}

public static synchronized @NonNull MessageRetriever getMessageRetriever() {
public static synchronized @NonNull BackgroundMessageRetriever getBackgroundMessageRetriever() {
assertInitialization();

if (messageRetriever == null) {
messageRetriever = provider.provideMessageRetriever();
if (backgroundMessageRetriever == null) {
backgroundMessageRetriever = provider.provideBackgroundMessageRetriever();
}

return messageRetriever;
return backgroundMessageRetriever;
}

public static synchronized @NonNull LiveRecipientCache getRecipientCache() {
Expand Down Expand Up @@ -234,6 +236,16 @@ public static synchronized void resetSignalServiceMessageReceiver() {
return earlyMessageCache;
}

public static synchronized @NonNull InitialMessageRetriever getInitialMessageRetriever() {
assertInitialization();

if (initialMessageRetriever == null) {
initialMessageRetriever = provider.provideInitialMessageRetriever();
}

return initialMessageRetriever;
}

private static void assertInitialization() {
if (application == null || provider == null) {
throw new UninitializedException();
Expand All @@ -247,13 +259,14 @@ public interface Provider {
@NonNull SignalServiceMessageReceiver provideSignalServiceMessageReceiver();
@NonNull SignalServiceNetworkAccess provideSignalServiceNetworkAccess();
@NonNull IncomingMessageProcessor provideIncomingMessageProcessor();
@NonNull MessageRetriever provideMessageRetriever();
@NonNull BackgroundMessageRetriever provideBackgroundMessageRetriever();
@NonNull LiveRecipientCache provideRecipientCache();
@NonNull JobManager provideJobManager();
@NonNull FrameRateTracker provideFrameRateTracker();
@NonNull KeyValueStore provideKeyValueStore();
@NonNull MegaphoneRepository provideMegaphoneRepository();
@NonNull EarlyMessageCache provideEarlyMessageCache();
@NonNull InitialMessageRetriever provideInitialMessageRetriever();
}

private static class UninitializedException extends IllegalStateException {
Expand Down
Expand Up @@ -7,11 +7,11 @@

import org.greenrobot.eventbus.EventBus;
import org.thoughtcrime.securesms.BuildConfig;
import org.thoughtcrime.securesms.IncomingMessageProcessor;
import org.thoughtcrime.securesms.messages.IncomingMessageProcessor;
import org.thoughtcrime.securesms.crypto.storage.SignalProtocolStoreImpl;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.events.ReminderUpdateEvent;
import org.thoughtcrime.securesms.gcm.MessageRetriever;
import org.thoughtcrime.securesms.messages.BackgroundMessageRetriever;
import org.thoughtcrime.securesms.jobmanager.JobManager;
import org.thoughtcrime.securesms.jobmanager.JobMigrator;
import org.thoughtcrime.securesms.jobmanager.impl.JsonDataSerializer;
Expand All @@ -20,10 +20,11 @@
import org.thoughtcrime.securesms.keyvalue.KeyValueStore;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.megaphone.MegaphoneRepository;
import org.thoughtcrime.securesms.messages.InitialMessageRetriever;
import org.thoughtcrime.securesms.push.SecurityEventListener;
import org.thoughtcrime.securesms.push.SignalServiceNetworkAccess;
import org.thoughtcrime.securesms.recipients.LiveRecipientCache;
import org.thoughtcrime.securesms.service.IncomingMessageObserver;
import org.thoughtcrime.securesms.messages.IncomingMessageObserver;
import org.thoughtcrime.securesms.util.AlarmSleepTimer;
import org.thoughtcrime.securesms.util.EarlyMessageCache;
import org.thoughtcrime.securesms.util.FeatureFlags;
Expand Down Expand Up @@ -111,8 +112,8 @@ public ApplicationDependencyProvider(@NonNull Application context, @NonNull Sign
}

@Override
public @NonNull MessageRetriever provideMessageRetriever() {
return new MessageRetriever();
public @NonNull BackgroundMessageRetriever provideBackgroundMessageRetriever() {
return new BackgroundMessageRetriever();
}

@Override
Expand Down Expand Up @@ -152,6 +153,11 @@ public ApplicationDependencyProvider(@NonNull Application context, @NonNull Sign
return new EarlyMessageCache();
}

@Override
public @NonNull InitialMessageRetriever provideInitialMessageRetriever() {
return new InitialMessageRetriever();
}

private static class DynamicCredentialsProvider implements CredentialsProvider {

private final Context context;
Expand Down
Expand Up @@ -12,6 +12,8 @@
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.jobs.PushNotificationReceiveJob;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.messages.BackgroundMessageRetriever;
import org.thoughtcrime.securesms.messages.RestStrategy;
import org.thoughtcrime.securesms.util.concurrent.SerialMonoLifoExecutor;
import org.thoughtcrime.securesms.util.concurrent.SignalExecutors;

Expand Down Expand Up @@ -68,8 +70,8 @@ public void onDestroy() {
}

private void fetch() {
MessageRetriever retriever = ApplicationDependencies.getMessageRetriever();
boolean success = retriever.retrieveMessages(this, new RestStrategy(), new RestStrategy());
BackgroundMessageRetriever retriever = ApplicationDependencies.getBackgroundMessageRetriever();
boolean success = retriever.retrieveMessages(this, new RestStrategy(), new RestStrategy());

if (success) {
Log.i(TAG, "Successfully retrieved messages.");
Expand Down
Expand Up @@ -11,6 +11,8 @@

import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.messages.BackgroundMessageRetriever;
import org.thoughtcrime.securesms.messages.RestStrategy;
import org.thoughtcrime.securesms.util.ServiceUtil;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.thoughtcrime.securesms.util.concurrent.SignalExecutors;
Expand Down Expand Up @@ -39,15 +41,15 @@ public static void schedule(@NonNull Context context) {
public boolean onStartJob(JobParameters params) {
Log.d(TAG, "onStartJob()");

if (MessageRetriever.shouldIgnoreFetch(this)) {
if (BackgroundMessageRetriever.shouldIgnoreFetch(this)) {
Log.i(TAG, "App is foregrounded. No need to run.");
return false;
}

SignalExecutors.UNBOUNDED.execute(() -> {
Context context = getApplicationContext();
MessageRetriever retriever = ApplicationDependencies.getMessageRetriever();
boolean success = retriever.retrieveMessages(context, new RestStrategy(), new RestStrategy());
Context context = getApplicationContext();
BackgroundMessageRetriever retriever = ApplicationDependencies.getBackgroundMessageRetriever();
boolean success = retriever.retrieveMessages(context, new RestStrategy(), new RestStrategy());

if (success) {
Log.i(TAG, "Successfully retrieved messages.");
Expand Down
Expand Up @@ -4,8 +4,8 @@
import androidx.annotation.NonNull;

import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.gcm.MessageRetriever;
import org.thoughtcrime.securesms.gcm.RestStrategy;
import org.thoughtcrime.securesms.messages.BackgroundMessageRetriever;
import org.thoughtcrime.securesms.messages.RestStrategy;
import org.thoughtcrime.securesms.jobmanager.Data;
import org.thoughtcrime.securesms.jobmanager.Job;
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
Expand Down Expand Up @@ -46,8 +46,8 @@ private PushNotificationReceiveJob(@NonNull Job.Parameters parameters) {

@Override
public void onRun() throws IOException {
MessageRetriever retriever = ApplicationDependencies.getMessageRetriever();
boolean result = retriever.retrieveMessages(context, new RestStrategy());
BackgroundMessageRetriever retriever = ApplicationDependencies.getBackgroundMessageRetriever();
boolean result = retriever.retrieveMessages(context, new RestStrategy());

if (result) {
Log.i(TAG, "Successfully pulled messages.");
Expand Down

0 comments on commit a299baf

Please sign in to comment.