1616using Newtonsoft . Json ;
1717using Newtonsoft . Json . Linq ;
1818using passbolt . Exceptions ;
19- using passbolt . Models . Authentication ;
19+ using passbolt . Models . Authentication ;
2020using passbolt . Models . Cookies ;
2121using passbolt . Models . CredentialLocker ;
2222using passbolt . Models . Messaging . Topics ;
3030using passbolt . Utils ;
3131using System ;
3232using System . Collections . Generic ;
33- using System . Threading . Tasks ;
33+ using System . Threading . Tasks ;
3434using Windows . ApplicationModel . DataTransfer ;
3535
3636namespace passbolt . Models . Messaging
3737{
3838 public class BackgroundTopic : WebviewTopic
3939 {
4040 private CredentialLockerService credentialLockerService ;
41- private string currentIndexBackground = "index-auth.html" ;
41+ private string currentIndexBackground = "index-auth.html" ;
4242 private string currentIndexRendered = "index-auth.html" ;
4343 private string passphrase ;
4444 private string pendingRequestId ;
4545 private RbacService rbacService ;
4646 private CookiesManager cookiesManager ;
47- private List < IPC > pendingMessages ;
47+ private List < IPC > pendingMessages ;
4848
4949 public BackgroundTopic ( WebView2 background , WebView2 rendered , LocalFolderService localFolderService , BackgroundWebviewService backgroundWebviewService ) : base ( background , rendered , localFolderService , backgroundWebviewService )
5050 {
@@ -67,67 +67,67 @@ public async override void ProceedMessage(IPC ipc)
6767 {
6868 case AllowedTopics . BACKGROUND_READY :
6969 //It means we have a single navigation of the Background webview
70- if ( this . pendingRequestId != null )
71- {
72- this . proceedPendingRequest ( ) ;
73- }
74- else
75- {
70+ if ( this . pendingRequestId != null )
71+ {
72+ this . proceedPendingRequest ( ) ;
73+ }
74+ else
75+ {
7676 WebviewOrchestratorService . Instance . SetBackgroundStatus ( true ) ;
7777 if ( WebviewOrchestratorService . Instance . AreAllReady ( ) )
7878 {
7979 rendered . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( new IPC ( AllowedTopics . BACKGROUND_READY ) ) ) ;
8080 background . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( new IPC ( AllowedTopics . RENDERED_READY ) ) ) ;
81- }
82- if ( passphrase != null )
83- {
84- background . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( new IPC ( AllowedTopics . BACKGROUND_STORE_PASSPHRASE , passphrase ) ) ) ;
81+ }
82+ if ( passphrase != null )
83+ {
84+ background . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( new IPC ( AllowedTopics . BACKGROUND_STORE_PASSPHRASE , passphrase ) ) ) ;
8585 rendered . Source = new Uri ( UriBuilderHelper . BuildHostUri ( RenderedNavigationService . Instance . trustedUrl , "/Rendered/index-workspace.html" ) ) ;
86- passphrase = null ;
87- }
88- }
86+ passphrase = null ;
87+ }
88+ }
8989 break ;
9090 case AllowedTopics . BACKGROUND_GET_COOKIE :
9191 string cookie = cookiesManager . getCookie ( ( string ) ipc . message ) ;
9292 var response = new IPC ( ) ;
9393 response . message = cookie ;
9494 response . status = "SUCCESS" ;
9595 response . topic = ipc . requestId ;
96- background . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( response ) ) ;
96+ background . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( response ) ) ;
9797 break ;
9898 case AllowedTopics . BACKGROUND_SET_THEME :
9999 accountMetaData . theme = ( string ) ipc . message ;
100- await this . credentialLockerService . Create ( "account-metadata" , JsonConvert . SerializeObject ( accountMetaData ) ) ;
100+ await this . credentialLockerService . Create ( "account-metadata" , JsonConvert . SerializeObject ( accountMetaData ) ) ;
101101 break ;
102102 case AllowedTopics . BACKGROUND_SET_LOCALE :
103103 accountMetaData . locale = ( string ) ipc . message ;
104- await this . credentialLockerService . Create ( "account-metadata" , JsonConvert . SerializeObject ( accountMetaData ) ) ;
104+ await this . credentialLockerService . Create ( "account-metadata" , JsonConvert . SerializeObject ( accountMetaData ) ) ;
105105 break ;
106106 case AllowedTopics . BACKGROUND_SET_SECURITY_TOKEN :
107107 accountMetaData . securityToken = SerializationHelper . DeserializeFromJson < SecurityToken > ( ( ( JObject ) ipc . message ) . ToString ( ) ) ;
108- await this . credentialLockerService . Create ( "account-metadata" , JsonConvert . SerializeObject ( accountMetaData ) ) ;
108+ await this . credentialLockerService . Create ( "account-metadata" , JsonConvert . SerializeObject ( accountMetaData ) ) ;
109109 break ;
110110 case AllowedTopics . BACKGROUND_ROTATE_KEY :
111111 var accountSecret = await this . credentialLockerService . GetAccountSecret ( ) ;
112112 accountSecret . userPrivateArmoredKey = ( string ) ipc . message ;
113113 await this . credentialLockerService . Create ( "account-secret" , JsonConvert . SerializeObject ( accountSecret ) ) ;
114- break ;
115- case AuthenticationTopics . REQUIRE_MFA :
116- var message = SerializationHelper . DeserializeFromJson < MfaAuthentication > ( ( ( JObject ) ipc . message ) . ToString ( ) ) ;
114+ break ;
115+ case AuthenticationTopics . REQUIRE_MFA :
116+ var message = SerializationHelper . DeserializeFromJson < MfaAuthentication > ( ( ( JObject ) ipc . message ) . ToString ( ) ) ;
117117 passphrase = message . passphrase ;
118118
119119 // Allow navigation for MFA authentication
120120 RenderedNavigationService . Instance . AllowMfaUrls ( accountMetaData . domain ) ;
121- rendered . Source = new Uri ( accountMetaData . domain + $ "/mfa/verify/{ message . provider } ?redirect=/") ;
121+ rendered . Source = new Uri ( accountMetaData . domain + $ "/mfa/verify/{ message . provider } ?redirect=/") ;
122122 break ;
123123 case AuthImportTopics . SAVE_ACCOUNT :
124- this . currentIndexBackground = "index-auth.html" ;
125- this . currentIndexRendered = "index-import.html" ;
126- pendingRequestId = SerializationHelper . DeserializeFromJson < RequestId > ( ( ( JObject ) ipc . message ) . ToString ( ) ) . requestId ;
124+ this . currentIndexBackground = "index-auth.html" ;
125+ this . currentIndexRendered = "index-import.html" ;
126+ pendingRequestId = SerializationHelper . DeserializeFromJson < RequestId > ( ( ( JObject ) ipc . message ) . ToString ( ) ) . requestId ;
127127 var metaData = SerializationHelper . DeserializeFromJson < AccountMetaData > ( ( ( JObject ) ipc . message ) . ToString ( ) ) ;
128128 var secrets = SerializationHelper . DeserializeFromJson < AccountSecret > ( ( ( JObject ) ipc . message ) . ToString ( ) ) ;
129129 await this . credentialLockerService . CreateAccount ( metaData , secrets ) ;
130- //We remove previous virtualhost and create a new one based on trusted domain
130+ //We remove previous virtualhost and create a new one based on trusted domain
131131 var backgroundUrl = await this . webviewService . SetVirtualHost ( ) ;
132132 await localFolderService . CreateBackgroundIndex ( this . currentIndexBackground , "background-auth" , metaData . domain ) ;
133133 background . Source = new Uri ( UriBuilderHelper . BuildHostUri ( backgroundUrl , "/index-auth.html" ) ) ;
@@ -148,26 +148,32 @@ public async override void ProceedMessage(IPC ipc)
148148 await downloadService . Download ( ipc ) ;
149149 break ;
150150 case LocalStorageTopics . BACKGROUND_LOCALSTORAGE_UPDATE :
151- this . canProceedMessage ( ipc ) ;
152- rendered . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( new IPC ( LocalStorageTopics . RENDERED_LOCALSTORAGE_UPDATE , SerializationHelper . SerializeToJson ( ipc . message ) ) ) ) ;
151+ if ( this . canProceedMessage ( ipc ) )
152+ {
153+ rendered . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( new IPC ( LocalStorageTopics . RENDERED_LOCALSTORAGE_UPDATE , SerializationHelper . SerializeToJson ( ipc . message ) ) ) ) ;
154+ }
153155 break ;
154156 case LocalStorageTopics . BACKGROUND_LOCALSTORAGE_DELETE :
155- this . canProceedMessage ( ipc ) ;
156- rendered . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( new IPC ( LocalStorageTopics . RENDERED_LOCALSTORAGE_DELETE , ( string ) ipc . message ) ) ) ;
157+ if ( this . canProceedMessage ( ipc ) )
158+ {
159+ rendered . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( new IPC ( LocalStorageTopics . RENDERED_LOCALSTORAGE_DELETE , ( string ) ipc . message ) ) ) ;
160+ }
157161 break ;
158162 case LocalStorageTopics . BACKGROUND_LOCALSTORAGE_CLEAR :
159- this . canProceedMessage ( ipc ) ;
160- rendered . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( new IPC ( LocalStorageTopics . RENDERED_LOCALSTORAGE_CLEAR ) ) ) ;
163+ if ( this . canProceedMessage ( ipc ) )
164+ {
165+ rendered . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( new IPC ( LocalStorageTopics . RENDERED_LOCALSTORAGE_CLEAR ) ) ) ;
166+ }
161167 break ;
162168 case AuthenticationTopics . LOG_OUT :
163- this . currentIndexBackground = "index-auth.html" ;
164- this . currentIndexRendered = "index-auth.html" ;
169+ this . currentIndexBackground = "index-auth.html" ;
170+ this . currentIndexRendered = "index-auth.html" ;
165171 await localFolderService . RemoveFile ( "Rendered" , "index-workspace.html" ) ;
166172 await localFolderService . RemoveFile ( "Background" , "index-workspace.html" ) ;
167- await localFolderService . CreateRenderedIndex ( this . currentIndexRendered , "rendered-auth" , "ext_authentication.min.css" , accountMetaData . domain ) ;
168- await localFolderService . CreateBackgroundIndex ( this . currentIndexBackground , "background-auth" , accountMetaData . domain ) ;
169- background . Source = new Uri ( UriBuilderHelper . BuildHostUri ( BackgroundNavigationService . Instance . trustedUrl , "/Background/index-auth.html" ) ) ;
170- rendered . Source = new Uri ( UriBuilderHelper . BuildHostUri ( RenderedNavigationService . Instance . trustedUrl , "/Rendered/index-auth.html" ) ) ;
173+ await localFolderService . CreateRenderedIndex ( this . currentIndexRendered , "rendered-auth" , "ext_authentication.min.css" , accountMetaData . domain ) ;
174+ await localFolderService . CreateBackgroundIndex ( this . currentIndexBackground , "background-auth" , accountMetaData . domain ) ;
175+ background . Source = new Uri ( UriBuilderHelper . BuildHostUri ( BackgroundNavigationService . Instance . trustedUrl , "/Background/index-auth.html" ) ) ;
176+ rendered . Source = new Uri ( UriBuilderHelper . BuildHostUri ( RenderedNavigationService . Instance . trustedUrl , "/Rendered/index-auth.html" ) ) ;
171177 break ;
172178 case AuthenticationTopics . AFTER_LOGIN :
173179 passphrase = ( string ) ipc . message ;
@@ -184,8 +190,10 @@ public async override void ProceedMessage(IPC ipc)
184190 {
185191 AllowedTopics . AddRequestId ( ipc . requestId ) ;
186192 }
187- this . canProceedMessage ( ipc ) ;
188- rendered . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( ipc ) ) ;
193+ if ( this . canProceedMessage ( ipc ) )
194+ {
195+ rendered . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( ipc ) ) ;
196+ }
189197 break ;
190198 case AllowedTopics . BACKGROUND_CLIPBOARD_SET_TEXT :
191199 DataPackage dataPackage = new DataPackage ( ) ;
@@ -202,85 +210,88 @@ public async override void ProceedMessage(IPC ipc)
202210 this . mapResponse ( ipc , value ) ;
203211 AllowedTopics . RemovePendingRequest ( ipc . topic ) ;
204212 }
205- this . canProceedMessage ( ipc ) ;
206213 rendered . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( ipc ) ) ;
214+
207215 break ;
208216 }
209- }
210-
211- /// <summary>
212- /// Create folder for workspace and redirect user to workspace
213- /// </summary>
217+ }
218+
219+ /// <summary>
220+ /// Create folder for workspace and redirect user to workspace
221+ /// </summary>
214222 /// <param name="accountMeta"></param>
215- public async Task RedirectToWorkspace ( )
216- {
223+ public async Task RedirectToWorkspace ( )
224+ {
217225 var accountMetaData = await this . credentialLockerService . GetAccountMetadata ( ) ;
218-
219- await localFolderService . RemoveFile ( "Rendered" , this . currentIndexRendered ) ;
220- await localFolderService . RemoveFile ( "Background" , this . currentIndexBackground ) ;
221- await localFolderService . CreateRenderedIndex ( "index-workspace.html" , "rendered-workspace" , "ext_app.min.css" , accountMetaData . domain ) ;
226+
227+ await localFolderService . RemoveFile ( "Rendered" , this . currentIndexRendered ) ;
228+ await localFolderService . RemoveFile ( "Background" , this . currentIndexBackground ) ;
229+ await localFolderService . CreateRenderedIndex ( "index-workspace.html" , "rendered-workspace" , "ext_app.min.css" , accountMetaData . domain ) ;
222230 await localFolderService . CreateBackgroundIndex ( "index-workspace.html" , "background-workspace" , accountMetaData . domain ) ;
223- var configuration = await credentialLockerService . GetApplicationConfiguration ( ) ;
224- background . Source = new Uri ( UriBuilderHelper . BuildHostUri ( configuration . backgroundUrl , "/Background/index-workspace.html" ) ) ;
231+ var configuration = await credentialLockerService . GetApplicationConfiguration ( ) ;
232+ background . Source = new Uri ( UriBuilderHelper . BuildHostUri ( configuration . backgroundUrl , "/Background/index-workspace.html" ) ) ;
225233 }
226234
227- /// <summary>
228- /// Process all pending messages when Rendered webview becomes ready
229- /// </summary>
230- public void ProcessPendingMessages ( )
231- {
232- if ( pendingMessages . Count > 0 )
233- {
234- var messages = new List < IPC > ( pendingMessages ) ;
235+ /// <summary>
236+ /// Process all pending messages when Rendered webview becomes ready
237+ /// </summary>
238+ public void ProcessPendingMessages ( )
239+ {
240+ if ( pendingMessages . Count > 0 )
241+ {
242+ var messages = new List < IPC > ( pendingMessages ) ;
235243
236- foreach ( var message in messages )
237- {
238- ProceedMessage ( message ) ;
244+ foreach ( var message in messages )
245+ {
246+ ProceedMessage ( message ) ;
239247 }
240248
241- pendingMessages . Clear ( ) ;
242- }
249+ pendingMessages . Clear ( ) ;
250+ }
243251 }
244252
245- /// <summary>
246- /// Proceed a pending request Id
253+ /// <summary>
254+ /// Proceed a pending request Id
247255 /// </summary>
248- public void proceedPendingRequest ( )
249- {
250- AllowedTopics . proceedRequestId ( this . pendingRequestId ) ;
251- var icpMessage = new IPC ( ) ;
252- icpMessage . status = "SUCCESS" ;
253- icpMessage . topic = this . pendingRequestId ;
254- rendered . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( icpMessage ) ) ;
255- this . pendingRequestId = null ;
256+ public void proceedPendingRequest ( )
257+ {
258+ AllowedTopics . proceedRequestId ( this . pendingRequestId ) ;
259+ var icpMessage = new IPC ( ) ;
260+ icpMessage . status = "SUCCESS" ;
261+ icpMessage . topic = this . pendingRequestId ;
262+ rendered . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( icpMessage ) ) ;
263+ this . pendingRequestId = null ;
256264 }
257265
258- /// <summary>
259- /// Map the response from the background webview
260- /// </summary>
261- /// <param name="ipc"></param>
262- /// <returns></returns>
263- public void mapResponse ( IPC ipc , string topic )
264- {
265- if ( topic == RbacTopics . FIND_ME )
266+ /// <summary>
267+ /// Map the response from the background webview
268+ /// </summary>
269+ /// <param name="ipc"></param>
270+ /// <returns></returns>
271+ public void mapResponse ( IPC ipc , string topic )
272+ {
273+ if ( topic == RbacTopics . FIND_ME )
266274 {
267275 var controls = SerializationHelper . DeserializeFromJson < List < ControlFunction > > ( ( ( JArray ) ipc . message ) . ToString ( ) ) ;
268276 this . rbacService . AddDesktopRbac ( controls ) ;
269- ipc . message = controls ;
270- }
277+ ipc . message = controls ;
278+ }
271279 }
272280
273281 /// <summary>
274282 /// Check if the rendered webview is listening and if not we add the ipc message as pending
275283 /// This method should be added before calling each topic calling the rendered webview
276284 /// </summary>
277285 /// <param name="ipc"></param>
278- private void canProceedMessage ( IPC ipc )
286+ /// <returns bool></returns>
287+ private bool canProceedMessage ( IPC ipc )
279288 {
280- if ( ! WebviewOrchestratorService . Instance . IsRenderedReady ( ) && ipc . topic != AllowedTopics . BACKGROUND_READY )
281- {
289+ if ( ! WebviewOrchestratorService . Instance . IsRenderedReady ( ) && ipc . topic != AllowedTopics . BACKGROUND_READY )
290+ {
282291 pendingMessages . Add ( ipc ) ;
283292 }
293+
294+ return true ;
284295 }
285296 }
286297}
0 commit comments