forked from signalapp/libsignal
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
147 additions
and
5 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
java/client/src/main/java/org/signal/libsignal/chat/DeviceClient.java
This file contains 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,40 @@ | ||
package org.signal.libsignal.chat; | ||
|
||
import com.google.protobuf.InvalidProtocolBufferException; | ||
import org.signal.chat.device.GetDevicesRequest; | ||
import org.signal.chat.device.GetDevicesResponse; | ||
import org.signal.libsignal.internal.Native; | ||
import org.signal.libsignal.internal.NativeHandleGuard; | ||
|
||
public class DeviceClient implements NativeHandleGuard.Owner { | ||
|
||
private static final String DEFAULT_TARGET = "https://grpcproxy.gluonhq.net:443"; | ||
|
||
private final long unsafeHandle; | ||
|
||
public DeviceClient() { | ||
this(DEFAULT_TARGET); | ||
} | ||
|
||
public DeviceClient(String target) { | ||
this.unsafeHandle = Native.DeviceClient_New(target); | ||
} | ||
|
||
@Override @SuppressWarnings("deprecation") | ||
protected void finalize() { | ||
Native.ProfileClient_Destroy(this.unsafeHandle); | ||
} | ||
|
||
public long unsafeNativeHandleWithoutGuard() { | ||
return this.unsafeHandle; | ||
} | ||
|
||
public GetDevicesResponse getDevices(GetDevicesRequest request, String authorization) throws SignalChatCommunicationFailureException { | ||
try (NativeHandleGuard guard = new NativeHandleGuard(this)) { | ||
byte[] serializedResponse = Native.DeviceClient_GetDevices(guard.nativeHandle(), request.toByteArray(), authorization); | ||
return GetDevicesResponse.parseFrom(serializedResponse); | ||
} catch (InvalidProtocolBufferException e) { | ||
throw new SignalChatCommunicationFailureException(e); | ||
} | ||
} | ||
} |
3 changes: 1 addition & 2 deletions
3
...gnal/libsignal/profile/ProfileClient.java → .../signal/libsignal/chat/ProfileClient.java
This file contains 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
2 changes: 1 addition & 1 deletion
2
...nalChatCommunicationFailureException.java → ...nalChatCommunicationFailureException.java
This file contains 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 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 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 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 |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
// SPDX-License-Identifier: AGPL-3.0-only | ||
// | ||
|
||
pub mod device; | ||
pub mod profile; |
This file contains 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,27 @@ | ||
// | ||
// Copyright 2023 Signal Messenger, LLC. | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
// | ||
|
||
use libsignal_bridge_macros::*; | ||
use signal_chat::Result; | ||
use signal_chat::device::DeviceClient; | ||
|
||
use crate::support::*; | ||
use crate::*; | ||
|
||
bridge_handle!(DeviceClient, clone = false, mut = true); | ||
|
||
#[bridge_fn(ffi = false, node = false)] | ||
pub fn DeviceClient_New(target: String) -> Result<DeviceClient> { | ||
DeviceClient::new(target) | ||
} | ||
|
||
#[bridge_fn(ffi = false, node = false)] | ||
pub fn DeviceClient_GetDevices( | ||
device_client: &mut DeviceClient, | ||
request: &[u8], | ||
authorization: String, | ||
) -> Result<Vec<u8>> { | ||
device_client.get_devices(request, authorization) | ||
} |
This file contains 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,8 @@ | ||
// | ||
// Copyright 2023 Signal Messenger, LLC. | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
// | ||
|
||
pub mod client; | ||
|
||
pub use client::DeviceClient; |
This file contains 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,64 @@ | ||
// | ||
// Copyright 2023 Signal Messenger, LLC. | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
// | ||
|
||
use prost::Message; | ||
|
||
use crate::error::{Error, Result}; | ||
use crate::proto::signal::device::{devices_client, GetDevicesRequest}; | ||
use std::panic::RefUnwindSafe; | ||
|
||
pub struct DeviceClient { | ||
target: String, | ||
pub tokio_runtime: tokio::runtime::Runtime, | ||
} | ||
|
||
impl RefUnwindSafe for DeviceClient {} | ||
|
||
impl DeviceClient { | ||
pub fn new(target: String) -> Result<Self> { | ||
Ok(DeviceClient { | ||
target, | ||
tokio_runtime: tokio::runtime::Builder::new_multi_thread() | ||
.enable_io() | ||
.enable_time() | ||
.build() | ||
.map_err(|e| Error::InvalidArgument(format!("tokio.create_runtime: {:?}", e)))?, | ||
}) | ||
} | ||
|
||
pub fn target(&mut self, target: &str) { | ||
self.target = target.to_owned(); | ||
} | ||
|
||
pub fn get_devices(&self, request: &[u8], authorization: String) -> Result<Vec<u8>> { | ||
self.tokio_runtime | ||
.block_on(async { self.async_get_devices(request, authorization).await }) | ||
} | ||
|
||
async fn async_get_devices(&self, request: &[u8], authorization: String) -> Result<Vec<u8>> { | ||
let channel = tonic::transport::Channel::from_shared(self.target.clone()) | ||
.map_err(|e| Error::InvalidArgument(format!("devices_client.connect: {:?}", e)))? | ||
.connect() | ||
.await | ||
.map_err(|e| Error::InvalidArgument(format!("devices_client.connect: {:?}", e)))?; | ||
|
||
let mut devices_client = devices_client::DevicesClient::with_interceptor(channel, move |mut req: tonic::Request<()>| { | ||
req.metadata_mut() | ||
.insert("authorization", tonic::metadata::MetadataValue::try_from(&authorization).unwrap()); | ||
Ok(req) | ||
}); | ||
|
||
let request: GetDevicesRequest = Message::decode(&request[..]) | ||
.map_err(|e| Error::InvalidArgument(format!("devices_client.decode: {:?}", e)))?; | ||
|
||
let response = devices_client | ||
.get_devices(request) | ||
.await | ||
.map_err(|e| Error::InvalidArgument(format!("devices_client.get_devices: {:?}", e)))?; | ||
|
||
let response = response.into_inner(); | ||
Ok(response.encode_to_vec()) | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
// | ||
|
||
mod error; | ||
pub mod device; | ||
pub mod profile; | ||
mod proto; | ||
|
||
|