Skip to content

Commit ccc8502

Browse files
authored
Update CDPSessionIdSupport.md
1 parent f31ad77 commit ccc8502

1 file changed

Lines changed: 24 additions & 11 deletions

File tree

specs/CDPSessionIdSupport.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,19 @@ and retrieving sessionId for received DevToolsProtocol events.
1717

1818
# Conceptual pages (How To)
1919

20-
To interact with different targets on the page, call DevToolsProtocol method `Target.setAutoAttach` with `flatten` set as `true`,
21-
and then listen to `Target.attachedToTarget` and `Target.detachedFromTarget` events to manage the sessionId for targets.
20+
To use the sessionId support, you must attach to targets with with `flatten` set as `true` when calling `Target.attachToTarget` or `Target.setAutoAttach`.
2221

23-
As shared worker will not be auto attached, to also interact with shared workers, call DevToolsProtocol method `Target.setDiscoverTargets`,
24-
and then call `Target.attachToTarget` for the shared worker target when `Target.targetCreated` event is received.
22+
You can listen to `Target.attachedToTarget` and `Target.detachedFromTarget` events to manage the sessionId for targets, and listen to `Target.targetInfoChanged` event to update target info like url of a target.
2523

26-
To update target info like url of a target, listen to `Target.targetInfoChanged` event.
24+
There is also some nuance for DevToolsProtocol's target management. If you are interested in only top page and iframes from different origins on the page, it will be simple and straight forward. All related methods and events like 'Target.getTargets', `Target.attachToTarget`, and `Target.targetCreated` event work as expected.
25+
26+
However, dedicated web workers are not returned from `'Target.getTargets'`, and you have to call DevToolsProtocol method `Target.setAutoAttach` to be able to attach to them.
27+
28+
Shared worker is separate from any page or iframe target, and therefore will not be auto attached. You have to call `Target.attachToTarget` to attach to them. The shared workers are enumerated with 'Target.getTargets'. They are also discoverable, that is, you can call `Target.setDiscoverTargets` to receive `Target.targetCreated` event for them.
2729

2830
# Examples
2931

30-
The example below illustrates how to collect messages logged by console.log calls by JavaScipt code from various parts of the web page.
32+
The example below illustrates how to collect messages logged by console.log calls by JavaScipt code from various parts of the web page, including dedicated web worker.
3133

