22using ElectronNET . API . Serialization ;
33using System ;
44using System . Text . Json ;
5+ using System . Threading . Tasks ;
56
67namespace ElectronNET . API
78{
@@ -54,10 +55,79 @@ public event Action<Cookie, CookieChangedCause, bool> OnChanged
5455 _changed -= value ;
5556
5657 if ( _changed == null )
58+ {
5759 BridgeConnector . Socket . Off ( "webContents-session-cookies-changed" + Id ) ;
60+ }
5861 }
5962 }
6063
6164 private event Action < Cookie , CookieChangedCause , bool > _changed ;
65+
66+
67+
68+ /// <summary>
69+ /// Sends a request to get all cookies matching filter, and resolves a callack with the response.
70+ /// </summary>
71+ /// <param name="filter">
72+ /// </param>
73+ /// <returns>A task which resolves an array of cookie objects.</returns>
74+ public Task < Cookie [ ] > GetAsync ( CookieFilter filter )
75+ {
76+ var tcs = new TaskCompletionSource < Cookie [ ] > ( ) ;
77+ var guid = Guid . NewGuid ( ) . ToString ( ) ;
78+
79+ BridgeConnector . Socket . Once < Cookie [ ] > ( "webContents-session-cookies-get-completed" + guid , tcs . SetResult ) ;
80+ BridgeConnector . Socket . Emit ( "webContents-session-cookies-get" , Id , filter , guid ) ;
81+
82+ return tcs . Task ;
83+ }
84+
85+ /// <summary>
86+ ///
87+ /// </summary>
88+ /// <param name="details"></param>
89+ /// <returns></returns>
90+ public Task SetAsync ( CookieDetails details )
91+ {
92+ var tcs = new TaskCompletionSource < object > ( ) ;
93+ var guid = Guid . NewGuid ( ) . ToString ( ) ;
94+
95+ BridgeConnector . Socket . Once < object > ( "webContents-session-cookies-set-completed" + guid , tcs . SetResult ) ;
96+ BridgeConnector . Socket . Emit ( "webContents-session-cookies-set" , Id , details , guid ) ;
97+
98+ return tcs . Task ;
99+ }
100+
101+ /// <summary>
102+ /// Removes the cookies matching url and name
103+ /// </summary>
104+ /// <param name="url">The URL associated with the cookie.</param>
105+ /// <param name="name">The name of cookie to remove.</param>
106+ /// <returns>A task which resolves when the cookie has been removed</returns>
107+ public Task RemoveAsync ( string url , string name )
108+ {
109+ var tcs = new TaskCompletionSource < object > ( ) ;
110+ var guid = Guid . NewGuid ( ) . ToString ( ) ;
111+
112+ BridgeConnector . Socket . Once < object > ( "webContents-session-cookies-remove-completed" + guid , tcs . SetResult ) ;
113+ BridgeConnector . Socket . Emit ( "webContents-session-cookies-remove" , Id , url , name , guid ) ;
114+
115+ return tcs . Task ;
116+ }
117+
118+ /// <summary>
119+ /// Writes any unwritten cookies data to disk.
120+ /// </summary>
121+ /// <returns>A task which resolves when the cookie store has been flushed</returns>
122+ public Task FlushStoreAsync ( )
123+ {
124+ var tcs = new TaskCompletionSource < object > ( ) ;
125+ var guid = Guid . NewGuid ( ) . ToString ( ) ;
126+
127+ BridgeConnector . Socket . Once < object > ( "webContents-session-cookies-flushStore-completed" + guid , tcs . SetResult ) ;
128+ BridgeConnector . Socket . Emit ( "webContents-session-cookies-flushStore" , Id , guid ) ;
129+
130+ return tcs . Task ;
131+ }
62132 }
63133}
0 commit comments