Skip to content

Commit

Permalink
Add a getBookmarkURLForKeyword API for iOS.
Browse files Browse the repository at this point in the history
Rust Places doesn't support setting keywords for URLs, and it's unclear
if we'll ever port keywords as they exist on Desktop today. However,
we do sync and round-trip keywords set on Desktop, so we can read them
directly from the `moz_bookmarks_synced` table.

Closes #953.
  • Loading branch information
linabutler committed May 29, 2019
1 parent 07ec77a commit 1f9a7b5
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ interface ReadableBookmarksConnection : InterruptibleConnection {
*/
fun getBookmarksWithURL(url: String): List<BookmarkItem>

/**
* Returns the URL for the provided search keyword, if one exists.
*
* @param The search keyword.
* @return The bookmarked URL for the keyword, if set.
*
* @throws OperationInterrupted if this database implements [InterruptibleConnection] and
* has its `interrupt()` method called on another thread.
*/
fun getBookmarkURLForKeyword(keyword: String): String?

/**
* Returns the list of bookmarks that match the provided search string.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,18 @@ open class PlacesReaderConnection internal constructor(connHandle: Long) :
}
}

override fun getBookmarkURLForKeyword(keyword: String): String? {
rustCall { err ->
LibPlacesFFI.INSTANCE.bookmarks_get_url_for_keyword(this.handle.get(), keyword, error)
}?.let {
try {
it.getString(0, "utf8")
} finally {
LibPlacesFFI.INSTANCE.places_destroy_string(it)
}
}
}

override fun searchBookmarks(query: String, limit: Int): List<BookmarkItem> {
val rustBuf = rustCall { err ->
LibPlacesFFI.INSTANCE.bookmarks_search(this.handle.get(), query, limit, err)
Expand Down
14 changes: 14 additions & 0 deletions components/places/ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,20 @@ pub extern "C" fn bookmarks_get_all_with_url(
})
}

#[no_mangle]
pub unsafe extern "C" fn bookmarks_get_url_for_keyword(
handle: u64,
keyword: FfiStr<'_>,
error: &mut ExternError,
) -> *mut c_char {
log::debug!("bookmarks_get_url_for_keyword");
CONNECTIONS.call_with_result(error, handle, |conn| -> places::Result<_> {
let url = bookmarks::bookmarks_get_url_for_keyword(conn, keyword.as_str())?;
Ok(url.map(url::Url::into_string))
})
}


#[no_mangle]
pub extern "C" fn bookmarks_search(
handle: u64,
Expand Down
31 changes: 31 additions & 0 deletions components/places/ios/Places/Places.swift
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,37 @@ public class PlacesReadConnection {
}
}

/**
* Returns the URL for the provided search keyword, if one exists.
*
* - Parameter keyword: The search keyword.
* - Returns: The bookmarked URL for the keyword, if set.
* - Throws:
* - `PlacesError.databaseInterrupted`: If a call is made to `interrupt()` on this
* object from another thread.
* - `PlacesError.connUseAfterAPIClosed`: If the PlacesAPI that returned this connection
* object has been closed. This indicates API
* misuse.
* - `PlacesError.databaseBusy`: If this query times out with a SQLITE_BUSY error.
* - `PlacesError.unexpected`: When an error that has not specifically been exposed
* to Swift is encountered (for example IO errors from
* the database code, etc).
* - `PlacesError.panic`: If the rust code panics while completing this
* operation. (If this occurs, please let us know).
*/
open func getBookmarkURLForKeyword(keyword: String) throws -> String? {
return try queue.sync {
try self.checkApi()
let maybeURL = try PlacesError.tryUnwrap { error in
bookmarks_get_url_for_keyword(self.handle, keyword, error)
}
guard let url = maybeURL else {
return nil
}
return String(freeingPlacesString: url)
}
}

/**
* Returns the list of bookmarks that match the provided search string.
*
Expand Down
4 changes: 4 additions & 0 deletions components/places/ios/Places/RustPlacesAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ PlacesRustBuffer bookmarks_get_all_with_url(PlacesConnectionHandle handle,
char const *_Nonnull url,
PlacesRustError *_Nonnull out_err);

char *_Nullable bookmarks_get_url_for_keyword(PlacesConnectionHandle handle,
char const *_Nonnull keyword,
PlacesRustError *_Nonnull out_err);

PlacesRustBuffer bookmarks_search(PlacesConnectionHandle handle,
char const *_Nonnull query,
int32_t limit,
Expand Down
4 changes: 4 additions & 0 deletions components/places/src/storage/bookmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,10 @@ impl<'de> Deserialize<'de> for BookmarkTreeNode {
}
}

pub fn bookmarks_get_url_for_keyword(db: &PlacesDb, keyword: &str) -> Result<Option<Url>> {
Ok(None)
}

#[cfg(test)]
mod test_serialize {
use super::*;
Expand Down

0 comments on commit 1f9a7b5

Please sign in to comment.