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
// 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 security boundary for websites.
    ///
    /// #WebKitSecurityOrigin is a representation of a security domain
    /// defined by websites. A security origin consists of a protocol, a
    /// hostname, and an optional port number.
    ///
    /// Resources with the same security origin can generally access each
    /// other for client-side scripting or database access. When comparing
    /// origins, beware that if both protocol and host are [`None`], the origins
    /// should not be treated as equal.
    #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct SecurityOrigin(Shared<ffi::WebKitSecurityOrigin>);

    match fn {
        ref => |ptr| ffi::webkit_security_origin_ref(ptr),
        unref => |ptr| ffi::webkit_security_origin_unref(ptr),
        type_ => || ffi::webkit_security_origin_get_type(),
    }
}

impl SecurityOrigin {
    /// Create a new security origin from the provided protocol, host and
    /// port.
    /// ## `protocol`
    /// The protocol for the new origin
    /// ## `host`
    /// The host for the new origin
    /// ## `port`
    /// The port number for the new origin, or 0 to indicate the
    ///        default port for @protocol
    ///
    /// # Returns
    ///
    /// A #WebKitSecurityOrigin.
    #[doc(alias = "webkit_security_origin_new")]
    pub fn new(protocol: &str, host: &str, port: u16) -> SecurityOrigin {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_full(ffi::webkit_security_origin_new(
                protocol.to_glib_none().0,
                host.to_glib_none().0,
                port,
            ))
        }
    }

    /// Create a new security origin from the provided.
    ///
    /// Create a new security origin from the provided URI. Components of
    /// @uri other than protocol, host, and port do not affect the created
    /// #WebKitSecurityOrigin.
    /// ## `uri`
    /// The URI for the new origin
    ///
    /// # Returns
    ///
    /// A #WebKitSecurityOrigin.
    #[doc(alias = "webkit_security_origin_new_for_uri")]
    #[doc(alias = "new_for_uri")]
    pub fn for_uri(uri: &str) -> SecurityOrigin {
        assert_initialized_main_thread!();
        unsafe {
            from_glib_full(ffi::webkit_security_origin_new_for_uri(
                uri.to_glib_none().0,
            ))
        }
    }

    /// Gets the hostname of @self.
    ///
    /// It is reasonable for this to be [`None`]
    /// if its protocol does not require a host component.
    ///
    /// # Returns
    ///
    /// The host of the #WebKitSecurityOrigin
    #[doc(alias = "webkit_security_origin_get_host")]
    #[doc(alias = "get_host")]
    pub fn host(&self) -> Option<glib::GString> {
        unsafe { from_glib_none(ffi::webkit_security_origin_get_host(self.to_glib_none().0)) }
    }

    /// Gets the port of @self.
    ///
    /// This function will always return 0 if the
    /// port is the default port for the given protocol. For example,
    /// http://example.com has the same security origin as
    /// http://example.com:80, and this function will return 0 for a
    /// #WebKitSecurityOrigin constructed from either URI.
    ///
    /// # Returns
    ///
    /// The port of the #WebKitSecurityOrigin.
    #[doc(alias = "webkit_security_origin_get_port")]
    #[doc(alias = "get_port")]
    pub fn port(&self) -> u16 {
        unsafe { ffi::webkit_security_origin_get_port(self.to_glib_none().0) }
    }

    /// Gets the protocol of @self.
    ///
    /// # Returns
    ///
    /// The protocol of the #WebKitSecurityOrigin
    #[doc(alias = "webkit_security_origin_get_protocol")]
    #[doc(alias = "get_protocol")]
    pub fn protocol(&self) -> Option<glib::GString> {
        unsafe {
            from_glib_none(ffi::webkit_security_origin_get_protocol(
                self.to_glib_none().0,
            ))
        }
    }

    /// Gets a string representation of @self.
    ///
    /// The string representation
    /// is a valid URI with only protocol, host, and port components, or
    /// [`None`].
    ///
    /// # Returns
    ///
    /// a URI representing @self.
    #[doc(alias = "webkit_security_origin_to_string")]
    #[doc(alias = "to_string")]
    pub fn to_str(&self) -> glib::GString {
        unsafe { from_glib_full(ffi::webkit_security_origin_to_string(self.to_glib_none().0)) }
    }
}

impl std::fmt::Display for SecurityOrigin {
    #[inline]
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str(&self.to_str())
    }
}