-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add ability to parse a Takeout zip file and extract+import bookmarks #6820
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
CDRussell
merged 1 commit into
develop
from
feature/craig/streamline_import_bookmarks_takeout_zip
Sep 23, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
.../java/com/duckduckgo/autofill/impl/importing/takeout/processor/TakeoutBookmarkImporter.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright (c) 2025 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.autofill.impl.importing.takeout.processor | ||
|
||
import android.net.Uri | ||
import com.duckduckgo.common.utils.DispatcherProvider | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.duckduckgo.savedsites.api.service.ImportSavedSitesResult | ||
import com.duckduckgo.savedsites.api.service.SavedSitesImporter | ||
import com.duckduckgo.savedsites.api.service.SavedSitesImporter.ImportFolder | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import java.io.File | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.withContext | ||
|
||
/** | ||
* Interface for importing bookmarks with flexible destination handling. | ||
* Supports both root-level imports and folder-based imports while preserving structure. | ||
*/ | ||
interface TakeoutBookmarkImporter { | ||
|
||
/** | ||
* Imports bookmarks from HTML content to the specified destination. | ||
* @param htmlContent The HTML bookmark content to import (in Netscape format) | ||
* @param destination Where to import the bookmarks (Root or named Folder within bookmarks root) | ||
* @return ImportSavedSitesResult indicating success with imported items or error | ||
*/ | ||
suspend fun importBookmarks(htmlContent: String, destination: ImportFolder): ImportSavedSitesResult | ||
} | ||
|
||
@ContributesBinding(AppScope::class) | ||
class RealTakeoutBookmarkImporter @Inject constructor( | ||
private val savedSitesImporter: SavedSitesImporter, | ||
private val dispatchers: DispatcherProvider, | ||
) : TakeoutBookmarkImporter { | ||
|
||
override suspend fun importBookmarks(htmlContent: String, destination: ImportFolder): ImportSavedSitesResult { | ||
return withContext(dispatchers.io()) { | ||
import(htmlContent = htmlContent, destination = destination) | ||
} | ||
} | ||
|
||
private suspend fun import(htmlContent: String, destination: ImportFolder = ImportFolder.Root): ImportSavedSitesResult { | ||
return try { | ||
// saved sites importer needs a file uri, so we create a temp file here | ||
val tempFile = File.createTempFile("bookmark_import_", ".html") | ||
try { | ||
tempFile.writeText(htmlContent) | ||
return savedSitesImporter.import(Uri.fromFile(tempFile), destination) | ||
} finally { | ||
// delete the temp file after import | ||
tempFile.takeIf { it.exists() }?.delete() | ||
} | ||
} catch (exception: Exception) { | ||
ImportSavedSitesResult.Error(exception) | ||
} | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
.../main/java/com/duckduckgo/autofill/impl/importing/takeout/zip/TakeoutBookmarkExtractor.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright (c) 2025 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.autofill.impl.importing.takeout.zip | ||
|
||
import android.content.Context | ||
import android.net.Uri | ||
import com.duckduckgo.autofill.impl.importing.takeout.zip.TakeoutBookmarkExtractor.ExtractionResult | ||
import com.duckduckgo.autofill.impl.importing.takeout.zip.ZipEntryContentReader.ReadResult | ||
import com.duckduckgo.common.utils.DispatcherProvider | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import java.util.zip.ZipEntry | ||
import java.util.zip.ZipInputStream | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.withContext | ||
import logcat.LogPriority.WARN | ||
import logcat.logcat | ||
|
||
interface TakeoutBookmarkExtractor { | ||
|
||
sealed class ExtractionResult { | ||
data class Success(val bookmarkHtmlContent: String) : ExtractionResult() { | ||
override fun toString(): String { | ||
return "ExtractionResult=success" | ||
} | ||
} | ||
data class Error(val exception: Exception) : ExtractionResult() | ||
} | ||
|
||
/** | ||
* Extracts the bookmark HTML content from the provided Google Takeout ZIP file URI. | ||
* @param fileUri The URI of the Google Takeout ZIP file containing the bookmarks. | ||
* @return ExtractionResult containing either the bookmark HTML content or an error. | ||
*/ | ||
suspend fun extractBookmarksHtml(fileUri: Uri): ExtractionResult | ||
} | ||
|
||
@ContributesBinding(AppScope::class) | ||
class TakeoutZipBookmarkExtractor @Inject constructor( | ||
private val context: Context, | ||
private val dispatchers: DispatcherProvider, | ||
private val zipEntryContentReader: ZipEntryContentReader, | ||
) : TakeoutBookmarkExtractor { | ||
|
||
override suspend fun extractBookmarksHtml(fileUri: Uri): ExtractionResult { | ||
return withContext(dispatchers.io()) { | ||
runCatching { | ||
context.contentResolver.openInputStream(fileUri)?.use { inputStream -> | ||
ZipInputStream(inputStream).use { zipInputStream -> | ||
processZipEntries(zipInputStream) | ||
} | ||
} ?: ExtractionResult.Error(Exception("Unable to open file: $fileUri")) | ||
}.getOrElse { ExtractionResult.Error(Exception(it)) } | ||
} | ||
} | ||
|
||
private fun processZipEntries(zipInputStream: ZipInputStream): ExtractionResult { | ||
var entry = zipInputStream.nextEntry | ||
|
||
if (entry == null) { | ||
logcat(WARN) { "No entries found in ZIP stream" } | ||
return ExtractionResult.Error(Exception("Invalid or empty ZIP file")) | ||
} | ||
|
||
while (entry != null) { | ||
val entryName = entry.name | ||
logcat { "Processing zip entry '$entryName'" } | ||
|
||
if (isBookmarkEntry(entry)) { | ||
return when (val readResult = zipEntryContentReader.readAndValidateContent(zipInputStream, entryName)) { | ||
is ReadResult.Success -> ExtractionResult.Success(readResult.content) | ||
is ReadResult.Error -> ExtractionResult.Error(readResult.exception) | ||
} | ||
} | ||
|
||
entry = zipInputStream.nextEntry | ||
} | ||
|
||
return ExtractionResult.Error(Exception("Chrome/Bookmarks.html not found in file")) | ||
} | ||
|
||
private fun isBookmarkEntry(entry: ZipEntry): Boolean { | ||
return !entry.isDirectory && entry.name.endsWith(EXPECTED_BOOKMARKS_FILENAME, ignoreCase = true) | ||
} | ||
|
||
companion object { | ||
private const val EXPECTED_BOOKMARKS_FILENAME = "Chrome/Bookmarks.html" | ||
} | ||
} |
84 changes: 84 additions & 0 deletions
84
...src/main/java/com/duckduckgo/autofill/impl/importing/takeout/zip/ZipEntryContentReader.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright (c) 2025 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.autofill.impl.importing.takeout.zip | ||
|
||
import com.duckduckgo.di.scopes.AppScope | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import java.util.zip.ZipInputStream | ||
import javax.inject.Inject | ||
import logcat.logcat | ||
|
||
interface ZipEntryContentReader { | ||
|
||
sealed class ReadResult { | ||
data class Success(val content: String) : ReadResult() | ||
data class Error(val exception: Exception) : ReadResult() | ||
} | ||
|
||
fun readAndValidateContent( | ||
zipInputStream: ZipInputStream, | ||
entryName: String, | ||
): ReadResult | ||
} | ||
|
||
@ContributesBinding(AppScope::class) | ||
class BookmarkZipEntryContentReader @Inject constructor() : ZipEntryContentReader { | ||
|
||
override fun readAndValidateContent( | ||
zipInputStream: ZipInputStream, | ||
entryName: String, | ||
): ZipEntryContentReader.ReadResult { | ||
logcat { "Reading content from ZIP entry: '$entryName'" } | ||
|
||
return try { | ||
val content = readContent(zipInputStream, entryName) | ||
|
||
if (isValidBookmarkContent(content)) { | ||
logcat { "Content validation passed for: '$entryName'" } | ||
ZipEntryContentReader.ReadResult.Success(content) | ||
} else { | ||
logcat { "Content validation failed for: '$entryName'" } | ||
ZipEntryContentReader.ReadResult.Error( | ||
Exception("File content is not a valid bookmark file"), | ||
) | ||
} | ||
} catch (e: Exception) { | ||
logcat { "Error reading ZIP entry content: ${e.message}" } | ||
ZipEntryContentReader.ReadResult.Error(e) | ||
} | ||
} | ||
|
||
private fun readContent(zipInputStream: ZipInputStream, entryName: String): String { | ||
val content = zipInputStream.bufferedReader(Charsets.UTF_8).use { it.readText() } | ||
logcat { "Read content from '$entryName', length: ${content.length}" } | ||
return content | ||
} | ||
|
||
private fun isValidBookmarkContent(content: String): Boolean { | ||
val hasNetscapeHeader = content.contains(NETSCAPE_HEADER, ignoreCase = true) | ||
val hasBookmarkTitle = content.contains(BOOKMARK_TITLE, ignoreCase = true) | ||
|
||
logcat { "Content validation: hasNetscapeHeader=$hasNetscapeHeader, hasBookmarkTitle=$hasBookmarkTitle" } | ||
|
||
return hasNetscapeHeader || hasBookmarkTitle | ||
} | ||
|
||
companion object { | ||
private const val NETSCAPE_HEADER = "<!DOCTYPE NETSCAPE-Bookmark-file" | ||
private const val BOOKMARK_TITLE = "<title>Bookmarks</title>" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.