@@ -44,13 +44,15 @@ public class BackgroundTopic : WebviewTopic
4444 private string pendingRequestId ;
4545 private RbacService rbacService ;
4646 private CookiesManager cookiesManager ;
47+ private List < IPC > pendingMessages ;
4748
4849 public BackgroundTopic ( WebView2 background , WebView2 rendered , LocalFolderService localFolderService , BackgroundWebviewService backgroundWebviewService ) : base ( background , rendered , localFolderService , backgroundWebviewService )
4950 {
5051 credentialLockerService = new CredentialLockerService ( ) ;
5152 passphrase = null ;
5253 this . rbacService = new RbacService ( ) ;
5354 cookiesManager = CookiesManager . Instance ;
55+ pendingMessages = new List < IPC > ( ) ;
5456 }
5557
5658 /// <summary>
@@ -71,8 +73,12 @@ public async override void ProceedMessage(IPC ipc)
7173 }
7274 else
7375 {
74- //Basic behaviour
75- rendered . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( new IPC ( AllowedTopics . BACKGROUND_READY ) ) ) ;
76+ WebviewOrchestratorService . Instance . SetBackgroundStatus ( true ) ;
77+ if ( WebviewOrchestratorService . Instance . AreAllReady ( ) )
78+ {
79+ rendered . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( new IPC ( AllowedTopics . BACKGROUND_READY ) ) ) ;
80+ background . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( new IPC ( AllowedTopics . RENDERED_READY ) ) ) ;
81+ }
7682 if ( passphrase != null )
7783 {
7884 background . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( new IPC ( AllowedTopics . BACKGROUND_STORE_PASSPHRASE , passphrase ) ) ) ;
@@ -142,13 +148,22 @@ public async override void ProceedMessage(IPC ipc)
142148 await downloadService . Download ( ipc ) ;
143149 break ;
144150 case LocalStorageTopics . BACKGROUND_LOCALSTORAGE_UPDATE :
145- 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+ }
146155 break ;
147156 case LocalStorageTopics . BACKGROUND_LOCALSTORAGE_DELETE :
148- 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+ }
149161 break ;
150162 case LocalStorageTopics . BACKGROUND_LOCALSTORAGE_CLEAR :
151- 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+ }
152167 break ;
153168 case AuthenticationTopics . LOG_OUT :
154169 this . currentIndexBackground = "index-auth.html" ;
@@ -175,7 +190,10 @@ public async override void ProceedMessage(IPC ipc)
175190 {
176191 AllowedTopics . AddRequestId ( ipc . requestId ) ;
177192 }
178- rendered . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( ipc ) ) ;
193+ if ( this . canProceedMessage ( ipc ) )
194+ {
195+ rendered . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( ipc ) ) ;
196+ }
179197 break ;
180198 case AllowedTopics . BACKGROUND_CLIPBOARD_SET_TEXT :
181199 DataPackage dataPackage = new DataPackage ( ) ;
@@ -193,6 +211,7 @@ public async override void ProceedMessage(IPC ipc)
193211 AllowedTopics . RemovePendingRequest ( ipc . topic ) ;
194212 }
195213 rendered . CoreWebView2 . PostWebMessageAsJson ( SerializationHelper . SerializeToJson ( ipc ) ) ;
214+
196215 break ;
197216 }
198217 }
@@ -213,6 +232,24 @@ public async Task RedirectToWorkspace()
213232 background . Source = new Uri ( UriBuilderHelper . BuildHostUri ( configuration . backgroundUrl , "/Background/index-workspace.html" ) ) ;
214233 }
215234
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 ) ;
243+
244+ foreach ( var message in messages )
245+ {
246+ ProceedMessage ( message ) ;
247+ }
248+
249+ pendingMessages . Clear ( ) ;
250+ }
251+ }
252+
216253 /// <summary>
217254 /// Proceed a pending request Id
218255 /// </summary>
@@ -240,5 +277,22 @@ public void mapResponse(IPC ipc, string topic)
240277 ipc . message = controls ;
241278 }
242279 }
280+
281+ /// <summary>
282+ /// Check if the rendered webview is listening and if not we add the ipc message as pending
283+ /// This method should be added before calling each topic calling the rendered webview
284+ /// </summary>
285+ /// <param name="ipc"></param>
286+ /// <returns bool></returns>
287+ private bool canProceedMessage ( IPC ipc )
288+ {
289+ if ( ! WebviewOrchestratorService . Instance . IsRenderedReady ( ) && ipc . topic != AllowedTopics . BACKGROUND_READY )
290+ {
291+ pendingMessages . Add ( ipc ) ;
292+ return false ;
293+ }
294+
295+ return true ;
296+ }
243297 }
244298}
0 commit comments