1 import type { Api } from '@proton/shared/lib/interfaces'
2 import { App_TYPES } from './Dependencies/Types'
3 import { AppDependencies } from './Dependencies/AppDependencies'
4 import type { CreateEmptyDocumentForConversion } from '../UseCase/CreateEmptyDocumentForConversion'
5 import type { DocLoader } from '../Services/DocumentLoader/DocLoader'
6 import type { DocLoaderInterface } from '../Services/DocumentLoader/DocLoaderInterface'
7 import { SyncedEditorState, type InternalEventBusInterface } from '@proton/docs-shared'
8 import type { ApplicationInterface } from './ApplicationInterface'
9 import type { WebsocketServiceInterface } from '../Services/Websockets/WebsocketServiceInterface'
10 import type { LoggerInterface } from '@proton/utils/logs'
11 import type { ImageProxyParams } from '../Api/Types/ImageProxyParams'
12 import type { CustomWindow } from './Window'
13 import type { RecentDocumentsInterface } from '../Services/RecentDocuments/types'
14 import type { MetricService } from '../Services/Metrics/MetricService'
15 import type { DriveCompatWrapper } from '@proton/drive-store/lib/DriveCompatWrapper'
16 import type { PublicDocLoader } from '../Services/DocumentLoader/PublicDocLoader'
17 import type { HttpHeaders } from '../Api/Types/HttpHeaders'
18 import type { DuplicateDocument } from '../UseCase/DuplicateDocument'
19 import type { UnleashClient } from '@proton/unleash'
20 import { UserState } from '../State/UserState'
21 import type { DocumentState, PublicDocumentState } from '../State/DocumentState'
23 declare const window: CustomWindow
25 export class Application implements ApplicationInterface {
26 public readonly userState = new UserState()
27 public readonly syncedEditorState = new SyncedEditorState()
29 private readonly deps = new AppDependencies(
31 this.imageProxyParams,
32 this.publicContextHeaders,
37 this.syncedEditorState,
41 private protonApi: Api,
42 private publicContextHeaders: HttpHeaders | undefined,
43 private imageProxyParams: ImageProxyParams | undefined,
44 public compatWrapper: DriveCompatWrapper,
45 private appVersion: string,
46 private unleashClient: UnleashClient,
48 this.deps.get<MetricService>(App_TYPES.MetricService).initialize()
51 public updateCompatWrapper(compatWrapper: DriveCompatWrapper) {
52 this.compatWrapper = compatWrapper
56 this.logger.info('Destroying application')
58 this.websocketService.destroy()
60 if (!this.compatWrapper.publicCompat) {
61 this.privateDocLoader.destroy()
63 this.publicDocLoader.destroy()
66 this.eventBus.deinit()
69 public get metrics(): MetricService {
70 return this.deps.get<MetricService>(App_TYPES.MetricService)
73 public get eventBus(): InternalEventBusInterface {
74 return this.deps.get<InternalEventBusInterface>(App_TYPES.EventBus)
77 public get logger(): LoggerInterface {
78 return this.deps.get<LoggerInterface>(App_TYPES.Logger)
81 public getDocLoader(): DocLoaderInterface<DocumentState | PublicDocumentState> {
82 if (this.compatWrapper.publicCompat) {
83 return this.publicDocLoader
85 return this.privateDocLoader
89 private get publicDocLoader(): DocLoaderInterface<PublicDocumentState> {
90 if (!this.compatWrapper.publicCompat) {
91 throw new Error('Public mode is not supported in private mode')
94 return this.deps.get<PublicDocLoader>(App_TYPES.PublicDocLoader)
97 private get privateDocLoader(): DocLoaderInterface<DocumentState> {
98 if (this.compatWrapper.publicCompat) {
99 throw new Error('Private mode is not supported in public mode')
102 return this.deps.get<DocLoader>(App_TYPES.DocLoader)
106 * Whether we are in a public document context, either as a public viewer or as a public editor.
108 public get isPublicMode(): boolean {
109 return !!this.compatWrapper.publicCompat
112 public get createEmptyDocumentForConversionUseCase(): CreateEmptyDocumentForConversion {
113 return this.deps.get<CreateEmptyDocumentForConversion>(App_TYPES.CreateEmptyDocumentForConversion)
116 public get duplicateDocumentUseCase(): DuplicateDocument {
117 return this.deps.get<DuplicateDocument>(App_TYPES.DuplicateDocument)
120 public get websocketService(): WebsocketServiceInterface {
121 return this.deps.get<WebsocketServiceInterface>(App_TYPES.WebsocketService)
124 public get recentDocumentsService(): RecentDocumentsInterface {
125 return this.deps.get<RecentDocumentsInterface>(App_TYPES.RecentDocumentsService)
128 public get isRunningInNativeMobileWeb(): boolean {
129 return window.Android != null || window.webkit?.messageHandlers?.iOS != null