• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //! This create implement the request proxy and stub
16 #![allow(dead_code, unused_imports, unused_variables)]
17 extern crate ipc_rust;
18 #[macro_use]
19 extern crate hilog_rust;
20 pub mod enumration;
21 mod request_binding;
22 mod request_service;
23 mod request_service_ability;
24 pub mod request_task;
25 pub mod task_config;
26 pub mod task_info;
27 pub mod task_manager;
28 pub mod form_item;
29 mod log;
30 pub mod progress;
31 pub mod utils;
32 pub mod c_string_wrapper;
33 mod download_server_ipc_interface_code;
34 pub mod filter;
35 
36 pub(crate) mod trace;
37 pub(crate) mod sys_event;
38 
39 use enumration::ErrorCode;
40 use hilog_rust::*;
41 use ipc_rust::{
42     define_remote_object, BorrowedMsgParcel, IRemoteBroker, InterfaceToken, IpcResult,
43     IpcStatusCode, RemoteObj, RemoteStub,
44 };
45 pub use log::LOG_LABEL;
46 pub use request_service::{start, stop, RequestService};
47 use std::convert::{TryFrom, TryInto};
48 use std::{
49     ffi::{c_char, CString},
50     file,
51     option::Option,
52 };
53 use task_manager::*;
54 use download_server_ipc_interface_code::*;
55 
56 impl TryFrom<u32> for RequestInterfaceCode {
57     type Error = IpcStatusCode;
try_from(code: u32) -> IpcResult<Self>58     fn try_from(code: u32) -> IpcResult<Self> {
59         match code {
60             _ if code == RequestInterfaceCode::Construct as u32 => Ok(RequestInterfaceCode::Construct),
61             _ if code == RequestInterfaceCode::Pause as u32 => Ok(RequestInterfaceCode::Pause),
62             _ if code == RequestInterfaceCode::Query as u32 => Ok(RequestInterfaceCode::Query),
63             _ if code == RequestInterfaceCode::QueryMimeType as u32 => Ok(RequestInterfaceCode::QueryMimeType),
64             _ if code == RequestInterfaceCode::Remove as u32 => Ok(RequestInterfaceCode::Remove),
65             _ if code == RequestInterfaceCode::Resume as u32 => Ok(RequestInterfaceCode::Resume),
66             _ if code == RequestInterfaceCode::On as u32 => Ok(RequestInterfaceCode::On),
67             _ if code == RequestInterfaceCode::Off as u32 => Ok(RequestInterfaceCode::Off),
68             _ if code == RequestInterfaceCode::Start as u32 => Ok(RequestInterfaceCode::Start),
69             _ if code == RequestInterfaceCode::Stop as u32 => Ok(RequestInterfaceCode::Stop),
70             _ if code == RequestInterfaceCode::Show as u32 => Ok(RequestInterfaceCode::Show),
71             _ if code == RequestInterfaceCode::Touch as u32 => Ok(RequestInterfaceCode::Touch),
72             _ if code == RequestInterfaceCode::Search as u32 => Ok(RequestInterfaceCode::Search),
73             _ => Err(IpcStatusCode::Failed),
74         }
75     }
76 }
77 
78 /// Function between proxy and stub of RequestServiceInterface
79 pub trait RequestServiceInterface: IRemoteBroker {
80     /// request construct--create task
construct(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>81     fn construct(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>;
82     /// pause--task object
pause(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>83     fn pause(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>;
84     /// resume--task object
resume(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>85     fn resume(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>;
86     /// on--task object
on(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>87     fn on(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>;
88     /// off--task object
off(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>89     fn off(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>;
90     /// start task--task object
start(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>91     fn start(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>;
92     /// stop task--task object
stop(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>93     fn stop(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>;
94     /// query mime type
query_mime_type( &self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel, ) -> IpcResult<()>95     fn query_mime_type(
96         &self,
97         data: &BorrowedMsgParcel,
98         reply: &mut BorrowedMsgParcel,
99     ) -> IpcResult<()>;
100     /// remove
remove(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>101     fn remove(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>;
102     /// Shows specified task details belongs to the caller.
show(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>103     fn show(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>;
104     /// Touches specified task with token.
touch(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>105     fn touch(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>;
106     /// Searches tasks, for system.
search(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>107     fn search(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>;
108     /// Searches tasks
query(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>109     fn query(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>;
110 }
111 
on_remote_request( stub: &dyn RequestServiceInterface, code: u32, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel, ) -> IpcResult<()>112 fn on_remote_request(
113     stub: &dyn RequestServiceInterface,
114     code: u32,
115     data: &BorrowedMsgParcel,
116     reply: &mut BorrowedMsgParcel,
117 ) -> IpcResult<()> {
118     info!(LOG_LABEL, "on_remote_request code {}", @public(code));
119     let service_token: InterfaceToken =
120         InterfaceToken::new("OHOS.Download.RequestServiceInterface");
121     let token: InterfaceToken = match data.read::<InterfaceToken>() {
122         Ok(i) => i,
123         _ => InterfaceToken::new("token error"),
124     };
125     if service_token.get_token() != token.get_token() {
126         error!(LOG_LABEL, "token error");
127         return Err(IpcStatusCode::Failed);
128     }
129     match code.try_into()? {
130         RequestInterfaceCode::Construct => stub.construct(data, reply),
131         RequestInterfaceCode::Pause => stub.pause(data, reply),
132         RequestInterfaceCode::Query => stub.query(data, reply),
133         RequestInterfaceCode::QueryMimeType => stub.query_mime_type(data, reply),
134         RequestInterfaceCode::Remove => stub.remove(data, reply),
135         RequestInterfaceCode::Resume => stub.resume(data, reply),
136         RequestInterfaceCode::On => stub.on(data, reply),
137         RequestInterfaceCode::Off => stub.off(data, reply),
138         RequestInterfaceCode::Start => stub.start(data, reply),
139         RequestInterfaceCode::Stop => stub.stop(data, reply),
140         RequestInterfaceCode::Show => stub.show(data, reply),
141         RequestInterfaceCode::Touch => stub.touch(data, reply),
142         RequestInterfaceCode::Search => stub.search(data, reply),
143         RequestInterfaceCode::Clear => todo!(),
144     }
145 }
146 
147 define_remote_object!(
148     RequestServiceInterface["ohos.request.service"] {
149         stub: RequestServiceStub(on_remote_request),
150         proxy: RequestServiceProxy,
151     }
152 );
153 
154 impl RequestServiceInterface for RequestServiceProxy {
construct(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>155     fn construct(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()> {
156         Ok(())
157     }
158 
pause(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>159     fn pause(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()> {
160         Ok(())
161     }
162 
query_mime_type( &self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel, ) -> IpcResult<()>163     fn query_mime_type(
164         &self,
165         data: &BorrowedMsgParcel,
166         reply: &mut BorrowedMsgParcel,
167     ) -> IpcResult<()> {
168         Ok(())
169     }
170 
remove(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>171     fn remove(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()> {
172         Ok(())
173     }
174 
resume(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>175     fn resume(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()> {
176         Ok(())
177     }
178 
on(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>179     fn on(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()> {
180         Ok(())
181     }
182 
off(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>183     fn off(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()> {
184         Ok(())
185     }
186 
start(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>187     fn start(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()> {
188         Ok(())
189     }
190 
stop(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>191     fn stop(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()> {
192         Ok(())
193     }
194 
search(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>195     fn search(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()> {
196         Ok(())
197     }
198 
show(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>199     fn show(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()> {
200         Ok(())
201     }
202 
touch(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>203     fn touch(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()> {
204         Ok(())
205     }
206 
query(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()>207     fn query(&self, data: &BorrowedMsgParcel, reply: &mut BorrowedMsgParcel) -> IpcResult<()> {
208         Ok(())
209     }
210 }
211