• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2024 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 use std::fmt::Display;
15 
16 use cxx::UniquePtr;
17 use ffi::WantWrapper;
18 pub struct EventHandler {
19     inner: Box<dyn CommonEventSubscriber>,
20 }
21 
22 impl EventHandler {
23     #[inline]
new(inner: Box<dyn CommonEventSubscriber>) -> Self24     fn new(inner: Box<dyn CommonEventSubscriber>) -> Self {
25         Self { inner }
26     }
27 }
28 
29 pub trait CommonEventSubscriber {
on_receive_event(&self, code: i32, data: String, want: Want)30     fn on_receive_event(&self, code: i32, data: String, want: Want);
31 }
32 
33 impl EventHandler {
34     #[inline]
on_receive_event(&self, code: i32, data: String, want: UniquePtr<WantWrapper>)35     fn on_receive_event(&self, code: i32, data: String, want: UniquePtr<WantWrapper>) {
36         self.inner.on_receive_event(code, data, Want::new(want));
37     }
38 }
39 
40 pub struct Want {
41     inner: UniquePtr<ffi::WantWrapper>,
42 }
43 
44 impl Want {
45     #[inline]
new(inner: UniquePtr<WantWrapper>) -> Self46     fn new(inner: UniquePtr<WantWrapper>) -> Self {
47         Self { inner }
48     }
49 
get_int_param(&self, key: &str) -> Option<i32>50     pub(crate) fn get_int_param(&self, key: &str) -> Option<i32> {
51         let res = self.inner.GetIntParam(key);
52         if res == -1 {
53             None
54         } else {
55             Some(res)
56         }
57     }
58 }
59 
60 // VALUE_TYPE_BOOLEAN = 1,
61 // VALUE_TYPE_BYTE = 2,
62 // VALUE_TYPE_CHAR = 3,
63 // VALUE_TYPE_SHORT = 4,
64 // VALUE_TYPE_INT = 5,
65 // VALUE_TYPE_LONG = 6,
66 // VALUE_TYPE_FLOAT = 7,
67 // VALUE_TYPE_DOUBLE = 8,
68 // VALUE_TYPE_STRING = 9,
69 // VALUE_TYPE_ARRAY = 102,
70 impl Display for Want {
fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result71     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72         write!(f, "{}", self.inner.ToString())
73     }
74 }
75 
subscribe_common_event<T: CommonEventSubscriber + 'static>( events: Vec<&str>, handler: T, ) -> Result<(), i32>76 pub fn subscribe_common_event<T: CommonEventSubscriber + 'static>(
77     events: Vec<&str>,
78     handler: T,
79 ) -> Result<(), i32> {
80     let res = ffi::SubscribeCommonEvent(events, Box::new(EventHandler::new(Box::new(handler))));
81     if res == 0 {
82         Ok(())
83     } else {
84         Err(res)
85     }
86 }
87 
88 #[allow(unused)]
89 #[cxx::bridge(namespace = "OHOS::Request")]
90 mod ffi {
91 
92     extern "Rust" {
93         type EventHandler;
on_receive_event(&self, code: i32, data: String, want: UniquePtr<WantWrapper>)94         fn on_receive_event(&self, code: i32, data: String, want: UniquePtr<WantWrapper>);
95     }
96     unsafe extern "C++" {
97         include!("common_event.h");
98         include!("common_event_data.h");
99         type WantWrapper;
100 
ToString(self: &WantWrapper) -> String101         fn ToString(self: &WantWrapper) -> String;
GetIntParam(self: &WantWrapper, key: &str) -> i32102         fn GetIntParam(self: &WantWrapper, key: &str) -> i32;
103 
SubscribeCommonEvent(events: Vec<&str>, handler: Box<EventHandler>) -> i32104         fn SubscribeCommonEvent(events: Vec<&str>, handler: Box<EventHandler>) -> i32;
105     }
106 }
107