Skip to content

Fix BrowserBookmark '100% CPU' issue #3826

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using Flow.Launcher.Plugin.BrowserBookmark.Helper;
Expand All @@ -25,10 +26,10 @@

// Updated query - removed favicon_id column
private const string QueryAllBookmarks = """
SELECT moz_places.url, moz_bookmarks.title

Check warning on line 29 in Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`moz` is not a recognized word. (unrecognized-spelling)
FROM moz_places

Check warning on line 30 in Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`moz` is not a recognized word. (unrecognized-spelling)
INNER JOIN moz_bookmarks ON (

Check warning on line 31 in Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`moz` is not a recognized word. (unrecognized-spelling)
moz_bookmarks.fk NOT NULL AND moz_bookmarks.title NOT NULL AND moz_bookmarks.fk = moz_places.id

Check warning on line 32 in Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`moz` is not a recognized word. (unrecognized-spelling)
)
ORDER BY moz_places.visit_count DESC
""";
Expand Down Expand Up @@ -79,9 +80,9 @@
.ToList();

// Load favicons after loading bookmarks
if (Main._settings.EnableFavicons)

Check warning on line 83 in Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`Favicons` is not a recognized word. (unrecognized-spelling)
{
var faviconDbPath = Path.Combine(Path.GetDirectoryName(placesPath), "favicons.sqlite");

Check warning on line 85 in Plugins/Flow.Launcher.Plugin.BrowserBookmark/FirefoxBookmarkLoader.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`favicons` is not a recognized word. (unrecognized-spelling)
if (File.Exists(faviconDbPath))
{
Main._context.API.StopwatchLogInfo(ClassName, $"Load {bookmarks.Count} favicons cost", () =>
Expand Down Expand Up @@ -134,10 +135,6 @@

try
{
if (string.IsNullOrEmpty(bookmark.Url))
return;

// Extract domain from URL
if (!Uri.TryCreate(bookmark.Url, UriKind.Absolute, out Uri uri))
return;

Expand All @@ -146,43 +143,48 @@
// Query for latest Firefox version favicon structure
using var cmd = connection.CreateCommand();
cmd.CommandText = @"
SELECT i.data
SELECT i.id, i.data
FROM moz_icons i
JOIN moz_icons_to_pages ip ON i.id = ip.icon_id
JOIN moz_pages_w_icons p ON ip.page_id = p.id
WHERE p.page_url LIKE @url
AND i.data IS NOT NULL
ORDER BY i.width DESC -- Select largest icon available
WHERE p.page_url LIKE @domain
ORDER BY i.width DESC
LIMIT 1";

cmd.Parameters.AddWithValue("@url", $"%{domain}%");
cmd.Parameters.AddWithValue("@domain", $"%{domain}%");

using var reader = cmd.ExecuteReader();
if (!reader.Read() || reader.IsDBNull(0))
if (!reader.Read() || reader.IsDBNull(1))
return;

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

if (imageData is not { Length: > 0 })
return;

string faviconPath;
if (FaviconHelper.IsSvgData(imageData))

if (imageData.Length > 2 && imageData[0] == 0x1f && imageData[1] == 0x8b)
{
faviconPath = Path.Combine(_faviconCacheDir, $"firefox_{domain}.svg");
using var inputStream = new MemoryStream(imageData);
using var gZipStream = new GZipStream(inputStream, CompressionMode.Decompress);
using var outputStream = new MemoryStream();
gZipStream.CopyTo(outputStream);
imageData = outputStream.ToArray();
}
else

var webpData = FaviconHelper.TryConvertToWebp(imageData);

if (webpData != null)
{
faviconPath = Path.Combine(_faviconCacheDir, $"firefox_{domain}.png");
}
var faviconPath = Path.Combine(_faviconCacheDir, $"firefox_{domain}_{iconId}.webp");

// Filter out duplicate favicons
if (savedPaths.TryAdd(faviconPath, true))
{
FaviconHelper.SaveBitmapData(imageData, faviconPath);
}
if (savedPaths.TryAdd(faviconPath, true))
{
FaviconHelper.SaveBitmapData(webpData, faviconPath);
}

bookmark.FaviconPath = faviconPath;
bookmark.FaviconPath = faviconPath;
}
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.3" />
<PackageReference Include="Svg.Skia" Version="3.0.3" />
<PackageReference Include="SkiaSharp" Version="3.119.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.IO;
using SkiaSharp;
using Svg.Skia;

namespace Flow.Launcher.Plugin.BrowserBookmark.Helper;

Expand Down Expand Up @@ -65,12 +67,58 @@ public static void SaveBitmapData(byte[] imageData, string outputPath)
}
}

public static bool IsSvgData(byte[] data)
public static byte[] TryConvertToWebp(byte[] data)
{
if (data.Length < 5)
return false;
string start = System.Text.Encoding.ASCII.GetString(data, 0, Math.Min(100, data.Length));
return start.Contains("<svg") ||
(start.StartsWith("<?xml") && start.Contains("<svg"));
if (data == null || data.Length == 0)
return null;

SKBitmap bitmap = null;

try
{
using (var ms = new MemoryStream(data))
{
var svg = new SKSvg();
if (svg.Load(ms) != null && svg.Picture != null)
{
bitmap = new SKBitmap((int)svg.Picture.CullRect.Width, (int)svg.Picture.CullRect.Height);
using (var canvas = new SKCanvas(bitmap))
{
canvas.Clear(SKColors.Transparent);
canvas.DrawPicture(svg.Picture);
canvas.Flush();
}
}
}
}
catch { /* Not an SVG */ }

if (bitmap == null)
{
try
{
bitmap = SKBitmap.Decode(data);
}
catch { /* Not a decodable bitmap */ }
}

if (bitmap != null)
{
try
{
using (var image = SKImage.FromBitmap(bitmap))
using (var webp = image.Encode(SKEncodedImageFormat.Webp, 65))
{
if (webp != null)
return webp.ToArray();
}
}
finally
{
bitmap.Dispose();
}
}

return null;
}
}
}
Loading