harden bookmarks bridge

This commit is contained in:
Fabian Freund
2026-06-01 16:27:43 +02:00
parent e66fbb2b41
commit 7f318277d8
2 changed files with 42 additions and 10 deletions
@@ -191,6 +191,7 @@ class GeckoBookmarksApiImpl() : GeckoBookmarksApi {
title = info.title, title = info.title,
url = info.url url = info.url
) )
val oldNode = components.core.bookmarksStorage.getBookmark(guid).getOrNull()
val result = components.core.bookmarksStorage.updateNode(guid, conceptInfo) val result = components.core.bookmarksStorage.updateNode(guid, conceptInfo)
result.fold( result.fold(
{ callback(Result.success(Unit)) }, { callback(Result.success(Unit)) },
@@ -199,10 +200,10 @@ class GeckoBookmarksApiImpl() : GeckoBookmarksApi {
if (result.isSuccess) { if (result.isSuccess) {
components.core.bookmarksStorage.getBookmark(guid).getOrNull()?.let { node -> components.core.bookmarksStorage.getBookmark(guid).getOrNull()?.let { node ->
if (info.title != null || info.url != null) { if (info.title != null || info.url != null) {
GeckoBookmarksExtensionBridge.emitChanged(node) GeckoBookmarksExtensionBridge.emitChanged(node, oldNode)
} }
if (info.parentGuid != null || info.position != null) { if (info.parentGuid != null || info.position != null) {
GeckoBookmarksExtensionBridge.emitMoved(node) GeckoBookmarksExtensionBridge.emitMoved(node, oldNode)
} }
} }
} }
@@ -249,6 +249,12 @@ object GeckoBookmarksExtensionBridge : BundleEventListener {
.getOrNull()?.children?.size ?: 0).toUInt() .getOrNull()?.children?.size ?: 0).toUInt()
} }
val oldNode = store.getBookmark(guid).getOrNull()
if (oldNode == null) {
callback.sendError("Bookmark not found: $guid")
return
}
store.updateNode( store.updateNode(
guid, guid,
BookmarkInfo(parentGuid = parentGuid, position = position, title = null, url = null), BookmarkInfo(parentGuid = parentGuid, position = position, title = null, url = null),
@@ -259,7 +265,7 @@ object GeckoBookmarksExtensionBridge : BundleEventListener {
callback.sendError("Bookmark not found: $guid") callback.sendError("Bookmark not found: $guid")
return return
} }
emitMoved(node) emitMoved(node, oldNode)
callback.sendSuccess(nodeToBundle(node, includeChildren = false)) callback.sendSuccess(nodeToBundle(node, includeChildren = false))
} }
@@ -276,6 +282,12 @@ object GeckoBookmarksExtensionBridge : BundleEventListener {
val title = message.getString("title") val title = message.getString("title")
val url = message.getString("url") val url = message.getString("url")
val oldNode = store.getBookmark(guid).getOrNull()
if (oldNode == null) {
callback.sendError("Bookmark not found: $guid")
return
}
store.updateNode( store.updateNode(
guid, guid,
BookmarkInfo(parentGuid = null, position = null, title = title, url = url), BookmarkInfo(parentGuid = null, position = null, title = title, url = url),
@@ -286,7 +298,7 @@ object GeckoBookmarksExtensionBridge : BundleEventListener {
callback.sendError("Bookmark not found: $guid") callback.sendError("Bookmark not found: $guid")
return return
} }
emitChanged(node) emitChanged(node, oldNode)
callback.sendSuccess(nodeToBundle(node, includeChildren = false)) callback.sendSuccess(nodeToBundle(node, includeChildren = false))
} }
@@ -312,7 +324,11 @@ object GeckoBookmarksExtensionBridge : BundleEventListener {
return return
} }
} }
store.deleteNode(guid).getOrThrow() val deleted = store.deleteNode(guid).getOrThrow()
if (!deleted) {
callback.sendError("Bookmark not found: $guid")
return
}
if (node != null) { if (node != null) {
emitRemoved(node) emitRemoved(node)
} }
@@ -349,20 +365,35 @@ object GeckoBookmarksExtensionBridge : BundleEventListener {
} }
/** Notify extension listeners that a bookmark's title and/or url changed. */ /** Notify extension listeners that a bookmark's title and/or url changed. */
fun emitChanged(node: BookmarkNode) { fun emitChanged(
node: BookmarkNode,
oldNode: BookmarkNode? = null,
titleChanged: Boolean = oldNode?.title != node.title,
urlChanged: Boolean = oldNode?.url != node.url,
) {
if (!titleChanged && !urlChanged) {
return
}
val bundle = GeckoBundle() val bundle = GeckoBundle()
bundle.putString("guid", node.guid) bundle.putString("guid", node.guid)
node.title?.let { bundle.putString("title", it) } if (titleChanged) {
node.url?.let { bundle.putString("url", it) } node.title?.let { bundle.putString("title", it) }
}
if (urlChanged) {
node.url?.let { bundle.putString("url", it) }
}
dispatch(EVENT_ON_CHANGED, bundle) dispatch(EVENT_ON_CHANGED, bundle)
} }
/** Notify extension listeners that a bookmark moved to a new parent/index. */ /** Notify extension listeners that a bookmark moved to a new parent/index. */
fun emitMoved(node: BookmarkNode) { fun emitMoved(node: BookmarkNode, oldNode: BookmarkNode? = null) {
val bundle = GeckoBundle() val bundle = GeckoBundle()
bundle.putString("guid", node.guid) bundle.putString("guid", node.guid)
node.parentGuid?.let { bundle.putString("parentId", it) } node.parentGuid?.let { bundle.putString("parentId", it) }
node.position?.let { bundle.putInt("index", it.toInt()) } node.position?.let { bundle.putInt("index", it.toInt()) }
oldNode?.parentGuid?.let { bundle.putString("oldParentId", it) }
oldNode?.position?.let { bundle.putInt("oldIndex", it.toInt()) }
dispatch(EVENT_ON_MOVED, bundle) dispatch(EVENT_ON_MOVED, bundle)
} }