Merge pull request #3285 from mozilla/jsonify-remotetabs

This commit is contained in:
Thom Chiovoloni 2020-06-29 08:13:25 -07:00 коммит произвёл GitHub
Родитель cc8e0b56ea 136d486484
Коммит d17b24e5c7
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 29 добавлений и 41 удалений

Просмотреть файл

@ -25,7 +25,7 @@ pub struct RemoteClient {
/// The type of a client. Please keep these variants in sync with the device
/// types in the FxA client and sync manager.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, serde::Deserialize, serde::Serialize)]
pub enum DeviceType {
Desktop,
Mobile,

Просмотреть файл

@ -4,6 +4,9 @@
package mozilla.appservices.remotetabs
import org.json.JSONArray
import org.json.JSONObject
data class ClientTabs(
val clientId: String, // FxA device ID or the Sync client record ID if unavailable.
val tabs: List<RemoteTab>
@ -27,16 +30,19 @@ data class RemoteTab(
val icon: String?,
val lastUsed: Long?
) {
internal fun toProtobuf(): MsgTypes.RemoteTab {
val builder = MsgTypes.RemoteTab.newBuilder()
builder.setTitle(title)
internal fun toJSON(): JSONObject {
val result = JSONObject()
result.put("title", title)
result.put("last_used", lastUsed ?: 0L)
icon?.let {
builder.setIcon(it)
result.put("icon", it)
}
builder.setLastUsed(lastUsed ?: 0L)
builder.addAllUrlHistory(urlHistory)
return builder.build()
result.put("url_history", JSONArray().apply {
urlHistory.forEach {
put(it)
}
})
return result
}
companion object {

Просмотреть файл

@ -4,12 +4,11 @@
package mozilla.appservices.remotetabs
import com.sun.jna.Native
import com.sun.jna.Pointer
import mozilla.appservices.remotetabs.rust.LibRemoteTabsFFI
import mozilla.appservices.support.native.toNioDirectBuffer
import mozilla.appservices.remotetabs.rust.RustError
import mozilla.appservices.sync15.SyncTelemetryPing
import org.json.JSONArray
import java.util.concurrent.atomic.AtomicLong
class RemoteTabsProvider : AutoCloseable {
@ -25,13 +24,14 @@ class RemoteTabsProvider : AutoCloseable {
* Update our local tabs state.
*/
fun setLocalTabs(localTabs: List<RemoteTab>) {
val remoteTabs = localTabs.map { it.toProtobuf() }
val remoteTabsCollection = MsgTypes.RemoteTabs.newBuilder().addAllRemoteTabs(remoteTabs).build()
val (nioBuf, len) = remoteTabsCollection.toNioDirectBuffer()
val remoteTabs = JSONArray().apply {
localTabs.forEach {
put(it.toJSON())
}
}
val remoteTabsJson = remoteTabs.toString()
rustCallWithLock { err ->
val ptr = Native.getDirectBufferPointer(nioBuf)
LibRemoteTabsFFI.INSTANCE.remote_tabs_update_local(this.handle.get(), ptr, len, err)
LibRemoteTabsFFI.INSTANCE.remote_tabs_update_local(this.handle.get(), remoteTabsJson, err)
}
}

Просмотреть файл

@ -26,8 +26,7 @@ internal interface LibRemoteTabsFFI : Library {
fun remote_tabs_update_local(
handle: TabsApiHandle,
local_state_data: Pointer,
local_state_len: Int,
local_state_json: String,
error: RustError.ByReference
)

Просмотреть файл

@ -64,19 +64,13 @@ pub extern "C" fn remote_tabs_sync(
#[no_mangle]
pub unsafe extern "C" fn remote_tabs_update_local(
handle: u64,
local_state_data: *const u8,
local_state_len: i32,
local_state: FfiStr<'_>,
error: &mut ExternError,
) {
log::debug!("remote_tabs_update_local");
use tabs::msg_types::RemoteTabs;
ENGINES.call_with_result(error, handle, |engine| -> Result<_> {
let buffer = get_buffer(local_state_data, local_state_len);
let remote_tabs: RemoteTabs = prost::Message::decode(buffer)?;
engine
.lock()
.unwrap()
.update_local_state(remote_tabs.into());
let remote_tabs = serde_json::from_str(local_state.as_str())?;
engine.lock().unwrap().update_local_state(remote_tabs);
Ok(())
})
}
@ -94,17 +88,6 @@ pub extern "C" fn remote_tabs_get_all(handle: u64, error: &mut ExternError) -> B
})
}
unsafe fn get_buffer<'a>(data: *const u8, len: i32) -> &'a [u8] {
assert!(len >= 0, "Bad buffer len: {}", len);
if len == 0 {
// This will still fail, but as a bad protobuf format.
&[]
} else {
assert!(!data.is_null(), "Unexpected null data pointer");
std::slice::from_raw_parts(data, len as usize)
}
}
define_string_destructor!(remote_tabs_destroy_string);
define_bytebuffer_destructor!(remote_tabs_destroy_bytebuffer);
define_handle_map_deleter!(ENGINES, remote_tabs_destroy);

Просмотреть файл

@ -10,7 +10,7 @@ const TAB_ENTRIES_LIMIT: usize = 5;
use std::cell::RefCell;
use sync15::clients::DeviceType;
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)]
pub struct RemoteTab {
pub title: String,
pub url_history: Vec<String>,
@ -18,7 +18,7 @@ pub struct RemoteTab {
pub last_used: u64, // In ms.
}
#[derive(Clone, Debug)]
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
pub struct ClientRemoteTabs {
pub client_id: String, // Corresponds to the `clients` collection ID of the client.
pub client_name: String,