Просмотр исходного кода

Nits & Wording

Use an object for passing flags towards the web client stop method
Add ResumeInfo type
Lennart Grahl 7 лет назад
Родитель
Сommit
1560646393

+ 1 - 1
public/i18n/de.json

@@ -327,6 +327,6 @@
         "SESSION_DELETED": "Die Sitzung wurde auf Ihrem Gerät gelöscht.",
         "WEBCLIENT_DISABLED": "Threema Web wurde auf Ihrem Gerät deaktiviert.",
         "SESSION_REPLACED": "Die Sitzung wurde beendet, weil Sie eine andere Sitzung gestartet haben.",
-        "SESSION_ERROR": "Die Sitzung wurde auf Grund eines Protokollfehlers beendet."
+        "SESSION_ERROR": "Die Sitzung wurde aufgrund eines Protokollfehlers beendet."
     }
 }

+ 10 - 2
src/controllers/status.ts

@@ -197,7 +197,11 @@ export class StatusController {
 
         // Function to soft-reconnect. Does not reset the loaded data.
         const doSoftReconnect = () => {
-            this.webClientService.stop(threema.DisconnectReason.SessionStopped, true, false, false);
+            this.webClientService.stop(threema.DisconnectReason.SessionStopped, {
+                send: true,
+                close: false,
+                redirect: false,
+            });
             this.webClientService.init(originalKeyStore, originalPeerPermanentKeyBytes, true);
             this.webClientService.start().then(
                 () => {
@@ -267,7 +271,11 @@ export class StatusController {
         // TODO: Make this more robust and hopefully faster
         const startTimeout = 500;
         this.$log.debug(this.logTag, 'Stopping old connection');
-        this.webClientService.stop(threema.DisconnectReason.SessionStopped, true, false, false);
+        this.webClientService.stop(threema.DisconnectReason.SessionStopped, {
+            send: true,
+            close: false,
+            redirect: false,
+        });
         this.$timeout(() => {
             this.$log.debug(this.logTag, 'Starting new connection');
             this.webClientService.init(originalKeyStore, originalPeerPermanentKeyBytes, true);

+ 10 - 2
src/partials/messenger.ts

@@ -1009,7 +1009,11 @@ class NavigationController {
             .ok(this.$translate.instant('common.YES'))
             .cancel(this.$translate.instant('common.CANCEL'));
         this.$mdDialog.show(confirm).then(() => {
-            this.webClientService.stop(threema.DisconnectReason.SessionStopped, true, true, true);
+            this.webClientService.stop(threema.DisconnectReason.SessionStopped, {
+                send: true,
+                close: true,
+                redirect: true,
+            });
             this.receiverService.setActive(undefined);
         }, () => {
             // do nothing
@@ -1027,7 +1031,11 @@ class NavigationController {
             .ok(this.$translate.instant('common.YES'))
             .cancel(this.$translate.instant('common.CANCEL'));
         this.$mdDialog.show(confirm).then(() => {
-            this.webClientService.stop(threema.DisconnectReason.SessionDeleted, true, true, true);
+            this.webClientService.stop(threema.DisconnectReason.SessionDeleted, {
+                send: true,
+                close: true,
+                redirect: true,
+            });
             this.receiverService.setActive(undefined);
         }, () => {
             // do nothing

+ 5 - 1
src/partials/welcome.ts

@@ -476,7 +476,11 @@ class WelcomeController {
 
         this.$mdDialog.show(confirm).then(() =>  {
             // Force-stop the webclient
-            this.webClientService.stop(threema.DisconnectReason.SessionDeleted, true, true, false);
+            this.webClientService.stop(threema.DisconnectReason.SessionDeleted, {
+                send: true,
+                close: true,
+                redirect: false,
+            });
 
             // Reset state
             this.stateService.updateConnectionBuildupState('new');

+ 6 - 7
src/protocol/cache.ts

@@ -39,14 +39,14 @@ export class ChunkCache {
     }
 
     /**
-     * Get the size of currently stored chunks.
+     * Get the size of currently cached chunks.
      */
     public get size(): number {
         return this._size;
     }
 
     /**
-     * Get the currently cached chunks.
+     * Get a reference to the currently cached chunks.
      */
     public get chunks(): CachedChunk[] {
         return this.cache;
@@ -57,10 +57,9 @@ export class ChunkCache {
      */
     public transfer(cache: CachedChunk[]): void {
         // Add chunks but remove all which should not be retransmitted
+        cache = cache.filter((chunk) => chunk !== null);
         for (const chunk of cache) {
-            if (chunk !== null) {
-                this.append(chunk);
-            }
+            this.append(chunk);
         }
     }
 
@@ -77,9 +76,9 @@ export class ChunkCache {
     }
 
     /**
-     * Acknowledge cached chunks and remove those from the cache.
+     * Prune cached chunks that have been acknowledged.
      */
-    public acknowledge(theirSequenceNumber: number): void {
+    public prune(theirSequenceNumber: number): void {
         try {
             this._sequenceNumber.validate(theirSequenceNumber);
         } catch (error) {

+ 36 - 14
src/services/webclient.ts

@@ -46,6 +46,17 @@ import InitializationStep = threema.InitializationStep;
 import ContactReceiverFeature = threema.ContactReceiverFeature;
 import DisconnectReason = threema.DisconnectReason;
 
+/**
+ * Payload of a connectionInfo message.
+ */
+interface ResumeInfo {
+    id: Uint8Array;
+    resume?: {
+        id: Uint8Array;
+        sequenceNumber: number;
+    };
+}
+
 /**
  * This service handles everything related to the communication with the peer.
  */
@@ -593,7 +604,11 @@ export class WebClientService {
      */
     private failSession() {
         // Stop session
-        this.stop(DisconnectReason.SessionError, true, true, true);
+        this.stop(DisconnectReason.SessionError, {
+            send: true,
+            close: true,
+            redirect: true,
+        });
 
         // Show an alert
         this.showAlert('connection.SESSION_ERROR');
@@ -605,7 +620,7 @@ export class WebClientService {
      * Important: Caller must invalidate the cache and connection ID after this
      *            function returned!
      */
-    private resumeSession(remoteInfo: any): void {
+    private resumeSession(remoteInfo: ResumeInfo): void {
         // Ensure we want to resume from the same previous connection
         if (!arraysAreEqual(this.previousConnectionId, remoteInfo.resume.id)) {
             this.$log.info('Cannot resume session: IDs of previous connection do not match');
@@ -613,9 +628,9 @@ export class WebClientService {
             return;
         }
 
-        // Acknowledge chunks that have been received by the remote side
+        // Remove chunks that have been received by the remote side
         try {
-            this.previousChunkCache.acknowledge(remoteInfo.resume.sequenceNumber);
+            this.previousChunkCache.prune(remoteInfo.resume.sequenceNumber);
         } catch (error) {
             // Not recoverable
             this.$log.error(this.logTag, `Unable to resume session: ${error}`);
@@ -904,15 +919,14 @@ export class WebClientService {
      */
     public stop(
         reason: DisconnectReason,
-        send: boolean,
-        close: boolean,
-        redirect: boolean,
+        flags: { send: boolean, close: boolean, redirect: boolean },
     ): void {
         this.$log.info(this.logTag, 'Disconnecting...');
+        let close = flags.close;
         let remove = false;
 
         // A redirect to the welcome page always implies a close
-        if (redirect) {
+        if (flags.redirect) {
             close = true;
         }
 
@@ -928,7 +942,7 @@ export class WebClientService {
         }
 
         // Send disconnect reason to the remote peer if requested
-        if (send && this.stateService.state === threema.GlobalConnectionState.Ok) {
+        if (flags.send && this.stateService.state === threema.GlobalConnectionState.Ok) {
             this._sendUpdate(WebClientService.SUB_TYPE_CONNECTION_DISCONNECT, false, undefined, {reason: reason});
         }
 
@@ -987,7 +1001,7 @@ export class WebClientService {
         }
 
         // Done, redirect now if requested
-        if (redirect) {
+        if (flags.redirect) {
             this.$timeout(() => {
                 this.$state.go('welcome');
             }, 0);
@@ -1966,10 +1980,10 @@ export class WebClientService {
         }
         const sequenceNumber = message.data.sequenceNumber;
 
-        // Acknowledge chunks
+        // Remove chunks which have already been received by the remote side
         const size = this.currentChunkCache.size;
         try {
-            this.currentChunkCache.acknowledge(sequenceNumber);
+            this.currentChunkCache.prune(sequenceNumber);
         } catch (error) {
             this.$log.error(this.logTag, error);
             this.failSession();
@@ -2021,7 +2035,11 @@ export class WebClientService {
         }
 
         // Stop and show an alert on the welcome page
-        this.stop(reason, false, true, true);
+        this.stop(reason, {
+            send: false,
+            close: true,
+            redirect: true,
+        });
         this.showAlert(alertMessage);
     }
 
@@ -3510,7 +3528,11 @@ export class WebClientService {
                 // TODO: Reactivate this and remove the special stop + alert
                 //       once the iOS beta has been closed
                 // this.failSession();
-                this.stop(DisconnectReason.SessionStopped, true, true, true);
+                this.stop(DisconnectReason.SessionStopped, {
+                    send: true,
+                    close: true,
+                    redirect: true,
+                });
                 this.showAlert('Please update the Threema app to use the latest iOS beta version');
                 return;
             }