@@ -47,10 +47,8 @@ also can be used to build the frame tree represented by `FrameInfo`s.
4747# Examples
4848C++
4949``` c++
50- void AppendAncestorFrameInfo (
51- wil::com_ptr<ICoreWebView2FrameInfo> frameInfo, std::wstring& result);
5250void AppendFrameInfo (
53- wil::com_ptr<ICoreWebView2FrameInfo > frameInfo, std::wstring & result);
51+ wil::com_ptr<ICoreWebView2FrameInfo> frameInfo, std::wstringstream & result);
5452wil::com_ptr<ICoreWebView2FrameInfo > GetAncestorFirstLevelFrameInfo(
5553 wil::com_ptr<ICoreWebView2FrameInfo > frameInfo);
5654wil::com_ptr<ICoreWebView2FrameInfo > GetAncestorMainFrameInfo(
@@ -60,7 +58,7 @@ std::wstring FrameKindToString(COREWEBVIEW2_FRAME_KIND kind);
6058// Display renderer process info with details which includes the list of
6159// associated frame infos for the renderer process. Also shows the process
6260// info of other type of process.
63- void ProcessComponent::ShowRendererProcessFrameInfo ()
61+ void ProcessComponent::ShowProcessFrameInfo ()
6462{
6563 auto environment14 =
6664 m_webViewEnvironment.try_query<ICoreWebView2Environment14 >();
@@ -75,8 +73,8 @@ void ProcessComponent::ShowRendererProcessFrameInfo()
7573 UINT32 processCount = 0;
7674 UINT32 rendererProcessCount = 0;
7775 CHECK_FAILURE(processCollection->get_Count(&processCount));
78- std::wstring result ;
79- std::wstring otherProcessResult ;
76+ std::wstringstream otherProcessInfos ;
77+ std::wstringstream rendererProcessInfos ;
8078 for (UINT32 i = 0; i < processCount; i++)
8179 {
8280 Microsoft::WRL::ComPtr<ICoreWebView2ProcessInfo > processInfo;
@@ -88,7 +86,7 @@ void ProcessComponent::ShowRendererProcessFrameInfo()
8886 if (kind == COREWEBVIEW2_PROCESS_KIND_RENDERER)
8987 {
9088 //! [ AssociatedFrameInfos]
91- std::wstring rendererProcessResult ;
89+ std::wstringstream rendererProcess ;
9290 wil::com_ptr<ICoreWebView2ProcessInfo2 > processInfo2;
9391 CHECK_FAILURE(
9492 processInfo->QueryInterface(IID_PPV_ARGS(&processInfo2)));
@@ -105,35 +103,37 @@ void ProcessComponent::ShowRendererProcessFrameInfo()
105103 wil::com_ptr<ICoreWebView2FrameInfo > frameInfo;
106104 CHECK_FAILURE(iterator->GetCurrent(&frameInfo));
107105
108- AppendFrameInfo(frameInfo, rendererProcessResult);
109- AppendAncestorFrameInfo(frameInfo, rendererProcessResult);
106+ AppendFrameInfo(frameInfo, rendererProcess);
110107
111108 BOOL hasNext = FALSE;
112109 CHECK_FAILURE(iterator->MoveNext(&hasNext));
113110 frameInfoCount++;
114111 }
115- rendererProcessResult.insert(0, std::to_wstring(frameInfoCount) +
116- L" frameInfo(s) found in Renderer Process ID:" +
117- std::to_wstring(processId) + L"\n");
118- result.append(rendererProcessResult + L"\n");
112+ rendererProcessInfos
113+ << std::to_wstring(frameInfoCount)
114+ << L" frameInfo(s) found in Renderer Process ID:"
115+ << std::to_wstring(processId) << L"\n"
116+ << rendererProcess.str() << std::endl;
119117 rendererProcessCount++;
120118 //! [AssociatedFrameInfos]
121119 }
122120 else
123121 {
124- otherProcessResult.append(L"Process Id:" + std::to_wstring(processId) +
125- L" | Process Kind:" + ProcessKindToString(kind) + L"\n");
122+ otherProcessInfos << L"Process Id:" << std::to_wstring(processId)
123+ << L" | Process Kind:"
124+ << ProcessKindToString(kind) << std::endl;
126125 }
127126 }
128- result.insert(0, std::to_wstring(processCount) +
129- L" process(es) found, from which " +
130- std::to_wstring(rendererProcessCount) +
131- L" renderer process(es) found\n\n");
132- otherProcessResult.insert(0, L"\nRemaining " +
133- std::to_wstring(processCount - rendererProcessCount) +
134- L" Process(es) Infos:\n");
135- result.append(otherProcessResult);
136- MessageBox(nullptr, result.c_str(), L"Process Info with Associated Frames", MB_OK);
127+ std::wstringstream message;
128+ message << std::to_wstring(processCount)
129+ << L" process(es) found, from which "
130+ << std::to_wstring(rendererProcessCount)
131+ << L" renderer process(es) found\n\n"
132+ << rendererProcessInfos.str() << L"Remaining Process(es) Infos:\n"
133+ << otherProcessInfos.str();
134+
135+ m_appWindow->AsyncMessageBox(
136+ std::move(message.str()), L"Process Info with Associated Frames");
137137 return S_OK;
138138 })
139139 .Get()));
@@ -157,115 +157,66 @@ wil::com_ptr<ICoreWebView2FrameInfo> ProcessComponent::GetAncestorMainFrameInfo(
157157 return mainFrameInfo;
158158}
159159
160- // Get the ancestor first level frameInfo.
161- // Return itself if it's a first level frame.
162- wil::com_ptr<ICoreWebView2FrameInfo > ProcessComponent::GetAncestorFirstLevelFrameInfo(
163- wil::com_ptr<ICoreWebView2FrameInfo > frameInfo)
164- {
165- wil::com_ptr<ICoreWebView2FrameInfo > mainFrameInfo;
166- wil::com_ptr<ICoreWebView2FrameInfo > firstLevelFrameInfo;
167- wil::com_ptr<ICoreWebView2FrameInfo2 > frameInfo2;
168- while (frameInfo)
169- {
170- firstLevelFrameInfo = mainFrameInfo;
171- mainFrameInfo = frameInfo;
172- CHECK_FAILURE(frameInfo->QueryInterface(IID_PPV_ARGS(&frameInfo2)));
173- CHECK_FAILURE(frameInfo2->get_ParentFrameInfo(&frameInfo));
174- }
175- return firstLevelFrameInfo;
176- }
177-
178- // Append the frameInfo's properties.
179160void ProcessComponent::AppendFrameInfo(
180- wil::com_ptr<ICoreWebView2FrameInfo > frameInfo, std::wstring & result)
161+ wil::com_ptr<ICoreWebView2FrameInfo > frameInfo, std::wstringstream & result)
181162{
182- if (!frameInfo)
183- {
184- return ;
185- }
186-
163+ UINT32 frameId = 0;
164+ UINT32 parentFrameId = 0;
165+ UINT32 mainFrameId = 0 ;
166+ UINT32 firstLevelFrameId = 0;
167+ std::wstring type = L"other child frame";
187168 wil::unique_cotaskmem_string nameRaw;
169+ wil::unique_cotaskmem_string sourceRaw;
170+ COREWEBVIEW2_FRAME_KIND frameKind = COREWEBVIEW2_FRAME_KIND_OTHER;
171+
188172 CHECK_FAILURE(frameInfo->get_Name(&nameRaw));
189- result.append(L"{frame name:");
190- result.append(nameRaw.get());
173+ std::wstring name = ((std::wstring)(nameRaw.get())).empty() ? L"none" : nameRaw.get();
174+ CHECK_FAILURE(frameInfo->get_Source(&sourceRaw));
175+ std::wstring source = ((std::wstring)(sourceRaw.get())).empty() ? L"none" : sourceRaw.get();
191176
192177 wil::com_ptr<ICoreWebView2FrameInfo2> frameInfo2;
193178 CHECK_FAILURE(frameInfo->QueryInterface(IID_PPV_ARGS(&frameInfo2)));
194- UINT32 frameId = 0;
195179 frameInfo2->get_FrameId(&frameId);
196- result.append(L" | frame Id:" + std::to_wstring(frameId));
197-
198- BOOL isMainFrameOrFirstLevelframeInfo = false;
199- wil::com_ptr<ICoreWebView2FrameInfo> mainFrameInfo =
200- GetAncestorMainFrameInfo(frameInfo);
201- wil::com_ptr<ICoreWebView2FrameInfo> firstLevelFrameInfo =
202- GetAncestorFirstLevelFrameInfo(frameInfo);
203- // Check if a frame is a main frame.
204- if (mainFrameInfo == frameInfo)
205- {
206- result.append(L" | frame type: main frame");
207- isMainFrameOrFirstLevelframeInfo = true;
208- }
209- // Check if a frame is a first level frame.
210- if (firstLevelFrameInfo == frameInfo)
211- {
212- result.append(L" | frame type: first level frame");
213- isMainFrameOrFirstLevelframeInfo = true;
214- }
215- if (!isMainFrameOrFirstLevelframeInfo)
216- {
217- result.append(L" | frame type: other child frame");
218- }
219-
220- COREWEBVIEW2_FRAME_KIND frameKind = COREWEBVIEW2_FRAME_KIND_OTHER;
221180 frameInfo2->get_FrameKind(&frameKind);
222- result.append(L"\n | frame kind:" + FrameKindToString(frameKind));
223181
224- // Append the frame's direct parent frame's ID if it exists.
225182 wil::com_ptr<ICoreWebView2FrameInfo> parentFrameInfo;
226183 CHECK_FAILURE(frameInfo2->get_ParentFrameInfo(&parentFrameInfo));
227184 if (parentFrameInfo)
228185 {
229186 CHECK_FAILURE(parentFrameInfo->QueryInterface(IID_PPV_ARGS(&frameInfo2)));
230- CHECK_FAILURE(frameInfo2->get_FrameId(&frameId));
231- result.append(L" | parent frame Id:" + std::to_wstring(frameId));
187+ CHECK_FAILURE(frameInfo2->get_FrameId(&parentFrameId));
232188 }
233189
234- wil::unique_cotaskmem_string sourceRaw;
235- CHECK_FAILURE(frameInfo->get_Source(&sourceRaw));
236- result.append(L"\n | frame source:\n\"");
237- result.append(sourceRaw.get());
238- result.append(L"\"");
239- }
240-
241- // Append the frameInfo's ancestor main frame(webview)'s ID and
242- // ancestor first level frame's ID if it exists.
243- void ProcessComponent::AppendAncestorFrameInfo(
244- wil::com_ptr<ICoreWebView2FrameInfo > frameInfo, std::wstring& result)
245- {
246- if (frameInfo)
190+ wil::com_ptr<ICoreWebView2FrameInfo> mainFrameInfo = GetAncestorMainFrameInfo(frameInfo);
191+ if (mainFrameInfo == frameInfo)
247192 {
248- return ;
193+ type = L"main frame" ;
249194 }
195+ CHECK_FAILURE(mainFrameInfo->QueryInterface(IID_PPV_ARGS(&frameInfo2)));
196+ CHECK_FAILURE(frameInfo2->get_FrameId(&mainFrameId));
250197
251- wil::com_ptr<ICoreWebView2FrameInfo> mainFrameInfo = GetAncestorMainFrameInfo(frameInfo);
252198 wil::com_ptr<ICoreWebView2FrameInfo> firstLevelFrameInfo =
253199 GetAncestorFirstLevelFrameInfo(frameInfo);
254- wil::com_ptr<ICoreWebView2FrameInfo2> frameInfo2;
255- UINT32 frameId = 0;
256- if (firstLevelFrameInfo)
200+ if (firstLevelFrameInfo == frameInfo)
257201 {
258- CHECK_FAILURE(firstLevelFrameInfo->QueryInterface(IID_PPV_ARGS(&frameInfo2)));
259- CHECK_FAILURE(frameInfo2->get_FrameId(&frameId));
260- result.append(L"\n | ancestor first level frame Id:" + std::to_wstring(frameId));
202+ type = L"first level frame";
261203 }
262- if (mainFrameInfo )
204+ if (firstLevelFrameInfo )
263205 {
264- CHECK_FAILURE(mainFrameInfo->QueryInterface(IID_PPV_ARGS(&frameInfo2)));
265- CHECK_FAILURE(frameInfo2->get_FrameId(&frameId ));
266- result.append(L"\n | ancestor main frame Id:" + std::to_wstring(frameId ));
206+ CHECK_FAILURE(
207+ firstLevelFrameInfo->QueryInterface(IID_PPV_ARGS(&frameInfo2) ));
208+ CHECK_FAILURE(frameInfo2->get_FrameId(&firstLevelFrameId ));
267209 }
268- result.append(L"},\n");
210+
211+ result << L"{frame name:" << name << L" | frame Id:" << std::to_wstring(frameId)
212+ << L" | parent frame Id:"
213+ << ((parentFrameId == 0) ? L"none" : std::to_wstring(parentFrameId))
214+ << L" | frame type:" << type << L"\n"
215+ << L" | ancestor main frame Id:" << std::to_wstring(mainFrameId)
216+ << L" | ancestor first level frame Id:"
217+ << ((firstLevelFrameId == 0) ? L"none" : std::to_wstring(firstLevelFrameId)) << L"\n"
218+ << L" | frame kind:" << FrameKindToString(frameKind) << L"\n"
219+ << L" | frame source:" << source << L"}," << std::endl;
269220}
270221
271222// Get a string for the frame kind enum value.
@@ -286,90 +237,96 @@ std::wstring ProcessComponent::FrameKindToString(const COREWEBVIEW2_FRAME_KIND k
286237```
287238C#
288239```c#
289- string AppendFrameInfo(CoreWebView2FrameInfo frameInfo, string type, string mainFrameId, string firstLevelFrameId ) {
240+ string AppendFrameInfo(CoreWebView2FrameInfo frameInfo) {
290241 string id = frameInfo.FrameId.ToString();
242+ string kind = frameInfo.FrameKind.ToString();
291243 string name = String.IsNullOrEmpty(frameInfo.Name) ? "none" : frameInfo.Name;
292244 string source = String.IsNullOrEmpty(frameInfo.Source) ? "none" : frameInfo.Source;
293245 string parentId = frameInfo.ParentFrameInfo == null ? "none" : frameInfo.ParentFrameInfo.FrameId.ToString();
246+ string type = "other frame";
247+
248+ CoreWebView2FrameInfo mainFrame = GetAncestorMainFrameInfo(frameInfo);
249+ string mainFrameId = mainFrame.FrameId.ToString();
250+ if (frameInfo == mainFrame) {
251+ type = "main frame";
252+ }
253+
254+ CoreWebView2FrameInfo firstLevelFrame = GetAncestorFirstLevelFrameInfo(frameInfo);
255+ string firstLevelFrameId = firstLevelFrame == null ? "none" : firstLevelFrame.FrameId.ToString();
256+ if (frameInfo == firstLevelFrame) {
257+ type = "first level frame";
258+ }
294259
295260 return $"{{frame Id:{id} " +
296261 $"| frame Name: {name} " +
297- $"| frame type : {type} " +
298- $"| parent frame Id: {parentId} " +
262+ $"| frame Type : {type} " +
263+ $"| parent frame Id: {parentId} \n " +
299264 $"| ancestor main frame Id: {mainFrameId} " +
300- $"| ancestor first level frame Id: {firstLevelFrameId} " +
301- $"| frame Kind: {frameInfo.FrameKind } " +
265+ $"| ancestor first level frame Id: {firstLevelFrameId} \n " +
266+ $"| frame Kind: {kind } " +
302267 $"| frame Source: \"{source}\"}}\n";
303268}
304269
305- // Display renderer process info with details which includes the list of
306- // associated frame infos for the renderer process. Also shows the process
307- // info of other type of process.
270+ CoreWebView2FrameInfo GetAncestorMainFrameInfo(CoreWebView2FrameInfo frameInfo) {
271+ while (frameInfo.ParentFrameInfo != null) {
272+ frameInfo = frameInfo.ParentFrameInfo;
273+ }
274+ return frameInfo;
275+ }
276+
277+ CoreWebView2FrameInfo GetAncestorFirstLevelFrameInfo(CoreWebView2FrameInfo frameInfo) {
278+ if (frameInfo.ParentFrameInfo == null) {
279+ return null;
280+ }
281+
282+ CoreWebView2FrameInfo firstLevelFrameInfo = null;
283+ CoreWebView2FrameInfo mainFrameInfo = null;
284+ while (frameInfo != null) {
285+ firstLevelFrameInfo = mainFrameInfo;
286+ mainFrameInfo = frameInfo;
287+ frameInfo = frameInfo.ParentFrameInfo;
288+ }
289+ return firstLevelFrameInfo;
290+ }
291+
308292private async void ProcessFrameInfoCmdExecuted(object target, ExecutedRoutedEventArgs e)
309293{
310294 try
311295 {
312296 // <GetProcessInfosWithDetails>
313297 IReadOnlyList<CoreWebView2ProcessInfo> processList = await webView.CoreWebView2.Environment.GetProcessInfosWithDetailsAsync();
314- int processListCount = processList.Count;
315- string rendererProcessInfosStr = $"{processListCount} process(es) found in total\n\n ";
316- string otherProcessInfosStr = $"\nRemaining Process Infos:\n ";
298+ int processCount = processList.Count;
299+ string rendererProcessInfos = " ";
300+ string otherProcessInfos = " ";
317301 int rendererProcessCount = 0;
318- for (int i = 0; i < processListCount ; ++i)
302+ for (int i = 0; i < processCount ; ++i)
319303 {
320304 CoreWebView2ProcessKind kind = processList[i].Kind;
321305 int processId = processList[i].ProcessId;
322306 if (kind == CoreWebView2ProcessKind.Renderer)
323307 {
324308 int frameInfoCount = 0;
325- string frameInfosStr = "";
309+ string frameInfos = "";
326310 // <AssociatedFrameInfos>
327311 IReadOnlyList<CoreWebView2FrameInfo> frameInfoList = processList[i].AssociatedFrameInfos;
328312 foreach (CoreWebView2FrameInfo frameInfo in frameInfoList)
329313 {
330- string ancestorMainFrameId = "none";
331- string ancestorFirstLevelFrameId = "none";
332- CoreWebView2FrameInfo parentFrameInfo = frameInfo.ParentFrameInfo;
333314 frameInfoCount++;
334- // If the frame has no parent, then it's a main frame.
335- if (parentFrameInfo == null)
336- {
337- ancestorMainFrameId = frameInfo.FrameId.ToString();
338- frameInfosStr += AppendFrameInfo(frameInfo, "main frame", ancestorMainFrameId, ancestorFirstLevelFrameId);
339- continue;
340- }
341-
342- CoreWebView2FrameInfo mainFrameInfo = parentFrameInfo;
343- CoreWebView2FrameInfo firstLevelFrameInfo = frameInfo;
344- // If the frame's parent has no parent frame, then it's a first level frame.
345- if (mainFrameInfo.ParentFrameInfo == null) {
346- ancestorMainFrameId = mainFrameInfo.FrameId.ToString();
347- ancestorFirstLevelFrameId = firstLevelFrameInfo.FrameId.ToString();
348- frameInfosStr += AppendFrameInfo(frameInfo, "first level frame", ancestorMainFrameId, ancestorFirstLevelFrameId);
349- continue;
350- }
351- // For other child frames, we traverse the parent frame until find the ancestor main frame.
352- while (mainFrameInfo.ParentFrameInfo != null) {
353- firstLevelFrameInfo = mainFrameInfo;
354- mainFrameInfo = mainFrameInfo.ParentFrameInfo;
355- }
356-
357- ancestorMainFrameId = mainFrameInfo.FrameId.ToString();
358- ancestorFirstLevelFrameId = firstLevelFrameInfo.FrameId.ToString();
359- frameInfosStr += AppendFrameInfo(frameInfo, "other child frame", ancestorMainFrameId, ancestorFirstLevelFrameId);
315+ frameInfos += AppendFrameInfo(frameInfo);
360316 }
361317 // </AssociatedFrameInfos>
362- string rendererProcessInfoStr = $"{frameInfoCount} frame info(s) found in renderer process ID: {processId}\n {frameInfosStr }";
363- rendererProcessInfosStr += $"{rendererProcessInfoStr } \n";
318+ string rendererProcessInfo = $"{frameInfoCount} frame info(s) found in renderer process ID: {processId}\n {frameInfos }";
319+ rendererProcessInfos += $"{rendererProcessInfo } \n";
364320 rendererProcessCount++;
365321 }
366322 else
367323 {
368- otherProcessInfosStr += $"Process ID: {processId} | Process Kind: {kind}\n";
324+ otherProcessInfos += $"Process ID: {processId} | Process Kind: {kind}\n";
369325 }
370326 }
371327 // </GetProcessInfosWithDetails>
372- string message = $"{rendererProcessCount} renderer process(es) found, {rendererProcessInfosStr + otherProcessInfosStr}";
328+ string message = $"{processCount} process(es) found in total, from which {rendererProcessCount} renderer process(es) found\n\n" +
329+ $"{rendererProcessInfos}\nRemaining Process Infos:\n{otherProcessInfos}";
373330 MessageBox.Show(this, message, "Process Info with Associated Frames");
374331 }
375332 catch (NotImplementedException exception)
@@ -479,7 +436,7 @@ interface ICoreWebView2_18 : ICoreWebView2_17 {
479436 /// the ` FrameId ` in ` CoreWebView2Frame ` and via ` CoreWebView2FrameInfo ` .
480437 /// Note that ` FrameId ` may not be valid if ` CoreWebView ` has not done
481438 /// any navigation. It's safe to get this value during or after the first
482- /// ` DOMContentLoaded ` event. Otherwise, it could return the invalid frame Id 0.
439+ /// ` ContentLoading ` event. Otherwise, it could return the invalid frame Id 0.
483440 [ propget] HRESULT FrameId([ out, retval] UINT32* id);
484441}
485442```
0 commit comments