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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files)
// from webkit-gir-files
// DO NOT EDIT

use crate::ffi;
use glib::translate::*;

glib::wrapper! {
    /// A boxed type representing the settings for the memory pressure handler
    ///
    /// #WebKitMemoryPressureSettings is a boxed type that can be used to provide some custom settings
    /// to control how the memory pressure situations are handled by the different processes.
    ///
    /// The memory pressure system implemented inside the different process will try to keep the memory usage
    /// under the defined memory limit. In order to do that, it will check the used memory with a user defined
    /// frequency and decide whether it should try to release memory. The thresholds passed will define how urgent
    /// is to release that memory.
    ///
    /// Take into account that badly defined parameters can greatly reduce the performance of the engine. For
    /// example, setting memory limit too low with a fast poll interval can cause the process to constantly
    /// be trying to release memory.
    ///
    /// A #WebKitMemoryPressureSettings can be passed to a #WebKitWebContext constructor, and the settings will
    /// be applied to all the web processes created by that context.
    ///
    /// A #WebKitMemoryPressureSettings can be passed to webkit_website_data_manager_set_memory_pressure_settings(),
    /// and the settings will be applied to all the network processes created after that call by any instance of
    /// #WebKitWebsiteDataManager.
    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct MemoryPressureSettings(Boxed<ffi::WebKitMemoryPressureSettings>);

    match fn {
        copy => |ptr| ffi::webkit_memory_pressure_settings_copy(mut_override(ptr)),
        free => |ptr| ffi::webkit_memory_pressure_settings_free(ptr),
        type_ => || ffi::webkit_memory_pressure_settings_get_type(),
    }
}

impl MemoryPressureSettings {
    /// Create a new #WebKitMemoryPressureSettings with the default values.
    ///
    /// # Returns
    ///
    /// A new #WebKitMemoryPressureSettings instance filled with the default values.
    #[doc(alias = "webkit_memory_pressure_settings_new")]
    pub fn new() -> MemoryPressureSettings {
        assert_initialized_main_thread!();
        unsafe { from_glib_full(ffi::webkit_memory_pressure_settings_new()) }
    }

    /// Gets the conservative memory usage threshold.
    ///
    /// # Returns
    ///
    /// value in the `(0, 1)` range.
    #[doc(alias = "webkit_memory_pressure_settings_get_conservative_threshold")]
    #[doc(alias = "get_conservative_threshold")]
    pub fn conservative_threshold(&mut self) -> f64 {
        unsafe {
            ffi::webkit_memory_pressure_settings_get_conservative_threshold(
                self.to_glib_none_mut().0,
            )
        }
    }

    /// Gets the kill memory usage threshold.
    ///
    /// # Returns
    ///
    /// positive value, can be zero.
    #[doc(alias = "webkit_memory_pressure_settings_get_kill_threshold")]
    #[doc(alias = "get_kill_threshold")]
    pub fn kill_threshold(&mut self) -> f64 {
        unsafe {
            ffi::webkit_memory_pressure_settings_get_kill_threshold(self.to_glib_none_mut().0)
        }
    }

    /// Gets the memory usage limit.
    ///
    /// # Returns
    ///
    /// current value, in megabytes.
    #[doc(alias = "webkit_memory_pressure_settings_get_memory_limit")]
    #[doc(alias = "get_memory_limit")]
    pub fn memory_limit(&mut self) -> u32 {
        unsafe { ffi::webkit_memory_pressure_settings_get_memory_limit(self.to_glib_none_mut().0) }
    }

    /// Gets the interval at which memory usage is checked.
    ///
    /// # Returns
    ///
    /// current interval value, in seconds.
    #[doc(alias = "webkit_memory_pressure_settings_get_poll_interval")]
    #[doc(alias = "get_poll_interval")]
    pub fn poll_interval(&mut self) -> f64 {
        unsafe { ffi::webkit_memory_pressure_settings_get_poll_interval(self.to_glib_none_mut().0) }
    }

