summaryrefslogtreecommitdiffstats
path: root/toolkit/components/bitsdownload/src/bits_interface/dispatch_callback.rs
blob: b42637a993d574773a485202ee75bdfc9d598f96 (plain)
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use super::{
    error::{BitsTaskError, ErrorCode, ErrorType},
    BitsRequest,
};
use log::{error, info, warn};
use nserror::{nsresult, NS_ERROR_FAILURE, NS_OK};
use xpcom::{
    interfaces::{nsIBitsCallback, nsIBitsNewRequestCallback},
    RefPtr,
};

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum IsCallbackExpected {
    CallbackExpected,
    CallbackOptional,
}
pub use self::IsCallbackExpected::{CallbackExpected, CallbackOptional};

// This is meant to be called at the end of a nsIBits Task. It attempts to
// return the result via the callback given. If the callback is unavailable, a
// log message will be printed indicating the results and (possibly) warning
// than an expected callback was missing.
pub fn maybe_dispatch_request_via_callback(
    result: Result<RefPtr<BitsRequest>, BitsTaskError>,
    maybe_callback: Result<&nsIBitsNewRequestCallback, BitsTaskError>,
    expected: IsCallbackExpected,
) -> Result<(), nsresult> {
    if let Err(error) = maybe_callback.as_ref() {
        if expected == CallbackExpected || error.error_type == ErrorType::CallbackOnWrongThread {
            error!(
                "Unexpected error when {} - No callback: {:?}",
                error.error_action.description(),
                error,
            );
        }
    }
    match result {
        Ok(request) => match (maybe_callback, expected) {
            (Ok(callback), _) => unsafe { callback.Success(request.coerce()) },
            (Err(error), CallbackExpected) => {
                error!(
                    "Success {} but there is no callback to return the result with",
                    error.error_action.description(),
                );
                NS_ERROR_FAILURE
            }
            (Err(error), CallbackOptional) => {
                info!("Success {}", error.error_action.description());
                NS_OK
            }
        },
        Err(error) => match (maybe_callback, expected) {
            (Ok(callback), _) => match error.error_code {
                ErrorCode::None => unsafe {
                    callback.Failure(
                        error.error_type.bits_code(),
                        error.error_action.as_error_code(),
                        error.error_stage.bits_code(),
                    )
                },
                ErrorCode::Hresult(error_code) => unsafe {
                    callback.FailureHresult(
                        error.error_type.bits_code(),
                        error.error_action.as_error_code(),
                        error.error_stage.bits_code(),
                        error_code,
                    )
                },
                ErrorCode::Nsresult(error_code) => unsafe {
                    callback.FailureNsresult(
                        error.error_type.bits_code(),
                        error.error_action.as_error_code(),
                        error.error_stage.bits_code(),
                        error_code,
                    )
                },
                ErrorCode::Message(message) => unsafe {
                    callback.FailureString(
                        error.error_type.bits_code(),
                        error.error_action.as_error_code(),
                        error.error_stage.bits_code(),
                        &*message,
                    )
                },
            },
            (Err(_), CallbackExpected) => {
                error!("Error {}: {:?}", error.error_action.description(), error);
                NS_ERROR_FAILURE
            }
            (Err(_), CallbackOptional) => {
                warn!("Error {}: {:?}", error.error_action.description(), error);
                NS_ERROR_FAILURE
            }
        },
    }
    .to_result()
}

// Intended to be used by an nsIBits XPCOM wrapper to return errors that occur
// before dispatching a task off-thread. No return value is returned because it
// will represent the return value of the callback function, which should not be
// propagated.
pub fn dispatch_pretask_interface_error(
    error: BitsTaskError,
    callback: &nsIBitsNewRequestCallback,
) {
    let _ = maybe_dispatch_request_via_callback(Err(error), Ok(callback), CallbackExpected);
}

// This is meant to be called at the end of a nsIBitsRequest Task. It attempts
// to return the result via the callback given. If the callback is unavailable,
// a log message will be printed indicating the results and (possibly) warning
// than an expected callback was missing.
pub fn maybe_dispatch_via_callback(
    result: Result<(), BitsTaskError>,
    maybe_callback: Result<&nsIBitsCallback, BitsTaskError>,
    expected: IsCallbackExpected,
) -> Result<(), nsresult> {
    if let Err(error) = maybe_callback.as_ref() {
        if expected == CallbackExpected || error.error_type == ErrorType::CallbackOnWrongThread {
            error!(
                "Unexpected error when {} - No callback: {:?}",
                error.error_action.description(),
                error,
            );
        }
    }
    match result {
        Ok(()) => match (maybe_callback, expected) {
            (Ok(callback), _) => unsafe { callback.Success() },
            (Err(error), CallbackExpected) => {
                error!(
                    "Success {} but there is no callback to return the result with",
                    error.error_action.description(),
                );
                NS_ERROR_FAILURE
            }
            (Err(error), CallbackOptional) => {
                info!("Success {}", error.error_action.description());
                NS_OK
            }
        },
        Err(error) => match (maybe_callback, expected) {
            (Ok(callback), _) => match error.error_code {
                ErrorCode::None => unsafe {
                    callback.Failure(
                        error.error_type.bits_code(),
                        error.error_action.as_error_code(),
                        error.error_stage.bits_code(),
                    )
                },
                ErrorCode::Hresult(error_code) => unsafe {
                    callback.FailureHresult(
                        error.error_type.bits_code(),
                        error.error_action.as_error_code(),
                        error.error_stage.bits_code(),
                        error_code,
                    )
                },
                ErrorCode::Nsresult(error_code) => unsafe {
                    callback.FailureNsresult(
                        error.error_type.bits_code(),
                        error.error_action.as_error_code(),
                        error.error_stage.bits_code(),
                        error_code,
                    )
                },
                ErrorCode::Message(message) => unsafe {
                    callback.FailureString(
                        error.error_type.bits_code(),
                        error.error_action.as_error_code(),
                        error.error_stage.bits_code(),
                        &*message,
                    )
                },
            },
            (Err(_), CallbackExpected) => {
                error!("Error {}: {:?}", error.error_action.description(), error);
                NS_ERROR_FAILURE
            }
            (Err(_), CallbackOptional) => {
                warn!("Error {}: {:?}", error.error_action.description(), error);
                NS_ERROR_FAILURE
            }
        },
    }
    .to_result()
}

// Intended to be used by an nsIBitsRequest XPCOM wrapper to return errors that
// occur before dispatching a task off-thread. No return value is returned
// because it will represent the return value of the callback function, which
// should not be propagated.
pub fn dispatch_pretask_request_error(error: BitsTaskError, callback: &nsIBitsCallback) {
    let _ = maybe_dispatch_via_callback(Err(error), Ok(callback), CallbackExpected);
}