forked from mozilla/application-services
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathffi.rs
More file actions
327 lines (293 loc) · 11.4 KB
/
ffi.rs
File metadata and controls
327 lines (293 loc) · 11.4 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* 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/. */
// This module implement the traits that make the FFI code easier to manage.
use crate::error::{Error, ErrorKind, InvalidPlaceInfo};
use crate::msg_types;
use crate::storage::history_metadata::{
DocumentType, HistoryHighlight, HistoryHighlightWeights, HistoryMetadata,
HistoryMetadataObservation,
};
use crate::{PlacesApi, PlacesDb};
use ffi_support::{
implement_into_ffi_by_delegation, implement_into_ffi_by_protobuf, ConcurrentHandleMap,
ErrorCode, ExternError, Handle, HandleError,
};
use std::sync::Arc;
lazy_static::lazy_static! {
pub static ref APIS: ConcurrentHandleMap<Arc<PlacesApi>> = ConcurrentHandleMap::new();
pub static ref CONNECTIONS: ConcurrentHandleMap<PlacesDb> = ConcurrentHandleMap::new();
}
fn parse_url(url: &str) -> crate::Result<url::Url> {
Ok(url::Url::parse(url)?)
}
fn places_get_latest_history_metadata_for_url(
handle: i64,
url: String,
) -> Result<Option<HistoryMetadata>, ErrorWrapper> {
CONNECTIONS.get(
Handle::from_u64(handle as u64)?,
|conn| -> Result<_, ErrorWrapper> {
let url = parse_url(url.as_str())?;
let metadata = crate::storage::history_metadata::get_latest_for_url(conn, &url)?;
Ok(metadata)
},
)
}
fn places_get_history_metadata_between(
handle: i64,
start: i64,
end: i64,
) -> Result<Vec<HistoryMetadata>, ErrorWrapper> {
log::debug!("places_get_history_metadata_between");
CONNECTIONS.get(
Handle::from_u64(handle as u64)?,
|conn| -> Result<_, ErrorWrapper> {
let between = crate::storage::history_metadata::get_between(conn, start, end)?;
Ok(between)
},
)
}
fn places_get_history_metadata_since(
handle: i64,
start: i64,
) -> Result<Vec<HistoryMetadata>, ErrorWrapper> {
log::debug!("places_get_history_metadata_since");
CONNECTIONS.get(
Handle::from_u64(handle as u64)?,
|conn| -> Result<_, ErrorWrapper> {
let since = crate::storage::history_metadata::get_since(conn, start)?;
Ok(since)
},
)
}
fn places_query_history_metadata(
handle: i64,
query: String,
limit: i32,
) -> Result<Vec<HistoryMetadata>, ErrorWrapper> {
log::debug!("places_get_history_metadata_since");
CONNECTIONS.get(
Handle::from_u64(handle as u64)?,
|conn| -> Result<_, ErrorWrapper> {
let metadata = crate::storage::history_metadata::query(conn, query.as_str(), limit)?;
Ok(metadata)
},
)
}
fn places_get_history_highlights(
handle: i64,
weights: HistoryHighlightWeights,
limit: i32,
) -> Result<Vec<HistoryHighlight>, ErrorWrapper> {
log::debug!("places_get_history_highlights");
CONNECTIONS.get(
Handle::from_u64(handle as u64)?,
|conn| -> Result<_, ErrorWrapper> {
let highlights =
crate::storage::history_metadata::get_highlights(conn, weights, limit)?;
Ok(highlights)
},
)
}
fn places_note_history_metadata_observation(
handle: i64,
data: HistoryMetadataObservation,
) -> Result<(), ErrorWrapper> {
log::debug!("places_note_history_metadata_observation");
CONNECTIONS.get(
Handle::from_u64(handle as u64)?,
|conn| -> Result<_, ErrorWrapper> {
crate::storage::history_metadata::apply_metadata_observation(conn, data)?;
Ok(())
},
)
}
fn places_metadata_delete_older_than(handle: i64, older_than: i64) -> Result<(), ErrorWrapper> {
log::debug!("places_note_history_metadata_observation");
CONNECTIONS.get(
Handle::from_u64(handle as u64)?,
|conn| -> Result<_, ErrorWrapper> {
crate::storage::history_metadata::delete_older_than(conn, older_than)?;
Ok(())
},
)
}
fn places_metadata_delete(
handle: i64,
url: String,
referrer_url: Option<String>,
search_term: Option<String>,
) -> Result<(), ErrorWrapper> {
log::debug!("places_metadata_delete_metadata");
CONNECTIONS.get(
Handle::from_u64(handle as u64)?,
|conn| -> Result<_, ErrorWrapper> {
crate::storage::history_metadata::delete_metadata(
conn,
url.as_str(),
referrer_url.as_deref(),
search_term.as_deref(),
)?;
Ok(())
},
)
}
pub mod error_codes {
// Note: 0 (success) and -1 (panic) are reserved by ffi_support
/// An unexpected error occurred which likely cannot be meaningfully handled
/// by the application.
pub const UNEXPECTED: i32 = 1;
/// A URL was provided that we failed to parse
pub const URL_PARSE_ERROR: i32 = 2;
/// The requested operation failed because the database was busy
/// performing operations on a separate connection to the same DB.
pub const DATABASE_BUSY: i32 = 3;
/// The requested operation failed because it was interrupted
pub const DATABASE_INTERRUPTED: i32 = 4;
/// The requested operation failed because the store is corrupt
pub const DATABASE_CORRUPT: i32 = 5;
// Skip a bunch of spaces to make it clear these are part of a group,
// even as more and more errors get added. We're only exposing the
// InvalidPlaceInfo items that can actually be triggered, the others
// (if they happen accidentally) will come through as unexpected.
/// `InvalidParent`: Attempt to add a child to a non-folder.
pub const INVALID_PLACE_INFO_INVALID_PARENT: i32 = 64;
/// `NoItem`: The GUID provided does not exist.
pub const INVALID_PLACE_INFO_NO_ITEM: i32 = 64 + 1;
/// `UrlTooLong`: The provided URL cannot be inserted, as it is over the
/// maximum URL length.
pub const INVALID_PLACE_INFO_URL_TOO_LONG: i32 = 64 + 2;
/// `IllegalChange`: Attempt to change a property on a bookmark node that
/// cannot have that property. E.g. trying to edit the URL of a folder,
/// title of a separator, etc.
pub const INVALID_PLACE_INFO_ILLEGAL_CHANGE: i32 = 64 + 3;
/// `CannotUpdateRoot`: Attempt to modify a root in a way that is illegal, e.g. adding a child
/// to root________, updating properties of a root, deleting a root, etc.
pub const INVALID_PLACE_INFO_CANNOT_UPDATE_ROOT: i32 = 64 + 4;
}
fn get_code(err: &Error) -> ErrorCode {
ErrorCode::new(get_error_number(err))
}
fn get_error_number(err: &Error) -> i32 {
match err.kind() {
ErrorKind::InvalidPlaceInfo(info) => {
log::error!("Invalid place info: {}", info);
match &info {
InvalidPlaceInfo::InvalidParent(..) => {
error_codes::INVALID_PLACE_INFO_INVALID_PARENT
}
InvalidPlaceInfo::NoSuchGuid(..) => error_codes::INVALID_PLACE_INFO_NO_ITEM,
InvalidPlaceInfo::UrlTooLong => error_codes::INVALID_PLACE_INFO_INVALID_PARENT,
InvalidPlaceInfo::IllegalChange(..) => {
error_codes::INVALID_PLACE_INFO_ILLEGAL_CHANGE
}
InvalidPlaceInfo::CannotUpdateRoot(..) => {
error_codes::INVALID_PLACE_INFO_CANNOT_UPDATE_ROOT
}
_ => error_codes::UNEXPECTED,
}
}
ErrorKind::UrlParseError(e) => {
log::error!("URL parse error: {}", e);
error_codes::URL_PARSE_ERROR
}
// Can't pattern match on `err` without adding a dep on the sqlite3-sys crate,
// so we just use a `if` guard.
ErrorKind::SqlError(rusqlite::Error::SqliteFailure(err, msg))
if err.code == rusqlite::ErrorCode::DatabaseBusy =>
{
log::error!("Database busy: {:?} {:?}", err, msg);
error_codes::DATABASE_BUSY
}
ErrorKind::SqlError(rusqlite::Error::SqliteFailure(err, _))
if err.code == rusqlite::ErrorCode::OperationInterrupted =>
{
log::info!("Operation interrupted");
error_codes::DATABASE_INTERRUPTED
}
ErrorKind::InterruptedError(_) => {
// Can't unify with the above ... :(
log::info!("Operation interrupted");
error_codes::DATABASE_INTERRUPTED
}
ErrorKind::Corruption(e) => {
log::info!("The store is corrupt: {}", e);
error_codes::DATABASE_CORRUPT
}
ErrorKind::SyncAdapterError(e) => {
use sync15::ErrorKind;
match e.kind() {
ErrorKind::StoreError(store_error) => {
// If it's a type-erased version of one of our errors, try
// and resolve it.
if let Some(places_err) = store_error.downcast_ref::<Error>() {
log::info!("Recursing to resolve places error");
get_error_number(places_err)
} else {
log::error!("Unexpected sync error: {:?}", err);
error_codes::UNEXPECTED
}
}
_ => {
// TODO: expose network errors...
log::error!("Unexpected sync error: {:?}", err);
error_codes::UNEXPECTED
}
}
}
err => {
log::error!("Unexpected error: {:?}", err);
error_codes::UNEXPECTED
}
}
}
/// This is very very hacky - we somehow need to ensure the same error hierarchy
/// exists for both hand-written FFI functions and those generated by uniffi,
/// and there doesn't seem to be a clean way of doing that. So our .udl defines
/// a single error type - ErrorWrapper::Wrapped(). The `String` message there
/// is, roughly, `format!("{}|{}", extern_error.code, extern_error.message)`.
/// There then exists code on the Swift and Kotlin side of the world which
/// unpacks this and returns the exact same error objects as if it was an
/// `ExternError` in the first place.
#[derive(Debug)]
pub enum ErrorWrapper {
Wrapped(String),
}
impl ToString for ErrorWrapper {
fn to_string(&self) -> String {
match self {
ErrorWrapper::Wrapped(e) => e.to_string(),
}
}
}
impl From<Error> for ErrorWrapper {
fn from(e: Error) -> ErrorWrapper {
ErrorWrapper::Wrapped(format!("{}|{}", get_error_number(&e), e.to_string()))
}
}
impl From<HandleError> for ErrorWrapper {
fn from(e: HandleError) -> ErrorWrapper {
ErrorWrapper::Wrapped(format!("{}|{}", error_codes::UNEXPECTED, e.to_string()))
}
}
impl From<Error> for ExternError {
fn from(e: Error) -> ExternError {
ExternError::new_error(get_code(&e), e.to_string())
}
}
implement_into_ffi_by_protobuf!(msg_types::SearchResultList);
implement_into_ffi_by_protobuf!(msg_types::TopFrecentSiteInfos);
implement_into_ffi_by_protobuf!(msg_types::HistoryVisitInfos);
implement_into_ffi_by_protobuf!(msg_types::HistoryVisitInfosWithBound);
implement_into_ffi_by_protobuf!(msg_types::BookmarkNode);
implement_into_ffi_by_protobuf!(msg_types::BookmarkNodeList);
implement_into_ffi_by_delegation!(
crate::storage::bookmarks::PublicNode,
msg_types::BookmarkNode
);
uniffi_macros::include_scaffolding!("places");
// Exists just to convince uniffi to generate `liftSequence*` helpers!
pub struct Dummy {
md: Option<Vec<HistoryMetadata>>,
}