77
88namespace PuppeteerSharp
99{
10- public class Session : IDisposable
10+ /// <summary>
11+ /// The CDPSession instances are used to talk raw Chrome Devtools Protocol:
12+ /// * Protocol methods can be called with <see cref="CDPSession.SendAsync(string, bool, dynamic)"/> method.
13+ /// * Protocol events, using the <see cref="CDPSession.MessageReceived"/> event.
14+ ///
15+ /// Documentation on DevTools Protocol can be found here: <see href="https://chromedevtools.github.io/devtools-protocol/"/>.
16+ ///
17+ /// <code>
18+ /// <![CDATA[
19+ /// var client = await Page.Target.CreateCDPSessionAsync();
20+ /// await client.SendAsync("Animation.enable");
21+ /// client.MessageReceived += (sender, e) =>
22+ /// {
23+ /// if (e.MessageID == "Animation.animationCreated")
24+ /// {
25+ /// Console.WriteLine("Animation created!");
26+ /// }
27+ /// };
28+ /// dynamic response = await client.SendAsync("Animation.getPlaybackRate");
29+ /// Console.WriteLine("playback rate is " + response.playbackRate);
30+ /// await client.SendAsync("Animation.setPlaybackRate", new
31+ /// {
32+ /// playbackRate = Convert.ToInt32(response.playbackRate / 2)
33+ /// });
34+ /// ]]></code>
35+ /// </summary>
36+ public class CDPSession : IDisposable
1137 {
12- public Session ( Connection connection , string targetId , string sessionId )
38+ internal CDPSession ( Connection connection , string targetId , string sessionId )
1339 {
1440 Connection = connection ;
1541 TargetId = targetId ;
1642 SessionId = sessionId ;
1743
1844 _callbacks = new Dictionary < int , MessageTask > ( ) ;
19- _logger = Connection . LoggerFactory . CreateLogger < Session > ( ) ;
45+ _logger = Connection . LoggerFactory . CreateLogger < CDPSession > ( ) ;
2046 }
2147
2248 #region Private Members
23- private int _lastId = 0 ;
49+ private int _lastId ;
2450 private readonly Dictionary < int , MessageTask > _callbacks ;
2551 private readonly ILogger _logger ;
2652 #endregion
2753
2854 #region Properties
55+ /// <summary>
56+ /// Gets the target identifier.
57+ /// </summary>
58+ /// <value>The target identifier.</value>
2959 public string TargetId { get ; }
60+ /// <summary>
61+ /// Gets the session identifier.
62+ /// </summary>
63+ /// <value>The session identifier.</value>
3064 public string SessionId { get ; }
65+ /// <summary>
66+ /// Gets the connection.
67+ /// </summary>
68+ /// <value>The connection.</value>
3169 public Connection Connection { get ; private set ; }
70+ /// <summary>
71+ /// Occurs when message received from Chromium.
72+ /// </summary>
3273 public event EventHandler < MessageEventArgs > MessageReceived ;
74+ /// <summary>
75+ /// Occurs when tracing is completed.
76+ /// </summary>
3377 public event EventHandler < TracingCompleteEventArgs > TracingComplete ;
78+ /// <summary>
79+ /// Gets or sets a value indicating whether this <see cref="T:PuppeteerSharp.CDPSession"/> is closed.
80+ /// </summary>
81+ /// <value><c>true</c> if is closed; otherwise, <c>false</c>.</value>
3482 public bool IsClosed { get ; internal set ; }
3583 #endregion
3684
3785 #region Public Methods
3886
39- public async Task < T > SendAsync < T > ( string method , dynamic args = null )
87+ internal async Task < T > SendAsync < T > ( string method , dynamic args = null )
4088 {
4189 var content = await SendAsync ( method , true , args ) ;
4290 return JsonConvert . DeserializeObject < T > ( content ) ;
4391 }
4492
45- public Task < dynamic > SendAsync ( string method , dynamic args = null )
93+ internal Task < dynamic > SendAsync ( string method , dynamic args = null )
4694 {
4795 return SendAsync ( method , false , args ?? new { } ) ;
4896 }
4997
50- public async Task < dynamic > SendAsync ( string method , bool rawContent , dynamic args = null )
98+ internal async Task < dynamic > SendAsync ( string method , bool rawContent , dynamic args = null )
5199 {
52100 if ( Connection == null )
53101 {
54102 throw new Exception ( $ "Protocol error ({ method } ): Session closed. Most likely the page has been closed.") ;
55103 }
56104 int id = ++ _lastId ;
57- var message = JsonConvert . SerializeObject ( new Dictionary < string , object > ( ) {
105+ var message = JsonConvert . SerializeObject ( new Dictionary < string , object >
106+ {
58107 { "id" , id } ,
59108 { "method" , method } ,
60109 { "params" , args }
@@ -72,7 +121,8 @@ public async Task<dynamic> SendAsync(string method, bool rawContent, dynamic arg
72121
73122 try
74123 {
75- await Connection . SendAsync ( "Target.sendMessageToTarget" , new Dictionary < string , object > ( ) {
124+ await Connection . SendAsync ( "Target.sendMessageToTarget" , new Dictionary < string , object >
125+ {
76126 { "sessionId" , SessionId } ,
77127 { "message" , message }
78128 } ) ;
@@ -100,10 +150,20 @@ public Task DetachAsync()
100150
101151 #region Private Methods
102152
153+ /// <summary>
154+ /// Releases all resource used by the <see cref="T:PuppeteerSharp.CDPSession"/> object by sending a ""Target.closeTarget"
155+ /// using the <see cref="Connection.SendAsync(string, dynamic)"/> method.
156+ /// </summary>
157+ /// <remarks>Call <see cref="Dispose"/> when you are finished using the <see cref="PuppeteerSharp.CDPSession"/>. The
158+ /// <see cref="Dispose"/> method leaves the <see cref="PuppeteerSharp.CDPSession"/> in an unusable state.
159+ /// After calling <see cref="Dispose"/>, you must release all references to the
160+ /// <see cref="PuppeteerSharp.CDPSession"/> so the garbage collector can reclaim the memory that the
161+ /// <see cref="PuppeteerSharp.CDPSession"/> was occupying.</remarks>
103162 public void Dispose ( )
104163 {
105- Connection . SendAsync ( "Target.closeTarget" , new Dictionary < string , object > ( ) {
106- { "targetId" , TargetId }
164+ Connection . SendAsync ( "Target.closeTarget" , new Dictionary < string , object >
165+ {
166+ [ "targetId" ] = TargetId
107167 } ) . GetAwaiter ( ) . GetResult ( ) ;
108168 }
109169
0 commit comments