    /// Gets the strict memory usage threshold.
    ///
    /// # Returns
    ///
    /// value in the `(0, 1)` range.
    #[doc(alias = "webkit_memory_pressure_settings_get_strict_threshold")]
    #[doc(alias = "get_strict_threshold")]
    pub fn strict_threshold(&mut self) -> f64 {
        unsafe {
            ffi::webkit_memory_pressure_settings_get_strict_threshold(self.to_glib_none_mut().0)
        }
    }

    /// Sets the memory limit for the conservative policy to start working.
    ///
    /// Sets @value as the fraction of the defined memory limit where the conservative
    /// policy starts working. This policy will try to reduce the memory footprint by
    /// releasing non critical memory.
    ///
    /// The threshold must be bigger than 0 and smaller than 1, and it must be smaller
    /// than the strict threshold defined in @self. The default value is 0.33.
    /// ## `value`
    /// fraction of the memory limit where the conservative policy starts working.
    #[doc(alias = "webkit_memory_pressure_settings_set_conservative_threshold")]
    pub fn set_conservative_threshold(&mut self, value: f64) {
        unsafe {
            ffi::webkit_memory_pressure_settings_set_conservative_threshold(
                self.to_glib_none_mut().0,
                value,
            );
        }
    }

    /// Sets @value as the fraction of the defined memory limit where the process will be
    /// killed.
    ///
    /// The threshold must be a value bigger or equal to 0. A value of 0 means that the process
    /// is never killed. If the threshold is not 0, then it must be bigger than the strict threshold
    /// defined in @self. The threshold can also have values bigger than 1. The default value is 0.
    /// ## `value`
    /// fraction of the memory limit where the process will be killed because
    ///   of excessive memory usage.
    #[doc(alias = "webkit_memory_pressure_settings_set_kill_threshold")]
    pub fn set_kill_threshold(&mut self, value: f64) {
        unsafe {
            ffi::webkit_memory_pressure_settings_set_kill_threshold(
                self.to_glib_none_mut().0,
                value,
            );
        }
    }

    /// Sets @memory_limit the memory limit value to @self.
    ///
    /// The default value is the system's RAM size with a maximum of 3GB.
    /// ## `memory_limit`
    /// amount of memory (in MB) that the process is allowed to use.
    #[doc(alias = "webkit_memory_pressure_settings_set_memory_limit")]
    pub fn set_memory_limit(&mut self, memory_limit: u32) {
        unsafe {
            ffi::webkit_memory_pressure_settings_set_memory_limit(
                self.to_glib_none_mut().0,
                memory_limit,
            );
        }
    }

    /// Sets @value as the poll interval used by @self.
    ///
    /// The poll interval value must be bigger than 0. The default value is 30 seconds.
    /// ## `value`
    /// period (in seconds) between memory usage measurements.
    #[doc(alias = "webkit_memory_pressure_settings_set_poll_interval")]
    pub fn set_poll_interval(&mut self, value: f64) {
        unsafe {
            ffi::webkit_memory_pressure_settings_set_poll_interval(
                self.to_glib_none_mut().0,
                value,
            );
        }
    }

    /// Sets the memory limit for the strict policy to start working.
    ///
    /// Sets @value as the fraction of the defined memory limit where the strict
    /// policy starts working. This policy will try to reduce the memory footprint by
    /// releasing critical memory.
    ///
    /// The threshold must be bigger than 0 and smaller than 1. Also, it must be bigger
    /// than the conservative threshold defined in @self, and smaller than the kill
    /// threshold if the latter is not 0. The default value is 0.5.
    /// ## `value`
    /// fraction of the memory limit where the strict policy starts working.
    #[doc(alias = "webkit_memory_pressure_settings_set_strict_threshold")]
    pub fn set_strict_threshold(&mut self, value: f64) {
        unsafe {
            ffi::webkit_memory_pressure_settings_set_strict_threshold(
                self.to_glib_none_mut().0,
                value,
            );
        }
    }
}

impl Default for MemoryPressureSettings {
    fn default() -> Self {
        Self::new()
    }
}