• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 //! This create implement the request proxy and stub
15 #![cfg_attr(test, feature(future_join))]
16 #![cfg_attr(test, allow(clippy::redundant_clone))]
17 #![allow(
18     unreachable_pub,
19     clippy::new_without_default,
20     unknown_lints,
21     stable_features
22 )]
23 #![warn(
24     missing_docs,
25     clippy::redundant_static_lifetimes,
26     clippy::enum_variant_names,
27     clippy::clone_on_copy,
28     clippy::unused_async
29 )]
30 #![feature(lazy_cell)]
31 #[macro_use]
32 mod macros;
33 
34 #[macro_use]
35 extern crate request_utils;
36 
37 cfg_oh! {
38     mod trace;
39     pub mod ability;
40     mod sys_event;
41     pub use service::interface;
42     pub use utils::form_item::FileSpec;
43 }
44 
45 mod database;
46 mod error;
47 mod manage;
48 mod service;
49 mod task;
50 mod utils;
51 pub use task::{config, info};
52 
53 cfg_oh! {
54     const TAG: &str = "RequestService\0";
55     const DOMAIN: u32 = 0xD001C50;
56 }
57 
58 #[cfg(feature = "oh")]
59 #[cfg(test)]
60 mod tests {
61     use super::manage::database::RequestDb;
62     use super::manage::SystemConfigManager;
63     use crate::ability::SYSTEM_CONFIG_MANAGER;
64     /// test init
test_init()65     pub(crate) fn test_init() {
66         static ONCE: std::sync::Once = std::sync::Once::new();
67         ONCE.call_once(|| {
68             unsafe { SYSTEM_CONFIG_MANAGER.write(SystemConfigManager::init()) };
69         });
70 
71         let _ = std::fs::create_dir("test_files/");
72 
73         unsafe { SetAccessTokenPermission() };
74     }
75 
lock_database<'a>() -> DatabaseLock<'a>76     pub(crate) fn lock_database<'a>() -> DatabaseLock<'a> {
77         let _inner = unsafe {
78             match DB_LOCK.lock() {
79                 Ok(inner) => inner,
80                 Err(_) => {
81                     RequestDb::get_instance()
82                         .execute("DELETE FROM request_task")
83                         .unwrap();
84                     DB_LOCK = std::sync::Mutex::new(());
85                     DB_LOCK.lock().unwrap()
86                 }
87             }
88         };
89         DatabaseLock { _inner }
90     }
91 
92     pub(crate) struct DatabaseLock<'a> {
93         _inner: std::sync::MutexGuard<'a, ()>,
94     }
95 
96     impl<'a> Drop for DatabaseLock<'a> {
drop(&mut self)97         fn drop(&mut self) {
98             RequestDb::get_instance()
99                 .execute("DELETE FROM request_task")
100                 .unwrap();
101         }
102     }
103 
104     static mut DB_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
105 
106     extern "C" {
SetAccessTokenPermission()107         fn SetAccessTokenPermission();
108     }
109 }
110