Skip to content

Commit

Permalink
fix: desktop interop
Browse files Browse the repository at this point in the history
  • Loading branch information
arielsvg committed Sep 3, 2020
1 parent 568fb14 commit 35fe78e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 58 deletions.
22 changes: 22 additions & 0 deletions app/assets/javascripts/services/bridge.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import { PurePayload, Environment } from "snjs";

/** Platform-specific (i-e Electron/browser) behavior is handled by a Bridge object. */
export interface Bridge {
environment: Environment,

getKeychainValue(): Promise<unknown>;
setKeychainValue(value: any): Promise<void>;
clearKeychainValue(): Promise<void>;

extensionsServerHost?: string;
syncComponents(payloads: PurePayload[]): void;
onMajorDataChange(): void;
onInitialDataLoad(): void;
onSearch(text?: string): void;
}

const KEYCHAIN_STORAGE_KEY = 'keychain';

export class BrowserBridge implements Bridge {
environment = Environment.Web;

async getKeychainValue(): Promise<unknown> {
const value = localStorage.getItem(KEYCHAIN_STORAGE_KEY);
Expand All @@ -23,4 +34,15 @@ export class BrowserBridge implements Bridge {
async clearKeychainValue(): Promise<void> {
localStorage.removeItem(KEYCHAIN_STORAGE_KEY);
}

/** No-ops */

syncComponents() {
}
onMajorDataChange() {
}
onInitialDataLoad() {
}
onSearch() {
}
}
78 changes: 22 additions & 56 deletions app/assets/javascripts/services/desktopManager.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { SNComponent, PurePayload, ComponentMutator, AppDataField } from 'snjs';
import { SNComponent, PurePayload, ComponentMutator, AppDataField, ContentType } from 'snjs';
/* eslint-disable camelcase */
import { WebApplication } from '@/ui_models/application';
// An interface used by the Desktop app to interact with SN
import { isDesktopApplication } from '@/utils';
import { EncryptionIntent, ApplicationService, ApplicationEvent, removeFromArray } from 'snjs';
import { Bridge } from './bridge';

type UpdateObserverCallback = (component: SNComponent) => void
type ComponentActivationCallback = (payload: PurePayload) => void
Expand All @@ -23,18 +24,14 @@ export class DesktopManager extends ApplicationService {
isDesktop = isDesktopApplication();

dataLoaded = false
dataLoadHandler?: () => void
majorDataChangeHandler?: () => void
extServerHost?: string
installationSyncHandler?: (payloads: PurePayload[]) => void
installComponentHandler?: (payload: PurePayload) => void
lastSearchedText?: string
searchHandler?: (text?: string) => void
private removeComponentObserver?: () => void;

constructor(
$rootScope: ng.IRootScopeService,
$timeout: ng.ITimeoutService,
application: WebApplication
application: WebApplication,
private bridge: Bridge,
) {
super(application);
this.$rootScope = $rootScope;
Expand All @@ -48,40 +45,38 @@ export class DesktopManager extends ApplicationService {
deinit() {
this.componentActivationObservers.length = 0;
this.updateObservers.length = 0;
this.removeComponentObserver?.();
this.removeComponentObserver = undefined;
super.deinit();
}

async onAppEvent(eventName: ApplicationEvent) {
super.onAppEvent(eventName);
if (eventName === ApplicationEvent.LocalDataLoaded) {
this.dataLoaded = true;
if (this.dataLoadHandler) {
this.dataLoadHandler();
}
this.bridge.onInitialDataLoad();
} else if (eventName === ApplicationEvent.MajorDataChange) {
if (this.majorDataChangeHandler) {
this.majorDataChangeHandler();
}
this.bridge.onMajorDataChange();
}
}

saveBackup() {
this.majorDataChangeHandler && this.majorDataChangeHandler();
this.bridge.onMajorDataChange();
}

getExtServerHost() {
console.assert(
this.extServerHost,
this.bridge.extensionsServerHost,
'extServerHost is null'
);
return this.extServerHost;
return this.bridge.extensionsServerHost;
}

/**
* Sending a component in its raw state is really slow for the desktop app
* Keys are not passed into ItemParams, so the result is not encrypted
*/
async convertComponentForTransmission(component: SNComponent) {
convertComponentForTransmission(component: SNComponent) {
return this.application!.protocolService!.payloadByEncryptingPayload(
component.payloadRepresentation(),
EncryptionIntent.FileDecrypted
Expand All @@ -96,16 +91,14 @@ export class DesktopManager extends ApplicationService {
Promise.all(components.map((component) => {
return this.convertComponentForTransmission(component);
})).then((payloads) => {
this.installationSyncHandler!(payloads);
this.bridge.syncComponents(
payloads.filter(payload =>
!payload.errorDecrypting && !payload.waitingForKey
)
);
});
}

async installComponent(component: SNComponent) {
this.installComponentHandler!(
await this.convertComponentForTransmission(component)
);
}

registerUpdateObserver(callback: UpdateObserverCallback) {
const observer = {
callback: callback
Expand All @@ -121,7 +114,7 @@ export class DesktopManager extends ApplicationService {
return;
}
this.lastSearchedText = text;
this.searchHandler && this.searchHandler(text);
this.bridge.onSearch(text);
}

redoSearch() {
Expand All @@ -130,11 +123,6 @@ export class DesktopManager extends ApplicationService {
}
}

// Pass null to cancel search
desktop_setSearchHandler(handler: (text?: string) => void) {
this.searchHandler = handler;
}

desktop_windowGainedFocus() {
this.$rootScope.$broadcast('window-gained-focus');
}
Expand Down Expand Up @@ -199,38 +187,16 @@ export class DesktopManager extends ApplicationService {
});
}

/* Used to resolve 'sn://' */
desktop_setExtServerHost(host: string) {
this.extServerHost = host;
onExtensionsReady() {
this.webApplication.getAppState().desktopExtensionsReady();
}

desktop_setComponentInstallationSyncHandler(handler: (payloads: PurePayload[]) => void) {
this.installationSyncHandler = handler;
}

desktop_setInstallComponentHandler(handler: (payload: PurePayload) => void) {
this.installComponentHandler = handler;
}

desktop_setInitialDataLoadHandler(handler: () => void) {
this.dataLoadHandler = handler;
if (this.dataLoaded) {
this.dataLoadHandler();
}
}

async desktop_requestBackupFile(callback: (data: any) => void) {
const data = await this.application!.createBackupFile(
desktop_requestBackupFile() {
return this.application!.createBackupFile(
undefined,
undefined,
true
);
callback(data);
}

desktop_setMajorDataChangeHandler(handler: () => void) {
this.majorDataChangeHandler = handler;
}

desktop_didBeginBackup() {
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/ui_models/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class WebApplication extends SNApplication {
bridge
);
super(
Environment.Web,
bridge.environment,
platformFromString(getPlatformString()),
deviceInterface,
new SNWebCrypto(),
Expand Down
3 changes: 2 additions & 1 deletion app/assets/javascripts/ui_models/application_group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ export class ApplicationGroup {
const desktopService = new DesktopManager(
this.$rootScope,
this.$timeout,
application
application,
this.bridge,
);
const keyboardService = new KeyboardManager();
const lockService = new LockManager(
Expand Down

0 comments on commit 35fe78e

Please sign in to comment.