mod.rs 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2022 Huawei Device Co., Ltd.
 * 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.
 */

pub mod remote_obj;
pub mod remote_stub;
pub mod macros;

C
chenchong_666 已提交
20
use crate::{BorrowedMsgParcel, MsgParcel, Result, DeathRecipient,};
21 22
use std::ops::{Deref};
use std::cmp::Ordering;
C
chenchong_666 已提交
23
use crate::String16;
24 25 26 27 28 29 30 31 32 33 34 35 36 37

// Export types of this module
pub use crate::RemoteObj;

/// Like C++ IRemoteObject class, define function for both proxy and stub object
pub trait IRemoteObj {
    /// Send a IPC request to remote service
    fn send_request(&self, code: u32, data: &MsgParcel, is_async: bool) -> Result<MsgParcel>;

    /// Add a death recipient
    fn add_death_recipient(&self, recipient: &mut DeathRecipient) -> bool;

    /// Remove a death recipient
    fn remove_death_recipient(&self, recipient: &mut DeathRecipient) -> bool;
C
chenchong_666 已提交
38 39 40 41 42 43 44 45 46 47 48 49

    /// Determine whether it is a proxy object
    fn is_proxy(&self) -> bool;

    /// Dump a service through a string
    fn dump(&self, fd: i32, args: &mut Vec<String16>) -> i32;

    /// Judge whether the object is dead
    fn is_dead(&self) -> bool;

    /// get interface descriptor
    fn interface_descriptor(&self) -> Result<String>;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
}

/// Like C++ IPCObjectStub class, define function for stub object only, like on_remote_request().
pub trait IRemoteStub: Send + Sync {
    /// Get object descriptor of this stub
    fn get_descriptor() -> &'static str;

    /// Callback for deal IPC request
    fn on_remote_request(&self, code: u32, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> i32;
}

/// Like C++ IRemoteBroker class
pub trait IRemoteBroker: Send + Sync {
    /// Convert self to RemoteObject
    fn as_object(&self) -> Option<RemoteObj> {
        panic!("This is not a RemoteObject.")
    }
}

/// Define function which how to convert a RemoteObj to RemoteObjRef, the later contains a
/// dynamic trait object: IRemoteObject. For example, "dyn ITest" should implements this trait
pub trait FromRemoteObj: IRemoteBroker {
    /// Convert a RemoteObj to RemoteObjeRef
T
tanyanying 已提交
73
    fn try_from(object: RemoteObj) -> Result<RemoteObjRef<Self>>;
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
}

/// Strong reference for "dyn IRemoteBroker" object, for example T is "dyn ITest"
pub struct RemoteObjRef<T: FromRemoteObj + ?Sized>(Box<T>);

impl<T: FromRemoteObj + ?Sized> RemoteObjRef<T> {
    /// Create a RemoteObjRef object
    pub fn new(object: Box<T>) -> Self {
        Self(object)
    }
}

impl<T: FromRemoteObj + ?Sized> Deref for RemoteObjRef<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<I: FromRemoteObj + ?Sized> Clone for RemoteObjRef<I> {
    fn clone(&self) -> Self {
        // non None
T
tanyanying 已提交
97
        FromRemoteObj::try_from(self.0.as_object().unwrap()).unwrap()
98 99 100
    }
}

C
chenchong_666 已提交
101
impl<I: FromRemoteObj + ?Sized> Ord for RemoteObjRef<I> {
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    fn cmp(&self, other: &Self) -> Ordering {
        self.0.as_object().cmp(&other.0.as_object())
    }
}

impl<I: FromRemoteObj + ?Sized> PartialOrd for RemoteObjRef<I> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.0.as_object().partial_cmp(&other.0.as_object())
    }
}

impl<I: FromRemoteObj + ?Sized> PartialEq for RemoteObjRef<I> {
    fn eq(&self, other: &Self) -> bool {
        self.0.as_object().eq(&other.0.as_object())
    }
}

impl<I: FromRemoteObj + ?Sized> Eq for RemoteObjRef<I> {}