1 /*
2 * Copyright (c) 2023 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 //! This module implements the proxy of the Asset service.
17
18 #![allow(dead_code)]
19
20 use ipc_rust::{FromRemoteObj, IRemoteBroker, IRemoteObj, IpcResult, MsgParcel, RemoteObj, RemoteObjRef};
21
22 use asset_definition::{log_throw_error, AssetMap, ErrCode, Result};
23 use asset_ipc::{deserialize_maps, ipc_err_handle, serialize_map, IAsset, IpcCode, IPC_SUCCESS, SA_NAME};
24
25 /// Proxy of Asset Service.
26 pub(crate) struct AssetProxy {
27 remote: RemoteObj,
28 }
29
30 impl AssetProxy {
31 /// Create proxy object by RemoteObj.
from_remote_object(remote: &RemoteObj) -> IpcResult<Self>32 fn from_remote_object(remote: &RemoteObj) -> IpcResult<Self> {
33 Ok(Self { remote: remote.clone() })
34 }
35
36 /// Get proxy object descriptor.
get_descriptor() -> &'static str37 pub fn get_descriptor() -> &'static str {
38 SA_NAME
39 }
40 }
41
42 impl IRemoteBroker for AssetProxy {
43 /// Get RemoteObject object from proxy.
as_object(&self) -> Option<RemoteObj>44 fn as_object(&self) -> Option<RemoteObj> {
45 Some(self.remote.clone())
46 }
47 }
48
49 impl FromRemoteObj for AssetProxy {
50 /// Convert RemoteObj to RemoteObjRef<dyn IAsset>.
try_from(object: RemoteObj) -> IpcResult<RemoteObjRef<AssetProxy>>51 fn try_from(object: RemoteObj) -> IpcResult<RemoteObjRef<AssetProxy>> {
52 Ok(RemoteObjRef::new(Box::new(AssetProxy::from_remote_object(&object)?)))
53 }
54 }
55
56 impl AssetProxy {
send_request(&self, parcel: MsgParcel, ipc_code: IpcCode) -> Result<MsgParcel>57 fn send_request(&self, parcel: MsgParcel, ipc_code: IpcCode) -> Result<MsgParcel> {
58 let reply = self.remote.send_request(ipc_code as u32, &parcel, false).map_err(ipc_err_handle)?;
59 match reply.read::<u32>().map_err(ipc_err_handle)? {
60 IPC_SUCCESS => Ok(reply),
61 e => {
62 let msg = reply.read::<String>().map_err(ipc_err_handle)?;
63 log_throw_error!(ErrCode::try_from(e)?, "{}", msg)
64 },
65 }
66 }
67 }
68
new_parcel() -> Result<MsgParcel>69 fn new_parcel() -> Result<MsgParcel> {
70 match MsgParcel::new() {
71 Some(p) => Ok(p),
72 None => log_throw_error!(ErrCode::IpcError, "[FATAL]Get MsgParcel failed."),
73 }
74 }
75
76 impl IAsset for AssetProxy {
add(&self, attributes: &AssetMap) -> Result<()>77 fn add(&self, attributes: &AssetMap) -> Result<()> {
78 let mut parcel = new_parcel()?;
79 serialize_map(attributes, &mut parcel.borrowed())?;
80 self.send_request(parcel, IpcCode::Add)?;
81 Ok(())
82 }
83
remove(&self, query: &AssetMap) -> Result<()>84 fn remove(&self, query: &AssetMap) -> Result<()> {
85 let mut parcel = new_parcel()?;
86 serialize_map(query, &mut parcel.borrowed())?;
87 self.send_request(parcel, IpcCode::Remove)?;
88 Ok(())
89 }
90
update(&self, query: &AssetMap, attributes_to_update: &AssetMap) -> Result<()>91 fn update(&self, query: &AssetMap, attributes_to_update: &AssetMap) -> Result<()> {
92 let mut parcel = new_parcel()?;
93 serialize_map(query, &mut parcel.borrowed())?;
94 serialize_map(attributes_to_update, &mut parcel.borrowed())?;
95 self.send_request(parcel, IpcCode::Update)?;
96 Ok(())
97 }
98
pre_query(&self, query: &AssetMap) -> Result<Vec<u8>>99 fn pre_query(&self, query: &AssetMap) -> Result<Vec<u8>> {
100 let mut parcel = new_parcel()?;
101 serialize_map(query, &mut parcel.borrowed())?;
102 let reply = self.send_request(parcel, IpcCode::PreQuery)?;
103 let res = reply.read::<Vec<u8>>().map_err(ipc_err_handle)?;
104 Ok(res)
105 }
106
query(&self, query: &AssetMap) -> Result<Vec<AssetMap>>107 fn query(&self, query: &AssetMap) -> Result<Vec<AssetMap>> {
108 let mut parcel = new_parcel()?;
109 serialize_map(query, &mut parcel.borrowed())?;
110 let mut reply = self.send_request(parcel, IpcCode::Query)?;
111 let res = deserialize_maps(&reply.borrowed())?;
112 Ok(res)
113 }
114
post_query(&self, query: &AssetMap) -> Result<()>115 fn post_query(&self, query: &AssetMap) -> Result<()> {
116 let mut parcel = new_parcel()?;
117 serialize_map(query, &mut parcel.borrowed())?;
118 self.send_request(parcel, IpcCode::PostQuery)?;
119 Ok(())
120 }
121 }
122