11# Background
2- A Favicon is an asset which is a part of every webpage, and typically displayed on each tab. Developers would
3- like to have an API which allows them to retrieve the Favicon of a webpage, as well as get an update whenever
2+ A Favicon is an asset which is a part of a webpage, and typically displayed on each tab. Developers would
3+ like to have an API which allows them to retrieve the Favicon of a webpage, if it has one, as well as get an update whenever
44the favicon has changed.
55
66# Description
7- We propose a new Webview2 event which would allow developers to get access to the current Favicon of a page,
7+ We propose a new Webview2 event which would allow developers access the current Favicon of a page,
88as well as be notified when the favicon changes.
99
1010# Examples
1111## Win32 C++ Registering a listener for favicon changes
12- ```cpp
13- CHECK_FAILURE(m_webView->add_FaviconChanged(
14- Microsoft::WRL::Callback<ICoreWebView2FaviconUpdateEventHandler>(
15- [this](ICoreWebView2* sender, IUnknown* args) -> HRESULT
16- {
12+ ``` cpp
13+ CHECK_FAILURE (m_webViewStaging4->add_FaviconChanged(
14+ Callback<ICoreWebView2StagingFaviconChangedEventHandler >(
15+ [ this] (ICoreWebView2Staging4* sender, IUnknown* args) -> HRESULT {
1716
18- LPWSTR value;
19- CHECK_FAILURE(sender->get_FaviconUrl(&value));
17+ wil::unique_cotaskmem_string url;
18+ CHECK_FAILURE(sender->get_FaviconUri(&url));
19+ wil::com_ptr<IStream > pStream = SHCreateMemStream(nullptr, 0);
2020
21+ sender->GetFavicon(
22+ COREWEBVIEW2_FAVICON_IMAGE_FORMAT_PNG,
23+ pStream,
24+ Callback<
25+ ICoreWebView2StagingGetFaviconCompletedHandler>(
26+ [&pStream, this](HRESULT code) -> HRESULT {
27+ Gdiplus::Bitmap* pBitmap = new Gdiplus::Bitmap(pStream);
28+ HICON icon;
29+ pBitmap->GetHICON(&icon);
30+ SendMessage(
31+ m_appWindow->GetMainWindow(), WM_SETICON, ICON_SMALL,
32+ (LPARAM)icon);
33+ return S_OK;
34+ })
35+ .Get());
2136
2237 return S_OK;
23- }
24- ) .Get(),
38+ })
39+ .Get(),
2540 &m_faviconChangedToken));
26- ```
41+ ```
2742## .NET / WinRT Registering a listener for favicon changes
2843```c#
29- webView .CoreWebView2 .FaviconChanged += (CoreWebView2 sender , Object arg ) =>
44+ webView.CoreWebView2.FaviconUriChanged += (CoreWebView2 sender, Object arg) =>
3045{
31- string value = sender .faviconUrl ;
46+ System.IO.Stream stream = new System.IO.MemoryStream();
47+ await webView.CoreWebView2.GetFaviconAsync(
48+ CoreWebView2FaviconImageFormat.Png,
49+ stream);
50+ // setting the window Icon to the bitmap
51+ this.Icon = BitmapFrame.Create(stream);
3252
3353};
3454```
3555# API Notes
56+ If a Web page does not have a Favicon, then the event is not fired.
3657See [ API Details] ( #api-details ) Section below for API reference
3758# API Details
3859## Win32 C++
@@ -47,6 +68,13 @@ interface ICoreWebView2FaviconChangedEventHandler : IUnknown {
4768 [ in] IUnknown* args);
4869}
4970
71+ [uuid(93ACC5AD-DC22-419E-9A3F-75D96A1538E4), object, pointer_default(unique)]
72+ interface ICoreWebView2GetFaviconCompletedHandler : IUnknown {
73+ /// Called to provide the implementer with the event args for the
74+ /// corresponding event.
75+ HRESULT Invoke([ in] HRESULT error_code);
76+ }
77+
5078[uuid(DC838C64-F64B-4DC7-98EC-0992108E2157 ), object, pointer_default(unique)]
5179interface ICoreWebView2_5 : ICoreWebView2_4 {
5280 /// Add an event handler for the ` FaviconChanged ` event.
@@ -59,19 +87,43 @@ interface ICoreWebView2_5 : ICoreWebView2_4 {
5987 HRESULT remove_FaviconChanged(
6088 [ in] EventRegistrationToken token);
6189
90+ /// Async function for getting the actual image data of the favicon
91+ HRESULT GetFaviconAsync(
92+ [ in] COREWEBVIEW2_FAVICON_IMAGE_FORMAT format,
93+ [ in] IStream * imageStream,
94+ [ in] ICoreWebView2GetFaviconCompletedHandler* eventHandler
95+ );
96+
6297 /// used to access the current value of the favicon
98+ /// If a page has no favicon then this returns a nullptr
6399 [ propget] HRESULT FaviconUri([ out, retval] LPWSTR* value);
100+
101+ }
102+ [v1_enum]
103+ typedef enum COREWEBVIEW2_FAVICON_IMAGE_FORMAT {
104+ /// Indicates that the function should return the favicon in PNG format
105+ COREWEBVIEW2_CAPTURE_PREVIEW_IMAGE_FORMAT_PNG,
106+
107+ /// Indicates that the function should return the favicon in JPG format
108+ COREWEBVIEW2_CAPTURE_PREVIEW_IMAGE_FORMAT_JPEG,
64109}
65110```
66111
67112## .Net/ WinRT
68113``` c#
69114namespace Microsoft .Web .WebView2 .Core {
70115
71- /// Interface for the Favicon changed event handler
116+ enum CoreWebView2FaviconImageFormat
117+ {
118+ Png = 0 ,
119+ Jpeg = 1 ,
120+ };
121+
122+ /// Interface for the Favicon uri changed event handler
72123 runtimeclass CoreWebView2 {
124+ Windows.Foundation.IAsyncAction GetFaviconAsync(CoreWebView2FaviconImageFormat imageFormat, Windows.Storage.Streams.IRandomAccessStream imageStream);
73125 event Windows.Foundation.TypedEventHandler<CoreWebView2, Object> FaviconChanged;
74- string FaviconUrl {get;};
126+ string FaviconUri {get;};
75127 }
76128}
77129```
0 commit comments