Skip to content

Commit 78b4c7d

Browse files
committed
Fix BrowserBookmark '100% CPU' issue
# Fix BrowserBookmark plugin locking threads at 100% CPU The following files were modified to implement this fix: 1. /Flow.Launcher.Plugin.BrowserBookmark.csproj - packages SkiaSharp and Svg.Skia added to output WEBP 2. /Helper/FaviconHelper.cs - new method, TryConvertToWebp, added to take any image data and encode to WEBP 3. /FirefoxBookmarkLoader.cs - LoadFaviconsFromDb to use the new helper which ensures only safe WEBP files are used by the UI, which resolves the CPU-locking. It was GZIPped SVGs that were causing the thread / CPU lock.
1 parent 3cdf919 commit 78b4c7d

File tree

3 files changed

+82
-30
lines changed

3 files changed

+82
-30
lines changed

Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.IO;
5+
using System.IO.Compression;
56
using System.Linq;
67
using System.Threading.Tasks;
78
using Flow.Launcher.Plugin.BrowserBookmark.Helper;
@@ -134,10 +135,6 @@ private void LoadFaviconsFromDb(string dbPath, List<Bookmark> bookmarks)
134135

135136
try
136137
{
137-
if (string.IsNullOrEmpty(bookmark.Url))
138-
return;
139-
140-
// Extract domain from URL
141138
if (!Uri.TryCreate(bookmark.Url, UriKind.Absolute, out Uri uri))
142139
return;
143140

@@ -146,43 +143,48 @@ private void LoadFaviconsFromDb(string dbPath, List<Bookmark> bookmarks)
146143
// Query for latest Firefox version favicon structure
147144
using var cmd = connection.CreateCommand();
148145
cmd.CommandText = @"
149-
SELECT i.data
146+
SELECT i.id, i.data
150147
FROM moz_icons i
151148
JOIN moz_icons_to_pages ip ON i.id = ip.icon_id
152149
JOIN moz_pages_w_icons p ON ip.page_id = p.id
153-
WHERE p.page_url LIKE @url
154-
AND i.data IS NOT NULL
155-
ORDER BY i.width DESC -- Select largest icon available
150+
WHERE p.page_url LIKE @domain
151+
ORDER BY i.width DESC
156152
LIMIT 1";
157153

158-
cmd.Parameters.AddWithValue("@url", $"%{domain}%");
154+
cmd.Parameters.AddWithValue("@domain", $"%{domain}%");
159155

160156
using var reader = cmd.ExecuteReader();
161-
if (!reader.Read() || reader.IsDBNull(0))
157+
if (!reader.Read() || reader.IsDBNull(1))
162158
return;
163159

160+
var iconId = reader.GetInt64(0).ToString();
164161
var imageData = (byte[])reader["data"];
165162

166163
if (imageData is not { Length: > 0 })
167164
return;
168-
169-
string faviconPath;
170-
if (FaviconHelper.IsSvgData(imageData))
165+
166+
if (imageData.Length > 2 && imageData[0] == 0x1f && imageData[1] == 0x8b)
171167
{
172-
faviconPath = Path.Combine(_faviconCacheDir, $"firefox_{domain}.svg");
168+
using var inputStream = new MemoryStream(imageData);
169+
using var gZipStream = new GZipStream(inputStream, CompressionMode.Decompress);
170+
using var outputStream = new MemoryStream();
171+
gZipStream.CopyTo(outputStream);
172+
imageData = outputStream.ToArray();
173173
}
174-
else
174+
175+
var webpData = FaviconHelper.TryConvertToWebp(imageData);
176+
177+
if (webpData != null)
175178
{
176-
faviconPath = Path.Combine(_faviconCacheDir, $"firefox_{domain}.png");
177-
}
179+
var faviconPath = Path.Combine(_faviconCacheDir, $"firefox_{domain}_{iconId}.webp");
178180

179-
// Filter out duplicate favicons
180-
if (savedPaths.TryAdd(faviconPath, true))
181-
{
182-
FaviconHelper.SaveBitmapData(imageData, faviconPath);
183-
}
181+
if (savedPaths.TryAdd(faviconPath, true))
182+
{
183+
FaviconHelper.SaveBitmapData(webpData, faviconPath);
184+
}
184185

185-
bookmark.FaviconPath = faviconPath;
186+
bookmark.FaviconPath = faviconPath;
187+
}
186188
}
187189
catch (Exception ex)
188190
{

Plugins/Flow.Launcher.Plugin.BrowserBookmark/Flow.Launcher.Plugin.BrowserBookmark.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@
9797
<ItemGroup>
9898
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
9999
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.3" />
100+
<PackageReference Include="Svg.Skia" Version="3.0.3" />
101+
<PackageReference Include="SkiaSharp" Version="3.119.0" />
100102
</ItemGroup>
101103

102104
</Project>

Plugins/Flow.Launcher.Plugin.BrowserBookmark/Helper/FaviconHelper.cs

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.IO;
3+
using SkiaSharp;
4+
using Svg.Skia;
35

46
namespace Flow.Launcher.Plugin.BrowserBookmark.Helper;
57

@@ -65,12 +67,58 @@ public static void SaveBitmapData(byte[] imageData, string outputPath)
6567
}
6668
}
6769

68-
public static bool IsSvgData(byte[] data)
70+
public static byte[] TryConvertToWebp(byte[] data)
6971
{
70-
if (data.Length < 5)
71-
return false;
72-
string start = System.Text.Encoding.ASCII.GetString(data, 0, Math.Min(100, data.Length));
73-
return start.Contains("<svg") ||
74-
(start.StartsWith("<?xml") && start.Contains("<svg"));
72+
if (data == null || data.Length == 0)
73+
return null;
74+
75+
SKBitmap bitmap = null;
76+
77+
try
78+
{
79+
using (var ms = new MemoryStream(data))
80+
{
81+
var svg = new SKSvg();
82+
if (svg.Load(ms) != null && svg.Picture != null)
83+
{
84+
bitmap = new SKBitmap((int)svg.Picture.CullRect.Width, (int)svg.Picture.CullRect.Height);
85+
using (var canvas = new SKCanvas(bitmap))
86+
{
87+
canvas.Clear(SKColors.Transparent);
88+
canvas.DrawPicture(svg.Picture);
89+
canvas.Flush();
90+
}
91+
}
92+
}
93+
}
94+
catch { /* Not an SVG */ }
95+
96+
if (bitmap == null)
97+
{
98+
try
99+
{
100+
bitmap = SKBitmap.Decode(data);
101+
}
102+
catch { /* Not a decodable bitmap */ }
103+
}
104+
105+
if (bitmap != null)
106+
{
107+
try
108+
{
109+
using (var image = SKImage.FromBitmap(bitmap))
110+
using (var webp = image.Encode(SKEncodedImageFormat.Webp, 65))
111+
{
112+
if (webp != null)
113+
return webp.ToArray();
114+
}
115+
}
116+
finally
117+
{
118+
bitmap.Dispose();
119+
}
120+
}
121+
122+
return null;
75123
}
76-
}
124+
}

0 commit comments

Comments
 (0)