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
159
160
161
162
use nnsdk::web::offlinewebsession::*;
use std::ffi::CString;

use crate::PageResult;

pub use nnsdk::web::{
    offlinewebsession::OfflineWebSession, OfflineBackgroundKind as Background,
    OfflineBootDisplayKind as BootDisplay, WebSessionBootMode as Visibility,
};

extern "C" {
    #[link_name = "\u{1}_ZN2nn3web17OfflineWebSession11RequestExitEv"]
    pub fn request_exit(session: &OfflineWebSession);
}

pub struct WebSession(pub(crate) OfflineWebSession);

impl WebSession {
    /// Sends a message, blocking until it succeeds
    pub fn send(&self, message: &str) {
        let len = message.len() + 1;
        let message = CString::new(message).unwrap();

        while unsafe { !TrySendContentMessage(&self.0, message.as_ptr() as _, len) } {}
    }

    /// Attempts to send a message, returning true if it succeeds
    pub fn try_send(&self, message: &str) -> bool {
        let len = message.len() + 1;
        let message = CString::new(message).unwrap();

        unsafe { TrySendContentMessage(&self.0, message.as_ptr() as _, len) }
    }

    /// Blocks until a message is recieved
    ///
    /// Up to 4 KiB in size, for larger or more efficient sizes use `recv_max`
    pub fn recv(&self) -> String {
        self.recv_max(0x10000)
    }

    /// Blocks until a message is recieved, up to `max_size` bytes
    pub fn recv_max(&self, max_size: usize) -> String {
        let mut buffer = vec![0u8; max_size];

        loop {
            if let Some(size) = self.inner_recv(&mut buffer) {
                if size != 0 {
                    buffer.truncate(size - 1);
                    buffer.shrink_to_fit();
                    let message = String::from_utf8(buffer).map(|string| string).unwrap();

                    break message;
                }
            }
        }
    }

    /// Attempts to recieve a message without blocking
    ///
    /// Up to 4 KiB in size, for larger or more efficient sizes use `try_recv_max`
    pub fn try_recv(&self) -> Option<String> {
        self.try_recv_max(0x10000)
    }

    /// Attempts to recieve a message without blocking, up to `max_size` bytes
    pub fn try_recv_max(&self, max_size: usize) -> Option<String> {
        let mut buffer = vec![0u8; max_size];

        self.inner_recv(&mut buffer)
            .map(|size| {
                if size != 0 {
                    buffer.truncate(size - 1);
                    buffer.shrink_to_fit();
                    String::from_utf8(buffer).map(|string| string).ok()
                } else {
                    None
                }
            })
            .flatten()
    }

    fn inner_recv<T: AsMut<[u8]>>(&self, buffer: &mut T) -> Option<usize> {
        let buffer = buffer.as_mut();
        let mut out_size = 0;

        unsafe {
            if skyline::nn::web::offlinewebsession::TryReceiveContentMessage(
                &self.0,
                &mut out_size,
                buffer.as_mut_ptr(),
                buffer.len(),
            ) != false
            {
                Some(out_size)
            } else {
                None
            }
        }
    }

    /// Show a previously hidden web session
    pub fn show(&self) {
        unsafe { Appear(&self.0) };
    }

    /// Wait until the page has been exited
    pub fn wait_for_exit(&self) -> PageResult {
        let return_value = PageResult::new();
        unsafe { WaitForExit(&self.0, return_value.as_ref()) };
        return_value
    }

    // Exit the browser forcefully
    pub fn exit(&self) {
        unsafe { request_exit(&self.0); }
    }
}



#[cfg(feature = "json")]
use serde::{de::DeserializeOwned, Serialize};

#[cfg(feature = "json")]
impl WebSession {
    /// Send a type as a JSON value, blocking until it sends
    pub fn send_json<T: Serialize>(&self, obj: &T) {
        self.send(&serde_json::to_string(obj).unwrap())
    }

    /// Attempt to send a type as a JSON value, returning false if it doesn't succeed
    pub fn try_send_json<T: Serialize>(&self, obj: &T) -> bool {
        self.try_send(&serde_json::to_string(obj).unwrap())
    }

    /// Receive a given type as a JSON message, blocking until one is ready
    pub fn recv_json<T: DeserializeOwned>(&self) -> serde_json::Result<T> {
        serde_json::from_str(&self.recv())
    }

    /// Receive a given type as a JSON message, returning None if a message is not ready
    pub fn try_recv_json<T: DeserializeOwned>(&self) -> Option<serde_json::Result<T>> {
        self.try_recv().map(|msg| serde_json::from_str(&msg))
    }

    /// Receive a given type as a JSON message, blocking until one is ready, setting a custom max
    /// payload size.
    pub fn recv_json_max<T: DeserializeOwned>(&self, max_size: usize) -> serde_json::Result<T> {
        serde_json::from_str(&self.recv_max(max_size))
    }

    /// Receive a given type as a JSON message, returning None if a message is not ready, with a
    /// max message size of `max_size`
    pub fn try_recv_json_max<T: DeserializeOwned>(
        &self,
        max_size: usize,
    ) -> Option<serde_json::Result<T>> {
        self.try_recv_max(max_size)
            .map(|msg| serde_json::from_str(&msg))
    }
}