• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 pub mod remote_obj;
17 pub mod remote_stub;
18 pub mod macros;
19 
20 use crate::{
21     BorrowedMsgParcel, MsgParcel, IpcResult, DeathRecipient,
22     FileDesc, IpcStatusCode,
23 };
24 use std::ops::{Deref};
25 use std::cmp::Ordering;
26 use crate::String16;
27 
28 // Export types of this module
29 pub use crate::RemoteObj;
30 
31 /// Like C++ IRemoteObject class, define function for both proxy and stub object
32 pub trait IRemoteObj {
33     /// Send a IPC request to remote service
send_request(&self, code: u32, data: &MsgParcel, is_async: bool) -> IpcResult<MsgParcel>34     fn send_request(&self, code: u32, data: &MsgParcel, is_async: bool) -> IpcResult<MsgParcel>;
35 
36     /// Add a death recipient
add_death_recipient(&self, recipient: &mut DeathRecipient) -> bool37     fn add_death_recipient(&self, recipient: &mut DeathRecipient) -> bool;
38 
39     /// Remove a death recipient
remove_death_recipient(&self, recipient: &mut DeathRecipient) -> bool40     fn remove_death_recipient(&self, recipient: &mut DeathRecipient) -> bool;
41 
42     /// Determine whether it is a proxy object
is_proxy(&self) -> bool43     fn is_proxy(&self) -> bool;
44 
45     /// Dump a service through a String16
dump(&self, fd: i32, args: &mut Vec<String16>) -> i3246     fn dump(&self, fd: i32, args: &mut Vec<String16>) -> i32;
47 
48     /// Judge whether the object is dead
is_dead(&self) -> bool49     fn is_dead(&self) -> bool;
50 
51     /// get interface descriptor
interface_descriptor(&self) -> IpcResult<String>52     fn interface_descriptor(&self) -> IpcResult<String>;
53 }
54 
55 /// Like C++ IPCObjectStub class, define function for stub object only, like on_remote_request().
56 pub trait IRemoteStub: Send + Sync {
57     /// Get object descriptor of this stub
get_descriptor() -> &'static str58     fn get_descriptor() -> &'static str;
59 
60     /// Callback for deal IPC request
on_remote_request(&self, code: u32, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> i3261     fn on_remote_request(&self, code: u32, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> i32;
62 
63     /// Callback for IPC dump
on_dump(&self, file: &FileDesc, args: &mut Vec<String16>) -> i3264     fn on_dump(&self, file: &FileDesc, args: &mut Vec<String16>) -> i32;
65 }
66 
67 /// Like C++ IRemoteBroker class
68 pub trait IRemoteBroker: Send + Sync {
69     /// Convert self to RemoteObject
as_object(&self) -> Option<RemoteObj>70     fn as_object(&self) -> Option<RemoteObj> {
71         panic!("This is not a RemoteObject.")
72     }
73     /// Default dump
dump(&self, _file: &FileDesc, _args: &mut Vec<String16>) -> i3274     fn dump(&self, _file: &FileDesc, _args: &mut Vec<String16>) -> i32 {
75         println!("This is the default dump function, and you can override it to implement your own dump function");
76         IpcStatusCode::Ok as i32
77     }
78 }
79 
80 /// Define function which how to convert a RemoteObj to RemoteObjRef, the later contains a
81 /// dynamic trait object: IRemoteObject. For example, "dyn ITest" should implements this trait
82 pub trait FromRemoteObj: IRemoteBroker {
83     /// Convert a RemoteObj to RemoteObjeRef
try_from(object: RemoteObj) -> IpcResult<RemoteObjRef<Self>>84     fn try_from(object: RemoteObj) -> IpcResult<RemoteObjRef<Self>>;
85 }
86 
87 /// Strong reference for "dyn IRemoteBroker" object, for example T is "dyn ITest"
88 pub struct RemoteObjRef<T: FromRemoteObj + ?Sized>(Box<T>);
89 
90 impl<T: FromRemoteObj + ?Sized> RemoteObjRef<T> {
91     /// Create a RemoteObjRef object
new(object: Box<T>) -> Self92     pub fn new(object: Box<T>) -> Self {
93         Self(object)
94     }
95 }
96 
97 impl<T: FromRemoteObj + ?Sized> Deref for RemoteObjRef<T> {
98     type Target = T;
99 
deref(&self) -> &Self::Target100     fn deref(&self) -> &Self::Target {
101         &self.0
102     }
103 }
104 
105 impl<I: FromRemoteObj + ?Sized> Clone for RemoteObjRef<I> {
clone(&self) -> Self106     fn clone(&self) -> Self {
107         // Clone is a method in the RemoteObjRef structure.
108         // T in RemoteObjRef<T>implements the trait FromRemoteObj,
109         // so self.0.as_ Object(). unwrap() must be a RemoteObj object that exists
110         FromRemoteObj::try_from(self.0.as_object().unwrap()).unwrap()
111     }
112 }
113 
114 impl<I: FromRemoteObj + ?Sized> Ord for RemoteObjRef<I> {
cmp(&self, other: &Self) -> Ordering115     fn cmp(&self, other: &Self) -> Ordering {
116         self.0.as_object().cmp(&other.0.as_object())
117     }
118 }
119 
120 impl<I: FromRemoteObj + ?Sized> PartialOrd for RemoteObjRef<I> {
partial_cmp(&self, other: &Self) -> Option<Ordering>121     fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
122         self.0.as_object().partial_cmp(&other.0.as_object())
123     }
124 }
125 
126 impl<I: FromRemoteObj + ?Sized> PartialEq for RemoteObjRef<I> {
eq(&self, other: &Self) -> bool127     fn eq(&self, other: &Self) -> bool {
128         self.0.as_object().eq(&other.0.as_object())
129     }
130 }
131 
132 impl<I: FromRemoteObj + ?Sized> Eq for RemoteObjRef<I> {}
133