Skip to content

Commit

Permalink
First shot at Android bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
eoger committed Feb 19, 2019
1 parent df49a5c commit 8f20af1
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.fxaclient.internal

data class TabData(
val title: String,
val url: String
)

sealed class AccountEvent {
class TabReceived(val from: Device?, val entries: Array<TabData>) : AccountEvent()

companion object {
private fun fromMessage(msg: MsgTypes.AccountEvent): AccountEvent {
when (msg.type) {
MsgTypes.AccountEvent.AccountEventType.TAB_RECEIVED -> {
val data = msg.tabReceivedData
return TabReceived(
from = if (data.hasFrom()) Device.fromMessage(data.from) else null,
entries = data.entriesList.map {
TabData(title = it.title, url = it.url)
}.toTypedArray()
)
}
}
}
internal fun fromCollectionMessage(msg: MsgTypes.AccountEvents): Array<AccountEvent> {
return msg.eventsList.map {
AccountEvent.fromMessage(it)
}.toTypedArray()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.fxaclient.internal

data class Device(
val id: String,
val displayName: String,
val deviceType: Device.Type,
val pushSubscription: Device.PushSubscription?,
val pushEndpointExpired: Boolean,
val isCurrentDevice: Boolean,
val location: Device.Location,
val lastAccessTime: Long?
) {
enum class Type {
DESKTOP,
MOBILE,
UNKNOWN;

companion object {
internal fun fromMessage(msg: MsgTypes.Device.Type): Type {
return when (msg) {
MsgTypes.Device.Type.DESKTOP -> DESKTOP
MsgTypes.Device.Type.MOBILE -> MOBILE
else -> UNKNOWN
}
}
}
}
data class PushSubscription(
val endpoint: String,
val publicKey: String,
val authKey: String
) {
companion object {
internal fun fromMessage(msg: MsgTypes.Device.PushSubscription): PushSubscription {
return PushSubscription(
endpoint = msg.endpoint,
publicKey = msg.publicKey,
authKey = msg.authKey
)
}
}
}
data class Location(
val city: String,
val country: String,
val state: String,
val stateCode: String
) {
companion object {
internal fun fromMessage(msg: MsgTypes.Device.Location): Location {
return Location(
city = msg.city,
country = msg.country,
state = msg.state,
stateCode = msg.stateCode
)
}
}
}
companion object {
internal fun fromMessage(msg: MsgTypes.Device): Device {
return Device(
id = msg.id,
displayName = msg.displayName,
deviceType = Device.Type.fromMessage(msg.type),
pushSubscription = if (msg.hasPushSubscription()) Device.PushSubscription.fromMessage(msg.pushSubscription) else null,
pushEndpointExpired = msg.pushEndpointExpired,
isCurrentDevice = msg.isCurrentDevice,
location = Device.Location.fromMessage(msg.location),
lastAccessTime = if (msg.hasLastAccessTime()) msg.lastAccessTime else null
)
}
internal fun fromCollectionMessage(msg: MsgTypes.Devices): Array<Device> {
return msg.devicesList.map {
Device.fromMessage(it)
}.toTypedArray()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,66 @@ class FirefoxAccount : RustObject {
}.getAndConsumeString()
}

fun setDevicePushSubscription(endpoint: String, publicKey: String, authKey: String) {
rustCall { e ->
FxaClient.INSTANCE.fxa_set_push_subscription(validHandle(), endpoint, publicKey, authKey, e)
}
}

fun setDeviceDisplayName(displayName: String) {
rustCall { e ->
FxaClient.INSTANCE.fxa_set_display_name(validHandle(), displayName, e)
}
}

fun getDevices(): Array<Device> {
val devicesBuffer = rustCall { e ->
FxaClient.INSTANCE.fxa_get_devices(validHandle(), e)
}
try {
val devices = MsgTypes.Devices.parseFrom(devicesBuffer.asCodedInputStream()!!)
return Device.fromCollectionMessage(devices)
} finally {
FxaClient.INSTANCE.fxa_bytebuffer_free(devicesBuffer)
}
}

fun pollRemoteCommands(): Array<AccountEvent> {
val eventsBuffer = rustCall { e ->
FxaClient.INSTANCE.fxa_poll_remote_commands(validHandle(), e)
}
try {
val events = MsgTypes.AccountEvents.parseFrom(eventsBuffer.asCodedInputStream()!!)
return AccountEvent.fromCollectionMessage(events)
} finally {
FxaClient.INSTANCE.fxa_bytebuffer_free(eventsBuffer)
}
}

fun handlePushMessage(payload: String): Array<AccountEvent> {
val eventsBuffer = rustCall { e ->
FxaClient.INSTANCE.fxa_handle_push_message(validHandle(), payload, e)
}
try {
val events = MsgTypes.AccountEvents.parseFrom(eventsBuffer.asCodedInputStream()!!)
return AccountEvent.fromCollectionMessage(events)
} finally {
FxaClient.INSTANCE.fxa_bytebuffer_free(eventsBuffer)
}
}

fun ensureSendTabRegistered() {
rustCall { e ->
FxaClient.INSTANCE.fxa_ensure_send_tab_registered(validHandle(), e)
}
}

fun sendSingleTab(targetDeviceId: String, title: String, url: String) {
rustCall { e ->
FxaClient.INSTANCE.fxa_send_tab(validHandle(), targetDeviceId, title, url, e)
}
}

companion object {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ internal interface FxaClient : Library {
fun fxa_complete_oauth_flow(fxa: FxaHandle, code: String, state: String, e: Error.ByReference)
fun fxa_get_access_token(fxa: FxaHandle, scope: String, e: Error.ByReference): AccessTokenInfo.Raw?

fun fxa_set_push_subscription(fxa: FxaHandle, endpoint: String, publicKey: String, authKey: String, e: Error.ByReference)
fun fxa_set_display_name(fxa: FxaHandle, displayName: String, e: Error.ByReference)
fun fxa_get_devices(fxa: FxaHandle, e: Error.ByReference): RustBuffer.ByValue
fun fxa_poll_remote_commands(fxa: FxaHandle, e: Error.ByReference): RustBuffer.ByValue
fun fxa_handle_push_message(fxa: FxaHandle, jsonPayload: String, e: Error.ByReference): RustBuffer.ByValue

fun fxa_ensure_send_tab_registered(fxa: FxaHandle, e: Error.ByReference)
fun fxa_send_tab(fxa: FxaHandle, targetDeviceId: String, title: String, url: String, e: Error.ByReference)

fun fxa_str_free(string: Pointer)
fun fxa_free(fxa: FxaHandle, err: Error.ByReference)

Expand Down

0 comments on commit 8f20af1

Please sign in to comment.