3234
## Win32 C++
3335
```cpp
@@ -41,8 +43,6 @@ void MyApp::HandleDevToolsProtocalPTargets()
4143
// std::map<std::wstring, std::wstring> m_devToolsTargetInfoMap;
4244
// GetJSONStringField is a helper function that can retrieve a string field from a json message.
4345

44-
// Enable Runtime events to receive Runtime.consoleAPICalled events.
45-
m_webView->CallDevToolsProtocolMethod(L"Runtime.enable", L"{}", nullptr);
4646
wil::com_ptr<ICoreWebView2DevToolsProtocolEventReceiver> receiver;
4747
CHECK_FAILURE(
4848
m_webView->GetDevToolsProtocolEventReceiver(L"Runtime.consoleAPICalled", &receiver));
@@ -52,6 +52,7 @@ void MyApp::HandleDevToolsProtocalPTargets()
5252
ICoreWebView2* sender,
5353
ICoreWebView2DevToolsProtocolEventReceivedEventArgs* args) -> HRESULT
5454
{
55+
// Get related target and details for the console.log
5556
wil::unique_cotaskmem_string parameterObjectAsJson;
5657
CHECK_FAILURE(args->get_ParameterObjectAsJson(&parameterObjectAsJson));
5758
std::wstring eventSource;
@@ -84,6 +85,7 @@ void MyApp::HandleDevToolsProtocalPTargets()
8485
ICoreWebView2* sender,
8586
ICoreWebView2DevToolsProtocolEventReceivedEventArgs* args) -> HRESULT
8687
{
88+
// A new target is attached, add its info to maps.
8789
wil::unique_cotaskmem_string jsonMessage;
8890
CHECK_FAILURE(args->get_ParameterObjectAsJson(&jsonMessage));
8991
std::wstring sessionId = GetJSONStringField(jsonMessage.get(), L"sessionId");
@@ -95,7 +97,7 @@ void MyApp::HandleDevToolsProtocalPTargets()
9597
wil::com_ptr<ICoreWebView2_10> webview2 = m_webView.try_query<ICoreWebView2_10>();
9698
if (webview2)
9799
{
98-
// Auto attach to targets created from this target.
100+
// Auto attach to targets further created from this target.
99101
webview2->CallDevToolsProtocolMethodForSession(
100102
sessionId.c_str(), L"Target.setAutoAttach",
101103
LR"({"autoAttach":true,"waitForDebuggerOnStart":false,"flatten":true})",
@@ -117,6 +119,7 @@ void MyApp::HandleDevToolsProtocalPTargets()
117119
ICoreWebView2* sender,
118120
ICoreWebView2DevToolsProtocolEventReceivedEventArgs* args) -> HRESULT
119121
{
122+
// A target is detached, remove it from the maps.
120123
wil::unique_cotaskmem_string jsonMessage;
121124
CHECK_FAILURE(args->get_ParameterObjectAsJson(&jsonMessage));
122125
std::wstring sessionId = GetJSONStringField(jsonMessage.get(), L"sessionId");
@@ -139,6 +142,7 @@ void MyApp::HandleDevToolsProtocalPTargets()
139142
ICoreWebView2* sender,
140143
ICoreWebView2DevToolsProtocolEventReceivedEventArgs* args) -> HRESULT
141144
{
145+
// A target's info like url changed, update it in the target description map.
142146
wil::unique_cotaskmem_string jsonMessage;
143147
CHECK_FAILURE(args->get_ParameterObjectAsJson(&jsonMessage));
144148
std::wstring targetId = GetJSONStringField(jsonMessage.get(), L"targetId");
@@ -154,6 +158,9 @@ void MyApp::HandleDevToolsProtocalPTargets()
154158
})
155159
.Get(),
156160
&m_targetInfoChangedToken));
161+
// Enable Runtime events to receive Runtime.consoleAPICalled events, which is fired when console.log is called.
162+
m_webView->CallDevToolsProtocolMethod(L"Runtime.enable", L"{}", nullptr);
163+
// Auto attach to iframe and dedicated worker targets created from this target.
157164
m_webView->CallDevToolsProtocolMethod(
158165
L"Target.setAutoAttach",
159166
LR"({"autoAttach":true,"waitForDebuggerOnStart":false,"flatten":true})", nullptr);
@@ -168,6 +175,7 @@ void MyApp::HandleDevToolsProtocalPTargets()
168175
169176
private void CoreWebView2_ConsoleAPICalled(CoreWebView2 sender, CoreWebView2DevToolsProtocolEventReceivedEventArgs args)
170177
{
178+
// Retrieve target info and details for the console.log.
171179
string eventSource;
172180
string sessionId = args.SessionId;
173181
if (sessionId.Length > 0)
@@ -181,22 +189,24 @@ private void CoreWebView2_ConsoleAPICalled(CoreWebView2 sender, CoreWebView2DevT
181189

182190
private void CoreWebView2_AttachedToTarget(CoreWebView2 sender, CoreWebView2DevToolsProtocolEventReceivedEventArgs args)
183191
{
192+
// A new target is attached, add its info to maps.
184193
string jsonMessage = args.ParameterObjectAsJson;
185194
string sessionId = GetJSONStringField(jsonMessage, L"sessionId");
186195
string targetId = GetJSONStringField(jsonMessage, L"targetId");
187196
m_devToolsSessionMap[sessionId] = targetId;
188197
string type = GetJSONStringField(jsonMessage, L"type");
189198
string url = GetJSONStringField(jsonMessage, L"url");
190199
m_devToolsTargetDescriptionMap[targetId] = type + L"," + url;
191-
// Auto attach to targets created from this target.
200+
// Auto attach to targets further created from this target.
192201
_ = m_webview.CallDevToolsProtocolMethodAsync(sessionId, "Target.setAutoAttach",
193202
@"{""autoAttach"":true,""waitForDebuggerOnStart"":false,""flatten"":true}");
194-
// Enable Runtime events to receive Runtime.consoleAPICalled from the target.
203+
// Enable Runtime events to receive Runtime.consoleAPICalled from the target, which is triggered by console.log calls.
195204
m_webview.CallDevToolsProtocolMethodAsync(sessionId, "Runtime.enable", "{}");
196205
}
197206

198207
private void CoreWebView2_DetachedFromTarget(CoreWebView2 sender, CoreWebView2DevToolsProtocolEventReceivedEventArgs args)
199208
{
209+
// A target is detached, remove it from the maps
200210
string jsonMessage = args.ParameterObjectAsJson;
201211
string sessionId = GetJSONStringField(jsonMessage, L"sessionId");
202212
if (m_devToolsSessionMap.ContainsKey(sessionId))
@@ -208,6 +218,7 @@ private void CoreWebView2_DetachedFromTarget(CoreWebView2 sender, CoreWebView2De
208218

209219
private void CoreWebView2_TargetInfoChanged(CoreWebView2 sender, CoreWebView2DevToolsProtocolEventReceivedEventArgs args)
210220
{
221+
// A target's info like url changed, update it in the target description map.
211222
string jsonMessage = args.ParameterObjectAsJson;
212223
string targetId = GetJSONStringField(jsonMessage, L"targetId");
213224
if (m_devToolsTargetDescriptionMap.ContainsKey(targetId))
@@ -224,7 +235,9 @@ private void CoreWebView2_TargetInfoChanged(CoreWebView2 sender, CoreWebView2Dev
224235
m_webview.GetDevToolsProtocolEventReceiver("Target.attachedToTarget").DevToolsProtocolEventReceived += CoreWebView2_AttachedToTarget;
225236
m_webview.GetDevToolsProtocolEventReceiver("Target.detachedFromTarget").DevToolsProtocolEventReceived += CoreWebView2_DetachedFromTarget;
226237
m_webview.GetDevToolsProtocolEventReceiver("Target.targetInfoChanged").DevToolsProtocolEventReceived += CoreWebView2_TargetInfoChanged;
238+
// Enable Runtime events to receive Runtime.consoleAPICalled events, which is fired when console.log is called.
227239
_ = m_webview.CallDevToolsProtocolMethodAsync("Runtime.enable", "{}");
240+
// Auto attach to iframe and dedicated worker targets created from this target.
228241
_ = m_webview.CallDevToolsProtocolMethodAsync("Target.setAutoAttach",
229242
@"{""autoAttach"":true,""waitForDebuggerOnStart"":false,""flatten"":true}");
230243
}

0 commit comments

Comments
 (0)