1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use std::rc::Rc;

use crate::{
    shell::{BinaryMessengerReply, Context, EngineHandle, EngineManager},
    Error, Result,
};

use super::{MethodCall, MethodCallError, MethodCallResult, MethodCodec};

pub struct MethodChannel<V>
where
    V: 'static,
{
    context: Rc<Context>,
    invoker: MethodInvoker<V>,
}

impl<V> MethodChannel<V> {
    pub fn new<F>(
        context: Rc<Context>,
        engine_handle: EngineHandle,
        channel_name: &str,
        codec: &'static dyn MethodCodec<V>,
        callback: F,
    ) -> Self
    where
        F: Fn(MethodCall<V>, MethodCallReply<V>) + 'static,
    {
        Self::new_with_engine_manager(
            context.clone(),
            engine_handle,
            channel_name,
            codec,
            callback,
            &context.engine_manager.borrow(),
        )
    }

    pub fn new_with_engine_manager<F>(
        context: Rc<Context>,
        engine_handle: EngineHandle,
        channel_name: &str,
        codec: &'static dyn MethodCodec<V>,
        callback: F,
        engine_manager: &EngineManager,
    ) -> Self
    where
        F: Fn(MethodCall<V>, MethodCallReply<V>) + 'static,
    {
        let res = MethodChannel {
            context: context.clone(),
            invoker: MethodInvoker {
                context,
                engine_handle,
                channel_name: channel_name.into(),
                codec,
            },
        };

        let engine = engine_manager.get_engine(engine_handle);
        if let Some(engine) = engine {
            let codec = codec;
            engine
                .binary_messenger()
                .register_channel_handler(channel_name, move |data, reply| {
                    let message = codec.decode_method_call(data).unwrap();
                    let reply = MethodCallReply { reply, codec };
                    callback(message, reply);
                });
        }
        res
    }

    pub fn invoker(&self) -> &MethodInvoker<V> {
        &self.invoker
    }
}

//
//
//

// Cloneable invoker that can call channel methods
#[derive(Clone)]
pub struct MethodInvoker<V>
where
    V: 'static,
{
    context: Rc<Context>,
    engine_handle: EngineHandle,
    channel_name: String,
    codec: &'static dyn MethodCodec<V>,
}

impl<V> MethodInvoker<V> {
    pub fn call_method<F>(&self, method: String, args: V, reply: F) -> Result<()>
    where
        F: FnOnce(MethodCallResult<V>) + 'static,
    {
        let encoded = self.codec.encode_method_call(&MethodCall { method, args });
        let engine_manager = self.context.engine_manager.borrow();
        let engine = engine_manager.get_engine(self.engine_handle);
        if let Some(engine) = engine {
            let codec = self.codec;
            engine
                .binary_messenger()
                .send_message(&self.channel_name, &encoded, move |message| {
                    let message = codec.decode_envelope(message).unwrap();
                    reply(message);
                })
        } else {
            Err(Error::InvalidEngineHandle)
        }
    }
}

//
//
//

pub struct MethodCallReply<V>
where
    V: 'static,
{
    reply: BinaryMessengerReply,
    codec: &'static dyn MethodCodec<V>,
}

impl<V> MethodCallReply<V> {
    pub fn send(self, value: MethodCallResult<V>) {
        let encoded = self.codec.encode_method_call_result(&value);
        self.reply.send(&encoded);
    }

    pub fn send_ok(self, value: V) {
        self.send(MethodCallResult::Ok(value))
    }

    pub fn send_error(self, code: &str, message: Option<&str>, details: V) {
        self.send(MethodCallResult::Err(MethodCallError {
            code: code.into(),
            message: message.map(|m| m.into()),
            details,
        }));
    }
}

impl<V> Drop for MethodChannel<V> {
    fn drop(&mut self) {
        let engine_manager = self.context.engine_manager.borrow();
        let engine = engine_manager.get_engine(self.invoker.engine_handle);
        if let Some(engine) = engine {
            engine
                .binary_messenger()
                .unregister_channel_handler(&self.invoker.channel_name);
        }
    }
}